Skip to content

Commit f1fff20

Browse files
committed
primitives/encoder.py: Simplify code.
1 parent 51f0bc0 commit f1fff20

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

v3/primitives/encoder.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# encoder.py Asynchronous driver for incremental quadrature encoder.
22

3-
# Copyright (c) 2021-2023 Peter Hinch
3+
# Copyright (c) 2021-2024 Peter Hinch
44
# Released under the MIT License (MIT) - see LICENSE file
55

66
# For an explanation of the design please see
@@ -17,19 +17,10 @@
1717
# Raul Kompaß (@rkompass) for suggesting a bugfix here
1818
# https://forum.micropython.org/viewtopic.php?f=15&t=9929&p=66175#p66156
1919

20+
# Now uses ThreadSafeFlag.clear()
21+
2022
import asyncio
2123
from machine import Pin
22-
from select import poll, POLLIN
23-
24-
25-
def ready(tsf, poller):
26-
r = (tsf, POLLIN)
27-
poller.register(*r)
28-
29-
def is_rdy():
30-
return r in poller.ipoll(0)
31-
32-
return is_rdy
3324

3425

3526
class Encoder:
@@ -58,7 +49,6 @@ def __init__(
5849
if ((vmin is not None) and v < vmin) or ((vmax is not None) and v > vmax):
5950
raise ValueError("Incompatible args: must have vmin <= v <= vmax")
6051
self._tsf = asyncio.ThreadSafeFlag()
61-
self._tsf_ready = ready(self._tsf, poll()) # Create a ready function
6252
trig = Pin.IRQ_RISING | Pin.IRQ_FALLING
6353
try:
6454
xirq = pin_x.irq(trigger=trig, handler=self._x_cb, hard=True)
@@ -90,10 +80,9 @@ async def _run(self, vmin, vmax, div, mod, cb, args):
9080
plcv = pcv # Previous value after limits applied
9181
delay = self.delay
9282
while True:
93-
if delay > 0 and self._tsf_ready(): # Ensure ThreadSafeFlag is clear
94-
await self._tsf.wait()
95-
await self._tsf.wait()
96-
await asyncio.sleep_ms(delay) # Wait for motion to stop.
83+
self._tsf.clear()
84+
await self._tsf.wait() # Wait for an edge. A stopped encoder waits here.
85+
await asyncio.sleep_ms(delay) # Optional rate limit for callback/trig.
9786
hv = self._v # Sample hardware (atomic read).
9887
if hv == pv: # A change happened but was negated before
9988
continue # this got scheduled. Nothing to do.

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