]> vault307.fbx.one Git - ir_remote.git/blob - primitives/tests/encoder_stop.py
infrared remote
[ir_remote.git] / primitives / tests / encoder_stop.py
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()