Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looking for more Tricks #17

Open
wyattbiker opened this issue Aug 5, 2023 · 11 comments
Open

Looking for more Tricks #17

wyattbiker opened this issue Aug 5, 2023 · 11 comments

Comments

@wyattbiker
Copy link

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

@todbot
Copy link
Owner

todbot commented Aug 5, 2023

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
Copy link
Owner

todbot commented Aug 5, 2023

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.

@wyattbiker
Copy link
Author

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.

@todbot
Copy link
Owner

todbot commented Aug 8, 2023

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.

@wyattbiker
Copy link
Author

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.

@prcutler
Copy link

prcutler commented Aug 9, 2023

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

@todbot
Copy link
Owner

todbot commented Aug 9, 2023

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

@wyattbiker
Copy link
Author

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.

@nucklearproject
Copy link

@todbot
Copy link
Owner

todbot commented Sep 17, 2023

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

@nucklearproject
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants