Skip to content

Commit 783a2c5

Browse files
committed
adding_example_improving_docs
1 parent e79e116 commit 783a2c5

File tree

10 files changed

+78
-45
lines changed

10 files changed

+78
-45
lines changed

README.rst

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Introduction
1313
:target: https://github.com/adafruit/Adafruit_CircuitPython_TCA9548A/actions/
1414
:alt: Build Status
1515

16+
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
17+
:target: https://github.com/psf/black
18+
:alt: Code Style: Black
19+
20+
1621
CircuitPython driver for the TCA9548A I2C Multiplexer.
1722

1823
Dependencies
@@ -56,28 +61,22 @@ Usage Example
5661

5762
.. code-block :: python
5863
59-
# This example shows using two TSL2491 light sensors attached to TCA9548A channels 0 and 1.
60-
# Use with other I2C sensors would be similar.
61-
import time
64+
# This example shows using sensors attached to TCA9548A channels 1 and 5.
6265
import board
63-
import busio
64-
import adafruit_tsl2591
6566
import adafruit_tca9548a
6667
6768
# Create I2C bus as normal
68-
i2c = busio.I2C(board.SCL, board.SDA)
69+
i2c = board.I2C() # uses board.SCL and board.SDA
6970
7071
# Create the TCA9548A object and give it the I2C bus
7172
tca = adafruit_tca9548a.TCA9548A(i2c)
7273
73-
# For each sensor, create it using the TCA9548A channel instead of the I2C object
74-
tsl1 = adafruit_tsl2591.TSL2591(tca[0])
75-
tsl2 = adafruit_tsl2591.TSL2591(tca[1])
76-
77-
# Loop and profit!
78-
while True:
79-
print(tsl1.lux, tsl2.lux)
80-
time.sleep(0.1)
74+
for channel in range(8):
75+
if tca[channel].try_lock():
76+
print("Channel {}:".format(channel), end="")
77+
addresses = tca[channel].scan()
78+
print([hex(address) for address in addresses if address != 0x70])
79+
tca[channel].unlock()
8180
8281
8382
Contributing

adafruit_tca9548a.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: MIT
44

55
"""
6-
``Adafruit_TCA9548A``
6+
`adafruit_tca9548a`
77
====================================================
88
99
CircuitPython driver for the TCA9548A I2C Multiplexer.
@@ -15,16 +15,20 @@
1515
1616
**Hardware:**
1717
18-
* TCA9548A I2C Multiplexer: https://www.adafruit.com/product/2717
18+
* `TCA9548A I2C Multiplexer
19+
<https://www.adafruit.com/product/2717>`_ (Product ID: 2717)
20+
1921
2022
**Software and Dependencies:**
2123
2224
* Adafruit CircuitPython firmware for the supported boards:
23-
https://github.com/adafruit/circuitpython/releases
24-
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
25+
https://circuitpython.org/downloads
26+
27+
* Adafruit's Bus Device library:
28+
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
29+
2530
"""
2631

27-
# imports
2832
from micropython import const
2933

3034
_DEFAULT_ADDRESS = const(0x70)
@@ -43,31 +47,31 @@ def __init__(self, tca, channel):
4347
self.channel_switch = bytearray([1 << channel])
4448

4549
def try_lock(self):
46-
"""Pass thru for try_lock."""
50+
"""Pass through for try_lock."""
4751
while not self.tca.i2c.try_lock():
4852
pass
4953
self.tca.i2c.writeto(self.tca.address, self.channel_switch)
5054
return True
5155

5256
def unlock(self):
53-
"""Pass thru for unlock."""
57+
"""Pass through for unlock."""
5458
self.tca.i2c.writeto(self.tca.address, b"\x00")
5559
return self.tca.i2c.unlock()
5660

5761
def readfrom_into(self, address, buffer, **kwargs):
58-
"""Pass thru for readfrom_into."""
62+
"""Pass through for readfrom_into."""
5963
if address == self.tca.address:
6064
raise ValueError("Device address must be different than TCA9548A address.")
6165
return self.tca.i2c.readfrom_into(address, buffer, **kwargs)
6266

