0
\$\begingroup\$

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.

enter image description here

\$\endgroup\$
7
  • 1
    \$\begingroup\$ What exactly is your question? Are you asking how to make an array for the user to select from? Or how to actually get a user input in Python? If its the second one, how will the user input the data? Through COM? \$\endgroup\$ Commented Jul 18, 2024 at 16:03
  • \$\begingroup\$ I'm using a raspberry pi so I'd have the user type what bytes they want to write to the slave device in an array \$\endgroup\$ Commented Jul 18, 2024 at 16:08
  • \$\begingroup\$ Sorry but what you want to do is not related to embedded systems or electronics, you simply are asking how to do a basic Python program that does something based on user input, correct? \$\endgroup\$ Commented Jul 18, 2024 at 16:09
  • 2
    \$\begingroup\$ I agree with @Justme this isn't really on topic for this site, it's a programming question. Also there's a thousand tutorials on how to do this, search up "use rpi to take in user input with python" and you'll be set \$\endgroup\$ Commented Jul 18, 2024 at 16:21
  • 1
    \$\begingroup\$ @InBedded16 that actually really helped. That got me 1 step closer to finishing this thing, thank you \$\endgroup\$ Commented Jul 18, 2024 at 19:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.