-
Couldn't load subscription status.
- Fork 77
Add timeouts to bus devices. #51
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
Changes from 8 commits
428b6eb
abbdfd5
7cfa623
263abef
c55c9e9
8e446a3
9a10024
465c5c1
b86d0ac
191901e
a2aafda
c749d8b
8bf07ac
0897638
49913e3
aae2263
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| `adafruit_bus_device.i2c_device` - I2C Bus Device | ||
| ==================================================== | ||
| """ | ||
| import time | ||
|
|
||
| __version__ = "0.0.0-auto.0" | ||
| __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BusDevice.git" | ||
|
|
@@ -59,12 +60,14 @@ class I2CDevice: | |
| device.write(bytes_read) | ||
| """ | ||
|
|
||
| def __init__(self, i2c, device_address, probe=True): | ||
| def __init__(self, i2c, device_address, probe=True, timeout=0.25): | ||
|
|
||
| self.i2c = i2c | ||
| self._has_write_read = hasattr(self.i2c, "writeto_then_readfrom") | ||
| self.device_address = device_address | ||
|
|
||
| self.timeout = timeout | ||
|
|
||
| if probe: | ||
| self.__probe_for_device() | ||
|
|
||
|
|
@@ -164,8 +167,15 @@ def write_then_readinto( | |
| # pylint: enable-msg=too-many-arguments | ||
|
|
||
| def __enter__(self): | ||
| while not self.i2c.try_lock(): | ||
| pass | ||
|
|
||
| if self.timeout is not None: | ||
| lock_start_time = time.monotonic() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: use |
||
| while not self.i2c.try_lock(): | ||
| if self.timeout > (time.monotonic() - lock_start_time): | ||
| raise Exception("'I2CDevice' lock timed out") | ||
|
||
| else: | ||
| while not self.i2c.try_lock(): | ||
| pass | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the
timeoutparameter to the docstring above here?