@@ -140,21 +140,21 @@ class Button(Debouncer):
140
140
:param DigitalInOut/function pin: the DigitalIO or function to debounce.
141
141
:param int short_duration_ms: the maximum length of a short press in milliseconds.
142
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).
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 ).
145
145
"""
146
146
147
147
def __init__ (
148
148
self ,
149
149
pin ,
150
150
short_duration_ms = 200 ,
151
151
long_duration_ms = 500 ,
152
- active_down = True ,
152
+ value_when_pressed = False ,
153
153
** kwargs
154
154
):
155
155
self .short_duration_ms = short_duration_ms
156
156
self .long_duration_ms = long_duration_ms
157
- self .active_down = active_down
157
+ self .value_when_pressed = value_when_pressed
158
158
self .last_change_ms = ticks_ms ()
159
159
self .short_counter = 0
160
160
self .short_to_show = 0
@@ -165,12 +165,16 @@ def __init__(
165
165
@property
166
166
def pressed (self ):
167
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 )
168
+ return (self .value_when_pressed and self .rose ) or (
169
+ not self .value_when_pressed and self .fell
170
+ )
169
171
170
172
@property
171
173
def released (self ):
172
174
"""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
+ )
174
178
175
179
def update (self , new_state = None ):
176
180
super ().update (new_state )
@@ -185,7 +189,7 @@ def update(self, new_state=None):
185
189
duration = ticks_diff (ticks_ms (), self .last_change_ms )
186
190
if (
187
191
not self .long_registered
188
- and self .value != self .active_down
192
+ and self .value == self .value_when_pressed
189
193
and duration > self .long_duration_ms
190
194
):
191
195
self .long_registered = True
@@ -194,7 +198,7 @@ def update(self, new_state=None):
194
198
self .short_counter = 0
195
199
elif (
196
200
self .short_counter > 0
197
- and self .value == self .active_down
201
+ and self .value != self .value_when_pressed
198
202
and duration > self .short_duration_ms
199
203
):
200
204
self .short_to_show = self .short_counter
0 commit comments