25
25
import time
26
26
import struct
27
27
28
+ try :
29
+ import typing # pylint: disable=unused-import
30
+ from typing_extensions import Literal
31
+ from circuitpython_typing import ReadableBuffer
32
+ from busio import UART
33
+ except ImportError :
34
+ pass
35
+
28
36
__version__ = "0.0.0+auto.0"
29
37
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TFmini.git"
30
38
@@ -46,7 +54,7 @@ class TFmini:
46
54
:param timeout: how long we'll wait for valid data or response, in seconds. Default is 1
47
55
"""
48
56
49
- def __init__ (self , uart , * , timeout = 1 ) :
57
+ def __init__ (self , uart : UART , * , timeout : float = 1 ) -> None :
50
58
self ._uart = uart
51
59
self ._uart .baudrate = 115200
52
60
self ._uart .reset_input_buffer ()
@@ -55,7 +63,7 @@ def __init__(self, uart, *, timeout=1):
55
63
self ._mode = None
56
64
57
65
@property
58
- def distance (self ):
66
+ def distance (self ) -> int :
59
67
"""The most recent distance measurement in centimeters"""
60
68
try :
61
69
self ._uart .reset_input_buffer ()
@@ -87,24 +95,24 @@ def distance(self):
87
95
raise RuntimeError ("Timed out looking for valid data" )
88
96
89
97
@property
90
- def strength (self ):
98
+ def strength (self ) -> int :
91
99
"""The signal validity, higher value means better measurement"""
92
100
_ = self .distance # trigger distance measurement
93
101
return self ._strength
94
102
95
103
@property
96
- def mode (self ):
104
+ def mode (self ) -> Literal [ 2 , 7 ] :
97
105
"""The measurement mode can be MODE_SHORT (2) or MODE_LONG (7)"""
98
106
_ = self .distance # trigger distance measurement
99
107
return self ._mode
100
108
101
109
@mode .setter
102
- def mode (self , newmode ) :
110
+ def mode (self , newmode : Literal [ 2 , 7 ]) -> None :
103
111
if not newmode in (MODE_LONG , MODE_SHORT ):
104
112
raise ValueError ("Invalid mode" )
105
113
self ._set_config (_CONFIGPARAM + bytes ([0 , 0 , newmode , 0x11 ]))
106
114
107
- def _set_config (self , command ) :
115
+ def _set_config (self , command : ReadableBuffer ) -> None :
108
116
"""Manager for sending commands, put sensor into config mode, config,
109
117
then exit configuration mode!"""
110
118
self ._uart .write (_STARTCONFIG )
0 commit comments