Reading global Python variable from C code
Posted: Tue May 29, 2018 1:02 am
Hi all,
I'm in the process of replacing the existing ladder logic/function block diagram embedded scripting application with MicroPython on our STM32 product and it's progressing really well. It's such a great relieve to switch to normal Python programming after a few years of GUI mouse programming and I'm really impressed with MicroPython project.
In my case I'm running MicroPython in a separate Keil RTX thread in a kind of a "virtual HW board" environment with all the callbacks called from another thread. Also, I've replaced FatFS with YAFFS and simplified a few pyb modules. Unfortunately, there is a few GCC dependencies for autogenerated things (I'm using Keil MDK-ARM), but surprisingly the whole porting process wasn't too painful. As far as I can tell after digging into MicroPython's internals it's a very, very good and highly customisable embedded project.
However, at the moment I'm struggling to understand how I can synchronise an internal small "database" of bool/int/float values between the MicroPython script and the main C++ code. Ideally, I would like to read/write global Python variables from my C++ code in a separate thread via a mechanism similar to PyEval_GetGlobals or PyObject_GetAttrString. Obviously, MicroPython doesn't support this functionality at the moment (and I'm not expecting it to do this) but I'm wondering what are my options apart from exposing variables through an additional module? The internal database can be changed from 3 different threads asynchronously and all other observers (including MicroPython) should update their values and also on a change to any of the monitored Python global variables the internal database should be updated accordingly. Thanks.
I'm in the process of replacing the existing ladder logic/function block diagram embedded scripting application with MicroPython on our STM32 product and it's progressing really well. It's such a great relieve to switch to normal Python programming after a few years of GUI mouse programming and I'm really impressed with MicroPython project.
In my case I'm running MicroPython in a separate Keil RTX thread in a kind of a "virtual HW board" environment with all the callbacks called from another thread. Also, I've replaced FatFS with YAFFS and simplified a few pyb modules. Unfortunately, there is a few GCC dependencies for autogenerated things (I'm using Keil MDK-ARM), but surprisingly the whole porting process wasn't too painful. As far as I can tell after digging into MicroPython's internals it's a very, very good and highly customisable embedded project.
However, at the moment I'm struggling to understand how I can synchronise an internal small "database" of bool/int/float values between the MicroPython script and the main C++ code. Ideally, I would like to read/write global Python variables from my C++ code in a separate thread via a mechanism similar to PyEval_GetGlobals or PyObject_GetAttrString. Obviously, MicroPython doesn't support this functionality at the moment (and I'm not expecting it to do this) but I'm wondering what are my options apart from exposing variables through an additional module? The internal database can be changed from 3 different threads asynchronously and all other observers (including MicroPython) should update their values and also on a change to any of the monitored Python global variables the internal database should be updated accordingly. Thanks.