""" Quick PoC of a Class using (software)Timers """
import machine, time
from machine import Timer, Pin
import micropython
# see: https://docs.micropython.org/en/latest/library/micropython.html#micropython.alloc_emergency_exception_buf
micropython.alloc_emergency_exception_buf(512)
led = Pin(25, Pin.OUT)
def cb_function(t):
print(f"callback Function\n{t}")
class App():
def __init__(self):
self.pulse_1 = machine.Timer(-1)
self.pulse_2 = machine.Timer(-1)
def start(self):
# specify the callbacks function objects themselved, so without the ()
self.pulse_1.init(mode=Timer.PERIODIC, period=200, callback=self.cb_method)
self.pulse_2.init(mode=Timer.PERIODIC, period=1000, callback=cb_function)
def cb_method(self, t):
# print(f"Callback Method")
led.toggle()
app = App()
app.start()
# and/or call classmethod callback from outside the class
if False:
t2 = machine.Timer(-1)
t2.init(period=333, callback=app.cb_method)