from machine import Pin class Encoder: _increment_grid = [[0, -1, 1, None], [1, 0, None, -1], [-1, None, 0, 1], [None, 1, -1, 0]] @property def total_change(self): return self._total_change def __init__(self, channel_a, channel_b): self.channel_a = self._configure_channel(channel_a) self.reset() def disable(self): self.channel_a.irq(None) def enable(self): self.channel_a.irq(self._irq_cb, trigger=Pin.IRQ_RISING) def reset(self): self._total_change = 0 def _irq_cb(self, input_pin): self._total_change += 1 def _configure_channel(self, pin): p = pin if isinstance(pin, Pin) else Pin(pin, Pin.IN, pull=Pin.PULL_UP) p.irq(self._irq_cb, trigger=Pin.IRQ_RISING) return p