Add timing properties

This commit is contained in:
Dave Astels 2019-08-14 15:29:58 -04:00
parent 650c36937b
commit 0d05248312

View file

@ -70,6 +70,8 @@ class Debouncer(object):
self._set_state(_DEBOUNCED_STATE | _UNSTABLE_STATE) self._set_state(_DEBOUNCED_STATE | _UNSTABLE_STATE)
self.previous_time = 0 self.previous_time = 0
self.interval = interval self.interval = interval
self._previous_state_duration = 0
self._state_changed_time = 0
def _set_state(self, bits): def _set_state(self, bits):
@ -102,6 +104,8 @@ class Debouncer(object):
self.previous_time = now self.previous_time = now
self._toggle_state(_DEBOUNCED_STATE) self._toggle_state(_DEBOUNCED_STATE)
self._set_state(_CHANGED_STATE) self._set_state(_CHANGED_STATE)
self._previous_state_duration = now - self._state_changed_time
self._state_changed_time = now
@property @property
@ -120,3 +124,13 @@ class Debouncer(object):
def fell(self): def fell(self):
"""Return whether the debounced value went from high to low at the most recent update.""" """Return whether the debounced value went from high to low at the most recent update."""
return (not self._get_state(_DEBOUNCED_STATE)) and self._get_state(_CHANGED_STATE) return (not self._get_state(_DEBOUNCED_STATE)) and self._get_state(_CHANGED_STATE)
@property
def lastDuration(self):
"""Return the amount of time the state was stable prior to the most recent transition."""
return self._previous_state_duration
@property
def currentDuration(self):
"""Return the time since the most recent transition."""
return time.monotonic() - self._state_changed_time