Skip to content

Commit 143df86

Browse files
committed
Don't scan the whole bus to verify existence of i2c device
Instead of sending an empty write request to every possible address on the but to see which devices respond, send it only to the one address that we are checking. This is not only much faster and more robust (there might be devices on the bus that respond badly to such requests at the wrong speed) but also makes it much easier to debug things with a logic analyzer, as it doesn't get flooded with all those requests.
1 parent 9c27f6d commit 143df86

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

adafruit_bus_device/i2c_device.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ def __init__(self, i2c, device_address):
5252
# Verify that a deivce with that address exists.
5353
while not i2c.try_lock():
5454
pass
55-
scan = i2c.scan()
56-
i2c.unlock()
57-
if device_address not in scan:
58-
raise ValueError("No i2c device at address: " + str(hex(device_address)))
55+
try:
56+
i2c.writeto(device_address, b'')
57+
except OSError:
58+
raise ValueError("No I2C device at address: %x" % device_address)
59+
finally:
60+
i2c.unlock()
5961

6062
self.i2c = i2c
6163
self.device_address = device_address

0 commit comments

Comments
 (0)