Skip to content

Use adafruit ticks library #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Dependencies
This driver depends on:

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

Please ensure all dependencies are available on the CircuitPython filesystem.
This is easily achieved by downloading
Expand Down
44 changes: 14 additions & 30 deletions adafruit_debouncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,27 @@
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

__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:
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
# SPDX-License-Identifier: Unlicense

Adafruit-Blinka
adafruit-circuitpython-ticks
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
author_email="[email protected]",
install_requires=[
"Adafruit-Blinka",
"adafruit-circuitpython-ticks",
],
# Choose your license
license="MIT",
Expand Down