67
67
# Find out whether the current CircuitPython supports time.monotonic_ns(),
68
68
# which doesn't have the accuracy limitation.
69
69
if hasattr (time , 'monotonic_ns' ):
70
- MONOTONIC_UNITS_PER_SEC = 1_000_000_000
71
- MONOTONIC_TIME = time .monotonic_ns
70
+ TICKS_PER_SEC = 1_000_000_000
71
+ MONOTONIC_TICKS = time .monotonic_ns
72
72
else :
73
- MONOTONIC_UNITS_PER_SEC = 1
74
- MONOTONIC_TIME = time .monotonic
73
+ TICKS_PER_SEC = 1
74
+ MONOTONIC_TICKS = time .monotonic
75
75
76
76
77
77
class Debouncer (object ):
@@ -89,10 +89,13 @@ def __init__(self, io_or_predicate, interval=0.010):
89
89
self .function = io_or_predicate
90
90
if self .function ():
91
91
self ._set_state (_DEBOUNCED_STATE | _UNSTABLE_STATE )
92
- self .previous_time = 0
93
- self .interval = interval
94
- self ._previous_state_duration = 0
95
- self ._state_changed_time = 0
92
+ self ._last_bounce_ticks = 0
93
+ self ._last_duration_ticks = 0
94
+ self ._state_changed_ticks = 0
95
+
96
+ # Could use the .interval setter, but pylint prefers that we explicitly
97
+ # set the real underlying attribute:
98
+ self ._interval_ticks = interval * TICKS_PER_SEC
96
99
97
100
98
101
def _set_state (self , bits ):
@@ -113,30 +116,30 @@ def _get_state(self, bits):
113
116
114
117
def update (self ):
115
118
"""Update the debouncer state. MUST be called frequently"""
116
- now = MONOTONIC_TIME ()
119
+ now_ticks = MONOTONIC_TICKS ()
117
120
self ._unset_state (_CHANGED_STATE )
118
121
current_state = self .function ()
119
122
if current_state != self ._get_state (_UNSTABLE_STATE ):
120
- self .previous_time = now
123
+ self ._last_bounce_ticks = now_ticks
121
124
self ._toggle_state (_UNSTABLE_STATE )
122
125
else :
123
- if now - self .previous_time >= self ._interval :
126
+ if now_ticks - self ._last_bounce_ticks >= self ._interval_ticks :
124
127
if current_state != self ._get_state (_DEBOUNCED_STATE ):
125
- self .previous_time = now
128
+ self ._last_bounce_ticks = now_ticks
126
129
self ._toggle_state (_DEBOUNCED_STATE )
127
130
self ._set_state (_CHANGED_STATE )
128
- self ._previous_state_duration = now - self ._state_changed_time
129
- self ._state_changed_time = now
131
+ self ._last_duration_ticks = now_ticks - self ._state_changed_ticks
132
+ self ._state_changed_ticks = now_ticks
130
133
131
134
@property
132
135
def interval (self ):
133
136
"""The debounce delay, in seconds"""
134
- return self ._interval / MONOTONIC_UNITS_PER_SEC
137
+ return self ._interval_ticks / TICKS_PER_SEC
135
138
136
139
137
140
@interval .setter
138
141
def interval (self , new_interval_s ):
139
- self ._interval = new_interval_s * MONOTONIC_UNITS_PER_SEC
142
+ self ._interval_ticks = new_interval_s * TICKS_PER_SEC
140
143
141
144
142
145
@property
@@ -158,10 +161,10 @@ def fell(self):
158
161
159
162
@property
160
163
def last_duration (self ):
161
- """Return the amount of time the state was stable prior to the most recent transition."""
162
- return self ._previous_state_duration
164
+ """Return the number of seconds the state was stable prior to the most recent transition."""
165
+ return self ._last_duration_ticks / TICKS_PER_SEC
163
166
164
167
@property
165
168
def current_duration (self ):
166
- """Return the time since the most recent transition."""
167
- return time . monotonic ( ) - self ._state_changed_time
169
+ """Return the number of seconds since the most recent transition."""
170
+ return ( MONOTONIC_TICKS ( ) - self ._state_changed_ticks ) / TICKS_PER_SEC
0 commit comments