41
41
# PyBadge & PyGamer
42
42
PYBADGE_BUTTON_A = const (2 )
43
43
44
- JOY_X_CTR = 32767.5
45
- JOY_Y_CTR = 32767.5
46
-
47
44
class CursorManager (object ):
48
45
"""Simple interaction user interface interaction for Adafruit_CursorControl.
49
46
50
47
:param adafruit_cursorcontrol cursor: The cursor object we are using.
51
48
"""
49
+
52
50
def __init__ (self , cursor ):
53
51
self ._cursor = cursor
54
52
self ._is_clicked = False
55
53
self ._init_hardware ()
54
+ self ._center_x = self ._joystick_x .value
55
+ self ._center_y = self ._joystick_y .value
56
56
57
57
def __enter__ (self ):
58
58
return self
@@ -70,27 +70,34 @@ def deinit(self):
70
70
def _is_deinited (self ):
71
71
"""Checks if CursorManager object has been deinitd."""
72
72
if self ._cursor is None :
73
- raise ValueError ("CursorManager object has been deinitialized and can no longer "
74
- "be used. Create a new CursorManager object." )
73
+ raise ValueError (
74
+ "CursorManager object has been deinitialized and can no longer "
75
+ "be used. Create a new CursorManager object."
76
+ )
75
77
76
78
def _init_hardware (self ):
77
79
"""Initializes PyBadge or PyGamer hardware."""
78
- if hasattr (board , 'BUTTON_CLOCK' ) and not hasattr (board , 'JOYSTICK_X' ):
79
- self ._pad_btns = {'btn_left' : PYBADGE_BUTTON_LEFT ,
80
- 'btn_right' : PYBADGE_BUTTON_RIGHT ,
81
- 'btn_up' : PYBADGE_BUTTON_UP ,
82
- 'btn_down' : PYBADGE_BUTTON_DOWN ,
83
- 'btn_a' : PYBADGE_BUTTON_A }
84
- elif hasattr (board , 'JOYSTICK_X' ):
80
+ if hasattr (board , "BUTTON_CLOCK" ) and not hasattr (board , "JOYSTICK_X" ):
81
+ self ._pad_btns = {
82
+ "btn_left" : PYBADGE_BUTTON_LEFT ,
83
+ "btn_right" : PYBADGE_BUTTON_RIGHT ,
84
+ "btn_up" : PYBADGE_BUTTON_UP ,
85
+ "btn_down" : PYBADGE_BUTTON_DOWN ,
86
+ "btn_a" : PYBADGE_BUTTON_A ,
87
+ }
88
+ elif hasattr (board , "JOYSTICK_X" ):
85
89
self ._joystick_x = analogio .AnalogIn (board .JOYSTICK_X )
86
90
self ._joystick_y = analogio .AnalogIn (board .JOYSTICK_Y )
87
- self ._pad_btns = {' btn_a' : PYBADGE_BUTTON_A }
91
+ self ._pad_btns = {" btn_a" : PYBADGE_BUTTON_A }
88
92
else :
89
- raise AttributeError ('Board must have a D-Pad or Joystick for use with CursorManager!' )
90
- self ._pad = GamePadShift (digitalio .DigitalInOut (board .BUTTON_CLOCK ),
91
- digitalio .DigitalInOut (board .BUTTON_OUT ),
92
- digitalio .DigitalInOut (board .BUTTON_LATCH ))
93
-
93
+ raise AttributeError (
94
+ "Board must have a D-Pad or Joystick for use with CursorManager!"
95
+ )
96
+ self ._pad = GamePadShift (
97
+ digitalio .DigitalInOut (board .BUTTON_CLOCK ),
98
+ digitalio .DigitalInOut (board .BUTTON_OUT ),
99
+ digitalio .DigitalInOut (board .BUTTON_LATCH ),
100
+ )
94
101
95
102
@property
96
103
def is_clicked (self ):
@@ -105,7 +112,7 @@ def update(self):
105
112
self ._check_cursor_movement (pressed )
106
113
if self ._is_clicked :
107
114
self ._is_clicked = False
108
- elif pressed & self ._pad_btns [' btn_a' ]:
115
+ elif pressed & self ._pad_btns [" btn_a" ]:
109
116
self ._is_clicked = True
110
117
111
118
def _read_joystick_x (self , samples = 3 ):
@@ -114,11 +121,10 @@ def _read_joystick_x(self, samples=3):
114
121
"""
115
122
reading = 0
116
123
# pylint: disable=unused-variable
117
- if hasattr (board , ' JOYSTICK_X' ):
124
+ if hasattr (board , " JOYSTICK_X" ):
118
125
for sample in range (0 , samples ):
119
126
reading += self ._joystick_x .value
120
127
reading /= samples
121
- reading -= JOY_X_CTR
122
128
return reading
123
129
124
130
def _read_joystick_y (self , samples = 3 ):
@@ -127,40 +133,41 @@ def _read_joystick_y(self, samples=3):
127
133
"""
128
134
reading = 0
129
135
# pylint: disable=unused-variable
130
- if hasattr (board , ' JOYSTICK_Y' ):
136
+ if hasattr (board , " JOYSTICK_Y" ):
131
137
for sample in range (0 , samples ):
132
138
reading += self ._joystick_y .value
133
139
reading /= samples
134
- reading -= JOY_Y_CTR
135
140
return reading
136
141
137
142
def _check_cursor_movement (self , pressed = None ):
138
143
"""Checks the PyBadge D-Pad or the PyGamer's Joystick for movement.
139
144
:param int pressed: 8-bit number with bits that correspond to buttons
140
145
which have been pressed down since the last call to get_pressed().
141
146
"""
142
- if hasattr (board , ' BUTTON_CLOCK' ) and not hasattr (board , ' JOYSTICK_X' ):
143
- if pressed & self ._pad_btns [' btn_right' ]:
147
+ if hasattr (board , " BUTTON_CLOCK" ) and not hasattr (board , " JOYSTICK_X" ):
148
+ if pressed & self ._pad_btns [" btn_right" ]:
144
149
self ._cursor .x += self ._cursor .speed
145
- elif pressed & self ._pad_btns [' btn_left' ]:
150
+ elif pressed & self ._pad_btns [" btn_left" ]:
146
151
self ._cursor .x -= self ._cursor .speed
147
- if pressed & self ._pad_btns [' btn_up' ]:
152
+ if pressed & self ._pad_btns [" btn_up" ]:
148
153
self ._cursor .y -= self ._cursor .speed
149
- elif pressed & self ._pad_btns [' btn_down' ]:
154
+ elif pressed & self ._pad_btns [" btn_down" ]:
150
155
self ._cursor .y += self ._cursor .speed
151
- elif hasattr (board , ' JOYSTICK_X' ):
156
+ elif hasattr (board , " JOYSTICK_X" ):
152
157
joy_x = self ._read_joystick_x ()
153
158
joy_y = self ._read_joystick_y ()
154
- if joy_x > 700 :
159
+ if joy_x > self . _center_x + 1000 :
155
160
self ._cursor .x += self ._cursor .speed
156
- elif joy_x < - 700 :
161
+ elif joy_x < self . _center_x - 1000 :
157
162
self ._cursor .x -= self ._cursor .speed
158
- if joy_y > 700 :
163
+ if joy_y > self . _center_y + 1000 :
159
164
self ._cursor .y += self ._cursor .speed
160
- elif joy_y < - 700 :
165
+ elif joy_y < self . _center_y - 1000 :
161
166
self ._cursor .y -= self ._cursor .speed
162
167
else :
163
- raise AttributeError ('Board must have a D-Pad or Joystick for use with CursorManager!' )
168
+ raise AttributeError (
169
+ "Board must have a D-Pad or Joystick for use with CursorManager!"
170
+ )
164
171
165
172
166
173
class DebouncedCursorManager (CursorManager ):
@@ -171,18 +178,22 @@ class DebouncedCursorManager(CursorManager):
171
178
172
179
:param adafruit_cursorcontrol cursor: The cursor object we are using.
173
180
"""
181
+
174
182
def __init__ (self , cursor , debounce_interval = 0.01 ):
175
183
CursorManager .__init__ (self , cursor )
176
184
self ._pressed = 0
177
- self ._debouncer = Debouncer (lambda : bool (self ._pressed & self ._pad_btns ['btn_a' ]),
178
- interval = debounce_interval )
185
+ self ._debouncer = Debouncer (
186
+ lambda : bool (self ._pressed & self ._pad_btns ["btn_a" ]),
187
+ interval = debounce_interval ,
188
+ )
179
189
180
190
@property
181
191
def is_clicked (self ):
182
192
"""Returns True if the cursor button was pressed
183
193
during previous call to update()
184
194
"""
185
195
return self ._debouncer .rose
196
+
186
197
pressed = is_clicked
187
198
188
199
@property
@@ -198,7 +209,6 @@ def held(self):
198
209
"""
199
210
return self ._debouncer .value
200
211
201
-
202
212
def update (self ):
203
213
"""Updates the cursor object."""
204
214
self ._pressed = self ._pad .get_pressed ()
0 commit comments