File tree Expand file tree Collapse file tree 1 file changed +10
-4
lines changed Expand file tree Collapse file tree 1 file changed +10
-4
lines changed Original file line number Diff line number Diff line change 25
25
26
26
import time
27
27
28
+ try :
29
+ from typing import Optional
30
+ from busio import UART
31
+ except ImportError :
32
+ pass
33
+
28
34
29
35
class US100 :
30
36
"""Control a US-100 ultrasonic range sensor."""
31
37
32
- def __init__ (self , uart ) :
38
+ def __init__ (self , uart : UART ) -> None :
33
39
self ._uart = uart
34
40
35
41
@property
36
- def distance (self ):
42
+ def distance (self ) -> Optional [ float ] :
37
43
"""Return the distance measured by the sensor in cm.
38
44
This is the function that will be called most often in user code.
39
45
If no signal is received, return ``None``. This can happen when the
@@ -45,7 +51,7 @@ def distance(self):
45
51
for the sensor to handle. In my experience, the sensor can not detect
46
52
objects over 460 cm away.
47
53
:return: Distance in centimeters.
48
- :rtype: float
54
+ :rtype: float or None
49
55
"""
50
56
for _ in range (2 ): # Attempt to read twice.
51
57
self ._uart .write (bytes ([0x55 ]))
@@ -67,7 +73,7 @@ def distance(self):
67
73
return dist
68
74
69
75
@property
70
- def temperature (self ):
76
+ def temperature (self ) -> Optional [ int ] :
71
77
"""Return the on-chip temperature, in Celsius"""
72
78
for _ in range (2 ): # Attempt to read twice.
73
79
self ._uart .write (bytes ([0x50 ]))
You can’t perform that action at this time.
0 commit comments