15
15
Implementation Notes
16
16
--------------------
17
17
18
- **Hardware:**
19
-
20
- Not all hardware / CircuitPython combinations are capable of running the
21
- debouncer correctly for an extended length of time. If this line works
22
- on your microcontroller, then the debouncer should work forever:
23
-
24
- ``from time import monotonic_ns``
25
-
26
- If it gives an ImportError, then the time values available in Python become
27
- less accurate over the days, and the debouncer will take longer to react to
28
- button presses.
29
-
30
18
**Software and Dependencies:**
31
19
32
20
* Adafruit CircuitPython firmware for the supported boards:
33
21
https://github.com/adafruit/circuitpython/releases
22
+ * Adafruit Ticks library:
23
+ https://github.com/adafruit/Adafruit_CircuitPython_Ticks
34
24
"""
35
25
36
26
# imports
37
27
38
28
__version__ = "0.0.0-auto.0"
39
29
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Debouncer.git"
40
30
41
- import time
31
+ from adafruit_ticks import ticks_ms , ticks_diff
42
32
from micropython import const
43
33
44
34
_DEBOUNCED_STATE = const (0x01 )
45
35
_UNSTABLE_STATE = const (0x02 )
46
36
_CHANGED_STATE = const (0x04 )
47
37
48
- # Find out whether the current CircuitPython really supports time.monotonic_ns(),
49
- # which doesn't have the accuracy limitation.
50
- try :
51
- time .monotonic_ns ()
52
- TICKS_PER_SEC = 1_000_000_000
53
- MONOTONIC_TICKS = time .monotonic_ns
54
- except (AttributeError , NotImplementedError ):
55
- TICKS_PER_SEC = 1
56
- MONOTONIC_TICKS = time .monotonic
38
+ _TICKS_PER_SEC = const (1000 )
57
39
58
40
59
41
class Debouncer :
@@ -77,7 +59,7 @@ def __init__(self, io_or_predicate, interval=0.010):
77
59
78
60
# Could use the .interval setter, but pylint prefers that we explicitly
79
61
# set the real underlying attribute:
80
- self ._interval_ticks = interval * TICKS_PER_SEC
62
+ self ._interval_ticks = interval * _TICKS_PER_SEC
81
63
82
64
def _set_state (self , bits ):
83
65
self .state |= bits
@@ -93,29 +75,31 @@ def _get_state(self, bits):
93
75
94
76
def update (self ):
95
77
"""Update the debouncer state. MUST be called frequently"""
96
- now_ticks = MONOTONIC_TICKS ()
78
+ now_ticks = ticks_ms ()
97
79
self ._unset_state (_CHANGED_STATE )
98
80
current_state = self .function ()
99
81
if current_state != self ._get_state (_UNSTABLE_STATE ):
100
82
self ._last_bounce_ticks = now_ticks
101
83
self ._toggle_state (_UNSTABLE_STATE )
102
84
else :
103
- if now_ticks - self ._last_bounce_ticks >= self ._interval_ticks :
85
+ if ticks_diff ( now_ticks , self ._last_bounce_ticks ) >= self ._interval_ticks :
104
86
if current_state != self ._get_state (_DEBOUNCED_STATE ):
105
87
self ._last_bounce_ticks = now_ticks
106
88
self ._toggle_state (_DEBOUNCED_STATE )
107
89
self ._set_state (_CHANGED_STATE )
108
- self ._last_duration_ticks = now_ticks - self ._state_changed_ticks
90
+ self ._last_duration_ticks = ticks_diff (
91
+ now_ticks , self ._state_changed_ticks
92
+ )
109
93
self ._state_changed_ticks = now_ticks
110
94
111
95
@property
112
96
def interval (self ):
113
97
"""The debounce delay, in seconds"""
114
- return self ._interval_ticks / TICKS_PER_SEC
98
+ return self ._interval_ticks / _TICKS_PER_SEC
115
99
116
100
@interval .setter
117
101
def interval (self , new_interval_s ):
118
- self ._interval_ticks = new_interval_s * TICKS_PER_SEC
102
+ self ._interval_ticks = new_interval_s * _TICKS_PER_SEC
119
103
120
104
@property
121
105
def value (self ):
@@ -137,9 +121,9 @@ def fell(self):
137
121
@property
138
122
def last_duration (self ):
139
123
"""Return the number of seconds the state was stable prior to the most recent transition."""
140
- return self ._last_duration_ticks / TICKS_PER_SEC
124
+ return self ._last_duration_ticks / _TICKS_PER_SEC
141
125
142
126
@property
143
127
def current_duration (self ):
144
128
"""Return the number of seconds since the most recent transition."""
145
- return ( MONOTONIC_TICKS () - self ._state_changed_ticks ) / TICKS_PER_SEC
129
+ return ticks_diff ( ticks_ms (), self ._state_changed_ticks ) / _TICKS_PER_SEC
0 commit comments