Skip to content

Commit

Permalink
Merge pull request #34 from prplz/main
Browse files Browse the repository at this point in the history
Use adafruit ticks library
  • Loading branch information
FoamyGuy committed Jan 24, 2022
2 parents 1ff5ba6 + 4decfb6 commit fc201a5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.rst
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
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
Expand Up @@ -3,3 +3,4 @@
# SPDX-License-Identifier: Unlicense

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

0 comments on commit fc201a5

Please sign in to comment.