27
27
28
28
try :
29
29
import typing # pylint: disable=unused-import
30
+ from typing_extensions import Literal
31
+ from circuitpython_typing import ReadableBuffer
30
32
from busio import UART
31
33
except ImportError :
32
34
pass
@@ -52,7 +54,7 @@ class TFmini:
52
54
:param timeout: how long we'll wait for valid data or response, in seconds. Default is 1
53
55
"""
54
56
55
- def __init__ (self , uart : UART , * , timeout : int = 1 ) -> None :
57
+ def __init__ (self , uart : UART , * , timeout : float = 1 ) -> None :
56
58
self ._uart = uart
57
59
self ._uart .baudrate = 115200
58
60
self ._uart .reset_input_buffer ()
@@ -61,7 +63,7 @@ def __init__(self, uart: UART, *, timeout: int = 1) -> None:
61
63
self ._mode = None
62
64
63
65
@property
64
- def distance (self ) -> None :
66
+ def distance (self ) -> int :
65
67
"""The most recent distance measurement in centimeters"""
66
68
try :
67
69
self ._uart .reset_input_buffer ()
@@ -99,18 +101,18 @@ def strength(self) -> int:
99
101
return self ._strength
100
102
101
103
@property
102
- def mode (self ) -> int :
104
+ def mode (self ) -> Literal [ 2 , 7 ] :
103
105
"""The measurement mode can be MODE_SHORT (2) or MODE_LONG (7)"""
104
106
_ = self .distance # trigger distance measurement
105
107
return self ._mode
106
108
107
109
@mode .setter
108
- def mode (self , newmode : int ) -> None :
110
+ def mode (self , newmode : Literal [ MODE_LONG , MODE_SHORT ] ) -> None :
109
111
if not newmode in (MODE_LONG , MODE_SHORT ):
110
112
raise ValueError ("Invalid mode" )
111
113
self ._set_config (_CONFIGPARAM + bytes ([0 , 0 , newmode , 0x11 ]))
112
114
113
- def _set_config (self , command : int ) -> None :
115
+ def _set_config (self , command : ReadableBuffer ) -> None :
114
116
"""Manager for sending commands, put sensor into config mode, config,
115
117
then exit configuration mode!"""
116
118
self ._uart .write (_STARTCONFIG )
0 commit comments