diff --git a/README.rst b/README.rst index f313277..7e954a5 100644 --- a/README.rst +++ b/README.rst @@ -23,6 +23,7 @@ Dependencies This driver depends on: * `Adafruit CircuitPython `_ +* `Adafruit Ticks `_ Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading diff --git a/adafruit_debouncer.py b/adafruit_debouncer.py index e8f8a52..7ab0579 100644 --- a/adafruit_debouncer.py +++ b/adafruit_debouncer.py @@ -15,22 +15,12 @@ Implementation Notes -------------------- -**Hardware:** - -Not all hardware / CircuitPython combinations are capable of running the -debouncer correctly for an extended length of time. If this line works -on your microcontroller, then the debouncer should work forever: - -``from time import monotonic_ns`` - -If it gives an ImportError, then the time values available in Python become -less accurate over the days, and the debouncer will take longer to react to -button presses. - **Software and Dependencies:** * Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases +* Adafruit Ticks library: + https://github.com/adafruit/Adafruit_CircuitPython_Ticks """ # imports @@ -38,22 +28,14 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Debouncer.git" -import time +from adafruit_ticks import ticks_ms, ticks_diff from micropython import const _DEBOUNCED_STATE = const(0x01) _UNSTABLE_STATE = const(0x02) _CHANGED_STATE = const(0x04) -# Find out whether the current CircuitPython really supports time.monotonic_ns(), -# which doesn't have the accuracy limitation. -try: - time.monotonic_ns() - TICKS_PER_SEC = 1_000_000_000 - MONOTONIC_TICKS = time.monotonic_ns -except (AttributeError, NotImplementedError): - TICKS_PER_SEC = 1 - MONOTONIC_TICKS = time.monotonic +_TICKS_PER_SEC = const(1000) class Debouncer: @@ -77,7 +59,7 @@ def __init__(self, io_or_predicate, interval=0.010): # Could use the .interval setter, but pylint prefers that we explicitly # set the real underlying attribute: - self._interval_ticks = interval * TICKS_PER_SEC + self._interval_ticks = interval * _TICKS_PER_SEC def _set_state(self, bits): self.state |= bits @@ -93,29 +75,31 @@ def _get_state(self, bits): def update(self): """Update the debouncer state. MUST be called frequently""" - now_ticks = MONOTONIC_TICKS() + now_ticks = ticks_ms() self._unset_state(_CHANGED_STATE) current_state = self.function() if current_state != self._get_state(_UNSTABLE_STATE): self._last_bounce_ticks = now_ticks self._toggle_state(_UNSTABLE_STATE) else: - if now_ticks - self._last_bounce_ticks >= self._interval_ticks: + if ticks_diff(now_ticks, self._last_bounce_ticks) >= self._interval_ticks: if current_state != self._get_state(_DEBOUNCED_STATE): self._last_bounce_ticks = now_ticks self._toggle_state(_DEBOUNCED_STATE) self._set_state(_CHANGED_STATE) - self._last_duration_ticks = now_ticks - self._state_changed_ticks + self._last_duration_ticks = ticks_diff( + now_ticks, self._state_changed_ticks + ) self._state_changed_ticks = now_ticks @property def interval(self): """The debounce delay, in seconds""" - return self._interval_ticks / TICKS_PER_SEC + return self._interval_ticks / _TICKS_PER_SEC @interval.setter def interval(self, new_interval_s): - self._interval_ticks = new_interval_s * TICKS_PER_SEC + self._interval_ticks = new_interval_s * _TICKS_PER_SEC @property def value(self): @@ -137,9 +121,9 @@ def fell(self): @property def last_duration(self): """Return the number of seconds the state was stable prior to the most recent transition.""" - return self._last_duration_ticks / TICKS_PER_SEC + return self._last_duration_ticks / _TICKS_PER_SEC @property def current_duration(self): """Return the number of seconds since the most recent transition.""" - return (MONOTONIC_TICKS() - self._state_changed_ticks) / TICKS_PER_SEC + return ticks_diff(ticks_ms(), self._state_changed_ticks) / _TICKS_PER_SEC diff --git a/requirements.txt b/requirements.txt index 17a850d..be51e28 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ # SPDX-License-Identifier: Unlicense Adafruit-Blinka +adafruit-circuitpython-ticks diff --git a/setup.py b/setup.py index 59aeff3..d481b56 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,7 @@ author_email="circuitpython@adafruit.com", install_requires=[ "Adafruit-Blinka", + "adafruit-circuitpython-ticks", ], # Choose your license license="MIT",