Repository navigation
Frozen python code as part of machine module? #8241
Description
Activity
There is not a direct way to do this, to combine Python and C code in the same (built in) module.
But the idea with u-module naming is that the C parts can be implemented in, eg,
umachineand then the Python parts wrap that up in themachinemodule. Egmachine/__init__.pywill dofrom umachine import *and then add everything else. Effectively the C code is in a hidden module and the user visible module is written in pure Python. Seeuasynciofor an example of this.See also #5025 for an idea of a way to implement built-in functions in Python.
Understood, thanks for the reply. So there is currently no way to implement Counter and Encoder as documented in #8072 in Python.
I wonder if it then makes sense to put highlevel stuff like Encoder and Counter that into the machine module at all. On some plattforms without dedicated hardware counter this may actually be implemented on top of Pin.irq() which in turn can nicely be done in Python and would not require any C.
But anyway, then I'll happily support the somewhat bloated solution in #6639 . As said in the PR: Better that version than nothing.
So i can achieve this by just changing
tomicropython/ports/esp32/mpconfigport.h
Line 237 in 1f04a9a
{ MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \ MP_OBJ_NEW_QSTR(MP_QSTR_umachine), (mp_obj_t)&mp_module_machine }and creating
modules/machine/__init__.pyas:from umachine import *This now copes with my little Encoder class quite nicely. But what are the chances, that such a patch would be accepted upstream? Also all other uXXXX modules use the u prefix interally for C functions and such as well. Would a proper patch renaming everything inside the esp32 port to umachine?
But what are the chances, that such a patch would be accepted upstream?
I think it's a good change. Some ports already have
umachineas the main name.See #8244 for a patch that makes this change.
Reacted by Till Harbaumumachineis now available as of fbd47fc
I am implementing an alternative to #6639 which only does the very basic ESP32 PCNT API in C. The Counter and Encoder classes as decribed at #8072 are pure Python and subclassed from this PCNT API.
I personally think that such an approach is nicer and cleaner than the current approach. I thus would like to provide a full implementation of my approach.
But Encoder and Counter classes as described in the aforementioned documentation are part of the machine module. And I don't know how to add Python code to that. Is that possible at all? Can I add python code to the machine module to be frozen into the firmware?
I simply tried to add machine/Encoder.py to port/esp32/modules. But that does not work. It compiles but Encoder does not how up in the machine module.