Skip to content

Commit c183479

Browse files
authored
Merge pull request #19 from dhalbert/discard-junk-input
Discard junk input bytes on creation
2 parents b9d5e8b + 327b2de commit c183479

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

adafruit_us100.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class US100:
3737

3838
def __init__(self, uart: UART) -> None:
3939
self._uart = uart
40+
# Some chips, such as ESP32-S3, may have junk input bytes immediately after UART creation.
41+
# Wait enough time for those to appear, and then clear the buffer.
42+
time.sleep(0.1)
43+
uart.reset_input_buffer()
4044

4145
@property
4246
def distance(self) -> Optional[float]:
@@ -52,6 +56,9 @@ def distance(self) -> Optional[float]:
5256
objects over 460 cm away.
5357
:return: Distance in centimeters.
5458
:rtype: float or None
59+
60+
May block for up to 400 msecs while attempting a read.
61+
Will always block for at least 100 msecs.
5562
"""
5663
for _ in range(2): # Attempt to read twice.
5764
self._uart.write(bytes([0x55]))
@@ -74,7 +81,11 @@ def distance(self) -> Optional[float]:
7481

7582
@property
7683
def temperature(self) -> Optional[int]:
77-
"""Return the on-chip temperature, in Celsius"""
84+
"""Return the on-chip temperature, in Celsius
85+
86+
May block for up to 200 msecs while attempting a read.
87+
Will always block for at least 100 msecs.
88+
"""
7889
for _ in range(2): # Attempt to read twice.
7990
self._uart.write(bytes([0x50]))
8091
time.sleep(0.1)

0 commit comments

Comments
 (0)