Skip to content

Commit fc201a5

Browse files
authored
Merge pull request #34 from prplz/main
Use adafruit ticks library
2 parents 1ff5ba6 + 4decfb6 commit fc201a5

File tree

4 files changed

+17
-30
lines changed

4 files changed

+17
-30
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Dependencies
2323
This driver depends on:
2424

2525
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
26+
* `Adafruit Ticks <https://github.com/adafruit/Adafruit_CircuitPython_Ticks>`_
2627

2728
Please ensure all dependencies are available on the CircuitPython filesystem.
2829
This is easily achieved by downloading

adafruit_debouncer.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,27 @@
1515
Implementation Notes
1616
--------------------
1717
18-
**Hardware:**
19-
20-
Not all hardware / CircuitPython combinations are capable of running the
21-
debouncer correctly for an extended length of time. If this line works
22-
on your microcontroller, then the debouncer should work forever:
23-
24-
``from time import monotonic_ns``
25-
26-
If it gives an ImportError, then the time values available in Python become
27-
less accurate over the days, and the debouncer will take longer to react to
28-
button presses.
29-
3018
**Software and Dependencies:**
3119
3220
* Adafruit CircuitPython firmware for the supported boards:
3321
https://github.com/adafruit/circuitpython/releases
22+
* Adafruit Ticks library:
23+
https://github.com/adafruit/Adafruit_CircuitPython_Ticks
3424
"""
3525

3626
# imports
3727

3828
__version__ = "0.0.0-auto.0"
3929
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Debouncer.git"
4030

41-
import time
31+
from adafruit_ticks import ticks_ms, ticks_diff
4232
from micropython import const
4333

4434
_DEBOUNCED_STATE = const(0x01)
4535
_UNSTABLE_STATE = const(0x02)
4636
_CHANGED_STATE = const(0x04)
4737

48-
# Find out whether the current CircuitPython really supports time.monotonic_ns(),
49-
# which doesn't have the accuracy limitation.
50-
try:
51-
time.monotonic_ns()
52-
TICKS_PER_SEC = 1_000_000_000
53-
MONOTONIC_TICKS = time.monotonic_ns
54-
except (AttributeError, NotImplementedError):
55-
TICKS_PER_SEC = 1
56-
MONOTONIC_TICKS = time.monotonic
38+
_TICKS_PER_SEC = const(1000)
5739

5840

5941
class Debouncer:
@@ -77,7 +59,7 @@ def __init__(self, io_or_predicate, interval=0.010):
7759

7860
# Could use the .interval setter, but pylint prefers that we explicitly
7961
# set the real underlying attribute:
80-
self._interval_ticks = interval * TICKS_PER_SEC
62+
self._interval_ticks = interval * _TICKS_PER_SEC
8163

8264
def _set_state(self, bits):
8365
self.state |= bits
@@ -93,29 +75,31 @@ def _get_state(self, bits):
9375

9476
def update(self):
9577
"""Update the debouncer state. MUST be called frequently"""
96-
now_ticks = MONOTONIC_TICKS()
78+
now_ticks = ticks_ms()
9779
self._unset_state(_CHANGED_STATE)
9880
current_state = self.function()
9981
if current_state != self._get_state(_UNSTABLE_STATE):
10082
self._last_bounce_ticks = now_ticks
10183
self._toggle_state(_UNSTABLE_STATE)
10284
else:
103-
if now_ticks - self._last_bounce_ticks >= self._interval_ticks:
85+
if ticks_diff(now_ticks, self._last_bounce_ticks) >= self._interval_ticks:
10486
if current_state != self._get_state(_DEBOUNCED_STATE):
10587
self._last_bounce_ticks = now_ticks
10688
self._toggle_state(_DEBOUNCED_STATE)
10789
self._set_state(_CHANGED_STATE)
108-
self._last_duration_ticks = now_ticks - self._state_changed_ticks
90+
self._last_duration_ticks = ticks_diff(
91+
now_ticks, self._state_changed_ticks
92+
)
10993
self._state_changed_ticks = now_ticks
11094

11195
@property
11296
def interval(self):
11397
"""The debounce delay, in seconds"""
114-
return self._interval_ticks / TICKS_PER_SEC
98+
return self._interval_ticks / _TICKS_PER_SEC
11599

116100
@interval.setter
117101
def interval(self, new_interval_s):
118-
self._interval_ticks = new_interval_s * TICKS_PER_SEC
102+
self._interval_ticks = new_interval_s * _TICKS_PER_SEC
119103

120104
@property
121105
def value(self):
@@ -137,9 +121,9 @@ def fell(self):
137121
@property
138122
def last_duration(self):
139123
"""Return the number of seconds the state was stable prior to the most recent transition."""
140-
return self._last_duration_ticks / TICKS_PER_SEC
124+
return self._last_duration_ticks / _TICKS_PER_SEC
141125

142126
@property
143127
def current_duration(self):
144128
"""Return the number of seconds since the most recent transition."""
145-
return (MONOTONIC_TICKS() - self._state_changed_ticks) / TICKS_PER_SEC
129+
return ticks_diff(ticks_ms(), self._state_changed_ticks) / _TICKS_PER_SEC

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
# SPDX-License-Identifier: Unlicense
44

55
Adafruit-Blinka
6+
adafruit-circuitpython-ticks

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
author_email="circuitpython@adafruit.com",
3737
install_requires=[
3838
"Adafruit-Blinka",
39+
"adafruit-circuitpython-ticks",
3940
],
4041
# Choose your license
4142
license="MIT",

0 commit comments

Comments
 (0)