Any hints?
Code: Select all
import machine
import micropython
micropython.alloc_emergency_exception_buf(100) # enable debugging in IRQ handler as per https://docs.micropython.org/en/latest/reference/isr_rules.html#the-emergency-exception-buffer
LICHTSCHRANKE_1_PIN = const(9)
LICHTSCHRANKE_2_PIN = const(11)
class lichtschranke:
def __init__(self, gpio):
self.pin = machine.Pin(gpio, machine.Pin.IN, machine.Pin.PULL_DOWN)
self.pin.irq(trigger = machine.Pin.IRQ_FALLING, handler = self.irq_handler)
self.status = self.pin.value()
def irq_handler(self, pin): # pin argument has to be set as it is sent by the IRQ
if self.status != self.pin.value(): # for whatever reason up to 8x IRQs fire with one change, debouncing does not seem to be the isse and disabling the IRQ does not make sense, hence filter
try:
micropython.schedule(self.process, self.pin.value()) # detach any follow-on data processing task to the main program routine to keep the IRQ routine short
self.status = self.pin.value()
except RuntimeError: # schedule queue fills up very quickly, ignore - not sure if this is the right thing to do though (..)
pass
def process(self, data):
print('%s: %s' % (self.pin, data))
l1 = lichtschranke(LICHTSCHRANKE_1_PIN)
l2 = lichtschranke(LICHTSCHRANKE_2_PIN)
while True:
pass