Skip to content

Discard junk input bytes on creation #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Comment on lines +42 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have 2 concerns about this:

  • if this is ESP32-S3 specific, this will unnecessarily delay other HW
  • if the issue is specific to UART (on ESP32-S3) rather than US-100, the fix should probably reside in CP itself

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The delay only happens once, in the constructor. I could probably make this 2 msecs instead of 100 msecs. I can test that.

Yes, ESP32-S3 should be fixed. I am looking at that. But this makes the library more robust for any case like this where it might get out of sync. And it makes the library usable without having to upgrade CircuitPython.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair argument, esp. the second part.


@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