Skip to content

Commit a0782ec

Browse files
add examples that mirror the arduino library. ex. 2 is unverified on hardware
1 parent 131ea88 commit a0782ec

File tree

4 files changed

+226
-2
lines changed

4 files changed

+226
-2
lines changed

examples/qwiic_tca9548a_ex1.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
#-----------------------------------------------------------------------------
3+
# qwiic_tca9548a_ex1.py
4+
#
5+
# Simple example for the Qwiic 8 Channel Mux
6+
#------------------------------------------------------------------------
7+
#
8+
# Written by SparkFun Electronics, November 2024
9+
#
10+
# This python library supports the SparkFun Electroncis qwiic
11+
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
12+
# board computers.
13+
#
14+
# More information on qwiic is at https://www.sparkfun.com/qwiic
15+
#
16+
# Do you like this library? Help support SparkFun. Buy a board!
17+
#
18+
#==================================================================================
19+
# Copyright (c) 2024 SparkFun Electronics
20+
#
21+
# Permission is hereby granted, free of charge, to any person obtaining a copy
22+
# of this software and associated documentation files (the "Software"), to deal
23+
# in the Software without restriction, including without limitation the rights
24+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25+
# copies of the Software, and to permit persons to whom the Software is
26+
# furnished to do so, subject to the following conditions:
27+
#
28+
# The above copyright notice and this permission notice shall be included in all
29+
# copies or substantial portions of the Software.
30+
#
31+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
# SOFTWARE.
38+
#==================================================================================
39+
# Description:
40+
# Some I2C devices respond to only one I2C address. This can be a problem
41+
# when you want to hook multiple of a device to the I2C bus. An I2C Mux
42+
# solves this issue by allowing you to change the 'channel' or port that
43+
# the master is talking to.
44+
45+
# This example shows how to connect to different ports.
46+
# First, any devices connected to port 0 and 1 will have their i2c addresses printed, then any devices connected to ports 2 and 3
47+
# Make sure you do not have multiple devices with the same i2c address connected to BOTH ports 0 and 1, or 2 and 3
48+
#==================================================================================
49+
# Hardware Connections:
50+
# Attach the Qwiic Mux Board to your control board with a Qwiic cable to the "Main" port on the Mux Board
51+
# Plug device(s) into ports 0, 1, 2 and/or 3
52+
#==================================================================================
53+
54+
import qwiic_tca9548a
55+
import qwiic_i2c
56+
import time
57+
import sys
58+
59+
def runExample():
60+
61+
print("\nSparkFun TCA9548A 8-Channel Mux Example 1\n")
62+
63+
myTca = qwiic_tca9548a.QwiicTCA9548A()
64+
65+
if myTca.connected == False:
66+
print("The Qwiic TCA9548A 8-Channel Mux device isn't connected to the system. Please check your connection", \
67+
file=sys.stderr)
68+
return
69+
70+
# We will use this object to see what devices are connected to the master after configuring the MUX
71+
i2c = qwiic_i2c.getI2CDriver()
72+
73+
while True:
74+
# By enabling channels 0 and 1, our Master device can speak to a device connected to port 0 or 1of the MUX
75+
print("Enabling channels 0 and 1")
76+
myTca.disable_all()
77+
myTca.enable_channels([0,1])
78+
myTca.list_channels()
79+
80+
# Find any i2c devices connected to the master, we should see the addresses of any devices we have connected to port 0 or 1 here,
81+
# but not any devices connected to other ports.
82+
print("Checking for i2c devices on ports 0 and 1")
83+
devices = i2c.scan()
84+
print("Devices found: ", devices)
85+
time.sleep(2)
86+
87+
# Enable channels 2 and 3, our Master device can now speak to a device connected to port 2 or 3 of the MUX
88+
print("Enabling channels 2 and 3")
89+
myTca.disable_all()
90+
myTca.enable_channels([2,3])
91+
myTca.list_channels()
92+
93+
# Find any i2c devices connected to the master, we should see the addresses of any devices we have connected to port 2 or 3 here,
94+
# but not any devices connected to other ports.
95+
print("Checking for i2c devices on ports 2 and 3")
96+
devices = i2c.scan()
97+
print("Devices found: ", devices)
98+
time.sleep(2)
99+
100+
if __name__ == '__main__':
101+
try:
102+
runExample()
103+
except (KeyboardInterrupt, SystemExit) as exErr:
104+
print("\nEnding Example 1")
105+
sys.exit(0)

