Skip to content

Commit c6b924e

Browse files
committed
change active_down to value_when_pressed
1 parent 3f4b39e commit c6b924e

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

adafruit_debouncer.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,21 @@ class Button(Debouncer):
140140
:param DigitalInOut/function pin: the DigitalIO or function to debounce.
141141
:param int short_duration_ms: the maximum length of a short press in milliseconds.
142142
: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).
143+
:param bool value_when_pressed: the value of the predicate when the button is
144+
pressed. Defaults to False (pull up buttons are common).
145145
"""
146146

147147
def __init__(
148148
self,
149149
pin,
150150
short_duration_ms=200,
151151
long_duration_ms=500,
152-
active_down=True,
152+
value_when_pressed=False,
153153
**kwargs
154154
):
155155
self.short_duration_ms = short_duration_ms
156156
self.long_duration_ms = long_duration_ms
157-
self.active_down = active_down
157+
self.value_when_pressed = value_when_pressed
158158
self.last_change_ms = ticks_ms()
159159
self.short_counter = 0
160160
self.short_to_show = 0
@@ -165,12 +165,16 @@ def __init__(
165165
@property
166166
def pressed(self):
167167
"""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)
168+
return (self.value_when_pressed and self.rose) or (
169+
not self.value_when_pressed and self.fell
170+
)
169171

170172
@property
171173
def released(self):
172174
"""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)
175+
return (self.value_when_pressed and self.fell) or (
176+
not self.value_when_pressed and self.rose
177+
)
174178

175179
def update(self, new_state=None):
176180
super().update(new_state)
@@ -185,7 +189,7 @@ def update(self, new_state=None):
185189
duration = ticks_diff(ticks_ms(), self.last_change_ms)
186190
if (
187191
not self.long_registered
188-
and self.value != self.active_down
192+
and self.value == self.value_when_pressed
189193
and duration > self.long_duration_ms
190194
):
191195
self.long_registered = True
@@ -194,7 +198,7 @@ def update(self, new_state=None):
194198
self.short_counter = 0
195199
elif (
196200
self.short_counter > 0
197-
and self.value == self.active_down
201+
and self.value != self.value_when_pressed
198202
and duration > self.short_duration_ms
199203
):
200204
self.short_to_show = self.short_counter

0 commit comments

Comments
 (0)