Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3"""
4Simple button example.
5"""
6
7import board
8import displayio
9import terminalio
10import adafruit_touchscreen
11from adafruit_button import Button
12
13# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
14# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
15# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
16display = board.DISPLAY
17
18# --| Button Config |-------------------------------------------------
19BUTTON_X = 110
20BUTTON_Y = 95
21BUTTON_WIDTH = 100
22BUTTON_HEIGHT = 50
23BUTTON_STYLE = Button.ROUNDRECT
24BUTTON_FILL_COLOR = 0x00FFFF
25BUTTON_OUTLINE_COLOR = 0xFF00FF
26BUTTON_LABEL = "HELLO WORLD"
27BUTTON_LABEL_COLOR = 0x000000
28# --| Button Config |-------------------------------------------------
29
30# Setup touchscreen (PyPortal)
31ts = adafruit_touchscreen.Touchscreen(
32 board.TOUCH_XL,
33 board.TOUCH_XR,
34 board.TOUCH_YD,
35 board.TOUCH_YU,
36 calibration=((5200, 59000), (5800, 57000)),
37 size=(display.width, display.height),
38)
39
40# Make the display context
41splash = displayio.Group()
42display.root_group = splash
43
44# Make the button
45button = Button(
46 x=BUTTON_X,
47 y=BUTTON_Y,
48 width=BUTTON_WIDTH,
49 height=BUTTON_HEIGHT,
50 style=BUTTON_STYLE,
51 fill_color=BUTTON_FILL_COLOR,
52 outline_color=BUTTON_OUTLINE_COLOR,
53 label=BUTTON_LABEL,
54 label_font=terminalio.FONT,
55 label_color=BUTTON_LABEL_COLOR,
56)
57
58# Add button to the display context
59splash.append(button)
60
61# Loop and look for touches
62while True:
63 p = ts.touch_point
64 if p:
65 if button.contains(p):
66 button.selected = True
67 else:
68 button.selected = False # if touch is dragged outside of button
69 else:
70 button.selected = False # if touch is released
Soundboard¶
A soundboard made with buttons
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3"""
4Soundboard example with buttons.
5"""
6
7import time
8from adafruit_pyportal import PyPortal
9from adafruit_button import Button
10
11SHOW_BUTTONS = False
12
13# the current working directory (where this file is)
14cwd = ("/" + __file__).rsplit("/", 1)[0]
15# No internet use version of pyportal
16pyportal = PyPortal(default_bg=cwd + "/button_background.bmp")
17
18spots = []
19spots.append({"label": "1", "pos": (10, 10), "size": (60, 60), "file": "01.wav"})
20spots.append({"label": "2", "pos": (90, 10), "size": (60, 60), "file": "02.wav"})
21spots.append({"label": "3", "pos": (170, 10), "size": (60, 60), "file": "03.wav"})
22spots.append({"label": "4", "pos": (250, 10), "size": (60, 60), "file": "04.wav"})
23spots.append({"label": "5", "pos": (10, 90), "size": (60, 60), "file": "05.wav"})
24spots.append({"label": "6", "pos": (90, 90), "size": (60, 60), "file": "06.wav"})
25spots.append({"label": "7", "pos": (170, 90), "size": (60, 60), "file": "07.wav"})
26spots.append({"label": "8", "pos": (250, 90), "size": (60, 60), "file": "08.wav"})
27spots.append({"label": "9", "pos": (10, 170), "size": (60, 60), "file": "09.wav"})
28spots.append({"label": "10", "pos": (90, 170), "size": (60, 60), "file": "10.wav"})
29spots.append({"label": "11", "pos": (170, 170), "size": (60, 60), "file": "11.wav"})
30spots.append({"label": "12", "pos": (250, 170), "size": (60, 60), "file": "12.wav"})
31
32buttons = []
33for spot in spots:
34 fill = outline = None
35 if SHOW_BUTTONS:
36 fill = None
37 outline = 0x00FF00
38 button = Button(
39 x=spot["pos"][0],
40 y=spot["pos"][1],
41 width=spot["size"][0],
42 height=spot["size"][1],
43 fill_color=fill,
44 outline_color=outline,
45 label=spot["label"],
46 label_color=None,
47 name=spot["file"],
48 )
49 pyportal.splash.append(button)
50 buttons.append(button)
51
52last_pressed = None
53currently_pressed = None
54while True:
55 p = pyportal.touchscreen.touch_point
56 if p:
57 print(p)
58 for b in buttons:
59 if b.contains(p):
60 print("Touched", b.name)
61 if currently_pressed != b: # don't restart if playing
62 pyportal.play_file(cwd + "/" + b.name, wait_to_finish=False)
63 currently_pressed = b
64 break
65 else:
66 currently_pressed = None
67 time.sleep(0.05)