Skip to content

Commit 926cbc4

Browse files
authored
Merge pull request #2135 from kattni/asyncio-cleanup
Code tweak, GIF upload.
2 parents c1d5837 + 7aa1172 commit 926cbc4

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

CircuitPython_Templates/asyncio/code.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
Two: THIS IS ONLY NECESSARY IF THE BUILT-IN BUTTON IS ACTIVE HIGH. For example the built-in
1717
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)",
1919
update the following line of code:
2020
with keypad.Keys((button,), value_when_pressed=False, pull=True) as key:
2121
To the following:
@@ -44,30 +44,30 @@ def __init__(self):
4444
self.delay = 0.5
4545

4646

47-
async def rainbow_cycle(animation_controls):
47+
async def rainbow_cycle(controls):
4848
"""Rainbow cycle animation on ring one."""
4949
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):
5151
for i in range(num_pixels):
5252
rc_index = (i * 256 // num_pixels) + j
5353
ring_one[i] = colorwheel(rc_index & 255)
5454
ring_one.show()
55-
await asyncio.sleep(animation_controls.wait)
55+
await asyncio.sleep(controls.wait)
5656

5757

58-
async def blink(animation_controls):
58+
async def blink(controls):
5959
"""Blink animation on ring two."""
6060
while True:
6161
ring_two.fill((0, 0, 255))
6262
ring_two.show()
63-
await asyncio.sleep(animation_controls.delay)
63+
await asyncio.sleep(controls.delay)
6464
ring_two.fill((0, 0, 0))
6565
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)
6868

6969

70-
async def monitor_button(button, animation_controls):
70+
async def monitor_button(button, controls):
7171
"""Monitor button that reverses rainbow direction and changes blink speed.
7272
Assume button is active low.
7373
"""
@@ -76,22 +76,21 @@ async def monitor_button(button, animation_controls):
7676
key_event = key.events.get()
7777
if key_event:
7878
if key_event.pressed:
79-
animation_controls.reverse = True
80-
animation_controls.delay = 0.1
79+
controls.reverse = True
80+
controls.delay = 0.1
8181
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
8584
await asyncio.sleep(0)
8685

8786

8887
async def main():
8988
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))
9190
animation_task = asyncio.create_task(rainbow_cycle(animation_controls))
9291
blink_task = asyncio.create_task(blink(animation_controls))
9392

9493
# 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)
9695

9796
asyncio.run(main())
13.5 MB
Loading
11.4 MB
Loading

0 commit comments

Comments
 (0)