2021-07-04 17:21:37 +00:00
|
|
|
# encoder.py Asynchronous driver for incremental quadrature encoder.
|
2023-11-10 17:05:49 +00:00
|
|
|
# This is minimised for micro-gui. Derived from
|
|
|
|
# https://github.com/peterhinch/micropython-async/blob/master/v3/primitives/encoder.py
|
2021-07-04 17:21:37 +00:00
|
|
|
|
2024-10-19 13:58:24 +00:00
|
|
|
# Copyright (c) 2021-2024 Peter Hinch
|
2021-07-04 17:21:37 +00:00
|
|
|
# Released under the MIT License (MIT) - see LICENSE file
|
|
|
|
|
2022-04-21 16:55:12 +00:00
|
|
|
# Thanks are due to @ilium007 for identifying the issue of tracking detents,
|
|
|
|
# https://github.com/peterhinch/micropython-async/issues/82.
|
|
|
|
# Also to Mike Teachman (@miketeachman) for design discussions and testing
|
|
|
|
# against a state table design
|
|
|
|
# https://github.com/miketeachman/micropython-rotary/blob/master/rotary.py
|
|
|
|
|
2024-10-19 13:58:24 +00:00
|
|
|
# Now uses ThreadSafeFlag.clear()
|
|
|
|
|
|
|
|
import asyncio
|
2021-07-04 17:21:37 +00:00
|
|
|
from machine import Pin
|
|
|
|
|
|
|
|
|
2023-11-08 10:28:10 +00:00
|
|
|
class Encoder:
|
2023-11-10 17:05:49 +00:00
|
|
|
delay = 100 # Debounce/detent delay (ms)
|
|
|
|
|
|
|
|
def __init__(self, pin_x, pin_y, div, callback):
|
2021-07-04 17:21:37 +00:00
|
|
|
self._pin_x = pin_x
|
|
|
|
self._pin_y = pin_y
|
2022-04-17 11:09:28 +00:00
|
|
|
self._x = pin_x()
|
|
|
|
self._y = pin_y()
|
2023-11-10 17:05:49 +00:00
|
|
|
self._v = 0 # Encoder value set by ISR
|
2021-07-04 17:21:37 +00:00
|
|
|
self._tsf = asyncio.ThreadSafeFlag()
|
|
|
|
trig = Pin.IRQ_RISING | Pin.IRQ_FALLING
|
|
|
|
try:
|
|
|
|
xirq = pin_x.irq(trigger=trig, handler=self._x_cb, hard=True)
|
|
|
|
yirq = pin_y.irq(trigger=trig, handler=self._y_cb, hard=True)
|
|
|
|
except TypeError: # hard arg is unsupported on some hosts
|
|
|
|
xirq = pin_x.irq(trigger=trig, handler=self._x_cb)
|
|
|
|
yirq = pin_y.irq(trigger=trig, handler=self._y_cb)
|
2023-11-10 17:05:49 +00:00
|
|
|
asyncio.create_task(self._run(div, callback))
|
2021-07-04 17:21:37 +00:00
|
|
|
|
2022-04-17 11:09:28 +00:00
|
|
|
def _x_cb(self, pin_x):
|
2022-04-17 13:42:23 +00:00
|
|
|
if (x := pin_x()) != self._x:
|
|
|
|
self._x = x
|
|
|
|
self._v += 1 if x ^ self._pin_y() else -1
|
|
|
|
self._tsf.set()
|
2021-07-04 17:21:37 +00:00
|
|
|
|
2022-04-17 11:09:28 +00:00
|
|
|
def _y_cb(self, pin_y):
|
2022-04-17 13:42:23 +00:00
|
|
|
if (y := pin_y()) != self._y:
|
|
|
|
self._y = y
|
2022-04-17 17:02:38 +00:00
|
|
|
self._v -= 1 if y ^ self._pin_x() else -1
|
2022-04-17 13:42:23 +00:00
|
|
|
self._tsf.set()
|
2021-07-04 17:21:37 +00:00
|
|
|
|
2023-11-10 17:05:49 +00:00
|
|
|
async def _run(self, div, cb):
|
|
|
|
pv = 0 # Prior hardware value
|
|
|
|
pcv = 0 # Prior divided value passed to callback
|
2021-07-04 17:21:37 +00:00
|
|
|
while True:
|
2024-10-19 13:34:31 +00:00
|
|
|
self._tsf.clear()
|
2023-11-08 10:28:10 +00:00
|
|
|
await self._tsf.wait() # Wait for an edge
|
2023-11-10 17:05:49 +00:00
|
|
|
await asyncio.sleep_ms(Encoder.delay) # Wait for motion/bounce to stop.
|
2022-04-21 12:50:32 +00:00
|
|
|
hv = self._v # Sample hardware (atomic read).
|
|
|
|
if hv == pv: # A change happened but was negated before
|
|
|
|
continue # this got scheduled. Nothing to do.
|
|
|
|
pv = hv
|
|
|
|
cv = round(hv / div) # cv is divided value.
|
2023-11-10 17:05:49 +00:00
|
|
|
if (cv - pcv) != 0: # dv is change in divided value.
|
|
|
|
cb(cv, cv - pcv) # Run user CB in uasyncio context
|
|
|
|
pcv = cv
|