Skip to content

solve issues #69 I2C init bug Jetson #70

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

Closed
wants to merge 3 commits into from
Closed
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
36 changes: 32 additions & 4 deletions adafruit_bus_device/i2c_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,21 @@ class I2CDevice:
device.write(bytes_read)
"""

def __init__(self, i2c, device_address, probe=True):

def __init__(self, i2c, device_address, probe=True, probe_with_write=True):
"""
If probe is true you will probing the device.
With probe_with_write you can switch between:
True - a probing with write (& read if OSError)
False - only probing with read
"""
self.i2c = i2c
self.device_address = device_address

if probe:
self.__probe_for_device()
if probe_with_write:
self.__write_probe_for_device()
else:
self.__only_read_probe_for_device()

def readinto(self, buf, *, start=0, end=None):
"""
Expand Down Expand Up @@ -142,8 +150,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.i2c.unlock()
return False

def __probe_for_device(self):
def __write_probe_for_device(self):
"""
Try to write empty byte string to an address.
If OSError:
Try to read a byte from an address,
if you get an OSError it means the device is not there
or that the device does not support these means of probing
Expand All @@ -164,3 +174,21 @@ def __probe_for_device(self):
# pylint: enable=raise-missing-from
finally:
self.i2c.unlock()

def __only_read_probe_for_device(self):
"""
Try to read a byte from an address,
if you get an OSError it means the device is not there
or that the device does not support these means of probing
"""
while not self.i2c.try_lock():
pass
try:
result = bytearray(1)
self.i2c.readfrom_into(self.device_address, result)
except OSError:
# pylint: disable=raise-missing-from
raise ValueError("No I2C device at address: 0x%x" % self.device_address)
# pylint: enable=raise-missing-from
finally:
self.i2c.unlock()