Skip to content

Commit 85dd861

Browse files
committed
DRIVERS.md: Improve encoder doc, add encoder_stop.py
1 parent 3dab4b5 commit 85dd861

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

v3/docs/DRIVERS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,9 @@ Synchronous method:
465465
Class variable:
466466
* `delay=100` After motion is detected the driver waits for `delay` ms before
467467
reading the current position. A delay can be used to limit the rate at which
468-
the callback is invoked.
468+
the callback is invoked. This is a minimal approach. See
469+
[this script](https://github.com/peterhinch/micropython-async/blob/master/v3/primitives/tests/encoder_stop.py)
470+
for a way to create a callback which runs only when the encoder stops moving.
469471

470472
Not all combinations of arguments make mathematical sense. The order in which
471473
operations are applied is:

v3/primitives/tests/encoder_stop.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# encoder_stop.py Demo of callback which occurs after motion has stopped.
2+
3+
from machine import Pin
4+
import uasyncio as asyncio
5+
from primitives.encoder import Encoder
6+
from primitives.delay_ms import Delay_ms
7+
8+
px = Pin('X1', Pin.IN, Pin.PULL_UP)
9+
py = Pin('X2', Pin.IN, Pin.PULL_UP)
10+
11+
tim = Delay_ms(duration=400) # High value for test
12+
d = 0
13+
14+
def tcb(pos, delta): # User callback gets args of encoder cb
15+
global d
16+
d = 0
17+
print(pos, delta)
18+
19+
def cb(pos, delta): # Encoder callback occurs rapidly
20+
global d
21+
tim.trigger() # Postpone the user callback
22+
tim.callback(tcb, (pos, d := d + delta)) # and update its args
23+
24+
async def main():
25+
while True:
26+
await asyncio.sleep(1)
27+
28+
def test():
29+
print('Running encoder test. Press ctrl-c to teminate.')
30+
Encoder.delay = 0 # No need for this delay
31+
enc = Encoder(px, py, callback=cb)
32+
try:
33+
asyncio.run(main())
34+
except KeyboardInterrupt:
35+
print('Interrupted')
36+
finally:
37+
asyncio.new_event_loop()
38+
39+
test()

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