program controlled REPL ?

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
User avatar
jcf
Posts: 60
Joined: Wed Mar 03, 2021 11:14 am

program controlled REPL ?

Post by jcf » Mon Apr 19, 2021 10:03 am

Hi,
I am asking myself if there is a way to put the Pico in REPL mode, by program control.
Why would I need that? I'm working on a DDS generator that should be controlled by a matrix keyboard (this part is working) and by remote control via computer.
If I could switch to REPL mode, I could just e.g. output "set_frequency(500)" from the computer to set the frequency.

Maybe there are better ideas, I don't know.
If you have one, I'm eager to hear it. Thanks in advance
JC

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: program controlled REPL ?

Post by aivarannamaa » Mon Apr 19, 2021 11:19 am

You could wrap the whole program into a function, eg. main, so in order to go to the REPL the program would simply return and in order to resume, you would execute main() on the REPL.

Alternatively, you could create a simple REPL inside your own code and exec the code provided by the user.
Aivar Annamaa
https://thonny.org

User avatar
jcf
Posts: 60
Joined: Wed Mar 03, 2021 11:14 am

Re: program controlled REPL ?

Post by jcf » Mon Apr 19, 2021 12:59 pm

Thank you.
I think I found a possibility to do it.

1. state: Pico has local control via matrix keyboard
From the PC, interrupt this with <Ctrl-C>, <Ctrl-C>

2. Now we are in REPL mode, from the PC I can send commands like "set_frequency(1000)" ...

3. To switch to local again, send "import main" from the PC -> local mode

-------------------

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: program controlled REPL ?

Post by dhylands » Mon Apr 19, 2021 4:51 pm

You could also just have your program collect bytes from stdin and 'eval' them when you get a full line (terminated by a CR - 0x0D).

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: program controlled REPL ?

Post by scruss » Tue Apr 20, 2021 4:26 pm

Yet another option (if you don't already have too many) is to use ampy's run command to send a (local) MicroPython file to the board and have it execute.

User avatar
jcf
Posts: 60
Joined: Wed Mar 03, 2021 11:14 am

Re: program controlled REPL ?

Post by jcf » Wed Apr 28, 2021 12:55 pm

It seems that my approach was not too bad.
It works, but not always, unfortunately.
The problem seems to be that the Pico does not always allow me to interrupt the main loop (maybe it is too tight, containing only while True: pass, while the keyboard action is done in a separate thread.)
Thonny seems to succeed in this however. Maybe I should look at the Thonny stop code?
---
Now I'm working on something connected to this project: I want to detect automatically the port on which my Pico with the right software is connected. The basic idea is simple: on the Pico there is a file info.py containing only one line:

Code: Select all

 
print ('SINGEN') 
(SINGEN is my sine wave program)
Over serial I give the command 'import info'. This sends the info to the PC where I can detect if the response conatins the keyword 'SINGEN'.
If yes, I have the right Pico on this port.

It works, but in detail I have some problems, see above (I think they are the same)

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: program controlled REPL ?

Post by aivarannamaa » Wed Apr 28, 2021 2:27 pm

jcf wrote:
Wed Apr 28, 2021 12:55 pm
Thonny seems to succeed in this however. Maybe I should look at the Thonny stop code?
If you mean Thonny's Stop/Restart button, then it simply does Ctrl+C several times after certain intervals: https://github.com/thonny/thonny/blob/d ... nd.py#L395
Aivar Annamaa
https://thonny.org

User avatar
jcf
Posts: 60
Joined: Wed Mar 03, 2021 11:14 am

Re: program controlled REPL ?

Post by jcf » Wed Apr 28, 2021 4:30 pm

Thank you!
I have managed to do it myself. Anyway, looking at the Thonny source is surely interesting...

- Maybe i should post this to a new thread?

The reason why it took me so long is that I did not think about an important thing: import loads the module once, so my info was printed once, and after that the module was not reloaded and so the info was not printed anymore. I have treated the problem now this way:

-> see new thread "Identifying a special Pico"

Post Reply