Skip to content

Use debouncer to handle button analysis #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 3, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions adafruit_cursorcontrol/cursorcontrol_cursormanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from micropython import const
import analogio
from gamepadshift import GamePadShift
from adafruit_debouncer import Debouncer

# PyBadge
PYBADGE_BUTTON_LEFT = const(128)
Expand Down Expand Up @@ -90,6 +91,7 @@ def _init_hardware(self):
digitalio.DigitalInOut(board.BUTTON_OUT),
digitalio.DigitalInOut(board.BUTTON_LATCH))


@property
def is_clicked(self):
"""Returns True if the cursor button was pressed
Expand Down Expand Up @@ -159,3 +161,42 @@ def _check_cursor_movement(self, pressed=None):
self._cursor.y -= self._cursor.speed
else:
raise AttributeError('Board must have a D-Pad or Joystick for use with CursorManager!')


class DebouncedCursorManager(CursorManager):
"""Simple interaction user interface interaction for Adafruit_CursorControl.

:param adafruit_cursorcontrol cursor: The cursor object we are using.
"""
def __init__(self, cursor, debounce_interval=0.01):
super().__init__(self, cursor)
self._pressed = 0
self._debouncer = Debouncer(lambda: bool(self._pressed & self._pad_btns['btn_a']), interval=debounce_interval)

@property
def is_clicked(self):
"""Returns True if the cursor button was pressed
during previous call to update()
"""
return self._debouncer.rose
pressed = is_clicked

@property
def released(self):
"""Returns True if the cursor button was released
during previous call to update()
"""
return self._debouncer.fell

@property
def held(self):
"""Returns True if the cursor button is currently being held
"""
return self._debouncer.value


def update(self):
"""Updates the cursor object."""
self._pressed = self._pad.get_pressed()
self._check_cursor_movement(self._pressed)
self._debouncer.update()