@@ -129,43 +129,52 @@ def current_duration(self):
129
129
return ticks_diff (ticks_ms (), self ._state_changed_ticks ) / _TICKS_PER_SEC
130
130
131
131
132
-
133
132
class Button (Debouncer ):
134
133
"""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
138
141
self .active_down = active_down
139
- self .last_change_ticks = ticks_ms ()
142
+ self .last_change_ms = ticks_ms ()
140
143
self .short_counter = 0
141
144
self .short_to_show = 0
142
145
self .long_registered = False
143
146
self .long_showed = False
144
147
super ().__init__ (pin , ** kwargs )
145
148
146
149
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 )
148
152
149
153
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 )
151
156
152
157
def update (self ):
153
158
super ().update ()
154
159
if self ._pushed ():
155
- self .last_change_ticks = ticks_ms ()
160
+ self .last_change_ms = ticks_ms ()
156
161
self .short_counter = self .short_counter + 1
157
162
elif self ._released ():
158
- self .last_change_ticks = ticks_ms ()
163
+ self .last_change_ms = ticks_ms ()
159
164
if self .long_registered :
160
165
self .long_registered = False
161
166
self .long_showed = False
162
167
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 :
165
172
self .long_registered = True
166
173
self .short_to_show = self .short_counter - 1
167
174
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 :
169
178
self .short_to_show = self .short_counter
170
179
self .short_counter = 0
171
180
0 commit comments