Skip to content

Commit a5e8031

Browse files
committed
Restore original class and make debouncer version a subclass
1 parent 5723369 commit a5e8031

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

adafruit_cursorcontrol/cursorcontrol_cursormanager.py

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ class CursorManager:
5151
"""
5252
def __init__(self, cursor):
5353
self._cursor = cursor
54-
self._pressed = 0
54+
self._is_clicked = False
5555
self._init_hardware()
56-
self._debouncer = Debouncer(lambda: bool(self._pressed & self._pad_btns['btn_a']))
5756

5857
def __enter__(self):
5958
return self
@@ -98,28 +97,16 @@ def is_clicked(self):
9897
"""Returns True if the cursor button was pressed
9998
during previous call to update()
10099
"""
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
117101

118102
def update(self):
119103
"""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
123110

124111
def _read_joystick_x(self, samples=3):
125112
"""Read the X analog joystick on the PyGamer.
@@ -174,3 +161,42 @@ def _check_cursor_movement(self, pressed=None):
174161
self._cursor.y -= self._cursor.speed
175162
else:
176163
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

Comments
 (0)