I am however able to run the following code. Am I missing something obvious?ISR’s cannot create instances of Python objects. This is because MicroPython needs to allocate memory for the object from a store of free memory block called the heap. This is not permitted in an interrupt handler because heap allocation is not re-entrant. In other words the interrupt might occur when the main program is part way through performing an allocation - to maintain the integrity of the heap the interpreter disallows memory allocations in ISR code.
Code: Select all
from machine import Timer
class Hello:
def __init__(self, name):
self._name = name
def greet(self):
print("Hello " + self._name)
def timer_greet(timer):
hello = Hello(name="world!")
hello.greet()
timer_0 = Timer(0)
timer_0.init(period=2000, mode=Timer.PERIODIC, callback=timer_greet)