Skip to content

Commit 0fde4e4

Browse files
authored
Merge pull request #35 from d-a-v/multi
Button class and example for short press counter and long press detector
2 parents b3aa8a3 + 016a726 commit 0fde4e4

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

adafruit_debouncer.py

+76-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
__version__ = "0.0.0-auto.0"
2929
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Debouncer.git"
3030

31-
from adafruit_ticks import ticks_ms, ticks_diff
3231
from micropython import const
32+
from adafruit_ticks import ticks_ms, ticks_diff
3333

3434
_DEBOUNCED_STATE = const(0x01)
3535
_UNSTABLE_STATE = const(0x02)
@@ -127,3 +127,78 @@ def last_duration(self):
127127
def current_duration(self):
128128
"""Return the number of seconds since the most recent transition."""
129129
return ticks_diff(ticks_ms(), self._state_changed_ticks) / _TICKS_PER_SEC
130+
131+
132+
class Button(Debouncer):
133+
"""Debounce counter"""
134+
135+
def __init__(
136+
self,
137+
pin,
138+
short_duration_ms=200,
139+
long_duration_ms=500,
140+
active_down=True,
141+
**kwargs
142+
):
143+
self.short_duration_ms = short_duration_ms
144+
self.long_duration_ms = long_duration_ms
145+
self.active_down = active_down
146+
self.last_change_ms = ticks_ms()
147+
self.short_counter = 0
148+
self.short_to_show = 0
149+
self.long_registered = False
150+
self.long_showed = False
151+
super().__init__(pin, **kwargs)
152+
153+
def _pushed(self):
154+
return (self.active_down and super().fell) or (
155+
not self.active_down and super().rose
156+
)
157+
158+
def _released(self):
159+
return (self.active_down and super().rose) or (
160+
not self.active_down and super().fell
161+
)
162+
163+
def update(self):
164+
super().update()
165+
if self._pushed():
166+
self.last_change_ms = ticks_ms()
167+
self.short_counter = self.short_counter + 1
168+
elif self._released():
169+
self.last_change_ms = ticks_ms()
170+
if self.long_registered:
171+
self.long_registered = False
172+
self.long_showed = False
173+
else:
174+
duration = ticks_diff(ticks_ms(), self.last_change_ms)
175+
if (
176+
not self.long_registered
177+
and self.value != self.active_down
178+
and duration > self.long_duration_ms
179+
):
180+
self.long_registered = True
181+
self.short_to_show = self.short_counter - 1
182+
self.short_counter = 0
183+
elif (
184+
self.short_counter > 0
185+
and self.value == self.active_down
186+
and duration > self.short_duration_ms
187+
):
188+
self.short_to_show = self.short_counter
189+
self.short_counter = 0
190+
191+
@property
192+
def short_count(self):
193+
"""Return the number of short press"""
194+
ret = self.short_to_show
195+
self.short_to_show = 0
196+
return ret
197+
198+
@property
199+
def long_press(self):
200+
"""Return whether long press has occured"""
201+
if self.long_registered and not self.long_showed:
202+
self.long_showed = True
203+
return True
204+
return False

examples/debouncer_multi.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2022 david gauchard
2+
# SPDX-License-Identifier: MIT
3+
4+
import board
5+
import digitalio
6+
from adafruit_debouncer import Button
7+
8+
# This example shows how to count short clicks or detect a long press
9+
10+
pin = digitalio.DigitalInOut(board.D12)
11+
pin.direction = digitalio.Direction.INPUT
12+
pin.pull = digitalio.Pull.UP
13+
switch = Button(pin)
14+
15+
while True:
16+
switch.update()
17+
if switch.long_press:
18+
print("long")
19+
count = switch.short_count
20+
if count != 0:
21+
print("count=", count)

0 commit comments

Comments
 (0)