Skip to content

Commit 0c6c613

Browse files
committed
Modify library to swap GamePadShift with ShiftRegisterKeys
1 parent c6cfe4e commit 0c6c613

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

adafruit_cursorcontrol/cursorcontrol_cursormanager.py

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
* Author(s): Brent Rubell
1010
"""
1111
import board
12-
import digitalio
1312
from micropython import const
1413
import analogio
15-
from gamepadshift import GamePadShift
14+
from keypad import ShiftRegisterKeys
1615
from adafruit_debouncer import Debouncer
1716

17+
1818
# PyBadge
19-
PYBADGE_BUTTON_LEFT = const(128)
20-
PYBADGE_BUTTON_UP = const(64)
21-
PYBADGE_BUTTON_DOWN = const(32)
22-
PYBADGE_BUTTON_RIGHT = const(16)
19+
PYBADGE_BUTTON_LEFT = const(7)
20+
PYBADGE_BUTTON_UP = const(6)
21+
PYBADGE_BUTTON_DOWN = const(5)
22+
PYBADGE_BUTTON_RIGHT = const(4)
2323
# PyBadge & PyGamer
24-
PYBADGE_BUTTON_A = const(2)
24+
PYBADGE_BUTTON_A = const(1)
2525

2626

2727
class CursorManager:
@@ -66,6 +66,7 @@ def _init_hardware(self):
6666
"btn_down": PYBADGE_BUTTON_DOWN,
6767
"btn_a": PYBADGE_BUTTON_A,
6868
}
69+
self._pad_states = 0
6970
elif hasattr(board, "JOYSTICK_X"):
7071
self._joystick_x = analogio.AnalogIn(board.JOYSTICK_X)
7172
self._joystick_y = analogio.AnalogIn(board.JOYSTICK_Y)
@@ -77,10 +78,12 @@ def _init_hardware(self):
7778
raise AttributeError(
7879
"Board must have a D-Pad or Joystick for use with CursorManager!"
7980
)
80-
self._pad = GamePadShift(
81-
digitalio.DigitalInOut(board.BUTTON_CLOCK),
82-
digitalio.DigitalInOut(board.BUTTON_OUT),
83-
digitalio.DigitalInOut(board.BUTTON_LATCH),
81+
self._pad = ShiftRegisterKeys(
82+
clock = board.BUTTON_CLOCK,
83+
data = board.BUTTON_OUT,
84+
latch = board.BUTTON_LATCH,
85+
key_count=8,
86+
value_when_pressed=True
8487
)
8588

8689
@property
@@ -92,11 +95,12 @@ def is_clicked(self):
9295

9396
def update(self):
9497
"""Updates the cursor object."""
95-
pressed = self._pad.get_pressed()
96-
self._check_cursor_movement(pressed)
98+
event = self._pad.events.get()
99+
self._store_button_states(event)
100+
self._check_cursor_movement(event)
97101
if self._is_clicked:
98102
self._is_clicked = False
99-
elif pressed & self._pad_btns["btn_a"]:
103+
elif self._pad_states & (1 << self._pad_btns["btn_a"]):
100104
self._is_clicked = True
101105

102106
def _read_joystick_x(self, samples=3):
@@ -118,24 +122,34 @@ def _read_joystick_y(self, samples=3):
118122
reading = 0
119123
# pylint: disable=unused-variable
120124
if hasattr(board, "JOYSTICK_Y"):
121-
for sample in range(0, samples):
125+
for _ in range(0, samples):
122126
reading += self._joystick_y.value
123127
reading /= samples
124128
return reading
125129

126-
def _check_cursor_movement(self, pressed=None):
127-
"""Checks the PyBadge D-Pad or the PyGamer's Joystick for movement.
128-
:param int pressed: 8-bit number with bits that correspond to buttons
129-
which have been pressed down since the last call to get_pressed().
130+
def _store_button_states(self, event):
131+
"""Stores the state of the PyBadge's D-Pad or the PyGamer's Joystick
132+
into a byte
133+
134+
:param Event event: The latest button press transition event detected.
130135
"""
136+
if event:
137+
bit_index = event.key_number
138+
current_state = (self._pad_states >> bit_index)
139+
if current_state != event.pressed:
140+
self._pad_states = (1 << bit_index) ^ self._pad_states
141+
142+
def _check_cursor_movement(self):
143+
"""Checks the PyBadge D-Pad or the PyGamer's Joystick for movement."""
131144
if hasattr(board, "BUTTON_CLOCK") and not hasattr(board, "JOYSTICK_X"):
132-
if pressed & self._pad_btns["btn_right"]:
145+
if self._pad_states & (1 << self._pad_btns["btn_right"]):
133146
self._cursor.x += self._cursor.speed
134-
elif pressed & self._pad_btns["btn_left"]:
147+
elif self._pad_states & (1 << self._pad_btns["btn_left"]):
135148
self._cursor.x -= self._cursor.speed
136-
if pressed & self._pad_btns["btn_up"]:
149+
150+
if self._pad_states & (1 << self._pad_btns["btn_up"]):
137151
self._cursor.y -= self._cursor.speed
138-
elif pressed & self._pad_btns["btn_down"]:
152+
elif self._pad_states & (1 << self._pad_btns["btn_down"]):
139153
self._cursor.y += self._cursor.speed
140154
elif hasattr(board, "JOYSTICK_X"):
141155
joy_x = self._read_joystick_x()
@@ -165,9 +179,8 @@ class DebouncedCursorManager(CursorManager):
165179

166180
def __init__(self, cursor, debounce_interval=0.01):
167181
CursorManager.__init__(self, cursor)
168-
self._pressed = 0
169182
self._debouncer = Debouncer(
170-
lambda: bool(self._pressed & self._pad_btns["btn_a"]),
183+
lambda: bool(self._pad_states & (1 << self._pad_btns["btn_a"])),
171184
interval=debounce_interval,
172185
)
173186

@@ -194,6 +207,7 @@ def held(self):
194207

195208
def update(self):
196209
"""Updates the cursor object."""
197-
self._pressed = self._pad.get_pressed()
198-
self._check_cursor_movement(self._pressed)
210+
event = self._pad.events.get()
211+
self._store_button_states(event)
212+
self._check_cursor_movement()
199213
self._debouncer.update()

0 commit comments

Comments
 (0)