From 327b2de84b354572bde34a46898de736e04a861d Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 7 Feb 2024 19:28:50 -0500 Subject: [PATCH 1/4] 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) From 7dbfad496eb22dc1e8ccf98147255dc76a2f7773 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 8 Feb 2024 10:27:08 +0100 Subject: [PATCH 2/4] add units, use f-string --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 8cd0bdc..33eb4ef 100644 --- a/README.rst +++ b/README.rst @@ -71,8 +71,8 @@ Usage Example while True: print("-----") - print("Temperature: ", us100.temperature) - print("Distance: ", us100.distance) + print(f"Temperature: {us100.temperature} cm") + print(f"Distance: {us100.distance}°C") time.sleep(0.5) From 58291e1a89d870405455c41855ccfeef939d7376 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 8 Feb 2024 10:27:34 +0100 Subject: [PATCH 3/4] fix units --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 33eb4ef..bc05aa1 100644 --- a/README.rst +++ b/README.rst @@ -71,8 +71,8 @@ Usage Example while True: print("-----") - print(f"Temperature: {us100.temperature} cm") - print(f"Distance: {us100.distance}°C") + print(f"Temperature: {us100.temperature}°C") + print(f"Distance: {us100.distance} cm") time.sleep(0.5) From 4bf6e13afa725ca8e2d4a94e62ef47f5fd167117 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 8 Feb 2024 10:28:17 +0100 Subject: [PATCH 4/4] add units, use f-string --- examples/us100_simpletest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/us100_simpletest.py b/examples/us100_simpletest.py index fe41d3a..cd85b47 100644 --- a/examples/us100_simpletest.py +++ b/examples/us100_simpletest.py @@ -24,7 +24,7 @@ while True: print("-----") - print("Temperature: ", us100.temperature) + print(f"Temperature: {us100.temperature}°C") time.sleep(0.5) - print("Distance: ", us100.distance) + print(f"Distance: {us100.distance} cm") time.sleep(0.5)