async def blink(controls):
"""Blink animation on ring two."""
while True:
ring_two.fill((0, 0, 255))
ring_two.show()
await asyncio.sleep(controls.delay)
ring_two.fill((0, 0, 0))
ring_two.show()
await asyncio.sleep(controls.delay)
await asyncio.sleep(controls.wait)
In both functions, you must call show() on the NeoPixel ring object to get the
animations to run because you set auto_write=False in the NeoPixel ring setup.
Notice that the controls object provides the animation direction ( controls.reverse )
, the delay between steps of the animation ( controls.delay ), and the delay
between complete animations ( controls.wait ).
In terms of the asyncio-specific parts of this code, you'll notice that both of these
functions begin with async def . Every function that contains an await must be
defined as async def , to indicate that it's a coroutine.
Both functions contain one or more await lines. What does await mean? await
means "I need to wait for something; let other tasks run until I'm ready to resume."
Both include await asyncio.sleep() . Basically, when when the code goes to
"sleep", another task can be run. When the sleep() is over, this coroutine will
resume.
The blink() includes the following line of code twice, which utilizes the .delay
attribute of the AnimationsControl object.
await asyncio.sleep(controls.delay)
Both functions end with the following line of code which utilizes the .wait attribute
of the AnimationsControl object.
await asyncio.sleep(controls.wait)
The next function is called main() .
In main() , first create a task. For the button_task , instantiate the monitor_butto
n() coroutine by calling it with the arguments desired, and then pass that coroutine
to asyncio.create_task() . create_task() wraps the coroutine in a task, and
©Adafruit Industries Page 187 of 263