33
33
34
34
try :
35
35
from typing import Callable , Optional , Union
36
+ from circuitpython_typing .io import ROValueIO
36
37
except ImportError :
37
38
pass
38
- from digitalio import DigitalInOut
39
39
40
40
_DEBOUNCED_STATE : int = const (0x01 )
41
41
_UNSTABLE_STATE : int = const (0x02 )
@@ -49,7 +49,7 @@ class Debouncer:
49
49
50
50
def __init__ (
51
51
self ,
52
- io_or_predicate : Union [DigitalInOut , Callable [[], bool ]],
52
+ io_or_predicate : Union [ROValueIO , Callable [[], bool ]],
53
53
interval : float = 0.010 ,
54
54
) -> None :
55
55
"""Make an instance.
@@ -58,7 +58,7 @@ def __init__(
58
58
:param float interval: bounce threshold in seconds (default is 0.010, i.e. 10 milliseconds)
59
59
"""
60
60
self .state = 0x00
61
- if isinstance (io_or_predicate , DigitalInOut ):
61
+ if hasattr (io_or_predicate , "value" ):
62
62
self .function = lambda : io_or_predicate .value
63
63
else :
64
64
self .function = io_or_predicate
@@ -113,7 +113,7 @@ def interval(self) -> float:
113
113
114
114
@interval .setter
115
115
def interval (self , new_interval_s : float ) -> None :
116
- self ._interval_ticks = int ( new_interval_s * _TICKS_PER_SEC )
116
+ self ._interval_ticks = new_interval_s * _TICKS_PER_SEC
117
117
118
118
@property
119
119
def value (self ) -> bool :
@@ -159,12 +159,12 @@ class Button(Debouncer):
159
159
160
160
def __init__ (
161
161
self ,
162
- pin : DigitalInOut ,
162
+ pin : Union [ ROValueIO , Callable [[], bool ]] ,
163
163
short_duration_ms : int = 200 ,
164
164
long_duration_ms : int = 500 ,
165
165
value_when_pressed : bool = False ,
166
166
** kwargs
167
- ):
167
+ ) -> None :
168
168
self .short_duration_ms = short_duration_ms
169
169
self .long_duration_ms = long_duration_ms
170
170
self .value_when_pressed = value_when_pressed
@@ -189,7 +189,7 @@ def released(self) -> bool:
189
189
not self .value_when_pressed and self .rose
190
190
)
191
191
192
- def update (self , new_state : Optional [int ] = None ):
192
+ def update (self , new_state : Optional [int ] = None ) -> None :
193
193
super ().update (new_state )
194
194
if self .pressed :
195
195
self .last_change_ms = ticks_ms ()
0 commit comments