|
28 | 28 | __version__ = "0.0.0-auto.0"
|
29 | 29 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Debouncer.git"
|
30 | 30 |
|
31 |
| -from adafruit_ticks import ticks_ms, ticks_diff |
32 | 31 | from micropython import const
|
| 32 | +from adafruit_ticks import ticks_ms, ticks_diff |
33 | 33 |
|
34 | 34 | _DEBOUNCED_STATE = const(0x01)
|
35 | 35 | _UNSTABLE_STATE = const(0x02)
|
@@ -132,52 +132,53 @@ def current_duration(self):
|
132 | 132 |
|
133 | 133 | class Button(Debouncer):
|
134 | 134 | """Debounce counter"""
|
135 |
| - def __init__(self, pin, short_duration = 0.2, long_duration = 0.5, active_down = True, **kwargs): |
| 135 | + def __init__(self, pin, short_duration=0.2, long_duration=0.5, active_down=True, **kwargs): |
136 | 136 | self.short_duration = short_duration
|
137 | 137 | self.long_duration = long_duration
|
138 | 138 | self.active_down = active_down
|
139 | 139 | self.last_change_ticks = ticks_ms()
|
140 | 140 | self.short_counter = 0
|
141 |
| - self.short_showed = 0 |
| 141 | + self.short_to_show = 0 |
142 | 142 | self.long_registered = False
|
143 | 143 | self.long_showed = False
|
144 | 144 | super(Button, self).__init__(pin, **kwargs)
|
145 | 145 |
|
146 |
| - def pushed (self): |
| 146 | + def _pushed(self): |
147 | 147 | return (self.active_down and super().fell) or (not self.active_down and super().rose)
|
148 | 148 |
|
149 |
| - def released (self): |
| 149 | + def _released(self): |
150 | 150 | return (self.active_down and super().rose) or (not self.active_down and super().fell)
|
151 | 151 |
|
152 |
| - def update (self): |
| 152 | + def update(self): |
153 | 153 | super().update()
|
154 |
| - if self.pushed(): |
| 154 | + if self._pushed(): |
155 | 155 | self.last_change_ticks = ticks_ms()
|
156 | 156 | self.short_counter = self.short_counter + 1
|
157 |
| - elif self.released(): |
| 157 | + elif self._released(): |
158 | 158 | self.last_change_ticks = ticks_ms()
|
159 | 159 | if self.long_registered:
|
160 | 160 | self.long_registered = False
|
161 | 161 | self.long_showed = False
|
162 | 162 | else:
|
163 |
| - now_ticks = ticks_ms() |
164 |
| - duration = ticks_diff(now_ticks, self.last_change_ticks) |
| 163 | + duration = ticks_diff(ticks_ms(), self.last_change_ticks) |
165 | 164 | if not self.long_registered and self.value != self.active_down and duration > self.long_duration:
|
166 | 165 | self.long_registered = True
|
167 |
| - self.short_showed = self.short_counter - 1 |
| 166 | + self.short_to_show = self.short_counter - 1 |
168 | 167 | self.short_counter = 0
|
169 | 168 | elif self.short_counter > 0 and self.value == self.active_down and duration > self.short_duration:
|
170 |
| - self.short_showed = self.short_counter |
| 169 | + self.short_to_show = self.short_counter |
171 | 170 | self.short_counter = 0
|
172 | 171 |
|
173 | 172 | @property
|
174 |
| - def short_count (self): |
175 |
| - ret = self.short_showed |
176 |
| - self.short_showed = 0 |
| 173 | + def short_count(self): |
| 174 | + """Return the number of short press""" |
| 175 | + ret = self.short_to_show |
| 176 | + self.short_to_show = 0 |
177 | 177 | return ret
|
178 | 178 |
|
179 | 179 | @property
|
180 |
| - def long_press (self): |
| 180 | + def long_press(self): |
| 181 | + """Return whether long press has occured""" |
181 | 182 | if self.long_registered and not self.long_showed:
|
182 | 183 | self.long_showed = True
|
183 | 184 | return True
|
|
0 commit comments