forked from miketeachman/micropython-rotary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_asyncio.py
49 lines (37 loc) · 1.15 KB
/
example_asyncio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# MIT License (MIT)
# Copyright (c) 2021 Junliang Yan
# https://opensource.org/licenses/MIT
# example for MicroPython rotary encoder
# - uasyncio implementation
import sys
if sys.platform == 'esp8266' or sys.platform == 'esp32':
from rotary_irq_esp import RotaryIRQ
elif sys.platform == 'pyboard':
from rotary_irq_pyb import RotaryIRQ
elif sys.platform == 'rp2':
from rotary_irq_rp2 import RotaryIRQ
else:
print('Warning: The Rotary module has not been tested on this platform')
import uasyncio as asyncio
# Use heartbeat to keep event loop not empty
async def heartbeat():
while True:
await asyncio.sleep_ms(10)
event = asyncio.Event()
def callback():
event.set()
async def main():
r = RotaryIRQ(pin_num_clk=13,
pin_num_dt=14)
r.add_listener(callback)
asyncio.create_task(heartbeat())
while True:
await event.wait()
print('result =', r.value())
event.clear()
try:
asyncio.run(main())
except (KeyboardInterrupt, Exception) as e:
print('Exception {} {}\n'.format(type(e).__name__, e))
finally:
ret = asyncio.new_event_loop() # Clear retained uasyncio state