-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconcurrent_sequences.py
More file actions
61 lines (47 loc) · 1.83 KB
/
Copy pathconcurrent_sequences.py
File metadata and controls
61 lines (47 loc) · 1.83 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
# examples/async/concurrent_sequences.py
#
# Demonstrates: a servo and an LED each running their own sequence simultaneously
# API style: async for loop (sequence) with asyncio.create_task()
# Sync equivalent: not possible — concurrent sequences require async
#
# CircuitPython note: asyncio.run(main()) works with adafruit_asyncio v3+.
# On older CircuitPython, use: asyncio.get_event_loop().run_until_complete(main())
import asyncio
import board
import pwmio
from adafruit_motor import servo
from varspeed_async import Vspeed
SERVO_MIN = 0
SERVO_MAX = 180
LED_MIN = 0
LED_MAX = 65535 # higher than 55000 isn't noticeably brighter for most LEDs
vs_servo = Vspeed(init_position=SERVO_MIN, result="int", debug=False)
vs_servo.set_bounds(lower_bound=SERVO_MIN, upper_bound=SERVO_MAX)
vs_led = Vspeed(init_position=LED_MIN, result="int", debug=False)
vs_led.set_bounds(lower_bound=LED_MIN, upper_bound=LED_MAX)
pwm_servo = pwmio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=50)
my_servo = servo.Servo(pwm_servo)
my_servo.angle = SERVO_MIN
pwm_led = pwmio.PWMOut(board.D2, frequency=5000, duty_cycle=0)
pwm_led.duty_cycle = LED_MIN
servo_seq = [
(SERVO_MAX, 4.0, 80, "SineEaseInOut"),
(SERVO_MIN, 4.0, 80, "SineEaseInOut"),
]
led_seq = [
(LED_MAX, 2.0, 40, "GammaEaseIn"),
(LED_MIN, 2.0, 40, "GammaEaseOut", 1.0),
]
async def run_actuator(vs, sequence, label):
async for position, running, changed in vs.sequence(sequence, loop_max=0):
if changed:
if label == "servo":
my_servo.angle = position
else:
pwm_led.duty_cycle = position
async def main():
task_servo = asyncio.create_task(run_actuator(vs_servo, servo_seq, "servo"))
task_led = asyncio.create_task(run_actuator(vs_led, led_seq, "led"))
await task_servo
await task_led
asyncio.run(main())