Skip to content

Commit 3e8a7ab

Browse files
authored
Add files via upload
1 parent 1f7562d commit 3e8a7ab

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

examples/asyncio_event_example.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# SIMPLE ASYNCIO EVENT EXAMPLE
2+
3+
# Brief program that illustrates using Events to coordinate tasks
4+
# within Asyncio programs. The present example involves only one led
5+
# and one event, but Asyncio allows a high degree of scaling. Adding
6+
# several copies of the functions 'blink', 'input_poll', or 'state'
7+
# should be straightforward with changes to names and objects.
8+
9+
import board
10+
import time
11+
import adafruit_ticks
12+
import digitalio
13+
from adafruit_debouncer import Debouncer
14+
import neopixel
15+
import asyncio
16+
17+
# Import library modules, as is tradition
18+
19+
pin = digitalio.DigitalInOut(board.BUTTON)
20+
pin.direction = digitalio.Direction.INPUT
21+
pin.pull = digitalio.Pull.UP
22+
button = Debouncer(pin)
23+
24+
# Instantiate the input, in this case, the 'BOOT' button on a
25+
# QT Py 2040. The debouncer ensures a clean hit.
26+
27+
BLANK = (0, 0, 0, 0)
28+
RED = (255, 0, 0)
29+
GREEN = (0, 255, 0)
30+
BLUE = (0, 0, 255)
31+
32+
COLORS = {0: BLANK, 1: RED, 2: GREEN, 3: BLUE}
33+
34+
# Define the various colors according to preference and set them into
35+
# a dictionary for later retrieval. (Blue is not used in this code.)
36+
37+
class color:
38+
def __init__(self, initial_value):
39+
self.value = initial_value
40+
41+
# Create a class to hold and track the color while code executes.
42+
43+
async def blink(color):
44+
with neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.1) as led:
45+
while True:
46+
led[0] = COLORS.get(0)
47+
await asyncio.sleep(1)
48+
led[0] = COLORS.get(color.value)
49+
await asyncio.sleep(0)
50+
51+
# Instantiate the led using 'with ... as' construction to keep this
52+
# function from blocking. 'COLORS.get(0)' indicates the led should show
53+
# no color (i.e., turn off), while 'COLORS.get(color.value)' instructs
54+
# the led to show the color pulled from the dictionary via the color
55+
# class' color.value. The line 'asyncio.sleep(1)' sets the blink rate;
56+
# in this case, once per second.
57+
58+
async def input_poll(swapper):
59+
count = 0
60+
while True:
61+
button.update()
62+
if button.fell:
63+
print('Press!')
64+
if count == 0:
65+
count += 1
66+
print('Event is set!')
67+
swapper.set()
68+
elif count == 1:
69+
count -= 1
70+
print('Event is clear!')
71+
swapper.clear()
72+
await asyncio.sleep(0)
73+
74+
# This function checks the button for activity and sets or clears the
75+
# Event depending on the button activity reflected in the 'count' variable.
76+
# The count begins set at 0 and is alternatingly incremented (count += 1)
77+
# and decremented (count -= 1) with each press of the button.
78+
79+
async def state(swapper, color):
80+
while True:
81+
if swapper.is_set():
82+
color.value = 2
83+
else:
84+
color.value = 1
85+
await asyncio.sleep(0)
86+
87+
async def main():
88+
89+
color.value = 1
90+
COLORS.get(color)
91+
92+
# Sets the color the led will first show on start
93+
94+
swapper = asyncio.Event()
95+
96+
# Creates and names the Event that signals the led to change color
97+
98+
blinky = asyncio.create_task(blink(color))
99+
poll = asyncio.create_task(input_poll(swapper))
100+
monitor = asyncio.create_task(state(swapper, color))
101+
102+
# Creates and names Tasks from the functions defined above
103+
104+
await asyncio.gather(monitor, blinky, poll)
105+
106+
# Don't forget the 'await'! The 'asyncio.gather()' command passes the
107+
# listed tasks to the asynchronous scheduler, where processing resources
108+
# are directed from one task to another depending upon whether said task
109+
# has signalled its ability to 'give up' control by reaching the 'await
110+
# asyncio.sleep()' line.
111+
112+
asyncio.run(main())

0 commit comments

Comments
 (0)