Skip to content

Commit 5e75cad

Browse files
committed
Add debouncer support for B,Select and Start buttons.
1 parent eb648da commit 5e75cad

File tree

1 file changed

+74
-16
lines changed

1 file changed

+74
-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.items():
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()

0 commit comments

Comments
 (0)