Skip to content

Commit

Permalink
Merge pull request #19 from dhalbert/discard-junk-input
Browse files Browse the repository at this point in the history
Discard junk input bytes on creation
  • Loading branch information
dhalbert authored Feb 8, 2024
2 parents b9d5e8b + 327b2de commit c183479
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion adafruit_us100.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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]))
Expand All @@ -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)
Expand Down

0 comments on commit c183479

Please sign in to comment.