From 572336904b8f873cb876fee24b6cffe63faf51bd Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Tue, 2 Jul 2019 16:28:39 -0400 Subject: [PATCH 1/5] Use debouncer to handle button analysis --- .../cursorcontrol_cursormanager.py | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 280d7f9..295f179 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -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) @@ -50,8 +51,9 @@ class CursorManager: """ def __init__(self, cursor): self._cursor = cursor - self._is_clicked = False + self._pressed = 0 self._init_hardware() + self._debouncer = Debouncer(lambda: bool(self._pressed & self._pad_btns['btn_a'])) def __enter__(self): return self @@ -90,21 +92,34 @@ 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 during previous call to update() """ - return self._is_clicked + 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.""" - pressed = self._pad.get_pressed() - self._check_cursor_movement(pressed) - if self._is_clicked: - self._is_clicked = False - elif pressed & self._pad_btns['btn_a']: - self._is_clicked = True + self._pressed = self._pad.get_pressed() + self._check_cursor_movement(self._pressed) + self._debouncer.update() def _read_joystick_x(self, samples=3): """Read the X analog joystick on the PyGamer. From a5e80317f94abc2522bdf13ab7572c2eb26a93bd Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Tue, 2 Jul 2019 17:00:22 -0400 Subject: [PATCH 2/5] Restore original class and make debouncer version a subclass --- .../cursorcontrol_cursormanager.py | 68 +++++++++++++------ 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 295f179..f392ea0 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -51,9 +51,8 @@ class CursorManager: """ def __init__(self, cursor): self._cursor = cursor - self._pressed = 0 + self._is_clicked = False self._init_hardware() - self._debouncer = Debouncer(lambda: bool(self._pressed & self._pad_btns['btn_a'])) def __enter__(self): return self @@ -98,28 +97,16 @@ 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 - + return self._is_clicked def update(self): """Updates the cursor object.""" - self._pressed = self._pad.get_pressed() - self._check_cursor_movement(self._pressed) - self._debouncer.update() + pressed = self._pad.get_pressed() + self._check_cursor_movement(pressed) + if self._is_clicked: + self._is_clicked = False + elif pressed & self._pad_btns['btn_a']: + self._is_clicked = True def _read_joystick_x(self, samples=3): """Read the X analog joystick on the PyGamer. @@ -174,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() From b63a241b6cd47091462c59f5778dd383c269f427 Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Tue, 2 Jul 2019 17:08:55 -0400 Subject: [PATCH 3/5] Tweak superclass initialization --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index f392ea0..827fc76 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -169,7 +169,7 @@ class DebouncedCursorManager(CursorManager): :param adafruit_cursorcontrol cursor: The cursor object we are using. """ def __init__(self, cursor, debounce_interval=0.01): - super().__init__(self, cursor) + CursorManager.__init__(self, cursor) self._pressed = 0 self._debouncer = Debouncer(lambda: bool(self._pressed & self._pad_btns['btn_a']), interval=debounce_interval) From 3a72dd35fce841d5ca8b06f4b394472f6b3eaa56 Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Tue, 2 Jul 2019 18:37:51 -0400 Subject: [PATCH 4/5] A couple tweaks --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 827fc76..935f042 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -44,7 +44,7 @@ JOY_X_CTR = 32767.5 JOY_Y_CTR = 32767.5 -class CursorManager: +class CursorManager(object): """Simple interaction user interface interaction for Adafruit_CursorControl. :param adafruit_cursorcontrol cursor: The cursor object we are using. @@ -171,7 +171,8 @@ class DebouncedCursorManager(CursorManager): def __init__(self, cursor, debounce_interval=0.01): CursorManager.__init__(self, cursor) self._pressed = 0 - self._debouncer = Debouncer(lambda: bool(self._pressed & self._pad_btns['btn_a']), interval=debounce_interval) + self._debouncer = Debouncer(lambda: bool(self._pressed & self._pad_btns['btn_a']), + interval=debounce_interval) @property def is_clicked(self): From ab67ed01807766c84058cf2bea45f3e7192736c6 Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Tue, 2 Jul 2019 18:40:25 -0400 Subject: [PATCH 5/5] Enhance the class docstring --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 935f042..72e20c3 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -165,6 +165,9 @@ def _check_cursor_movement(self, pressed=None): class DebouncedCursorManager(CursorManager): """Simple interaction user interface interaction for Adafruit_CursorControl. + This subclass provide a debounced version on the A button and provides queries for when + the button is just pressed, and just released, as well it's current state. "Just" in this + context means "since the previous call to update." :param adafruit_cursorcontrol cursor: The cursor object we are using. """