Skip to content

Commit 33167c3

Browse files
committed
primitives/encoder.py: Add async iterator protocol.
1 parent 30fc1e0 commit 33167c3

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

v3/primitives/encoder.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, pin_x, pin_y, v=0, div=1, vmin=None, vmax=None,
2323
self._v = v * div # Initialise hardware value
2424
self._cv = v # Current (divided) value
2525
self.delay = delay # Pause (ms) for motion to stop/limit callback frequency
26+
self._trig = asyncio.Event()
2627

2728
if ((vmin is not None) and v < vmin) or ((vmax is not None) and v > vmax):
2829
raise ValueError('Incompatible args: must have vmin <= v <= vmax')
@@ -74,8 +75,17 @@ async def _run(self, vmin, vmax, div, mod, cb, args):
7475
self._cv = lcv # update ._cv for .value() before CB.
7576
if lcv != plcv:
7677
cb(lcv, lcv - plcv, *args) # Run user CB in uasyncio context
78+
self._trig.set() # Enable async iterator
7779
pcv = cv
7880
plcv = lcv
7981

82+
def __aiter__(self):
83+
return self
84+
85+
def __anext__(self):
86+
await self._trig.wait()
87+
self.trig.clear()
88+
return self._cv
89+
8090
def value(self):
8191
return self._cv

0 commit comments

Comments
 (0)