From 8e7f4a502bd490dc0f640c60c015c20f378fb9a3 Mon Sep 17 00:00:00 2001 From: johnnohj <166672127+johnnohj@users.noreply.github.com> Date: Thu, 6 Jun 2024 20:49:10 +0000 Subject: [PATCH] Pending changes exported from your codespace --- examples/asyncio_event_example.py | 26 +++++++++++++++++++------- examples/asyncio_simpletest.py | 1 - 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/examples/asyncio_event_example.py b/examples/asyncio_event_example.py index d61ae03..ff62259 100644 --- a/examples/asyncio_event_example.py +++ b/examples/asyncio_event_example.py @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2024 J Fletcher +# +# SPDX-License-Identifier: MIT + # SIMPLE ASYNCIO EVENT EXAMPLE # Brief program that illustrates using Events to coordinate tasks @@ -33,13 +37,16 @@ # Define the various colors according to preference and set them into # a dictionary for later retrieval. (Blue is not used in this code.) + class Color: # pylint: disable=too-few-public-methods def __init__(self, initial_value): self.value = initial_value + # Create a class to hold and track the color while code executes. + async def blink(color): with neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.1) as led: while True: @@ -48,6 +55,7 @@ async def blink(color): led[0] = COLORS.get(color.value) await asyncio.sleep(0) + # Instantiate the led using 'with ... as' construction to keep this # function from blocking. 'COLORS.get(0)' indicates the led should show # no color (i.e., turn off), while 'COLORS.get(color.value)' instructs @@ -55,27 +63,30 @@ async def blink(color): # class' color.value. The line 'asyncio.sleep(1)' sets the blink rate; # in this case, once per second. + async def input_poll(swapper): count = 0 while True: button.update() if button.fell: - print('Press!') + print("Press!") if count == 0: count += 1 - print('Event is set!') + print("Event is set!") swapper.set() elif count == 1: count -= 1 - print('Event is clear!') + print("Event is clear!") swapper.clear() await asyncio.sleep(0) + # This function checks the button for activity and sets or clears the # Event depending on the button activity reflected in the 'count' variable. # The count begins set at 0 and is alternatingly incremented (count += 1) # and decremented (count -= 1) with each press of the button. + async def state(swapper, color): while True: if swapper.is_set(): @@ -84,25 +95,26 @@ async def state(swapper, color): color.value = 1 await asyncio.sleep(0) -async def main(): +async def main(): color = Color(1) COLORS.get(color) -# Sets the color the led will first show on start + # Sets the color the led will first show on start swapper = asyncio.Event() -# Creates and names the Event that signals the led to change color + # Creates and names the Event that signals the led to change color blinky = asyncio.create_task(blink(color)) poll = asyncio.create_task(input_poll(swapper)) monitor = asyncio.create_task(state(swapper, color)) -# Creates and names Tasks from the functions defined above + # Creates and names Tasks from the functions defined above await asyncio.gather(monitor, blinky, poll) + # Don't forget the 'await'! The 'asyncio.gather()' command passes the # listed tasks to the asynchronous scheduler, where processing resources # are directed from one task to another depending upon whether said task diff --git a/examples/asyncio_simpletest.py b/examples/asyncio_simpletest.py index 160f2d1..693a13e 100644 --- a/examples/asyncio_simpletest.py +++ b/examples/asyncio_simpletest.py @@ -54,4 +54,3 @@ async def main(): asyncio.run(main()) -