Skip to content

Commit 98abd08

Browse files
authored
Merge pull request #43 from adafruit/pca9546a_update
Adding a PCA9546A class and examples
2 parents a3acfb3 + 893d14d commit 98abd08

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

adafruit_tca9548a.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,22 @@ def __getitem__(self, key: Literal[0, 1, 2, 3, 4, 5, 6, 7]) -> "TCA9548A_Channel
116116
if self.channels[key] is None:
117117
self.channels[key] = TCA9548A_Channel(self, key)
118118
return self.channels[key]
119+
120+
121+
class PCA9546A:
122+
"""Class which provides interface to TCA9546A I2C multiplexer."""
123+
124+
def __init__(self, i2c: I2C, address: int = _DEFAULT_ADDRESS) -> None:
125+
self.i2c = i2c
126+
self.address = address
127+
self.channels = [None] * 4
128+
129+
def __len__(self) -> Literal[4]:
130+
return 4
131+
132+
def __getitem__(self, key: Literal[0, 1, 2, 3]) -> "TCA9548A_Channel":
133+
if not 0 <= key <= 3:
134+
raise IndexError("Channel must be an integer in the range: 0-3.")
135+
if self.channels[key] is None:
136+
self.channels[key] = TCA9548A_Channel(self, key)
137+
return self.channels[key]

examples/pca9546a_multisensor.py

Lines changed: 25 additions & 0 deletions
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 PCA9546A 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 PCA9546A object and give it the I2C bus
16+
mux = adafruit_tca9548a.PCA9546A(i2c)
17+
18+
# For each sensor, create it using the PCA9546A channel instead of the I2C object
19+
tsl1 = adafruit_tsl2591.TSL2591(mux[0])
20+
tsl2 = adafruit_tsl2591.TSL2591(mux[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/pca9546a_simpletest.py

Lines changed: 20 additions & 0 deletions
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 TCA9548A 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 PCA9546A object and give it the I2C bus
13+
mux = adafruit_tca9548a.PCA9546A(i2c)
14+
15+
for channel in range(4):
16+
if mux[channel].try_lock():
17+
print("Channel {}:".format(channel), end="")
18+
addresses = mux[channel].scan()
19+
print([hex(address) for address in addresses if address != 0x70])
20+
mux[channel].unlock()

0 commit comments

Comments
 (0)