Description
I have written a proof-of-concept (read: hacked together) a loader for .so files (in ELF format). This means it is possible to write code in C, package it as a shared object and let MicroPython import it like any .py or .mpy file. Read Extending Python with C or C++ for the CPython tutorial on it.
TL;DR: should I continue working on it and send a pull request?
There are a few reasons why this might be useful:
- It is possible to write special algorithms in C, that will be too slow for Python. I've tried
@micropython.native
and@micropython.viper
but for some algorithms this is still way too slow (and consumes too much memory). Also consider that a big compiler like GCC can do a lot more optimization than MicroPython will ever be able to. That's why I wrote a new extmod module for LED animations but I doubt this would be suitable for core MicroPython being more of a kitchen sink addition. - Some special hardware requires native code to handle it properly, think about the more esoteric hardware features of any MCU or bitbanged drivers. Things like WS2812 and DHT are commonly used by the community and are built in, but there is much more hardware that is less commonly used and not suitable for core MicroPython.
- Extract currently built-in modules as shared objects for the people that really need them, reducing the MicroPython core size for the rest. No idea whether this would be desirable. These modules can be built together with the core and provided as separate downloads.
Of course you can manage your own port (with your own builds etc.) or write in assembly but it may be much easier to simply drop a .so file in the flash storage and import it from Python.
Currently it works only for the ESP8266 (that's the only MCU I have that can run MicroPython) and being a hacked together system many features are not implemented (e.g. .data
). But simple functions work fine and adding more shouldn't be too much effort. Porting over to e.g. linux/x86_64 shouldn't be too difficult either. Maybe it's even possible to add some support for Arduino, as some people are probably more familiar with that C++ dialect and it opens possibilities for easily porting a wealth of Arduino libraries.
I've worked on this for about two days now and it turned out to be a bit more work than expected (mainly because of lacking documentation for Xtensa) so before I continue I'd like to know whether such a feature would be desirable.