How to add python code to C user module

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jand
Posts: 2
Joined: Tue Feb 16, 2021 6:06 pm

How to add python code to C user module

Post by jand » Tue Feb 16, 2021 8:52 pm

I've created a C/C++ user module, which exposes a class and function, and that works so far. The module itself is located in an external directory and build with e.g. with

Code: Select all

$ cd ports/unix
$ make USER_C_MODULES=~/src/mymodule CFLAGS_EXTRA=-DMODULE_MYMODULE_ENABLED=1 all
In addition to the C/C++ code, I need to add some additional Python (glue) code to the module (e.g. a Python class or function). How can this be accomplished (so that the Python code is located together with the rest of the module somewhere below ~/src/mymodule)?

thanks in advance
Jan

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

Re: How to add python code to C user module

Post by jimmo » Wed Feb 17, 2021 2:48 am

jand wrote:
Tue Feb 16, 2021 8:52 pm
In addition to the C/C++ code, I need to add some additional Python (glue) code to the module (e.g. a Python class or function). How can this be accomplished (so that the Python code is located together with the rest of the module somewhere below ~/src/mymodule)?
At the moment you can't write a module that is half-Python, half-C. (Although see https://github.com/micropython/micropython/pull/5025)

You'd have to make two separate modules.

The way to add Python modules to the firmware is via the freezing process, which you control via manifest.py

You'd need to add FROZEN_MANIFEST=mymanifest.py to your make command line and then in mymanifest.py you can freeze() whatever you need and then include the base manifest for your platform/board.

jand
Posts: 2
Joined: Tue Feb 16, 2021 6:06 pm

Re: How to add python code to C user module

Post by jand » Wed Feb 17, 2021 9:18 am

Excellent - that both helps a lot, I got a first PoC working. Thanks.

Post Reply