From 327b2de84b354572bde34a46898de736e04a861d Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 7 Feb 2024 19:28:50 -0500 Subject: [PATCH] Discard junk input bytes on creation. --- adafruit_us100.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/adafruit_us100.py b/adafruit_us100.py index 9a8adee..318f3f0 100644 --- a/adafruit_us100.py +++ b/adafruit_us100.py @@ -37,6 +37,10 @@ class US100: def __init__(self, uart: UART) -> None: self._uart = uart + # Some chips, such as ESP32-S3, may have junk input bytes immediately after UART creation. + # Wait enough time for those to appear, and then clear the buffer. + time.sleep(0.1) + uart.reset_input_buffer() @property def distance(self) -> Optional[float]: @@ -52,6 +56,9 @@ def distance(self) -> Optional[float]: objects over 460 cm away. :return: Distance in centimeters. :rtype: float or None + + May block for up to 400 msecs while attempting a read. + Will always block for at least 100 msecs. """ for _ in range(2): # Attempt to read twice. self._uart.write(bytes([0x55])) @@ -74,7 +81,11 @@ def distance(self) -> Optional[float]: @property def temperature(self) -> Optional[int]: - """Return the on-chip temperature, in Celsius""" + """Return the on-chip temperature, in Celsius + + May block for up to 200 msecs while attempting a read. + Will always block for at least 100 msecs. + """ for _ in range(2): # Attempt to read twice. self._uart.write(bytes([0x50])) time.sleep(0.1)