Simple test
Ensure your device works with these tests.
examples/async_button_example.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2023 Phil Underwood for Underwood Underground
3#
4# SPDX-License-Identifier: Unlicense
5import asyncio
6
7import board
8import digitalio
9
10from microcontroller import Pin
11
12from async_button import Button
13
14
15def setUpLed(pin: Pin):
16 dio = digitalio.DigitalInOut(pin)
17 dio.direction = digitalio.Direction.OUTPUT
18 dio.value = True
19 return dio
20
21
22CLICK_NAMES = {
23 Button.SINGLE: "Single click",
24 Button.DOUBLE: "Double click",
25 Button.TRIPLE: "Triple click",
26 Button.LONG: "Long click",
27}
28
29
30async def counter():
31 i = 0
32 while True:
33 print(f"COUNTER: {i}")
34 await asyncio.sleep(1)
35 i += 1
36
37
38async def button_led_watcher(button: Button, led: digitalio.DigitalInOut, click):
39 while True:
40 await button.wait((click,))
41 led.value = False
42 await asyncio.sleep(0.5)
43 led.value = True
44
45
46async def click_watcher(button: Button):
47 while True:
48 click = await button.wait_for_click()
49 print(f"{CLICK_NAMES[click]} seen")
50
51
52async def main():
53 # note Button must be created in an async environment
54 button = Button(
55 board.D5,
56 value_when_pressed=False,
57 triple_click_enable=True,
58 long_click_enable=True,
59 )
60 green = setUpLed(board.D9)
61 red = setUpLed(board.D8)
62 blue = setUpLed(board.D7)
63
64 red_watcher = button_led_watcher(button, red, Button.SINGLE)
65 green_watcher = button_led_watcher(button, green, Button.DOUBLE)
66 blue_watcher = button_led_watcher(button, blue, Button.TRIPLE)
67 await asyncio.gather(
68 counter(), click_watcher(button), red_watcher, green_watcher, blue_watcher
69 )
70
71
72asyncio.run(main())
examples/async_simplebutton_example.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2023 Phil Underwood for Underwood Underground
3#
4# SPDX-License-Identifier: Unlicense
5import asyncio
6
7import board
8import digitalio
9
10import async_button
11
12# green = digitalio.DigitalInOut(board.D9)
13# red = digitalio.DigitalInOut(board.D8)
14# blue = digitalio.DigitalInOut(board.D7)
15
16
17async def counter():
18 i = 0
19 while True:
20 print(f"COUNTER: {i}")
21 await asyncio.sleep(1)
22 i += 1
23
24
25async def button_watcher():
26 button = async_button.SimpleButton(board.D5, value_when_pressed=False)
27 led = digitalio.DigitalInOut(board.D8)
28 led.direction = digitalio.Direction.OUTPUT
29 led.value = True
30 while True:
31 await button.pressed()
32 print("Button pressed")
33 led.value = 0
34 await button.released()
35 print("Button released")
36 led.value = 1
37
38
39async def main():
40 await asyncio.gather(counter(), button_watcher())
41
42
43asyncio.run(main())
examples/async_multibutton_example.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2023 Phil Underwood for Underwood Underground
3#
4# SPDX-License-Identifier: Unlicense
5import asyncio
6
7import board
8
9from async_button import Button, MultiButton
10
11CLICK_NAMES = {
12 Button.SINGLE: "Single click",
13 Button.DOUBLE: "Double click",
14 Button.TRIPLE: "Triple click",
15 Button.LONG: "Long click",
16}
17
18
19async def counter():
20 i = 0
21 while True:
22 print(f"COUNTER: {i}")
23 await asyncio.sleep(1)
24 i += 1
25
26
27async def click_watcher(button: MultiButton):
28 while True:
29 button_name, click = await button.wait(a=Button.ANY_CLICK, b=Button.ANY_CLICK)
30 print(f"{button_name}: {CLICK_NAMES[click]} seen")
31
32
33async def main():
34 # note Button must be created in an async environment
35 button_a = Button(
36 board.D3,
37 value_when_pressed=False,
38 long_click_enable=True,
39 )
40 button_b = Button(
41 board.D4,
42 value_when_pressed=False,
43 long_click_enable=True,
44 )
45 multibutton = MultiButton(a=button_a, b=button_b)
46
47 await asyncio.gather(counter(), click_watcher(multibutton))
48
49
50asyncio.run(main())