9
9
* Author(s): Brent Rubell
10
10
"""
11
11
import board
12
- import digitalio
13
12
from micropython import const
14
13
import analogio
15
- from gamepadshift import GamePadShift
14
+ from keypad import ShiftRegisterKeys
16
15
from adafruit_debouncer import Debouncer
17
16
17
+
18
18
# 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 )
23
23
# PyBadge & PyGamer
24
- PYBADGE_BUTTON_A = const (2 )
24
+ PYBADGE_BUTTON_A = const (1 )
25
25
26
26
27
27
class CursorManager :
@@ -66,6 +66,7 @@ def _init_hardware(self):
66
66
"btn_down" : PYBADGE_BUTTON_DOWN ,
67
67
"btn_a" : PYBADGE_BUTTON_A ,
68
68
}
69
+ self ._pad_states = 0
69
70
elif hasattr (board , "JOYSTICK_X" ):
70
71
self ._joystick_x = analogio .AnalogIn (board .JOYSTICK_X )
71
72
self ._joystick_y = analogio .AnalogIn (board .JOYSTICK_Y )
@@ -77,10 +78,12 @@ def _init_hardware(self):
77
78
raise AttributeError (
78
79
"Board must have a D-Pad or Joystick for use with CursorManager!"
79
80
)
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
84
87
)
85
88
86
89
@property
@@ -92,11 +95,12 @@ def is_clicked(self):
92
95
93
96
def update (self ):
94
97
"""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 )
97
101
if self ._is_clicked :
98
102
self ._is_clicked = False
99
- elif pressed & self ._pad_btns ["btn_a" ]:
103
+ elif self . _pad_states & ( 1 << self ._pad_btns ["btn_a" ]) :
100
104
self ._is_clicked = True
101
105
102
106
def _read_joystick_x (self , samples = 3 ):
@@ -118,24 +122,34 @@ def _read_joystick_y(self, samples=3):
118
122
reading = 0
119
123
# pylint: disable=unused-variable
120
124
if hasattr (board , "JOYSTICK_Y" ):
121
- for sample in range (0 , samples ):
125
+ for _ in range (0 , samples ):
122
126
reading += self ._joystick_y .value
123
127
reading /= samples
124
128
return reading
125
129
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.
130
135
"""
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."""
131
144
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" ]) :
133
146
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" ]) :
135
148
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" ]):
137
151
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" ]) :
139
153
self ._cursor .y += self ._cursor .speed
140
154
elif hasattr (board , "JOYSTICK_X" ):
141
155
joy_x = self ._read_joystick_x ()
@@ -165,9 +179,8 @@ class DebouncedCursorManager(CursorManager):
165
179
166
180
def __init__ (self , cursor , debounce_interval = 0.01 ):
167
181
CursorManager .__init__ (self , cursor )
168
- self ._pressed = 0
169
182
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" ]) ),
171
184
interval = debounce_interval ,
172
185
)
173
186
@@ -194,6 +207,7 @@ def held(self):
194
207
195
208
def update (self ):
196
209
"""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 ()
199
213
self ._debouncer .update ()
0 commit comments