Skip to content

Commit 2337452

Browse files
committed
primitives/encoder.py: Document use with async for.
1 parent 04a0950 commit 2337452

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

v3/docs/DRIVERS.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,25 @@ operations are applied is:
542542
2. Restrict the divided value by any maximum or minimum.
543543
3. Reduce modulo N if specified.
544544

545+
An `Encoder` instance is an asynchronous iterator. This enables it to be used
546+
as follows, with successive values being retrieved with `async for`:
547+
```python
548+
from machine import Pin
549+
import uasyncio as asyncio
550+
from primitives import Encoder
551+
552+
async def main():
553+
px = Pin(16, Pin.IN, Pin.PULL_UP) # Change to match hardware
554+
py = Pin(17, Pin.IN, Pin.PULL_UP)
555+
enc = Encoder(px, py, div=4) # div mtches mechanical detents
556+
async for value in enc:
557+
print(f"Value = {value}")
558+
559+
try:
560+
asyncio.run(main())
561+
finally:
562+
asyncio.new_event_loop()
563+
```
545564
See [this doc](https://github.com/peterhinch/micropython-samples/blob/master/encoders/ENCODERS.md)
546565
for further information on encoders and their limitations.
547566

0 commit comments

Comments
 (0)