Skip to content

Commit 0c5cc05

Browse files
authored
Merge pull request #3 from dastels/master
Use debouncer to handle button analysis
2 parents 4f1f480 + ab67ed0 commit 0c5cc05

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

adafruit_cursorcontrol/cursorcontrol_cursormanager.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from micropython import const
3232
import analogio
3333
from gamepadshift import GamePadShift
34+
from adafruit_debouncer import Debouncer
3435

3536
# PyBadge
3637
PYBADGE_BUTTON_LEFT = const(128)
@@ -43,7 +44,7 @@
4344
JOY_X_CTR = 32767.5
4445
JOY_Y_CTR = 32767.5
4546

46-
class CursorManager:
47+
class CursorManager(object):
4748
"""Simple interaction user interface interaction for Adafruit_CursorControl.
4849
4950
:param adafruit_cursorcontrol cursor: The cursor object we are using.
@@ -90,6 +91,7 @@ def _init_hardware(self):
9091
digitalio.DigitalInOut(board.BUTTON_OUT),
9192
digitalio.DigitalInOut(board.BUTTON_LATCH))
9293

94+
9395
@property
9496
def is_clicked(self):
9597
"""Returns True if the cursor button was pressed
@@ -159,3 +161,46 @@ def _check_cursor_movement(self, pressed=None):
159161
self._cursor.y -= self._cursor.speed
160162
else:
161163
raise AttributeError('Board must have a D-Pad or Joystick for use with CursorManager!')
164+
165+
166+
class DebouncedCursorManager(CursorManager):
167+
"""Simple interaction user interface interaction for Adafruit_CursorControl.
168+
This subclass provide a debounced version on the A button and provides queries for when
169+
the button is just pressed, and just released, as well it's current state. "Just" in this
170+
context means "since the previous call to update."
171+
172+
:param adafruit_cursorcontrol cursor: The cursor object we are using.
173+
"""
174+
def __init__(self, cursor, debounce_interval=0.01):
175+
CursorManager.__init__(self, cursor)
176+
self._pressed = 0
177+
self._debouncer = Debouncer(lambda: bool(self._pressed & self._pad_btns['btn_a']),
178+
interval=debounce_interval)
179+
180+
@property
181+
def is_clicked(self):
182+
"""Returns True if the cursor button was pressed
183+
during previous call to update()
184+
"""
185+
return self._debouncer.rose
186+
pressed = is_clicked
187+
188+
@property
189+
def released(self):
190+
"""Returns True if the cursor button was released
191+
during previous call to update()
192+
"""
193+
return self._debouncer.fell
194+
195+
@property
196+
def held(self):
197+
"""Returns True if the cursor button is currently being held
198+
"""
199+
return self._debouncer.value
200+
201+
202+
def update(self):
203+
"""Updates the cursor object."""
204+
self._pressed = self._pad.get_pressed()
205+
self._check_cursor_movement(self._pressed)
206+
self._debouncer.update()

0 commit comments

Comments
 (0)