15
15
16
16
Two: THIS IS ONLY NECESSARY IF THE BUILT-IN BUTTON IS ACTIVE HIGH. For example the built-in
17
17
buttons on the Circuit Playground Bluefruit and the MagTag are active high.
18
- If your button is ACTIVE HIGH, under "async def monitor_button(button, animation_controls )",
18
+ If your button is ACTIVE HIGH, under "async def monitor_button(button, controls )",
19
19
update the following line of code:
20
20
with keypad.Keys((button,), value_when_pressed=False, pull=True) as key:
21
21
To the following:
@@ -44,30 +44,30 @@ def __init__(self):
44
44
self .delay = 0.5
45
45
46
46
47
- async def rainbow_cycle (animation_controls ):
47
+ async def rainbow_cycle (controls ):
48
48
"""Rainbow cycle animation on ring one."""
49
49
while True :
50
- for j in range (255 , - 1 , - 1 ) if animation_controls .reverse else range (0 , 256 , 1 ):
50
+ for j in range (255 , - 1 , - 1 ) if controls .reverse else range (0 , 256 , 1 ):
51
51
for i in range (num_pixels ):
52
52
rc_index = (i * 256 // num_pixels ) + j
53
53
ring_one [i ] = colorwheel (rc_index & 255 )
54
54
ring_one .show ()
55
- await asyncio .sleep (animation_controls .wait )
55
+ await asyncio .sleep (controls .wait )
56
56
57
57
58
- async def blink (animation_controls ):
58
+ async def blink (controls ):
59
59
"""Blink animation on ring two."""
60
60
while True :
61
61
ring_two .fill ((0 , 0 , 255 ))
62
62
ring_two .show ()
63
- await asyncio .sleep (animation_controls .delay )
63
+ await asyncio .sleep (controls .delay )
64
64
ring_two .fill ((0 , 0 , 0 ))
65
65
ring_two .show ()
66
- await asyncio .sleep (animation_controls .delay )
67
- await asyncio .sleep (animation_controls .wait )
66
+ await asyncio .sleep (controls .delay )
67
+ await asyncio .sleep (controls .wait )
68
68
69
69
70
- async def monitor_button (button , animation_controls ):
70
+ async def monitor_button (button , controls ):
71
71
"""Monitor button that reverses rainbow direction and changes blink speed.
72
72
Assume button is active low.
73
73
"""
@@ -76,22 +76,21 @@ async def monitor_button(button, animation_controls):
76
76
key_event = key .events .get ()
77
77
if key_event :
78
78
if key_event .pressed :
79
- animation_controls .reverse = True
80
- animation_controls .delay = 0.1
79
+ controls .reverse = True
80
+ controls .delay = 0.1
81
81
elif key_event .released :
82
- animation_controls .reverse = False
83
- animation_controls .delay = 0.5
84
- # Let another task run.
82
+ controls .reverse = False
83
+ controls .delay = 0.5
85
84
await asyncio .sleep (0 )
86
85
87
86
88
87
async def main ():
89
88
animation_controls = AnimationControls ()
90
- buttons_task = asyncio .create_task (monitor_button (button_pin , animation_controls ))
89
+ button_task = asyncio .create_task (monitor_button (button_pin , animation_controls ))
91
90
animation_task = asyncio .create_task (rainbow_cycle (animation_controls ))
92
91
blink_task = asyncio .create_task (blink (animation_controls ))
93
92
94
93
# This will run forever, because no tasks ever finish.
95
- await asyncio .gather (buttons_task , animation_task , blink_task )
94
+ await asyncio .gather (button_task , animation_task , blink_task )
96
95
97
96
asyncio .run (main ())
0 commit comments