circuitpython-tricks icon indicating copy to clipboard operation
circuitpython-tricks copied to clipboard

Looking for more Tricks

Open wyattbiker opened this issue 1 year ago • 11 comments

How to read battery voltage level. I am using an Adafruit RP2040 feather and power it using 3.3V 500mAh rechargeable battery.

How to run code.py from REPL. Just curioys if there some way to execute code.py from REPL

How to break out of code into REPL but retain state of imports and variables. It could be either a hotkey or some bython command, almost like a breakpoint.

Thanks

wyattbiker avatar Aug 05 '23 02:08 wyattbiker

Hi!

As for how to read battery voltage level on the Feather RP2040, that's pretty particular to that board. You will need to do some wiring as mentioned here in the Learn Guide. And the actual code to use once you have that wired up is, unfortunately, in a guide for one of the other Feathers.

To run code from the REPL is really easy. If you have a a file called "mycode.py", you can type import mycode. That will load "mycode.py" and run it. But subsequent "imports" will not run the code again. If you want that, put the code in a function and call the function as many times as you want after "import".

Unfortunately, CircuitPython clears all state of running code when you Ctrl-C into the REPL. This is one of the ways it differs from MicroPython. Scott (lead developer of CircuitPython) recently talked about this on the Hackaday livestream I think, and the reasoning was so that users don't get frustrated from the unintended side effects of old variables or unreleased board resources causing errors. (e.g. you would have to make sure to do myanalog0.deinit() if your code did myanalog0 = analogio.AnalogIn(board.A0).

todbot avatar Aug 05 '23 05:08 todbot

If you want to have a point in your code that pauses until you do some action, you could add an input("press RETURN to continue") in your code and then your code would stop until you press RETURN in the serial terminal window.

todbot avatar Aug 05 '23 05:08 todbot

The import and execute module code in a function is very handy but if you have to change code you need to restart. I was hoping for a some sort of a REPL software reset (I tried supervisor.reload() ) but does nothing.

wyattbiker avatar Aug 07 '23 23:08 wyattbiker

I'm unclear what you're looking for. Can't you Ctrl-C to stop the code, then do "import othercode" to run a different program in othercode.py?

Or do you want to do a reset? If you'd like to reset the whole board from the REPL, you can do:

import microcontroller; microcontroller.reset()

I use this all the time. If you're using a serial terminal that auto-reconnects (like tio) then it's pretty seamless.

Sometimes I'll also use this trick with a microcontroller.on_next_reset(microcontroller.RunMode.UF2) when I want to reboot the board into UF2 bootloader mode without needing to touch it.

todbot avatar Aug 08 '23 00:08 todbot

I figured out the import, fix code and import again flow :) thanks. I used tio and its really powerful and will use it. Mind telling me which IDE you are using for circuitpython. I am now using Mu but it is limited.

wyattbiker avatar Aug 09 '23 18:08 wyattbiker

I figured out the import, fix code and import again flow :) thanks. I used tio and its really powerful and will use it. Mind telling me which IDE you are using for circuitpython. I am now using Mu but it is limited.

You can use any text editor, or an IDE like VS Code or PyCharm. Check out tio to run in a terminal to get the serial output if you're using macOS or Linux: https://github.com/tio/tio

prcutler avatar Aug 09 '23 19:08 prcutler

Yep, I just use a standard text editor (Emacs or Sublime Text, depending) to edit my CircuitPython. I didn't grow up using IDEs so I'm not much of a user of them. I know some folks use VSCode to great effect, opening up tio in a terminal sub-window. Some even install the CircuitPython add-on for VSCode but it's not required.

But my regular workflow is very IDE-like: text editor in one window, a terminal window next to it running tio, and another terminal window to run circup and other file management tasks:

Screenshot 2023-08-09 at 12 16 45 PM

todbot avatar Aug 09 '23 19:08 todbot

I've setup with VSCode and the plugin for Circuitpython using tio for terminal. Only problem, cant use code.py as a filename because of conflict with python stdlib, so i changed it to main.py. Works great.

wyattbiker avatar Aug 10 '23 18:08 wyattbiker

Please a tutorial with midi timing clock 🙏https://docs.circuitpython.org/projects/midi/en/latest/api.html#adafruit_midi.timing_clock.TimingClock

nucklearproject avatar Sep 17 '23 03:09 nucklearproject

Please a tutorial with midi timing clock 🙏https://docs.circuitpython.org/projects/midi/en/latest/api.html#adafruit_midi.timing_clock.TimingClock

CircuitPython isn't really fast enough to handle MIDI clock, but you can see an attempt here: https://github.com/todbot/picostepseq/blob/1dd4e8f9bf26a4e42e25cc4d55d806cd80b6176c/circuitpython/picostepseq/code.py#L76

todbot avatar Sep 17 '23 18:09 todbot

Please a tutorial with midi timing clock 🙏https://docs.circuitpython.org/projects/midi/en/latest/api.html#adafruit_midi.timing_clock.TimingClock

CircuitPython isn't really fast enough to handle MIDI clock, but you can see an attempt here: https://github.com/todbot/picostepseq/blob/1dd4e8f9bf26a4e42e25cc4d55d806cd80b6176c/circuitpython/picostepseq/code.py#L76

Hi, I made the attempt, but the timing isn't calculated correctly. To have my DAW at 120 BPM, I need to set it to 125. Is that what you mean by it being slow?

import time, serial, mido

import adafruit_midi
from adafruit_midi import MIDI
from adafruit_midi.timing_clock import TimingClock


uart = serial.Serial(port='/dev/ttyAMA0', baudrate=31250)
midi = MIDI(midi_out= uart, out_channel=12-1, debug=False) #Channel 12

# BPM
bpm = 125 ## time in a any DAW is 120BPM... why??? 🥴

# Calcula el intervalo de tiempo deseado en segundos
intervalo_de_tiempo = 60.0 / bpm / 24

while True:
    #print(intervalo_entre_mensajes)
    midi.send(TimingClock())
    time.sleep(intervalo_de_tiempo )

Thanks

nucklearproject avatar Sep 23 '23 04:09 nucklearproject