Skip to content

Add support for PCA9547D devices #56

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions adafruit_tca9548a.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,29 @@ def __getitem__(self, key: Literal[0, 1, 2, 3]) -> "TCA9548A_Channel":
if self.channels[key] is None:
self.channels[key] = TCA9548A_Channel(self, key)
return self.channels[key]


class TCA9547D_Channel(TCA9548A_Channel):
"""Helper class to represent an output channel on the TCA9547D and take care
of the necessary I2C commands for channel switching. This class needs to
behave like an I2CDevice."""

def __init__(self, tca: "TCA9547D", channel: int) -> None:
super().__init__(tca, channel)
self.tca = tca
"""
B3 enables/disables the mux. B2-B0 control which channel is used.
ref: https://www.nxp.com/docs/en/data-sheet/PCA9547.pdf
"""
self.channel_switch = (channel + (1 << 3)).to_bytes(1, "little")


class TCA9547D(TCA9548A):
"""Class which provides interface to TCA9547D I2C multiplexer."""

def __getitem__(self, key: Literal[0, 1, 2, 3, 4, 5, 6, 7]) -> "TCA9548A_Channel":
if not 0 <= key <= 7:
raise IndexError("Channel must be an integer in the range: 0-7.")
if self.channels[key] is None:
self.channels[key] = TCA9547D_Channel(self, key)
return self.channels[key]
25 changes: 25 additions & 0 deletions examples/tca9547d_multisensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# This example shows using two TSL2491 light sensors attached to TCA9547D channels 0 and 1.
# Use with other I2C sensors would be similar.
import time
import board
import adafruit_tsl2591
import adafruit_tca9548a

# Create I2C bus as normal
i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller

# Create the TCA9548A object and give it the I2C bus
tca = adafruit_tca9548a.TCA9547D(i2c)

# For each sensor, create it using the TCA9548A channel instead of the I2C object
tsl1 = adafruit_tsl2591.TSL2591(tca[0])
tsl2 = adafruit_tsl2591.TSL2591(tca[1])

# After initial setup, can just use sensors as normal.
while True:
print(tsl1.lux, tsl2.lux)
time.sleep(0.1)
20 changes: 20 additions & 0 deletions examples/tca9547d_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
# SPDX-License-Identifier: MIT

# This example shows using TCA9547D to perform a simple scan for connected devices
import board
import adafruit_tca9548a

# Create I2C bus as normal
i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller

# Create the TCA95487D object and give it the I2C bus
tca = adafruit_tca9548a.TCA9547D(i2c)

for channel in range(8):
if tca[channel].try_lock():
print("Channel {}:".format(channel), end="")
addresses = tca[channel].scan()
print([hex(address) for address in addresses if address != 0x70])
tca[channel].unlock()
Loading