|
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)
|
@@ -127,3 +127,78 @@ def last_duration(self):
|
127 | 127 | def current_duration(self):
|
128 | 128 | """Return the number of seconds since the most recent transition."""
|
129 | 129 | 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 |
0 commit comments