Merge pull request #2135 from kattni/asyncio-cleanup

Code tweak, GIF upload.
This commit is contained in:
Dan Halbert 2022-04-07 12:43:25 -04:00 committed by GitHub
commit 926cbc464f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 16 deletions

View file

@ -15,7 +15,7 @@ On the Feather M4 Express, you would wire the external button to A0, and update
Two: THIS IS ONLY NECESSARY IF THE BUILT-IN BUTTON IS ACTIVE HIGH. For example the built-in Two: THIS IS ONLY NECESSARY IF THE BUILT-IN BUTTON IS ACTIVE HIGH. For example the built-in
buttons on the Circuit Playground Bluefruit and the MagTag are active high. buttons on the Circuit Playground Bluefruit and the MagTag are active high.
If your button is ACTIVE HIGH, under "async def monitor_button(button, animation_controls)", If your button is ACTIVE HIGH, under "async def monitor_button(button, controls)",
update the following line of code: update the following line of code:
with keypad.Keys((button,), value_when_pressed=False, pull=True) as key: with keypad.Keys((button,), value_when_pressed=False, pull=True) as key:
To the following: To the following:
@ -44,30 +44,30 @@ class AnimationControls:
self.delay = 0.5 self.delay = 0.5
async def rainbow_cycle(animation_controls): async def rainbow_cycle(controls):
"""Rainbow cycle animation on ring one.""" """Rainbow cycle animation on ring one."""
while True: while True:
for j in range(255, -1, -1) if animation_controls.reverse else range(0, 256, 1): for j in range(255, -1, -1) if controls.reverse else range(0, 256, 1):
for i in range(num_pixels): for i in range(num_pixels):
rc_index = (i * 256 // num_pixels) + j rc_index = (i * 256 // num_pixels) + j
ring_one[i] = colorwheel(rc_index & 255) ring_one[i] = colorwheel(rc_index & 255)
ring_one.show() ring_one.show()
await asyncio.sleep(animation_controls.wait) await asyncio.sleep(controls.wait)
async def blink(animation_controls): async def blink(controls):
"""Blink animation on ring two.""" """Blink animation on ring two."""
while True: while True:
ring_two.fill((0, 0, 255)) ring_two.fill((0, 0, 255))
ring_two.show() ring_two.show()
await asyncio.sleep(animation_controls.delay) await asyncio.sleep(controls.delay)
ring_two.fill((0, 0, 0)) ring_two.fill((0, 0, 0))
ring_two.show() ring_two.show()
await asyncio.sleep(animation_controls.delay) await asyncio.sleep(controls.delay)
await asyncio.sleep(animation_controls.wait) await asyncio.sleep(controls.wait)
async def monitor_button(button, animation_controls): async def monitor_button(button, controls):
"""Monitor button that reverses rainbow direction and changes blink speed. """Monitor button that reverses rainbow direction and changes blink speed.
Assume button is active low. Assume button is active low.
""" """
@ -76,22 +76,21 @@ async def monitor_button(button, animation_controls):
key_event = key.events.get() key_event = key.events.get()
if key_event: if key_event:
if key_event.pressed: if key_event.pressed:
animation_controls.reverse = True controls.reverse = True
animation_controls.delay = 0.1 controls.delay = 0.1
elif key_event.released: elif key_event.released:
animation_controls.reverse = False controls.reverse = False
animation_controls.delay = 0.5 controls.delay = 0.5
# Let another task run.
await asyncio.sleep(0) await asyncio.sleep(0)
async def main(): async def main():
animation_controls = AnimationControls() animation_controls = AnimationControls()
buttons_task = asyncio.create_task(monitor_button(button_pin, animation_controls)) button_task = asyncio.create_task(monitor_button(button_pin, animation_controls))
animation_task = asyncio.create_task(rainbow_cycle(animation_controls)) animation_task = asyncio.create_task(rainbow_cycle(animation_controls))
blink_task = asyncio.create_task(blink(animation_controls)) blink_task = asyncio.create_task(blink(animation_controls))
# This will run forever, because no tasks ever finish. # This will run forever, because no tasks ever finish.
await asyncio.gather(buttons_task, animation_task, blink_task) await asyncio.gather(button_task, animation_task, blink_task)
asyncio.run(main()) asyncio.run(main())

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 MiB