Skip to content

Commit 8e3a77f

Browse files
authored
Update asyncio_simpletest.py
Simple copy/paste beginning from https://learn.adafruit.com/cooperative-multitasking-in-circuitpython-with-asyncio/concurrent-tasks#one-led-3106269 to correct an issue raised, of all places, in the Issues section.
1 parent 3e8a7ab commit 8e3a77f

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

examples/asyncio_simpletest.py

+56-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2-
# SPDX-FileCopyrightText: Copyright (c) 2021 Dan Halbert for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
32
#
4-
# SPDX-License-Identifier: Unlicense
3+
# SPDX-License-Identifier: MIT
4+
5+
### Example for one led:
6+
7+
import asyncio
8+
import board
9+
import digitalio
10+
11+
12+
async def blink(pin, interval, count): # Don't forget the async!
13+
with digitalio.DigitalInOut(pin) as led:
14+
led.switch_to_output(value=False)
15+
for _ in range(count):
16+
led.value = True
17+
await asyncio.sleep(interval) # Don't forget the await!
18+
led.value = False
19+
await asyncio.sleep(interval) # Don't forget the await!
20+
21+
22+
async def main(): # Don't forget the async!
23+
led_task = asyncio.create_task(blink(board.D1, 0.25, 10))
24+
await asyncio.gather(led_task) # Don't forget the await!
25+
print("done")
26+
27+
28+
asyncio.run(main())
29+
30+
31+
### Example for two leds:
32+
33+
import asyncio
34+
import board
35+
import digitalio
36+
37+
38+
async def blink(pin, interval, count):
39+
with digitalio.DigitalInOut(pin) as led:
40+
led.switch_to_output(value=False)
41+
for _ in range(count):
42+
led.value = True
43+
await asyncio.sleep(interval) # Don't forget the "await"!
44+
led.value = False
45+
await asyncio.sleep(interval) # Don't forget the "await"!
46+
47+
48+
async def main():
49+
led1_task = asyncio.create_task(blink(board.D1, 0.25, 10))
50+
led2_task = asyncio.create_task(blink(board.D2, 0.1, 20))
51+
52+
await asyncio.gather(led1_task, led2_task) # Don't forget "await"!
53+
print("done")
54+
55+
56+
asyncio.run(main())
57+

0 commit comments

Comments
 (0)