Skip to content

Commit 53ad808

Browse files
authored
Merge pull request #34 from flom84/pygamer-start-select-buttons-debouncer
Add debouncer support for B,Select and Start buttons.
2 parents 5730d0c + 649e84a commit 53ad808

File tree

2 files changed

+232
-16
lines changed

2 files changed

+232
-16
lines changed

adafruit_cursorcontrol/cursorcontrol_cursormanager.py

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -236,45 +236,103 @@ def _check_cursor_movement(self) -> None:
236236

237237

238238
class DebouncedCursorManager(CursorManager):
239-
"""Simple interaction user interface interaction for Adafruit_CursorControl.
240-
This subclass provide a debounced version on the A button and provides queries for when
241-
the button is just pressed, and just released, as well it's current state. "Just" in this
242-
context means "since the previous call to update."
239+
"""Simple user interface interaction for Adafruit_CursorControl.
240+
This subclass provide a debounced version on the A, B, Start or Select buttons
241+
and provides queries for when the buttons are just pressed, and just released,
242+
as well their current state. "Just" in this context means "since the previous call to update."
243243
244244
:param Cursor cursor: The cursor object we are using.
245245
"""
246246

247247
def __init__(self, cursor: Cursor, debounce_interval: float = 0.01) -> None:
248248
CursorManager.__init__(self, cursor)
249-
self._debouncer = Debouncer(
250-
lambda: bool(self._pad_states & (1 << self._pad_btns["btn_a"])),
251-
interval=debounce_interval,
252-
)
249+
self._debouncers = {}
250+
for btn in self._pad_btns:
251+
self._debouncers[btn] = Debouncer(
252+
lambda btn=btn: bool((self._pad_states & (1 << self._pad_btns[btn]))),
253+
interval=debounce_interval,
254+
)
253255

254256
@property
255257
def is_clicked(self) -> bool:
256-
"""Returns True if the cursor button was pressed
258+
"""Returns True if the cursor A button was pressed
259+
during previous call to update()
260+
"""
261+
return self._debouncers["btn_a"].rose
262+
263+
@property
264+
def is_alt_clicked(self) -> bool:
265+
"""Returns True if the cursor B button was pressed
257266
during previous call to update()
258267
"""
259-
return self._debouncer.rose
268+
return self._debouncers["btn_b"].rose
260269

261-
pressed = is_clicked
270+
@property
271+
def is_select_clicked(self) -> bool:
272+
"""Returns True if the Select button was pressed
273+
during previous call to update()
274+
"""
275+
return self._debouncers["btn_select"].rose
276+
277+
@property
278+
def is_start_clicked(self) -> bool:
279+
"""Returns True if the Start button was pressed
280+
during previous call to update()
281+
"""
282+
return self._debouncers["btn_start"].rose
262283

263284
@property
264285
def released(self) -> bool:
265-
"""Returns True if the cursor button was released
286+
"""Returns True if the cursor A button was released
287+
during previous call to update()
288+
"""
289+
return self._debouncers["btn_a"].fell
290+
291+
@property
292+
def alt_released(self) -> bool:
293+
"""Returns True if the cursor B button was released
266294
during previous call to update()
267295
"""
268-
return self._debouncer.fell
296+
return self._debouncers["btn_b"].fell
297+
298+
@property
299+
def start_released(self) -> bool:
300+
"""Returns True if the cursor Start button was released
301+
during previous call to update()
302+
"""
303+
return self._debouncers["btn_start"].fell
304+
305+
@property
306+
def select_released(self) -> bool:
307+
"""Returns True if the cursor Select button was released
308+
during previous call to update()
309+
"""
310+
return self._debouncers["btn_select"].fell
269311

270312
@property
271313
def held(self) -> bool:
272-
"""Returns True if the cursor button is currently being held"""
273-
return self._debouncer.value
314+
"""Returns True if the cursor A button is currently being held"""
315+
return self._debouncers["btn_a"].value
316+
317+
@property
318+
def alt_held(self) -> bool:
319+
"""Returns True if the cursor B button is currently being held"""
320+
return self._debouncers["btn_b"].value
321+
322+
@property
323+
def start_held(self) -> bool:
324+
"""Returns True if the cursor Start button is currently being held"""
325+
return self._debouncers["btn_start"].value
326+
327+
@property
328+
def select_held(self) -> bool:
329+
"""Returns True if the cursor Select is currently being held"""
330+
return self._debouncers["btn_select"].value
274331

275332
def update(self) -> None:
276333
"""Updates the cursor object."""
277334
if self._pad.events.get_into(self._event):
278335
self._store_button_states()
279336
self._check_cursor_movement()
280-
self._debouncer.update()
337+
for debouncer in self._debouncers.values():
338+
debouncer.update()
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)