forked from adafruit/Adafruit_CircuitPython_asyncio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncio_simpletest.py
56 lines (38 loc) · 1.55 KB
/
asyncio_simpletest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# -----[Example for one led]-----------------------------
import asyncio
import board
import digitalio
async def blink(pin, interval, count): # Don't forget the async!
with digitalio.DigitalInOut(pin) as led:
led.switch_to_output(value=False)
for _ in range(count):
led.value = True
await asyncio.sleep(interval) # Don't forget the await!
led.value = False
await asyncio.sleep(interval) # Don't forget the await!
async def main(): # Don't forget the async!
led_task = asyncio.create_task(blink(board.D1, 0.25, 10))
await asyncio.gather(led_task) # Don't forget the await!
print("done")
asyncio.run(main())
# -----[Example for two leds]-------------------------------
# Import modules as above
async def blink(pin, interval, count):
# pylint: disable=function-redefined
with digitalio.DigitalInOut(pin) as led:
led.switch_to_output(value=False)
for _ in range(count):
led.value = True
await asyncio.sleep(interval) # Don't forget the "await"!
led.value = False
await asyncio.sleep(interval) # Don't forget the "await"!
async def main():
# pylint: disable=function-redefined
led1_task = asyncio.create_task(blink(board.D1, 0.25, 10))
led2_task = asyncio.create_task(blink(board.D2, 0.1, 20))
await asyncio.gather(led1_task, led2_task) # Don't forget "await"!
print("done")
asyncio.run(main())