2525import time
2626import struct
2727
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+
2836__version__ = "0.0.0+auto.0"
2937__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TFmini.git"
3038
@@ -46,7 +54,7 @@ class TFmini:
4654 :param timeout: how long we'll wait for valid data or response, in seconds. Default is 1
4755 """
4856
49- def __init__ (self , uart , * , timeout = 1 ) :
57+ def __init__ (self , uart : UART , * , timeout : float = 1 ) -> None :
5058 self ._uart = uart
5159 self ._uart .baudrate = 115200
5260 self ._uart .reset_input_buffer ()
@@ -55,7 +63,7 @@ def __init__(self, uart, *, timeout=1):
5563 self ._mode = None
5664
5765 @property
58- def distance (self ):
66+ def distance (self ) -> int :
5967 """The most recent distance measurement in centimeters"""
6068 try :
6169 self ._uart .reset_input_buffer ()
@@ -87,24 +95,24 @@ def distance(self):
8795 raise RuntimeError ("Timed out looking for valid data" )
8896
8997 @property
90- def strength (self ):
98+ def strength (self ) -> int :
9199 """The signal validity, higher value means better measurement"""
92100 _ = self .distance # trigger distance measurement
93101 return self ._strength
94102
95103 @property
96- def mode (self ):
104+ def mode (self ) -> Literal [ 2 , 7 ] :
97105 """The measurement mode can be MODE_SHORT (2) or MODE_LONG (7)"""
98106 _ = self .distance # trigger distance measurement
99107 return self ._mode
100108
101109 @mode .setter
102- def mode (self , newmode ) :
110+ def mode (self , newmode : Literal [ 2 , 7 ]) -> None :
103111 if not newmode in (MODE_LONG , MODE_SHORT ):
104112 raise ValueError ("Invalid mode" )
105113 self ._set_config (_CONFIGPARAM + bytes ([0 , 0 , newmode , 0x11 ]))
106114
107- def _set_config (self , command ) :
115+ def _set_config (self , command : ReadableBuffer ) -> None :
108116 """Manager for sending commands, put sensor into config mode, config,
109117 then exit configuration mode!"""
110118 self ._uart .write (_STARTCONFIG )
0 commit comments