examples/qwiic_tca9548a_ex2.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env python3
2+
#-----------------------------------------------------------------------------
3+
# qwiic_tca9548a_ex2.py
4+
#
5+
# Example for the Qwiic 8 Channel Mux interfacing with two Qwiic VL53L1X Distance Sensors
6+
#------------------------------------------------------------------------
7+
#
8+
# Written by SparkFun Electronics, November 2024
9+
#
10+
# This python library supports the SparkFun Electroncis qwiic
11+
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
12+
# board computers.
13+
#
14+
# More information on qwiic is at https://www.sparkfun.com/qwiic
15+
#
16+
# Do you like this library? Help support SparkFun. Buy a board!
17+
#
18+
#==================================================================================
19+
# Copyright (c) 2024 SparkFun Electronics
20+
#
21+
# Permission is hereby granted, free of charge, to any person obtaining a copy
22+
# of this software and associated documentation files (the "Software"), to deal
23+
# in the Software without restriction, including without limitation the rights
24+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25+
# copies of the Software, and to permit persons to whom the Software is
26+
# furnished to do so, subject to the following conditions:
27+
#
28+
# The above copyright notice and this permission notice shall be included in all
29+
# copies or substantial portions of the Software.
30+
#
31+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
# SOFTWARE.
38+
#==================================================================================
39+
# Description:
40+
# This example shows how to hook up two VL53L1X laser distance sensors with the same address.
41+
# You can read the VL53L1X hookup guide and get the library from https://learn.sparkfun.com/tutorials/qwiic-distance-sensor-vl53l1x-hookup-guide
42+
#==================================================================================
43+
# Hardware Connections:
44+
# Attach the Qwiic Mux Board to your control board with a Qwiic cable to the "Main" port on the Mux Board
45+
# Plug a VL53L1X device into port 0 and another into port 1
46+
#==================================================================================
47+
48+
import qwiic_tca9548a
49+
import qwiic_vl53l1x # Get the library here: https://github.com/sparkfun/Qwiic_VL53L1X_Py/tree/master or with mip
50+
import qwiic_i2c
51+
import time
52+
import sys
53+
54+
NUMBER_OF_SENSORS = 2 # Change this number if you have more than 2 VL53L1X sensors. Connect all sensors to lowest available ports on MUX
55+
56+
def runExample():
57+
print("\nSparkFun TCA9548A 8-Channel Mux Example 2\n")
58+
59+
# Create an instance of our MUX object
60+
myTca = qwiic_tca9548a.QwiicTCA9548A()
61+
62+
if myTca.connected == False:
63+
print("The Qwiic TCA9548A 8-Channel Mux device isn't connected to the system. Please check your connection", \
64+
file=sys.stderr)
65+
return
66+
67+
# Create a list of distance sensor objects
68+
distance_sensors = [None] * NUMBER_OF_SENSORS
69+
70+
for i in range(NUMBER_OF_SENSORS):
71+
distance_sensors[i] = qwiic_vl53l1x.QwiicVL53L1X()
72+
73+
# We will use this object to see what devices are connected to the master after configuring the MUX
74+
i2c = qwiic_i2c.getI2CDriver()
75+
76+
print("Disabling all MUX Ports")
77+
myTca.disable_all()
78+
myTca.list_channels()
79+
80+
# Now configure each sensor one at a time
81+
for i in range(NUMBER_OF_SENSORS):
82+
print("Enabling sensor on channel", i)
83+
myTca.disable_all()
84+
myTca.enable_channels([i])
85+
86+
# If a device is connected, initialize it
87+
if distance_sensors[i].sensor_init() == None:
88+
print("Sensor", i, " connected!")
89+
else:
90+
print("Sensor", i, " not connected! Exiting Program. Please connect sensor", i, "and restart program.")
91+
sys.exit(0)
92+
93+
while True:
94+
# Loop through each sensor and enable its MUX channel then read the distance
95+
for i in range(NUMBER_OF_SENSORS):
96+
# Enable the MUX channel for the sensor we want to read
97+
myTca.disable_all()
98+
myTca.enable_channels([i])
99+
100+
# Read distance from the sensor
101+
distance_sensors[i].start_ranging()
102+
time.sleep(0.005)
103+
distance = distance_sensors[i].get_distance()
104+
time.sleep(0.005)
105+
distance_sensors[i].stop_ranging()
106+
107+
distanceInches = distance / 25.4
108+
distanceFeet = distanceInches / 12.0
109+
110+
print("Distance", i, "(in): ", distanceInches, " Distance", i, "(ft): ", distanceFeet)
111+
112+
time.sleep(0.5)
113+
114+
if __name__ == '__main__':
115+
try:
116+
runExample()
117+
except (KeyboardInterrupt, SystemExit) as exErr:
118+
print("\nEnding Example 2")
119+
sys.exit(0)

examples/ex2_disable_channels.py renamed to examples/simple_disable_channels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#-----------------------------------------------------------------------
2-
# Qwiic Mux - Example 2
2+
# Qwiic Mux - Simple Disable Channels Example
33
#-----------------------------------------------------------------------
44
#
55
# Written by SparkFun Electronics, June 2019

examples/ex1_enable_channels.py renamed to examples/simple_enable_channels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#-----------------------------------------------------------------------
2-
# Qwiic Mux - Example 1
2+
# Qwiic Mux - Simple Enable Channels Example
33
#-----------------------------------------------------------------------
44
#
55
# Written by SparkFun Electronics, June 2019

0 commit comments

Comments
 (0)