Skip to content

Commit 649e84a

Browse files
committed
Add code example for debounced buttons + fix review comments.
1 parent 5e75cad commit 649e84a

File tree

2 files changed

+159
-1
lines changed

2 files changed

+159
-1
lines changed

adafruit_cursorcontrol/cursorcontrol_cursormanager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class DebouncedCursorManager(CursorManager):
247247
def __init__(self, cursor: Cursor, debounce_interval: float = 0.01) -> None:
248248
CursorManager.__init__(self, cursor)
249249
self._debouncers = {}
250-
for btn, _ in self._pad_btns.items():
250+
for btn in self._pad_btns:
251251
self._debouncers[btn] = Debouncer(
252252
lambda btn=btn: bool((self._pad_states & (1 << self._pad_btns[btn]))),
253253
interval=debounce_interval,
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# SPDX-FileCopyrightText: 2022 flom84 for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import displayio
7+
import terminalio
8+
9+
from adafruit_button import Button
10+
from adafruit_cursorcontrol.cursorcontrol import Cursor
11+
from adafruit_cursorcontrol.cursorcontrol_cursormanager import DebouncedCursorManager
12+
13+
SELECT_BUTTON_X = 0
14+
SELECT_BUTTON_Y = 103
15+
SELECT_BUTTON_WIDTH = 60
16+
SELECT_BUTTON_HEIGHT = 25
17+
SELECT_BUTTON_STYLE = Button.ROUNDRECT
18+
SELECT_BUTTON_OUTLINE_COLOR = 0xFFFFFF
19+
SELECT_BUTTON_LABEL = "Select"
20+
SELECT_BUTTON_LABEL_COLOR = 0xFFFFFF
21+
22+
START_BUTTON_X = 100
23+
START_BUTTON_Y = 103
24+
START_BUTTON_WIDTH = 60
25+
START_BUTTON_HEIGHT = 25
26+
START_BUTTON_STYLE = Button.ROUNDRECT
27+
START_BUTTON_OUTLINE_COLOR = 0xFFFFFF
28+
START_BUTTON_LABEL = "Start"
29+
START_BUTTON_LABEL_COLOR = 0xFFFFFF
30+
31+
A_BUTTON_X = 120
32+
A_BUTTON_Y = 20
33+
A_BUTTON_WIDTH = 30
34+
A_BUTTON_HEIGHT = 30
35+
A_BUTTON_STYLE = Button.ROUNDRECT
36+
A_BUTTON_OUTLINE_COLOR = 0xFFFFFF
37+
A_BUTTON_LABEL = "A"
38+
A_BUTTON_LABEL_COLOR = 0xFFFFFF
39+
40+
B_BUTTON_X = 80
41+
B_BUTTON_Y = 30
42+
B_BUTTON_WIDTH = 30
43+
B_BUTTON_HEIGHT = 30
44+
B_BUTTON_STYLE = Button.ROUNDRECT
45+
B_BUTTON_OUTLINE_COLOR = 0xFFFFFF
46+
B_BUTTON_LABEL = "B"
47+
B_BUTTON_LABEL_COLOR = 0xFFFFFF
48+
49+
BUTTON_FILL_PURPLE = 0xB400FF
50+
BUTTON_FILL_BLACK = 0x000000
51+
52+
start_button = Button(
53+
x=START_BUTTON_X,
54+
y=START_BUTTON_Y,
55+
width=START_BUTTON_WIDTH,
56+
height=START_BUTTON_HEIGHT,
57+
style=START_BUTTON_STYLE,
58+
fill_color=BUTTON_FILL_BLACK,
59+
outline_color=START_BUTTON_OUTLINE_COLOR,
60+
label=START_BUTTON_LABEL,
61+
label_font=terminalio.FONT,
62+
label_color=START_BUTTON_LABEL_COLOR,
63+
)
64+
65+
select_button = Button(
66+
x=SELECT_BUTTON_X,
67+
y=SELECT_BUTTON_Y,
68+
width=SELECT_BUTTON_WIDTH,
69+
height=SELECT_BUTTON_HEIGHT,
70+
style=SELECT_BUTTON_STYLE,
71+
fill_color=BUTTON_FILL_BLACK,
72+
outline_color=SELECT_BUTTON_OUTLINE_COLOR,
73+
label=SELECT_BUTTON_LABEL,
74+
label_font=terminalio.FONT,
75+
label_color=SELECT_BUTTON_LABEL_COLOR,
76+
)
77+
78+
a_button = Button(
79+
x=A_BUTTON_X,
80+
y=A_BUTTON_Y,
81+
width=A_BUTTON_WIDTH,
82+
height=A_BUTTON_HEIGHT,
83+
style=A_BUTTON_STYLE,
84+
fill_color=BUTTON_FILL_BLACK,
85+
outline_color=A_BUTTON_OUTLINE_COLOR,
86+
label=A_BUTTON_LABEL,
87+
label_font=terminalio.FONT,
88+
label_color=A_BUTTON_LABEL_COLOR,
89+
)
90+
91+
b_button = Button(
92+
x=B_BUTTON_X,
93+
y=B_BUTTON_Y,
94+
width=B_BUTTON_WIDTH,
95+
height=B_BUTTON_HEIGHT,
96+
style=B_BUTTON_STYLE,
97+
fill_color=BUTTON_FILL_BLACK,
98+
outline_color=B_BUTTON_OUTLINE_COLOR,
99+
label=B_BUTTON_LABEL,
100+
label_font=terminalio.FONT,
101+
label_color=B_BUTTON_LABEL_COLOR,
102+
)
103+
104+
# Create the display
105+
display = board.DISPLAY
106+
107+
# Create the display context
108+
splash = displayio.Group()
109+
110+
# initialize the mouse cursor object
111+
mouse_cursor = Cursor(display, display_group=splash)
112+
113+
# initialize the debounced cursor manager
114+
debounced_cursor = DebouncedCursorManager(mouse_cursor)
115+
116+
# create displayio group
117+
splash.append(start_button)
118+
splash.append(select_button)
119+
splash.append(a_button)
120+
splash.append(b_button)
121+
display.show(splash)
122+
123+
while True:
124+
debounced_cursor.update()
125+
126+
if debounced_cursor.is_clicked:
127+
a_button.fill_color = BUTTON_FILL_PURPLE
128+
print("A pressed: " + str(debounced_cursor.held))
129+
130+
if debounced_cursor.released:
131+
a_button.fill_color = BUTTON_FILL_BLACK
132+
print("A pressed: " + str(debounced_cursor.held))
133+
134+
if debounced_cursor.is_alt_clicked:
135+
b_button.fill_color = BUTTON_FILL_PURPLE
136+
print("B pressed: " + str(debounced_cursor.alt_held))
137+
138+
if debounced_cursor.alt_released:
139+
b_button.fill_color = BUTTON_FILL_BLACK
140+
print("B pressed: " + str(debounced_cursor.alt_held))
141+
142+
if debounced_cursor.is_start_clicked:
143+
start_button.fill_color = BUTTON_FILL_PURPLE
144+
print("Start pressed: " + str(debounced_cursor.start_held))
145+
146+
if debounced_cursor.start_released:
147+
start_button.fill_color = BUTTON_FILL_BLACK
148+
print("Start pressed: " + str(debounced_cursor.start_held))
149+
150+
if debounced_cursor.is_select_clicked:
151+
select_button.fill_color = BUTTON_FILL_PURPLE
152+
print("Select pressed: " + str(debounced_cursor.select_held))
153+
154+
if debounced_cursor.select_released:
155+
select_button.fill_color = BUTTON_FILL_BLACK
156+
print("Select pressed: " + str(debounced_cursor.select_held))
157+
158+
time.sleep(0.01)

0 commit comments

Comments
 (0)