Skip to content

Commit 70235f4

Browse files
committed
add address validation to initialization
1 parent b1a232b commit 70235f4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

adafruit_tca9548a.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ class TCA9548A:
103103
"""Class which provides interface to TCA9548A I2C multiplexer."""
104104

105105
def __init__(self, i2c: I2C, address: int = _DEFAULT_ADDRESS) -> None:
106+
tries = 0
107+
while not i2c.try_lock():
108+
if (tries >= 200):
109+
raise ValueError("Unable to lock I2C bus.")
110+
tries += 1
111+
time.sleep(0)
112+
113+
if address not in i2c.scan():
114+
i2c.unlock()
115+
raise ValueError(f"No TCA9548A detected at {hex(address)}.")
116+
i2c.unlock()
117+
106118
self.i2c = i2c
107119
self.address = address
108120
self.channels = [None] * 8

0 commit comments

Comments
 (0)