Skip to content

Commit 3f4b39e

Browse files
committed
fix the button class doing some double edge detection and docs
1 parent 72b7319 commit 3f4b39e

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

adafruit_debouncer.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,16 @@ def current_duration(self):
133133

134134

135135
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+
"""
137146

138147
def __init__(
139148
self,
@@ -150,29 +159,28 @@ def __init__(
150159
self.short_counter = 0
151160
self.short_to_show = 0
152161
self.long_registered = False
153-
self.long_showed = False
162+
self.long_to_show = False
154163
super().__init__(pin, **kwargs)
155164

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)
160169

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)
165174

166175
def update(self, new_state=None):
167176
super().update(new_state)
168-
if self._pushed():
177+
if self.pressed:
169178
self.last_change_ms = ticks_ms()
170179
self.short_counter = self.short_counter + 1
171-
elif self._released():
180+
elif self.released:
172181
self.last_change_ms = ticks_ms()
173182
if self.long_registered:
174183
self.long_registered = False
175-
self.long_showed = False
176184
else:
177185
duration = ticks_diff(ticks_ms(), self.last_change_ms)
178186
if (
@@ -181,6 +189,7 @@ def update(self, new_state=None):
181189
and duration > self.long_duration_ms
182190
):
183191
self.long_registered = True
192+
self.long_to_show = True
184193
self.short_to_show = self.short_counter - 1
185194
self.short_counter = 0
186195
elif (
@@ -190,18 +199,17 @@ def update(self, new_state=None):
190199
):
191200
self.short_to_show = self.short_counter
192201
self.short_counter = 0
202+
else:
203+
self.long_to_show = False
204+
self.short_to_show = 0
193205

194206
@property
195207
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
200211

201212
@property
202213
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

examples/debouncer_multi.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@
1616
switch.update()
1717
if switch.long_press:
1818
print("long")
19-
count = switch.short_count
20-
if count != 0:
21-
print("count=", count)
19+
if switch.short_count != 0:
20+
print("count=", switch.short_count)

0 commit comments

Comments
 (0)