diff --git a/adafruit_tca9548a.py b/adafruit_tca9548a.py index c766176..0436166 100644 --- a/adafruit_tca9548a.py +++ b/adafruit_tca9548a.py @@ -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] diff --git a/examples/tca9547d_multisensor.py b/examples/tca9547d_multisensor.py new file mode 100644 index 0000000..824f33c --- /dev/null +++ b/examples/tca9547d_multisensor.py @@ -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) diff --git a/examples/tca9547d_simpletest.py b/examples/tca9547d_simpletest.py new file mode 100644 index 0000000..2cb3127 --- /dev/null +++ b/examples/tca9547d_simpletest.py @@ -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()