|
31 | 31 | from micropython import const
|
32 | 32 | import analogio
|
33 | 33 | from gamepadshift import GamePadShift
|
| 34 | +from adafruit_debouncer import Debouncer |
34 | 35 |
|
35 | 36 | # PyBadge
|
36 | 37 | PYBADGE_BUTTON_LEFT = const(128)
|
|
43 | 44 | JOY_X_CTR = 32767.5
|
44 | 45 | JOY_Y_CTR = 32767.5
|
45 | 46 |
|
46 |
| -class CursorManager: |
| 47 | +class CursorManager(object): |
47 | 48 | """Simple interaction user interface interaction for Adafruit_CursorControl.
|
48 | 49 |
|
49 | 50 | :param adafruit_cursorcontrol cursor: The cursor object we are using.
|
@@ -90,6 +91,7 @@ def _init_hardware(self):
|
90 | 91 | digitalio.DigitalInOut(board.BUTTON_OUT),
|
91 | 92 | digitalio.DigitalInOut(board.BUTTON_LATCH))
|
92 | 93 |
|
| 94 | + |
93 | 95 | @property
|
94 | 96 | def is_clicked(self):
|
95 | 97 | """Returns True if the cursor button was pressed
|
@@ -159,3 +161,46 @@ def _check_cursor_movement(self, pressed=None):
|
159 | 161 | self._cursor.y -= self._cursor.speed
|
160 | 162 | else:
|
161 | 163 | 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