Skip to content

Commit c4f1f83

Browse files
committed
Encoder: minimise ISR code.
1 parent 5fb8a42 commit c4f1f83

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

v3/primitives/encoder.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,20 @@ def __init__(self, pin_x, pin_y, v=0, vmin=None, vmax=None, div=1,
2929
yirq = pin_y.irq(trigger=trig, handler=self._y_cb)
3030
asyncio.create_task(self._run(vmin, vmax, div, callback, args))
3131

32-
# Hardware IRQ's. Duration 36μs on Pyboard 1.
32+
# Hardware IRQ's. Duration 36μs on Pyboard 1 ~50μs on ESP32.
33+
# IRQ latency: 2nd edge may have occured by the time ISR runs, in
34+
# which case there is no movement.
3335
def _x_cb(self, pin_x):
34-
if (x := pin_x()) == self._x: # IRQ latency: if 2nd edge has
35-
return # occurred there is no movement.
36-
self._x = x
37-
fwd = x ^ self._pin_y()
38-
self._v += 1 if fwd else -1
39-
self._tsf.set()
36+
if (x := pin_x()) != self._x:
37+
self._x = x
38+
self._v += 1 if x ^ self._pin_y() else -1
39+
self._tsf.set()
4040

4141
def _y_cb(self, pin_y):
42-
if (y := pin_y()) == self._y:
43-
return
44-
self._y = y
45-
fwd = y ^ self._pin_x() ^ 1
46-
self._v += 1 if fwd else -1
47-
self._tsf.set()
42+
if (y := pin_y()) != self._y:
43+
self._y = y
44+
self._v += 1 if y ^ self._pin_x() ^ 1 else -1
45+
self._tsf.set()
4846

4947
async def _run(self, vmin, vmax, div, cb, args):
5048
pv = self._v # Prior hardware value

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy