Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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
16 changes: 13 additions & 3 deletions adafruit_bus_device/i2c_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add the timeout parameter to the docstring above here?


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()

Expand Down Expand Up @@ -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()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion: use time.monotonic_ns() throughout as it'll avoid issues with floating point precision when the program runs for a long time.

while not self.i2c.try_lock():
if self.timeout > (time.monotonic() - lock_start_time):
raise Exception("'I2CDevice' lock timed out")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't raise bare Exceptions. It seems other Adafruit libraries use RuntimeError here. source.

else:
while not self.i2c.try_lock():
pass
return self

def __exit__(self, exc_type, exc_val, exc_tb):
Expand Down
15 changes: 12 additions & 3 deletions adafruit_bus_device/spi_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
`adafruit_bus_device.spi_device` - SPI Bus Device
====================================================
"""
import time

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BusDevice.git"
Expand Down Expand Up @@ -75,20 +76,28 @@ def __init__(
baudrate=100000,
polarity=0,
phase=0,
extra_clocks=0
extra_clocks=0,
timeout=0.25
):
self.spi = spi
self.baudrate = baudrate
self.polarity = polarity
self.phase = phase
self.extra_clocks = extra_clocks
self.chip_select = chip_select
self.timeout = timeout
if self.chip_select:
self.chip_select.switch_to_output(value=True)

def __enter__(self):
while not self.spi.try_lock():
pass
if self.timeout is not None:
lock_start_time = time.monotonic()
while not self.spi.try_lock():
if self.timeout > (time.monotonic() - lock_start_time):
raise Exception("'SPIDevice' lock timed out")
else:
while not self.spi.try_lock():
pass
self.spi.configure(
baudrate=self.baudrate, polarity=self.polarity, phase=self.phase
)
Expand Down