Skip to content

Commit e2e89a0

Browse files
committed
Add asyncio template code.
1 parent 913ca10 commit e2e89a0

File tree

1 file changed

+84
-0
lines changed
  • CircuitPython_Templates/asyncio

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2022 Dan Halbert for Adafruit Industries
2+
# SPDX-FileCopyrightText: Copyright (c) 2022 Kattni Rembor for Adafruit Industries
3+
#
4+
# SPDX-License-Identifier: MIT
5+
"""
6+
CircuitPython asyncio example for two NeoPixel rings and one button.
7+
REMOVE THIS LINE AND THE REST OF THIS DOCSTRING BEFORE SUBMITTING TO LEARN
8+
9+
Update BUTTON to the pin that matches the pin to which the button is connected.
10+
11+
For example, on the QT Py RP2040, you would leave it as BUTTON to use the built-in Boot button.
12+
On the Feather M4 Express, you would wire the external button to A0, and update BUTTON to A0.
13+
"""
14+
import asyncio
15+
import board
16+
import neopixel
17+
import keypad
18+
from rainbowio import colorwheel
19+
20+
button_pin = board.BUTTON # The pin the button is connected to.
21+
num_pixels = 16 # The number of NeoPixels on a single ring.
22+
brightness = 0.2 # The LED brightness.
23+
24+
# Set up NeoPixel rings.
25+
ring_one = neopixel.NeoPixel(board.A1, num_pixels, brightness=brightness, auto_write=False)
26+
ring_two = neopixel.NeoPixel(board.A2, num_pixels, brightness=brightness, auto_write=False)
27+
28+
29+
class AnimationControls:
30+
"""The controls to allow you to vary the rainbow and blink animations."""
31+
def __init__(self):
32+
self.reverse = False
33+
self.wait = 0.0
34+
self.delay = 0.5
35+
36+
37+
async def rainbow_cycle(animation_controls):
38+
"""Rainbow cycle animation on ring one."""
39+
while True:
40+
for j in range(255, -1, -1) if animation_controls.reverse else range(0, 256, 1):
41+
for i in range(num_pixels):
42+
rc_index = (i * 256 // num_pixels) + j
43+
ring_one[i] = colorwheel(rc_index & 255)
44+
ring_one.show()
45+
await asyncio.sleep(animation_controls.wait)
46+
47+
48+
async def blink(animation_controls):
49+
"""Blink animation on ring two."""
50+
while True:
51+
ring_two[:] = [(0, 0, abs(ring_two[0][2] - 255))] * num_pixels
52+
await asyncio.sleep(animation_controls.delay)
53+
ring_two.show()
54+
await asyncio.sleep(animation_controls.wait)
55+
56+
57+
async def monitor_button(button, animation_controls):
58+
"""Monitor button that reverses rainbow direction and changes blink speed.
59+
Assume button is active low.
60+
"""
61+
with keypad.Keys((button,), value_when_pressed=False, pull=True) as key:
62+
while True:
63+
key_event = key.events.get()
64+
if key_event:
65+
if key_event.pressed:
66+
animation_controls.reverse = True
67+
animation_controls.delay = 0.1
68+
elif key_event.released:
69+
animation_controls.reverse = False
70+
animation_controls.delay = 0.5
71+
# Let another task run.
72+
await asyncio.sleep(0)
73+
74+
75+
async def main():
76+
animation_controls = AnimationControls()
77+
buttons_task = asyncio.create_task(monitor_button(button_pin, animation_controls))
78+
animation_task = asyncio.create_task(rainbow_cycle(animation_controls))
79+
blink_task = asyncio.create_task(blink(animation_controls))
80+
81+
# This will run forever, because no tasks ever finish.
82+
await asyncio.gather(buttons_task, animation_task, blink_task)
83+
84+
asyncio.run(main())

0 commit comments

Comments
 (0)