Change the main program runtime

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
urbanspaceman
Posts: 4
Joined: Fri Jun 24, 2022 10:29 am

Change the main program runtime

Post by urbanspaceman » Fri Jun 24, 2022 8:49 pm

in my script, I read the contents of the "apps" folder where there are py files inside, in the main I select one of these files and execute it with this command "exec (open (filename) .read ())" it works but not with the expected result. I would like to replace the main with another file py

is it possible?

KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

Re: Change the main program runtime

Post by KJM » Fri Jun 24, 2022 9:51 pm

you could put something like

Code: Select all

import 'my_other_prog'

into main.py?

urbanspaceman
Posts: 4
Joined: Fri Jun 24, 2022 10:29 am

Re: Change the main program runtime

Post by urbanspaceman » Sat Jun 25, 2022 9:22 am

no, in this way I'm importing the other file but it doesn't change the code behaviour
I need to find a proper way to have multiple script in the RP2040 and a main script that switch between them

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Change the main program runtime

Post by karfas » Sat Jun 25, 2022 10:13 am

urbanspaceman wrote:
Fri Jun 24, 2022 8:49 pm
"exec (open (filename) .read ())" it works but not with the expected result.
What are the results you expect and what do you get ?
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Change the main program runtime

Post by jimmo » Mon Jun 27, 2022 2:38 am

urbanspaceman wrote:
Sat Jun 25, 2022 9:22 am
no, in this way I'm importing the other file but it doesn't change the code behaviour
I need to find a proper way to have multiple script in the RP2040 and a main script that switch between them
importing and executing are really the same thing. You can think of MicroPython as doing `import main` to run your main.py.

federico
Posts: 34
Joined: Tue Feb 07, 2017 11:34 pm

Re: Change the main program runtime

Post by federico » Tue Jul 12, 2022 12:23 am

Hoping to be of some help-
Time ago, I have faced the need of how to choose wich micropython (example to run) script to import at runtime inside the main.py file.

The following is the WEB Server solution (*) which uses a Color Picker to choose the micropython script.
The code depends on the ESP-01S board used to add WiFi connectivity to the Raspberry Pi Pico. Clearly, "mutatis mutandi", with the new Pico W card the code will be simplified and no additional wiring will be required.
Coming to the code, the main work depends on the values ​​of the R and G colors.
The two bytes of RED are used to select the number of the script (in the micropython listdir ()) that will be used in the string of the import statement in main.py
For example, if we enter the hexadecimal value "0E" (dec 15), this means that the file listdir () [15] will be imported - in listdir () [15] it matches the string of the micropython file, for example:
'rgb_rp2.py'. From this string, by cutting the end ".py" we build a new string "import rgb_rp2" which will become the argument of the exec instruction.

exec ('import rgb_rp2')

On the browser WEB page , we check if the file name is the one that we want to be imported, and if all is OK we proceed by retyping the file number "0E" followed by the "00" ( the Green two bytes) which will confirm the execution of the micropython's import instruction. ( see the code )
The RGB "000000" value is used in the example code only to make a reboot of the board.
If you need to choose between a small range of micropython files see the (**) about the Micropython Menu WEB Server where the code is simplified .

Hoping to make the things easier, see the pictures.

(*)
for the code of the IR ( Infrared) solution see near the end of the following YouTube video:
https://m.youtube.com/watch?v=cBZi8MhPaK0

(**)
for the Micropython Menu WEB Server see:

Raspberry Pi Pico - Micropython Menu, RGB LED & Servo WEB Servers

https://m.youtube.com/watch?v=Iqn3MU_FaiM
Attachments
root_dir.png
Micropython Scripts in the cwd
root_dir.png (180.44 KiB) Viewed 2242 times
how_to_load_and_run_a_script.png
How to import
how_to_load_and_run_a_script.png (185.83 KiB) Viewed 2242 times
how_to_choose.png
How to choose
how_to_choose.png (217.29 KiB) Viewed 2242 times

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Change the main program runtime

Post by jimmo » Tue Jul 12, 2022 3:33 am

federico wrote:
Tue Jul 12, 2022 12:23 am
exec ('import rgb_rp2')
FWIW, instead of using exec, you can also just use import directly via builtins.__import__. e.g. to import the sys module:

Code: Select all

import builtins
sys = builtins.__import__('sys')

federico
Posts: 34
Joined: Tue Feb 07, 2017 11:34 pm

Re: Change the main program runtime

Post by federico » Tue Jul 12, 2022 6:00 am

Thanks jmmo for your correct suggestion

Post Reply