Skip to content

Commit b89a85a

Browse files
committed
ticks are now in ms
1 parent 0e64e85 commit b89a85a

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

adafruit_debouncer.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,43 +129,52 @@ def current_duration(self):
129129
return ticks_diff(ticks_ms(), self._state_changed_ticks) / _TICKS_PER_SEC
130130

131131

132-
133132
class Button(Debouncer):
134133
"""Debounce counter"""
135-
def __init__(self, pin, short_duration=0.2, long_duration=0.5, active_down=True, **kwargs):
136-
self.short_duration = short_duration
137-
self.long_duration = long_duration
134+
def __init__(self, pin,
135+
short_duration_ms=200,
136+
long_duration_ms=500,
137+
active_down=True,
138+
**kwargs):
139+
self.short_duration_ms = short_duration_ms
140+
self.long_duration_ms = long_duration_ms
138141
self.active_down = active_down
139-
self.last_change_ticks = ticks_ms()
142+
self.last_change_ms = ticks_ms()
140143
self.short_counter = 0
141144
self.short_to_show = 0
142145
self.long_registered = False
143146
self.long_showed = False
144147
super().__init__(pin, **kwargs)
145148

146149
def _pushed(self):
147-
return (self.active_down and super().fell) or (not self.active_down and super().rose)
150+
return (self.active_down and super().fell) or \
151+
(not self.active_down and super().rose)
148152

149153
def _released(self):
150-
return (self.active_down and super().rose) or (not self.active_down and super().fell)
154+
return (self.active_down and super().rose) or \
155+
(not self.active_down and super().fell)
151156

152157
def update(self):
153158
super().update()
154159
if self._pushed():
155-
self.last_change_ticks = ticks_ms()
160+
self.last_change_ms = ticks_ms()
156161
self.short_counter = self.short_counter + 1
157162
elif self._released():
158-
self.last_change_ticks = ticks_ms()
163+
self.last_change_ms = ticks_ms()
159164
if self.long_registered:
160165
self.long_registered = False
161166
self.long_showed = False
162167
else:
163-
duration = ticks_diff(ticks_ms(), self.last_change_ticks)
164-
if not self.long_registered and self.value != self.active_down and duration > self.long_duration:
168+
duration = ticks_diff(ticks_ms(), self.last_change_ms)
169+
if not self.long_registered and \
170+
self.value != self.active_down and \
171+
duration > self.long_duration_ms:
165172
self.long_registered = True
166173
self.short_to_show = self.short_counter - 1
167174
self.short_counter = 0
168-
elif self.short_counter > 0 and self.value == self.active_down and duration > self.short_duration:
175+
elif self.short_counter > 0 and \
176+
self.value == self.active_down and \
177+
duration > self.short_duration_ms:
169178
self.short_to_show = self.short_counter
170179
self.short_counter = 0
171180

0 commit comments

Comments
 (0)