Page 1 of 1

Add frozen code to the machine module?

Posted: Fri Jan 28, 2022 10:31 am
by MasterOfGizmo
There are discussions about machine.Encoder and machine.Counter classes in github. These are implemented in C. I would like to give a python variant a trial and would like to add it as a frozen class to the machine module so my versions are compatible with the ones from the existing PR's.

Is that possible at all? Can one mix python and C in the same module? If yes, how? Simply adding machine/Encoder.py to port/esp32/modules does not work.

Re: Add frozen code to the machine module?

Posted: Tue Feb 01, 2022 7:12 am
by pidou46
Hello,

If I understand your need accordingly, I think Damien is cooking something up:

https://github.com/micropython/micropython/pull/8191

Re: Add frozen code to the machine module?

Posted: Tue Feb 01, 2022 11:57 am
by MasterOfGizmo
pidou46 wrote:
Tue Feb 01, 2022 7:12 am
If I understand your need accordingly, I think Damien is cooking something up:
Thanks for the response. But ... no, I don't think this is what I am searching for. I don't have a problem including my code into the firmware. This already works.

I can add

Code: Select all

port/esp32/modules/mymachine/Encoder.py
This will be converted into MPY and included into the firmware. This can then be used like

Code: Select all

from mymachine import Encoder
But the documentation of the Counter and Encoder classes requires them to be part of the machine module. So I'd like to add

Code: Select all

port/esp32/modules/machine/Encoder.py
That also compiles successfully. But

Code: Select all

from machine import Encoder
won't work. Probably because I cannot simply add something to the internal machine module. The question is: How do I add python code to a built-in native module? Is this possible at all?