Skip to content

Commit b726198

Browse files
committed
Adding ability to skip device probing upon object init. Some hardware devices do not respond to these probes, or could be in reset when software object is created
1 parent cf7eabc commit b726198

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

adafruit_bus_device/i2c_device.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,26 @@ class I2CDevice:
5757
device.write(bytes_read)
5858
"""
5959

60-
def __init__(self, i2c, device_address):
60+
def __init__(self, i2c, device_address, probe=True):
6161
"""
6262
Try to read a byte from an address,
6363
if you get an OSError it means the device is not there
6464
"""
65-
while not i2c.try_lock():
66-
pass
67-
try:
68-
i2c.writeto(device_address, b'')
69-
except OSError:
70-
# some OS's dont like writing an empty bytesting...
71-
# Retry by reading a byte
65+
if probe:
66+
while not i2c.try_lock():
67+
pass
7268
try:
73-
result = bytearray(1)
74-
i2c.readfrom_into(device_address, result)
69+
i2c.writeto(device_address, b'')
7570
except OSError:
76-
raise ValueError("No I2C device at address: %x" % device_address)
77-
finally:
78-
i2c.unlock()
71+
# some OS's dont like writing an empty bytesting...
72+
# Retry by reading a byte
73+
try:
74+
result = bytearray(1)
75+
i2c.readfrom_into(device_address, result)
76+
except OSError:
77+
raise ValueError("No I2C device at address: %x" % device_address)
78+
finally:
79+
i2c.unlock()
7980

8081
self.i2c = i2c
8182
self.device_address = device_address

0 commit comments

Comments
 (0)