Skip to content

Commit 981e939

Browse files
authoredDec 5, 2024··
Merge pull request #53 from FoamyGuy/debounced_example
add debounced examples
2 parents 1ec53fc + 7da4638 commit 981e939

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
 

‎examples/display_button_debounced.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
Basic debounced button example.
5+
"""
6+
7+
import adafruit_touchscreen
8+
import board
9+
import displayio
10+
import terminalio
11+
12+
from 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
17+
display = board.DISPLAY
18+
19+
# --| Button Config |-------------------------------------------------
20+
BUTTON_X = 110
21+
BUTTON_Y = 95
22+
BUTTON_WIDTH = 100
23+
BUTTON_HEIGHT = 50
24+
BUTTON_STYLE = Button.ROUNDRECT
25+
BUTTON_FILL_COLOR = 0x00FFFF
26+
BUTTON_OUTLINE_COLOR = 0xFF00FF
27+
BUTTON_LABEL = "HELLO WORLD"
28+
BUTTON_LABEL_COLOR = 0x000000
29+
# --| Button Config |-------------------------------------------------
30+
31+
# Setup touchscreen (PyPortal)
32+
ts = 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
42+
splash = displayio.Group()
43+
display.root_group = splash
44+
45+
# Make the button
46+
button = 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
60+
splash.append(button)
61+
62+
# Loop and look for touches
63+
while True:
64+
p = ts.touch_point
65+
if p:
66+
if button.contains(p):
67+
if not button.selected:
68+
button.selected = True
69+
print("pressed")
70+
else:
71+
button.selected = False # if touch is dragged outside of button
72+
else:
73+
button.selected = False # if touch is released
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
import time
4+
5+
import adafruit_touchscreen
6+
import board
7+
import displayio
8+
import terminalio
9+
10+
from adafruit_button.sprite_button import SpriteButton
11+
12+
"""
13+
Sprite button debounced example
14+
"""
15+
16+
# These pins are used as both analog and digital! XL, XR and YU must be analog
17+
# and digital capable. YD just need to be digital
18+
ts = adafruit_touchscreen.Touchscreen(
19+
board.TOUCH_XL,
20+
board.TOUCH_XR,
21+
board.TOUCH_YD,
22+
board.TOUCH_YU,
23+
calibration=((5200, 59000), (5800, 57000)),
24+
size=(board.DISPLAY.width, board.DISPLAY.height),
25+
)
26+
27+
# Make the display context
28+
main_group = displayio.Group()
29+
board.DISPLAY.root_group = main_group
30+
31+
BUTTON_WIDTH = 10 * 16
32+
BUTTON_HEIGHT = 3 * 16
33+
BUTTON_MARGIN = 20
34+
35+
font = terminalio.FONT
36+
37+
buttons = []
38+
39+
40+
button_0 = SpriteButton(
41+
x=BUTTON_MARGIN,
42+
y=BUTTON_MARGIN,
43+
width=BUTTON_WIDTH,
44+
height=BUTTON_HEIGHT,
45+
label="button0",
46+
label_font=font,
47+
bmp_path="bmps/gradient_button_0.bmp",
48+
selected_bmp_path="bmps/gradient_button_1.bmp",
49+
transparent_index=0,
50+
)
51+
52+
buttons.append(button_0)
53+
54+
for b in buttons:
55+
main_group.append(b)
56+
while True:
57+
p = ts.touch_point
58+
if p:
59+
for i, b in enumerate(buttons):
60+
if b.contains(p):
61+
if not b.selected:
62+
print("Button %d pressed" % i)
63+
b.selected = True
64+
b.label = "pressed"
65+
else:
66+
b.selected = False
67+
b.label = f"button{i}"
68+
69+
else:
70+
for i, b in enumerate(buttons):
71+
if b.selected:
72+
b.selected = False
73+
b.label = f"button{i}"
74+
time.sleep(0.01)

0 commit comments

Comments
 (0)
Please sign in to comment.