6367
def writeto(self, address, buffer, **kwargs):
64-
"""Pass thru for writeto."""
68+
"""Pass through for writeto."""
6569
if address == self.tca.address:
6670
raise ValueError("Device address must be different than TCA9548A address.")
6771
return self.tca.i2c.writeto(address, buffer, **kwargs)
6872

6973
def writeto_then_readfrom(self, address, buffer_out, buffer_in, **kwargs):
70-
"""Pass thru for writeto_then_readfrom."""
74+
"""Pass through for writeto_then_readfrom."""
7175
# In linux, at least, this is a special kernel function call
7276
if address == self.tca.address:
7377
raise ValueError("Device address must be different than TCA9548A address.")
@@ -93,7 +97,7 @@ def __len__(self):
9397

9498
def __getitem__(self, key):
9599
if not 0 <= key <= 7:
96-
raise IndexError("Channel must be an integer in the range: 0-7")
100+
raise IndexError("Channel must be an integer in the range: 0-7.")
97101
if self.channels[key] is None:
98102
self.channels[key] = TCA9548A_Channel(self, key)
99103
return self.channels[key]

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
77
.. automodule:: adafruit_tca9548a
88
:members:
9+
:member-order: bysource

docs/conf.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
"https://circuitpython.readthedocs.io/projects/busdevice/en/latest/",
3535
None,
3636
),
37-
"Register": (
38-
"https://circuitpython.readthedocs.io/projects/register/en/latest/",
39-
None,
40-
),
4137
"CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None),
4238
}
4339

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/tca9548a_simpletest.py
77
:caption: examples/tca9548a_simpletest.py
88
:linenos:
9+
10+
Multisensor test
11+
----------------
12+
13+
Shows how to use the I2C Multiplexer with two sensors
14+
15+
.. literalinclude:: ../examples/tca9548a_multisensor.py
16+
:caption: examples/tca9548a_multisensor.py
17+
:linenos:

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Table of Contents
2323
.. toctree::
2424
:caption: Tutorials
2525

26+
TCA9548A I2C Multiplexer Learning Guide <https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/overview>
27+
2628
.. toctree::
2729
:caption: Related Products
2830

examples/tca9548a_multisensor.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 TCA9548A 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+
14+
# Create the TCA9548A object and give it the I2C bus
15+
tca = adafruit_tca9548a.TCA9548A(i2c)
16+
17+
# For each sensor, create it using the TCA9548A channel instead of the I2C object
18+
tsl1 = adafruit_tsl2591.TSL2591(tca[0])
19+
tsl2 = adafruit_tsl2591.TSL2591(tca[1])
20+
21+
# After initial setup, can just use sensors as normal.
22+
while True:
23+
print(tsl1.lux, tsl2.lux)
24+
time.sleep(0.1)

examples/tca9548a_simpletest.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
# This example shows using two TSL2491 light sensors attached to TCA9548A channels 0 and 1.
5-
# Use with other I2C sensors would be similar.
6-
import time
4+
# This example shows using sensors attached to TCA9548A channels 1 and 5.
75
import board
8-
import busio
9-
import adafruit_tsl2591
106
import adafruit_tca9548a
117

128
# Create I2C bus as normal
13-
i2c = busio.I2C(board.SCL, board.SDA)
9+
i2c = board.I2C() # uses board.SCL and board.SDA
1410

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

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)
14+
for channel in range(8):
15+
if tca[channel].try_lock():
16+
print("Channel {}:".format(channel), end="")
17+
addresses = tca[channel].scan()
18+
print([hex(address) for address in addresses if address != 0x70])
19+
tca[channel].unlock()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
# SPDX-License-Identifier: Unlicense
44

55
Adafruit-Blinka
6+
adafruit-circuitpython-busdevice

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
# Author details
3535
author="Adafruit Industries",
3636
author_email="[email protected]",
37-
install_requires=["Adafruit-Blinka"],
37+
install_requires=[
38+
"Adafruit-Blinka",
39+
"adafruit-circuitpython-busdevice",
40+
],
3841
# Choose your license
3942
license="MIT",
4043
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)