31
31
from micropython import const
32
32
import analogio
33
33
from gamepadshift import GamePadShift
34
+ from adafruit_debouncer import Debouncer
34
35
35
36
# PyBadge
36
37
PYBADGE_BUTTON_LEFT = const (128 )
@@ -50,8 +51,9 @@ class CursorManager:
50
51
"""
51
52
def __init__ (self , cursor ):
52
53
self ._cursor = cursor
53
- self ._is_clicked = False
54
+ self ._pressed = 0
54
55
self ._init_hardware ()
56
+ self ._debouncer = Debouncer (lambda : bool (self ._pressed & self ._pad_btns ['btn_a' ]))
55
57
56
58
def __enter__ (self ):
57
59
return self
@@ -90,21 +92,34 @@ def _init_hardware(self):
90
92
digitalio .DigitalInOut (board .BUTTON_OUT ),
91
93
digitalio .DigitalInOut (board .BUTTON_LATCH ))
92
94
95
+
93
96
@property
94
97
def is_clicked (self ):
95
98
"""Returns True if the cursor button was pressed
96
99
during previous call to update()
97
100
"""
98
- return self ._is_clicked
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
+
99
117
100
118
def update (self ):
101
119
"""Updates the cursor object."""
102
- pressed = self ._pad .get_pressed ()
103
- self ._check_cursor_movement (pressed )
104
- if self ._is_clicked :
105
- self ._is_clicked = False
106
- elif pressed & self ._pad_btns ['btn_a' ]:
107
- self ._is_clicked = True
120
+ self ._pressed = self ._pad .get_pressed ()
121
+ self ._check_cursor_movement (self ._pressed )
122
+ self ._debouncer .update ()
108
123
109
124
def _read_joystick_x (self , samples = 3 ):
110
125
"""Read the X analog joystick on the PyGamer.
0 commit comments