Skip to content

Commit 72abb17

Browse files
committed
Switch to use get_into, add missing bit mask operation
1 parent 6580287 commit 72abb17

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

adafruit_cursorcontrol/cursorcontrol_cursormanager.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(self, cursor):
3737
self._cursor = cursor
3838
self._is_clicked = False
3939
self._pad_states = 0
40+
self._event = None
4041
self._init_hardware()
4142

4243
def __enter__(self):
@@ -99,8 +100,8 @@ def is_clicked(self):
99100

100101
def update(self):
101102
"""Updates the cursor object."""
102-
event = self._pad.events.get()
103-
self._store_button_states(event)
103+
if self._pad.events.get_into(self._event):
104+
self._store_button_states()
104105
self._check_cursor_movement()
105106
if self._is_clicked:
106107
self._is_clicked = False
@@ -131,17 +132,16 @@ def _read_joystick_y(self, samples=3):
131132
reading /= samples
132133
return reading
133134

134-
def _store_button_states(self, event):
135+
def _store_button_states(self):
135136
"""Stores the state of the PyBadge's D-Pad or the PyGamer's Joystick
136137
into a byte
137138
138139
:param Event event: The latest button press transition event detected.
139140
"""
140-
if event:
141-
bit_index = event.key_number
142-
current_state = self._pad_states >> bit_index
143-
if current_state != event.pressed:
144-
self._pad_states = (1 << bit_index) ^ self._pad_states
141+
bit_index = self._event.key_number
142+
current_state = (self._pad_states >> bit_index) & 1
143+
if current_state != self._event.pressed:
144+
self._pad_states = (1 << bit_index) ^ self._pad_states
145145

146146
def _check_cursor_movement(self):
147147
"""Checks the PyBadge D-Pad or the PyGamer's Joystick for movement."""
@@ -211,7 +211,7 @@ def held(self):
211211

212212
def update(self):
213213
"""Updates the cursor object."""
214-
event = self._pad.events.get()
215-
self._store_button_states(event)
214+
if self._pad.events.get_into(self._event):
215+
self._store_button_states()
216216
self._check_cursor_movement()
217217
self._debouncer.update()

0 commit comments

Comments
 (0)