@@ -51,9 +51,8 @@ class CursorManager:
51
51
"""
52
52
def __init__ (self , cursor ):
53
53
self ._cursor = cursor
54
- self ._pressed = 0
54
+ self ._is_clicked = False
55
55
self ._init_hardware ()
56
- self ._debouncer = Debouncer (lambda : bool (self ._pressed & self ._pad_btns ['btn_a' ]))
57
56
58
57
def __enter__ (self ):
59
58
return self
@@ -98,28 +97,16 @@ def is_clicked(self):
98
97
"""Returns True if the cursor button was pressed
99
98
during previous call to update()
100
99
"""
101
- return self ._debouncer .rose
102
- pressed = is_clicked
103
-
104
- @property
105
- def released (self ):
106
- """Returns True if the cursor button was released
107
- during previous call to update()
108
- """
109
- return self ._debouncer .fell
110
-
111
- @property
112
- def held (self ):
113
- """Returns True if the cursor button is currently being held
114
- """
115
- return self ._debouncer .value
116
-
100
+ return self ._is_clicked
117
101
118
102
def update (self ):
119
103
"""Updates the cursor object."""
120
- self ._pressed = self ._pad .get_pressed ()
121
- self ._check_cursor_movement (self ._pressed )
122
- self ._debouncer .update ()
104
+ pressed = self ._pad .get_pressed ()
105
+ self ._check_cursor_movement (pressed )
106
+ if self ._is_clicked :
107
+ self ._is_clicked = False
108
+ elif pressed & self ._pad_btns ['btn_a' ]:
109
+ self ._is_clicked = True
123
110
124
111
def _read_joystick_x (self , samples = 3 ):
125
112
"""Read the X analog joystick on the PyGamer.
@@ -174,3 +161,42 @@ def _check_cursor_movement(self, pressed=None):
174
161
self ._cursor .y -= self ._cursor .speed
175
162
else :
176
163
raise AttributeError ('Board must have a D-Pad or Joystick for use with CursorManager!' )
164
+
165
+
166
+ class DebouncedCursorManager (CursorManager ):
167
+ """Simple interaction user interface interaction for Adafruit_CursorControl.
168
+
169
+ :param adafruit_cursorcontrol cursor: The cursor object we are using.
170
+ """
171
+ def __init__ (self , cursor , debounce_interval = 0.01 ):
172
+ super ().__init__ (self , cursor )
173
+ self ._pressed = 0
174
+ self ._debouncer = Debouncer (lambda : bool (self ._pressed & self ._pad_btns ['btn_a' ]), interval = debounce_interval )
175
+
176
+ @property
177
+ def is_clicked (self ):
178
+ """Returns True if the cursor button was pressed
179
+ during previous call to update()
180
+ """
181
+ return self ._debouncer .rose
182
+ pressed = is_clicked
183
+
184
+ @property
185
+ def released (self ):
186
+ """Returns True if the cursor button was released
187
+ during previous call to update()
188
+ """
189
+ return self ._debouncer .fell
190
+
191
+ @property
192
+ def held (self ):
193
+ """Returns True if the cursor button is currently being held
194
+ """
195
+ return self ._debouncer .value
196
+
197
+
198
+ def update (self ):
199
+ """Updates the cursor object."""
200
+ self ._pressed = self ._pad .get_pressed ()
201
+ self ._check_cursor_movement (self ._pressed )
202
+ self ._debouncer .update ()
0 commit comments