From 982005736a9f5a0df528c6877393ec2e669fe267 Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Sun, 17 Apr 2022 14:42:23 +0100 Subject: [PATCH] Improve encoder precision. --- gui/primitives/encoder.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/gui/primitives/encoder.py b/gui/primitives/encoder.py index 71005f9..60fbac9 100644 --- a/gui/primitives/encoder.py +++ b/gui/primitives/encoder.py @@ -29,22 +29,20 @@ class Encoder: yirq = pin_y.irq(trigger=trig, handler=self._y_cb) asyncio.create_task(self._run(vmin, vmax, div, callback, args)) - # Hardware IRQ's. Duration 36μs on Pyboard 1. + # Hardware IRQ's. Duration 36μs on Pyboard 1 ~50μs on ESP32. + # IRQ latency: 2nd edge may have occured by the time ISR runs, in + # which case there is no movement. def _x_cb(self, pin_x): - if (x := pin_x()) == self._x: # IRQ latency: if 2nd edge has - return # occurred there is no movement. - self._x = x - fwd = x ^ self._pin_y() - self._v += 1 if fwd else -1 - self._tsf.set() + if (x := pin_x()) != self._x: + self._x = x + self._v += 1 if x ^ self._pin_y() else -1 + self._tsf.set() def _y_cb(self, pin_y): - if (y := pin_y()) == self._y: - return - self._y = y - fwd = y ^ self._pin_x() ^ 1 - self._v += 1 if fwd else -1 - self._tsf.set() + if (y := pin_y()) != self._y: + self._y = y + self._v += 1 if y ^ self._pin_x() ^ 1 else -1 + self._tsf.set() async def _run(self, vmin, vmax, div, cb, args): pv = self._v # Prior hardware value