Skip to content

Commit 4350de8

Browse files
committed
Add support for PCA9547D devices
1 parent 0c8bf95 commit 4350de8

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

adafruit_tca9548a.py

+23
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,26 @@ def __getitem__(self, key: Literal[0, 1, 2, 3]) -> "TCA9548A_Channel":
135135
if self.channels[key] is None:
136136
self.channels[key] = TCA9548A_Channel(self, key)
137137
return self.channels[key]
138+
139+
class TCA9547D_Channel(TCA9548A_Channel):
140+
"""Helper class to represent an output channel on the TCA9547D and take care
141+
of the necessary I2C commands for channel switching. This class needs to
142+
behave like an I2CDevice."""
143+
144+
def __init__(self, tca: "TCA9547D", channel: int) -> None:
145+
self.tca = tca
146+
"""
147+
B3 enables/disables the mux. B2-B0 control which channel is used.
148+
ref: https://www.nxp.com/docs/en/data-sheet/PCA9547.pdf
149+
"""
150+
self.channel_switch = (channel + (1<<3)).to_bytes(1, "little")
151+
152+
class TCA9547D(TCA9548A):
153+
"""Class which provides interface to TCA9547D I2C multiplexer."""
154+
155+
def __getitem__(self, key: Literal[0, 1, 2, 3, 4, 5, 6, 7]) -> "TCA9548A_Channel":
156+
if not 0 <= key <= 7:
157+
raise IndexError("Channel must be an integer in the range: 0-7.")
158+
if self.channels[key] is None:
159+
self.channels[key] = TCA9547D_Channel(self, key)
160+
return self.channels[key]

examples/tca9547d_multisensor.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# This example shows using two TSL2491 light sensors attached to TCA9547D channels 0 and 1.
5+
# Use with other I2C sensors would be similar.
6+
import time
7+
import board
8+
import adafruit_tsl2591
9+
import adafruit_tca9548a
10+
11+
# Create I2C bus as normal
12+
i2c = board.I2C() # uses board.SCL and board.SDA
13+
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
14+
15+
# Create the TCA9548A object and give it the I2C bus
16+
tca = adafruit_tca9548a.TCA9547D(i2c)
17+
18+
# For each sensor, create it using the TCA9548A channel instead of the I2C object
19+
tsl1 = adafruit_tsl2591.TSL2591(tca[0])
20+
tsl2 = adafruit_tsl2591.TSL2591(tca[1])
21+
22+
# After initial setup, can just use sensors as normal.
23+
while True:
24+
print(tsl1.lux, tsl2.lux)
25+
time.sleep(0.1)

examples/tca9547d_simpletest.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# This example shows using TCA9547D to perform a simple scan for connected devices
5+
import board
6+
import adafruit_tca9548a
7+
8+
# Create I2C bus as normal
9+
i2c = board.I2C() # uses board.SCL and board.SDA
10+
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
11+
12+
# Create the TCA95487D object and give it the I2C bus
13+
tca = adafruit_tca9548a.TCA9547D(i2c)
14+
15+
for channel in range(8):
16+
if tca[channel].try_lock():
17+
print("Channel {}:".format(channel), end="")
18+
addresses = tca[channel].scan()
19+
print([hex(address) for address in addresses if address != 0x70])
20+
tca[channel].unlock()

0 commit comments

Comments
 (0)