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