1+ # SPDX-FileCopyrightText: 2024 J Fletcher
2+ #
3+ # SPDX-License-Identifier: MIT
4+
15# SIMPLE ASYNCIO EVENT EXAMPLE
26
37# Brief program that illustrates using Events to coordinate tasks
3337# Define the various colors according to preference and set them into
3438# a dictionary for later retrieval. (Blue is not used in this code.)
3539
40+
3641class Color :
3742 # pylint: disable=too-few-public-methods
3843 def __init__ (self , initial_value ):
3944 self .value = initial_value
4045
46+
4147# Create a class to hold and track the color while code executes.
4248
49+
4350async def blink (color ):
4451 with neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.1 ) as led :
4552 while True :
@@ -48,34 +55,38 @@ async def blink(color):
4855 led [0 ] = COLORS .get (color .value )
4956 await asyncio .sleep (0 )
5057
58+
5159# Instantiate the led using 'with ... as' construction to keep this
5260# function from blocking. 'COLORS.get(0)' indicates the led should show
5361# no color (i.e., turn off), while 'COLORS.get(color.value)' instructs
5462# the led to show the color pulled from the dictionary via the color
5563# class' color.value. The line 'asyncio.sleep(1)' sets the blink rate;
5664# in this case, once per second.
5765
66+
5867async def input_poll (swapper ):
5968 count = 0
6069 while True :
6170 button .update ()
6271 if button .fell :
63- print (' Press!' )
72+ print (" Press!" )
6473 if count == 0 :
6574 count += 1
66- print (' Event is set!' )
75+ print (" Event is set!" )
6776 swapper .set ()
6877 elif count == 1 :
6978 count -= 1
70- print (' Event is clear!' )
79+ print (" Event is clear!" )
7180 swapper .clear ()
7281 await asyncio .sleep (0 )
7382
83+
7484# This function checks the button for activity and sets or clears the
7585# Event depending on the button activity reflected in the 'count' variable.
7686# The count begins set at 0 and is alternatingly incremented (count += 1)
7787# and decremented (count -= 1) with each press of the button.
7888
89+
7990async def state (swapper , color ):
8091 while True :
8192 if swapper .is_set ():
@@ -84,25 +95,26 @@ async def state(swapper, color):
8495 color .value = 1
8596 await asyncio .sleep (0 )
8697
87- async def main ():
8898
99+ async def main ():
89100 color = Color (1 )
90101 COLORS .get (color )
91102
92- # Sets the color the led will first show on start
103+ # Sets the color the led will first show on start
93104
94105 swapper = asyncio .Event ()
95106
96- # Creates and names the Event that signals the led to change color
107+ # Creates and names the Event that signals the led to change color
97108
98109 blinky = asyncio .create_task (blink (color ))
99110 poll = asyncio .create_task (input_poll (swapper ))
100111 monitor = asyncio .create_task (state (swapper , color ))
101112
102- # Creates and names Tasks from the functions defined above
113+ # Creates and names Tasks from the functions defined above
103114
104115 await asyncio .gather (monitor , blinky , poll )
105116
117+
106118# Don't forget the 'await'! The 'asyncio.gather()' command passes the
107119# listed tasks to the asynchronous scheduler, where processing resources
108120# are directed from one task to another depending upon whether said task
0 commit comments