From c6cfe4e223bb665a5a3be667084ab5985de52133 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 15 Nov 2021 11:14:01 -0500 Subject: [PATCH 01/13] Remove gamepadshift from autodoc_mock_imports --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 46dc049..2e4a1b6 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,7 +25,7 @@ # Uncomment the below if you use native CircuitPython modules such as # digitalio, micropython and busio. List the modules you use. Without it, the # autodoc module docs will fail to generate with a warning. -autodoc_mock_imports = ["displayio", "gamepadshift"] +autodoc_mock_imports = ["displayio"] intersphinx_mapping = { From 0c6c613fdfc4e460e74067d9a9c6e30e2d052b99 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 15 Nov 2021 11:22:19 -0500 Subject: [PATCH 02/13] Modify library to swap GamePadShift with ShiftRegisterKeys --- .../cursorcontrol_cursormanager.py | 68 +++++++++++-------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 1b4cbcf..4d9e841 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -9,19 +9,19 @@ * Author(s): Brent Rubell """ import board -import digitalio from micropython import const import analogio -from gamepadshift import GamePadShift +from keypad import ShiftRegisterKeys from adafruit_debouncer import Debouncer + # PyBadge -PYBADGE_BUTTON_LEFT = const(128) -PYBADGE_BUTTON_UP = const(64) -PYBADGE_BUTTON_DOWN = const(32) -PYBADGE_BUTTON_RIGHT = const(16) +PYBADGE_BUTTON_LEFT = const(7) +PYBADGE_BUTTON_UP = const(6) +PYBADGE_BUTTON_DOWN = const(5) +PYBADGE_BUTTON_RIGHT = const(4) # PyBadge & PyGamer -PYBADGE_BUTTON_A = const(2) +PYBADGE_BUTTON_A = const(1) class CursorManager: @@ -66,6 +66,7 @@ def _init_hardware(self): "btn_down": PYBADGE_BUTTON_DOWN, "btn_a": PYBADGE_BUTTON_A, } + self._pad_states = 0 elif hasattr(board, "JOYSTICK_X"): self._joystick_x = analogio.AnalogIn(board.JOYSTICK_X) self._joystick_y = analogio.AnalogIn(board.JOYSTICK_Y) @@ -77,10 +78,12 @@ def _init_hardware(self): raise AttributeError( "Board must have a D-Pad or Joystick for use with CursorManager!" ) - self._pad = GamePadShift( - digitalio.DigitalInOut(board.BUTTON_CLOCK), - digitalio.DigitalInOut(board.BUTTON_OUT), - digitalio.DigitalInOut(board.BUTTON_LATCH), + self._pad = ShiftRegisterKeys( + clock = board.BUTTON_CLOCK, + data = board.BUTTON_OUT, + latch = board.BUTTON_LATCH, + key_count=8, + value_when_pressed=True ) @property @@ -92,11 +95,12 @@ def is_clicked(self): def update(self): """Updates the cursor object.""" - pressed = self._pad.get_pressed() - self._check_cursor_movement(pressed) + event = self._pad.events.get() + self._store_button_states(event) + self._check_cursor_movement(event) if self._is_clicked: self._is_clicked = False - elif pressed & self._pad_btns["btn_a"]: + elif self._pad_states & (1 << self._pad_btns["btn_a"]): self._is_clicked = True def _read_joystick_x(self, samples=3): @@ -118,24 +122,34 @@ def _read_joystick_y(self, samples=3): reading = 0 # pylint: disable=unused-variable if hasattr(board, "JOYSTICK_Y"): - for sample in range(0, samples): + for _ in range(0, samples): reading += self._joystick_y.value reading /= samples return reading - def _check_cursor_movement(self, pressed=None): - """Checks the PyBadge D-Pad or the PyGamer's Joystick for movement. - :param int pressed: 8-bit number with bits that correspond to buttons - which have been pressed down since the last call to get_pressed(). + def _store_button_states(self, event): + """Stores the state of the PyBadge's D-Pad or the PyGamer's Joystick + into a byte + + :param Event event: The latest button press transition event detected. """ + if event: + bit_index = event.key_number + current_state = (self._pad_states >> bit_index) + if current_state != event.pressed: + self._pad_states = (1 << bit_index) ^ self._pad_states + + def _check_cursor_movement(self): + """Checks the PyBadge D-Pad or the PyGamer's Joystick for movement.""" if hasattr(board, "BUTTON_CLOCK") and not hasattr(board, "JOYSTICK_X"): - if pressed & self._pad_btns["btn_right"]: + if self._pad_states & (1 << self._pad_btns["btn_right"]): self._cursor.x += self._cursor.speed - elif pressed & self._pad_btns["btn_left"]: + elif self._pad_states & (1 << self._pad_btns["btn_left"]): self._cursor.x -= self._cursor.speed - if pressed & self._pad_btns["btn_up"]: + + if self._pad_states & (1 << self._pad_btns["btn_up"]): self._cursor.y -= self._cursor.speed - elif pressed & self._pad_btns["btn_down"]: + elif self._pad_states & (1 << self._pad_btns["btn_down"]): self._cursor.y += self._cursor.speed elif hasattr(board, "JOYSTICK_X"): joy_x = self._read_joystick_x() @@ -165,9 +179,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"]), + lambda: bool(self._pad_states & (1 << self._pad_btns["btn_a"])), interval=debounce_interval, ) @@ -194,6 +207,7 @@ def held(self): def update(self): """Updates the cursor object.""" - self._pressed = self._pad.get_pressed() - self._check_cursor_movement(self._pressed) + event = self._pad.events.get() + self._store_button_states(event) + self._check_cursor_movement() self._debouncer.update() From cc018cd6f8fdaa09d6a5e86fa412efed0eb85a2d Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 15 Nov 2021 11:23:54 -0500 Subject: [PATCH 03/13] Add license to __init --- adafruit_cursorcontrol/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/adafruit_cursorcontrol/__init__.py b/adafruit_cursorcontrol/__init__.py index e69de29..097ed25 100644 --- a/adafruit_cursorcontrol/__init__.py +++ b/adafruit_cursorcontrol/__init__.py @@ -0,0 +1,4 @@ +# SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries +# +# SPDX-License-Identifier: MIT + From 1c128525819f8280aa42b9c89193432771c1cdd9 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 15 Nov 2021 11:24:10 -0500 Subject: [PATCH 04/13] Add version and repo information --- adafruit_cursorcontrol/__init__.py | 3 +++ adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/adafruit_cursorcontrol/__init__.py b/adafruit_cursorcontrol/__init__.py index 097ed25..464cd24 100644 --- a/adafruit_cursorcontrol/__init__.py +++ b/adafruit_cursorcontrol/__init__.py @@ -2,3 +2,6 @@ # # SPDX-License-Identifier: MIT +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git" + diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 4d9e841..637baff 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -14,6 +14,9 @@ from keypad import ShiftRegisterKeys from adafruit_debouncer import Debouncer +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_Eddystone.git" + # PyBadge PYBADGE_BUTTON_LEFT = const(7) From 10d26316a597e05e8f25b72c5775fbde84010f64 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 15 Nov 2021 11:34:44 -0500 Subject: [PATCH 05/13] Add module docstring for __init__.py --- adafruit_cursorcontrol/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/adafruit_cursorcontrol/__init__.py b/adafruit_cursorcontrol/__init__.py index 464cd24..d2e86af 100644 --- a/adafruit_cursorcontrol/__init__.py +++ b/adafruit_cursorcontrol/__init__.py @@ -1,7 +1,14 @@ # SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries # # SPDX-License-Identifier: MIT +""" +`adafruit_cursorcontrol.cursorcontrol_cursormanager` +================================================================================ +Mouse cursor for interaction with CircuitPython UI elements +* Author(s): Brent Rubell + +""" __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git" From b0d6861962047ec9b9f43d29848fdca44e6e368e Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 15 Nov 2021 11:35:00 -0500 Subject: [PATCH 06/13] Fix repo variable --- 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 637baff..cc8a3e0 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -15,7 +15,7 @@ from adafruit_debouncer import Debouncer __version__ = "0.0.0-auto.0" -__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_Eddystone.git" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git" # PyBadge From 6e5884b0c895eac87b3a55e94bd1e8687c8b3fcf Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 15 Nov 2021 11:35:21 -0500 Subject: [PATCH 07/13] Add _pad_states to __init__() per pylint recommendation --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index cc8a3e0..07841d4 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -36,6 +36,7 @@ class CursorManager: def __init__(self, cursor): self._cursor = cursor self._is_clicked = False + self._pad_states = 0 self._init_hardware() def __enter__(self): From 092e3e66435a2b950e9c3ba1c490bec89a580129 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 15 Nov 2021 11:35:47 -0500 Subject: [PATCH 08/13] Remove event argument from _check_cursor_movement() --- 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 07841d4..854c4e1 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -101,7 +101,7 @@ def update(self): """Updates the cursor object.""" event = self._pad.events.get() self._store_button_states(event) - self._check_cursor_movement(event) + self._check_cursor_movement() if self._is_clicked: self._is_clicked = False elif self._pad_states & (1 << self._pad_btns["btn_a"]): From 65802879084cd365fada427c50745b8000b368a9 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 15 Nov 2021 11:36:04 -0500 Subject: [PATCH 09/13] Reformatted per pre-commit --- adafruit_cursorcontrol/__init__.py | 1 - .../cursorcontrol_cursormanager.py | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/adafruit_cursorcontrol/__init__.py b/adafruit_cursorcontrol/__init__.py index d2e86af..b49f500 100644 --- a/adafruit_cursorcontrol/__init__.py +++ b/adafruit_cursorcontrol/__init__.py @@ -11,4 +11,3 @@ """ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git" - diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 854c4e1..5c559ea 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -83,11 +83,11 @@ def _init_hardware(self): "Board must have a D-Pad or Joystick for use with CursorManager!" ) self._pad = ShiftRegisterKeys( - clock = board.BUTTON_CLOCK, - data = board.BUTTON_OUT, - latch = board.BUTTON_LATCH, + clock=board.BUTTON_CLOCK, + data=board.BUTTON_OUT, + latch=board.BUTTON_LATCH, key_count=8, - value_when_pressed=True + value_when_pressed=True, ) @property @@ -134,12 +134,12 @@ def _read_joystick_y(self, samples=3): def _store_button_states(self, event): """Stores the state of the PyBadge's D-Pad or the PyGamer's Joystick into a byte - + :param Event event: The latest button press transition event detected. """ if event: bit_index = event.key_number - current_state = (self._pad_states >> bit_index) + current_state = self._pad_states >> bit_index if current_state != event.pressed: self._pad_states = (1 << bit_index) ^ self._pad_states From 72abb178548f4569d71dddfc33198460de2f527a Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Mon, 15 Nov 2021 20:50:02 -0500 Subject: [PATCH 10/13] Switch to use get_into, add missing bit mask operation --- .../cursorcontrol_cursormanager.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index 5c559ea..6789599 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -37,6 +37,7 @@ def __init__(self, cursor): self._cursor = cursor self._is_clicked = False self._pad_states = 0 + self._event = None self._init_hardware() def __enter__(self): @@ -99,8 +100,8 @@ def is_clicked(self): def update(self): """Updates the cursor object.""" - event = self._pad.events.get() - self._store_button_states(event) + if self._pad.events.get_into(self._event): + self._store_button_states() self._check_cursor_movement() if self._is_clicked: self._is_clicked = False @@ -131,17 +132,16 @@ def _read_joystick_y(self, samples=3): reading /= samples return reading - def _store_button_states(self, event): + def _store_button_states(self): """Stores the state of the PyBadge's D-Pad or the PyGamer's Joystick into a byte :param Event event: The latest button press transition event detected. """ - if event: - bit_index = event.key_number - current_state = self._pad_states >> bit_index - if current_state != event.pressed: - self._pad_states = (1 << bit_index) ^ self._pad_states + bit_index = self._event.key_number + current_state = (self._pad_states >> bit_index) & 1 + if current_state != self._event.pressed: + self._pad_states = (1 << bit_index) ^ self._pad_states def _check_cursor_movement(self): """Checks the PyBadge D-Pad or the PyGamer's Joystick for movement.""" @@ -211,7 +211,7 @@ def held(self): def update(self): """Updates the cursor object.""" - event = self._pad.events.get() - self._store_button_states(event) + if self._pad.events.get_into(self._event): + self._store_button_states() self._check_cursor_movement() self._debouncer.update() From b6101eae57f663707b0275bb1119faa6a444cc70 Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Mon, 15 Nov 2021 20:50:19 -0500 Subject: [PATCH 11/13] Use underscore for throwaway variable --- 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 6789599..e6676aa 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -115,7 +115,7 @@ def _read_joystick_x(self, samples=3): reading = 0 # pylint: disable=unused-variable if hasattr(board, "JOYSTICK_X"): - for sample in range(0, samples): + for _ in range(0, samples): reading += self._joystick_x.value reading /= samples return reading From a91babc7557590988eca9f294747d85e2ebcd2f9 Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Mon, 15 Nov 2021 22:24:29 -0500 Subject: [PATCH 12/13] Initialize self._event as Event --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index e6676aa..c06c22b 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -11,7 +11,7 @@ import board from micropython import const import analogio -from keypad import ShiftRegisterKeys +from keypad import ShiftRegisterKeys, Event from adafruit_debouncer import Debouncer __version__ = "0.0.0-auto.0" @@ -37,7 +37,7 @@ def __init__(self, cursor): self._cursor = cursor self._is_clicked = False self._pad_states = 0 - self._event = None + self._event = Event() self._init_hardware() def __enter__(self): From 0f9df2b4fbad68d71c352b68c644c7af6a6e871f Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Mon, 15 Nov 2021 22:25:33 -0500 Subject: [PATCH 13/13] Deinit self._event to None when needed --- adafruit_cursorcontrol/cursorcontrol_cursormanager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py index c06c22b..3ebe4c6 100755 --- a/adafruit_cursorcontrol/cursorcontrol_cursormanager.py +++ b/adafruit_cursorcontrol/cursorcontrol_cursormanager.py @@ -52,6 +52,7 @@ def deinit(self): self._pad.deinit() self._cursor.deinit() self._cursor = None + self._event = None def _is_deinited(self): """Checks if CursorManager object has been deinitd."""