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 , Event
16
15
from adafruit_debouncer import Debouncer
17
16
17
+ __version__ = "0.0.0-auto.0"
18
+ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git"
19
+
20
+
18
21
# 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 )
22
+ PYBADGE_BUTTON_LEFT = const (7 )
23
+ PYBADGE_BUTTON_UP = const (6 )
24
+ PYBADGE_BUTTON_DOWN = const (5 )
25
+ PYBADGE_BUTTON_RIGHT = const (4 )
23
26
# PyBadge & PyGamer
24
- PYBADGE_BUTTON_A = const (2 )
27
+ PYBADGE_BUTTON_A = const (1 )
25
28
26
29
27
30
class CursorManager :
@@ -33,6 +36,8 @@ class CursorManager:
33
36
def __init__ (self , cursor ):
34
37
self ._cursor = cursor
35
38
self ._is_clicked = False
39
+ self ._pad_states = 0
40
+ self ._event = Event ()
36
41
self ._init_hardware ()
37
42
38
43
def __enter__ (self ):
@@ -47,6 +52,7 @@ def deinit(self):
47
52
self ._pad .deinit ()
48
53
self ._cursor .deinit ()
49
54
self ._cursor = None
55
+ self ._event = None
50
56
51
57
def _is_deinited (self ):
52
58
"""Checks if CursorManager object has been deinitd."""
@@ -66,6 +72,7 @@ def _init_hardware(self):
66
72
"btn_down" : PYBADGE_BUTTON_DOWN ,
67
73
"btn_a" : PYBADGE_BUTTON_A ,
68
74
}
75
+ self ._pad_states = 0
69
76
elif hasattr (board , "JOYSTICK_X" ):
70
77
self ._joystick_x = analogio .AnalogIn (board .JOYSTICK_X )
71
78
self ._joystick_y = analogio .AnalogIn (board .JOYSTICK_Y )
@@ -77,10 +84,12 @@ def _init_hardware(self):
77
84
raise AttributeError (
78
85
"Board must have a D-Pad or Joystick for use with CursorManager!"
79
86
)
80
- self ._pad = GamePadShift (
81
- digitalio .DigitalInOut (board .BUTTON_CLOCK ),
82
- digitalio .DigitalInOut (board .BUTTON_OUT ),
83
- digitalio .DigitalInOut (board .BUTTON_LATCH ),
87
+ self ._pad = ShiftRegisterKeys (
88
+ clock = board .BUTTON_CLOCK ,
89
+ data = board .BUTTON_OUT ,
90
+ latch = board .BUTTON_LATCH ,
91
+ key_count = 8 ,
92
+ value_when_pressed = True ,
84
93
)
85
94
86
95
@property
@@ -92,11 +101,12 @@ def is_clicked(self):
92
101
93
102
def update (self ):
94
103
"""Updates the cursor object."""
95
- pressed = self ._pad .get_pressed ()
96
- self ._check_cursor_movement (pressed )
104
+ if self ._pad .events .get_into (self ._event ):
105
+ self ._store_button_states ()
106
+ self ._check_cursor_movement ()
97
107
if self ._is_clicked :
98
108
self ._is_clicked = False
99
- elif pressed & self ._pad_btns ["btn_a" ]:
109
+ elif self . _pad_states & ( 1 << self ._pad_btns ["btn_a" ]) :
100
110
self ._is_clicked = True
101
111
102
112
def _read_joystick_x (self , samples = 3 ):
@@ -106,7 +116,7 @@ def _read_joystick_x(self, samples=3):
106
116
reading = 0
107
117
# pylint: disable=unused-variable
108
118
if hasattr (board , "JOYSTICK_X" ):
109
- for sample in range (0 , samples ):
119
+ for _ in range (0 , samples ):
110
120
reading += self ._joystick_x .value
111
121
reading /= samples
112
122
return reading
@@ -118,24 +128,33 @@ def _read_joystick_y(self, samples=3):
118
128
reading = 0
119
129
# pylint: disable=unused-variable
120
130
if hasattr (board , "JOYSTICK_Y" ):
121
- for sample in range (0 , samples ):
131
+ for _ in range (0 , samples ):
122
132
reading += self ._joystick_y .value
123
133
reading /= samples
124
134
return reading
125
135
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().
136
+ def _store_button_states (self ):
137
+ """Stores the state of the PyBadge's D-Pad or the PyGamer's Joystick
138
+ into a byte
139
+
140
+ :param Event event: The latest button press transition event detected.
130
141
"""
142
+ bit_index = self ._event .key_number
143
+ current_state = (self ._pad_states >> bit_index ) & 1
144
+ if current_state != self ._event .pressed :
145
+ self ._pad_states = (1 << bit_index ) ^ self ._pad_states
146
+
147
+ def _check_cursor_movement (self ):
148
+ """Checks the PyBadge D-Pad or the PyGamer's Joystick for movement."""
131
149
if hasattr (board , "BUTTON_CLOCK" ) and not hasattr (board , "JOYSTICK_X" ):
132
- if pressed & self ._pad_btns ["btn_right" ]:
150
+ if self . _pad_states & ( 1 << self ._pad_btns ["btn_right" ]) :
133
151
self ._cursor .x += self ._cursor .speed
134
- elif pressed & self ._pad_btns ["btn_left" ]:
152
+ elif self . _pad_states & ( 1 << self ._pad_btns ["btn_left" ]) :
135
153
self ._cursor .x -= self ._cursor .speed
136
- if pressed & self ._pad_btns ["btn_up" ]:
154
+
155
+ if self ._pad_states & (1 << self ._pad_btns ["btn_up" ]):
137
156
self ._cursor .y -= self ._cursor .speed
138
- elif pressed & self ._pad_btns ["btn_down" ]:
157
+ elif self . _pad_states & ( 1 << self ._pad_btns ["btn_down" ]) :
139
158
self ._cursor .y += self ._cursor .speed
140
159
elif hasattr (board , "JOYSTICK_X" ):
141
160
joy_x = self ._read_joystick_x ()
@@ -165,9 +184,8 @@ class DebouncedCursorManager(CursorManager):
165
184
166
185
def __init__ (self , cursor , debounce_interval = 0.01 ):
167
186
CursorManager .__init__ (self , cursor )
168
- self ._pressed = 0
169
187
self ._debouncer = Debouncer (
170
- lambda : bool (self ._pressed & self ._pad_btns ["btn_a" ]),
188
+ lambda : bool (self ._pad_states & ( 1 << self ._pad_btns ["btn_a" ]) ),
171
189
interval = debounce_interval ,
172
190
)
173
191
@@ -194,6 +212,7 @@ def held(self):
194
212
195
213
def update (self ):
196
214
"""Updates the cursor object."""
197
- self ._pressed = self ._pad .get_pressed ()
198
- self ._check_cursor_movement (self ._pressed )
215
+ if self ._pad .events .get_into (self ._event ):
216
+ self ._store_button_states ()
217
+ self ._check_cursor_movement ()
199
218
self ._debouncer .update ()
0 commit comments