File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -465,7 +465,9 @@ Synchronous method:
465
465
Class variable:
466
466
* ` delay=100 ` After motion is detected the driver waits for ` delay ` ms before
467
467
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.
469
471
470
472
Not all combinations of arguments make mathematical sense. The order in which
471
473
operations are applied is:
Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments