Minimal MicroPython Init/Deinit For Scripts

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Loxy618
Posts: 21
Joined: Wed Apr 24, 2019 2:01 am

Minimal MicroPython Init/Deinit For Scripts

Post by Loxy618 » Tue Apr 26, 2022 6:22 pm

Hi!
I'm a little new at this so hoping someone can provide some guidance. I'm trying to run MicroPython as a static library on a small iot device that has no console access. While the device isn't executing a script its running an application doing whatever else it needs to do. However, the iot device can download a string (the script) at anytime based user's configuration and/or behavior.

When the iot device downloads a script, I want to be able to pass that script to MP to run. What is the minimal setup that MP needs to do to be able to run this script and then shut down? I don't have any concern about doing this every time a script needs to run but want to make sure I'm init-ing and deinit-ing everything properly. Thanks

Code: Select all

mp_stack_set_limit(...) 
gc_init(...)
mp_init()
do_str("for i in range(10):\r\n  x=i", MP_PARSE_FILE_INPUT); #this would be my script
mp_deinit()

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Minimal MicroPython Init/Deinit For Scripts

Post by stijn » Wed Apr 27, 2022 7:08 am

What you show is about right, but it also depends on build options and how garbage collection is implemented exactly, and so on. So it's not really possible to give one correct answer here. E.g. mp_stack_set_limit() is only needed if you have MICROPY_STACK_CHECK. And you don't have mp_stack_ctrl_init() or mp_stack_set_top() but that might be because your GC implementation doesn't need it?

Best thing to do probably is copying the relevant parts from main.c of the MicroPython port from which you're deriving yours.

Post Reply