formica wrote: ↑Wed Aug 04, 2021 1:40 pm
but I obtain a memory error.
You can't allocate memory inside a hard IRQ (in this case creating the bytes object inside to_bytes). See
https://docs.micropython.org/en/latest/ ... rules.html
So either you can make it a soft IRQ (set hard=False in pin.irq), or ensure that you only do memory allocation outside the IRQ handler. (Do you need to convert it to bytes inside the IRQ handler, can you just do that whenever you reference count later?)
Note that setting "ct = bytearray(2)" doesn't actually re-use the same buffer -- when you write "ct = count.to_bytes(2,'big')" it will allocate a new one each time.
If you want to convert an int to bytes without memory allocation, you can do this:
Code: Select all
# Pre-allocate the bytearray
ct = bytearray(2)
def callback():
...
# big endian uint16 to bytes
ct[0] = (count >> 8) & 0xff
c[1] = count & 0xff