I am the maintainer of pigpio_encoder a library to access easily a rotary encoder on the RaspberryPi.
Currently I am porting the code to support micropython. A first working example is here for testing:
Code: Select all
https://github.com/vash3d/pigpio_encoder/blob/deploy/src/pigpio_encoder/rotary_mp.py
For a good handling of the encoder I have to know if a GPIO is falling or rising. Pigpio gives me a callback for both triggers OOTB:
Code: Select all
def setup_pigpio_callbacks(self):
self.pi.callback(self.clk_gpio, pigpio.FALLING_EDGE, self.clk_gpio_fall)
self.pi.callback(self.clk_gpio, pigpio.RISING_EDGE, self.clk_gpio_rise)
Code: Select all
dt_Pin.irq(handler=self.dt_gpio_call, trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING)
def dt_gpio_call(self, pin):
val = pin.value()
if val < self.dt_state:
self.dt_gpio_fall()
elif val > self.dt_state:
self.dt_gpio_rise()
self.dt_state = val
def dt_gpio_fall(self):
if len(self.sequence) > 2:
self.sequence = ''
self.sequence += dt_gpio1
def dt_gpio_rise(self):
self.sequence += dt_gpio0
if self.sequence == SEQUENCE_DOWN:
if self.counter > self.min:
self.counter -= self.scale
if self.down_callback:
self.down_callback(self._counter)
self.sequence = ''
Micropython knows what trigger leads to triggering the callback.
* So a first step can be to expose this information to the callback as an additional parameter.
* A second step may be to enable Pin.irq to have several handlers for different patterns of triggers as other solutions already have.
Cheers,
Volker