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. # SPDX-FileCopyrightText: Copyright (c) 2022 Dan Halbert for Adafruit Industries # This will run forever, because no tasks ever finish.Īwait asyncio.gather(button_task, animation_task, blink_task) With keypad.Keys((button,), value_when_pressed=False, pull=True) as key:īutton_task = asyncio.create_task(monitor_button(button_pin, animation_controls))Īnimation_task = asyncio.create_task(rainbow_cycle(animation_controls))īlink_task = asyncio.create_task(blink(animation_controls)) """Monitor button that reverses rainbow direction and changes blink speed. """Rainbow cycle animation on ring one."""įor j in range(255, -1, -1) if controls.reverse else range(0, 256, 1):Īsync def monitor_button(button, controls): """The controls to allow you to vary the rainbow and blink animations.""" Ring_two = neopixel.NeoPixel(board.A2, num_pixels, brightness=brightness, auto_write=False) Ring_one = neopixel.NeoPixel(board.A1, num_pixels, brightness=brightness, auto_write=False) Num_pixels = 16 # The number of NeoPixels on a single ring. # SPDX-FileCopyrightText: Copyright (c) 2022 Kattni Rembor for Adafruit IndustriesĬircuitPython asyncio example for two NeoPixel rings and one button.īutton_pin = board.BUTTON # The pin the button is connected to. This diagram shows the scheduler, running an event loop, with three tasks: Task 1 is running, Task 2 is ready to run, and is waiting for Task 1 to give up control, and Task 3 is waiting for something else, and isn't ready to run yet.Ĭopy Code # SPDX-FileCopyrightText: Copyright (c) 2022 Dan Halbert for Adafruit Industries A coroutine is declared with the keyword async, and the keyword await indicates that the coroutine is giving up control at that point. When the coroutine is called again, it starts where it left off. A coroutine can stop in the middle of some code. The scheduler runs an event loop which repeats this process over and over for all the tasks assigned to the event loop.Ī task is a kind of coroutine. When a task gives up control and starts waiting, the scheduler starts another task that is ready to run. In cooperative multitasking, a scheduler manages the tasks. Each task runs until it needs to wait for something, or until it decides it has run for long enough and should let another task run. Cooperative multitasking is a style of programming in which multiple tasks take turns running. CircuitPython uses the asyncio library to support cooperative multitasking in CircuitPython, which includes the async and await language keywords.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |