Skip to content

Commit e76243a

Browse files
committed
Add asyncio example
1 parent b8c0427 commit e76243a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

examples/knob_async.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
This example shows how to use the ModulinoKnob class to read the value of a rotary encoder knob asynchronously.
3+
4+
Asyncio is used to read the knob value and to blink the built-in LED at the same time.
5+
The knob is used to increase or decrease a value. The knob is rotated clockwise to increase the value and counter-clockwise to decrease it.
6+
7+
You can register callbacks for the following events:
8+
- Press: The knob is pressed.
9+
- Release: The knob is released.
10+
- Rotate clockwise: The knob is rotated clockwise.
11+
- Rotate counter clockwise: The knob is rotated counter clockwise.
12+
13+
Initial author: Sebastian Romero ([email protected])
14+
"""
15+
16+
from modulino import ModulinoKnob
17+
from machine import Pin
18+
import asyncio
19+
20+
led = Pin("LED_BUILTIN", Pin.OUT)
21+
22+
knob = ModulinoKnob()
23+
24+
knob.on_press = lambda: print("🔘 Pressed!")
25+
knob.on_release = lambda: print("🔘 Released!")
26+
knob.on_rotate_clockwise = lambda steps, value: print(f"🎛️ Rotated {steps} steps clockwise! Value: {value}")
27+
knob.on_rotate_counter_clockwise = lambda steps, value: print(f"🎛️ Rotated {steps} steps counter clockwise! Value: {value}")
28+
29+
async def blink_led():
30+
while True:
31+
led.value(0)
32+
await asyncio.sleep(0.5)
33+
led.value(1)
34+
await asyncio.sleep(0.5)
35+
36+
async def read_knob():
37+
while True:
38+
if(knob.update()):
39+
print("👀 Knob value or state changed!")
40+
await asyncio.sleep_ms(20)
41+
42+
async def main():
43+
await asyncio.gather(
44+
blink_led(),
45+
read_knob()
46+
)
47+
48+
asyncio.run(main())

0 commit comments

Comments
 (0)