@@ -133,7 +133,16 @@ def current_duration(self):
133
133
134
134
135
135
class Button (Debouncer ):
136
- """Debounce counter"""
136
+ """
137
+ Debounce counter. Counts multiple short presses (double click, triple click, etc.)
138
+ Reports long presses.
139
+
140
+ :param DigitalInOut/function pin: the DigitalIO or function to debounce.
141
+ :param int short_duration_ms: the maximum length of a short press in milliseconds.
142
+ :param int long_duration_ms: the minimum length of a long press in milliseconds.
143
+ :param bool active_down: True if a `False` value for the predicate represents
144
+ "pressed" (like pull up buttons).
145
+ """
137
146
138
147
def __init__ (
139
148
self ,
@@ -150,29 +159,28 @@ def __init__(
150
159
self .short_counter = 0
151
160
self .short_to_show = 0
152
161
self .long_registered = False
153
- self .long_showed = False
162
+ self .long_to_show = False
154
163
super ().__init__ (pin , ** kwargs )
155
164
156
- def _pushed ( self ):
157
- return (self . active_down and super (). fell ) or (
158
- not self . active_down and super (). rose
159
- )
165
+ @ property
166
+ def pressed (self ):
167
+ """Return whether the button was pressed or not at the last update."""
168
+ return ( self . active_down and self . fell ) or ( not self . active_down and self . rose )
160
169
161
- def _released ( self ):
162
- return (self . active_down and super (). rose ) or (
163
- not self . active_down and super (). fell
164
- )
170
+ @ property
171
+ def released (self ):
172
+ """Return whether the button was release or not at the last update."""
173
+ return ( self . active_down and self . rose ) or ( not self . active_down and self . fell )
165
174
166
175
def update (self , new_state = None ):
167
176
super ().update (new_state )
168
- if self ._pushed () :
177
+ if self .pressed :
169
178
self .last_change_ms = ticks_ms ()
170
179
self .short_counter = self .short_counter + 1
171
- elif self ._released () :
180
+ elif self .released :
172
181
self .last_change_ms = ticks_ms ()
173
182
if self .long_registered :
174
183
self .long_registered = False
175
- self .long_showed = False
176
184
else :
177
185
duration = ticks_diff (ticks_ms (), self .last_change_ms )
178
186
if (
@@ -181,6 +189,7 @@ def update(self, new_state=None):
181
189
and duration > self .long_duration_ms
182
190
):
183
191
self .long_registered = True
192
+ self .long_to_show = True
184
193
self .short_to_show = self .short_counter - 1
185
194
self .short_counter = 0
186
195
elif (
@@ -190,18 +199,17 @@ def update(self, new_state=None):
190
199
):
191
200
self .short_to_show = self .short_counter
192
201
self .short_counter = 0
202
+ else :
203
+ self .long_to_show = False
204
+ self .short_to_show = 0
193
205
194
206
@property
195
207
def short_count (self ):
196
- """Return the number of short press"""
197
- ret = self .short_to_show
198
- self .short_to_show = 0
199
- return ret
208
+ """Return the number of short press if a series of short presses has
209
+ ended at the last update."""
210
+ return self .short_to_show
200
211
201
212
@property
202
213
def long_press (self ):
203
- """Return whether long press has occured"""
204
- if self .long_registered and not self .long_showed :
205
- self .long_showed = True
206
- return True
207
- return False
214
+ """Return whether a long press has occured at the last update."""
215
+ return self .long_to_show
0 commit comments