I'm wrapping up the firmware for my project using 4 TCA9548As to switch between 32 different ports for I2C slave devices. I have a raspberry pi and clamp on master SDA and SCL lines for master input.
I am using the adafruit TCA9548A library for my scan, read from, and write to functions (I'll attach a picture below). I am new to python coding, this project has been how I have learned and my first python coding project. I want to make the buffer arrray and the slave address inputs inside the read and write functions to be a user input that the user decides when executing the program. How would I make that buffer something that the user inputs themself? This is what I have so far:
import adafruit_tca9548a
import time
import board
from smbus2 import SMBus as smb
# Create the I2C Bus
i2c = board.I2C() # uses board.SCL and board.SDA
# Create the 4 MUXs and give them to the I2C bus
TCA1 = adafruit_tca9548a.TCA9548A_Channel(i2c, 0x70)
TCA2 = adafruit_tca9548a.TCA9548A_Channel(i2c, 0x71)
TCA3 = adafruit_tca9548a.TCA9548A_Channel(i2c, 0x72)
TCA4 = adafruit_tca9548a.TCA9548A_Channel(i2c, 0x73)
...
# A scan function for each MUX...
...
SlavAddr = input("Input the address of the slave device:")
buffer = input("Input the buffer to which you want the 'write to' to be or where you want the 'read from' result to go:")
# Go through MUX 1
# Decide wether user wants to write to or read from a device connected to the MUX, if neither, pass on to next MUX or complete all transactions then pass on to MUX 2
while True:
print("Mux 1, address 0x70")
action = input("Select the channel the device you which to address if connected to: ") # Select a channel on MUX1 that your target device is connected to
# If C0 is selected for MUX 1
if action == '0': # If C0 is inputted, target the device on channel 0 MUX 1
TCA1[0].try_lock() # Enable channel 0 MUX 1
try:
action = input("Do you want to write to or read from the slave device? (w/r): "). lower() # Ask user to choose between writng to or reading from the device
if action == 'w': # If user selects 'w' proceed with write to protocols
TCA1[0].writeto(SlavAddr, buffer) # User only inputs the slave address and the bytes to send to the device, MUX address and channel pre-inputted
print("")
elif action == 'r': # If user selects 'r' proceed with read from protocols
TCA1[0].readfrom_into(SlavAddr, buffer) # User only inputs the slave address and the length of the read function to send to the device, MUX address and channel are pre-inputted
print("Read Result: {buffer}") # Read the result of the read function
print("")
except:
print("Invalid Input, enter either 'w' or 'r'.") # Error message that write to or read from command was incorrect
TCA1[0].unlock() # Disable the channel and continue to next part of the loop
and I'll repeat these loops for every MUX and channel.
