from machine import Pin
from time import sleep

# Define MUX control pins
sig = Pin(34, Pin.IN)

# s3 = Pin(2, Pin.OUT)
# s2 = Pin(4, Pin.OUT)
# s1 = Pin(5, Pin.OUT)
# s0 = Pin(18, Pin.OUT)
en = Pin(19, Pin.OUT)  # MUX Enable pin
control_pins = [Pin(18, Pin.OUT), Pin(5, Pin.OUT), Pin(4, Pin.OUT), Pin(2, Pin.OUT)]


def read_mux(channel):
    for i in range(4):
        control_pins[i].value(channel >> i & 1)
    sleep(0.5)
    return sig.value()

# Other control pins
ds = Pin(14, Pin.OUT)
oe = Pin(25, Pin.OUT)
mr = Pin(27, Pin.OUT, Pin.PULL_UP)
latch = Pin(12, Pin.OUT)
clock = Pin(13, Pin.OUT)

def set_mux_channel(channel):
    print("set mux channel")
    s0.value((channel >> 0) & 1)
    s1.value((channel >> 1) & 1)
    s2.value((channel >> 2) & 1)
    s3.value((channel >> 3) & 1)
    en.value(0)  # Enable MUX
    
    # print(f"Setting MUX channel to {channel}")
    # print(f"s0: {(channel >> 0) & 1}, s1: {(channel >> 1) & 1}, s2: {(channel >> 2) & 1}, s3: {(channel >> 3) & 1}")
    
    sleep(0.05)  # Increase delay to ensure the settings stabilize

num_of_pins = 4


def read_ios():
    print("read io")
    values = []
    for channel in range(num_of_pins):
        print(channel)
        # set_mux_channel(channel)
        # sleep(2)  # Delay before reading the signal value
        values.append(read_mux(channel))
        print(read_mux(channel))
        en.value(5)  # Disable MUX
        sleep(1)
    return values

def shift_out(value):
    print("shift out")
    oe.value(1)  # Disable output
    latch.value(0)  # Prepare latch
    
    # # Clear the shift register
    mr.value(1)
    sleep(0.001) 
    mr.value(0)
    sleep(0.001) 
    mr.value(1)

    # Shift out the "high"" value
    for i in range(num_of_pins-1, -1, -1):
        ds.value((value >> i) & 1)  # Set data pin
        clock.value(0)  
        sleep(0.001)  
        clock.value(1)
    
    latch.value(1)  # Latch to output
    oe.value(0)  # Enable output

def turn_on_io(n):
    print("turn on io")
    shift_out(1 << n)

def analyze_matrix(matrix):
    num_pins = len(matrix)
    error_found = False  # Flag to detect any issues
    
    for i in range(num_pins):
        if matrix[i][i] == 0:  # Check diagonal element
            print(f"Pin {i+1} is disconnected.")
            error_found = True
        for j in range(i+1, num_pins):  # Check upper triangle
            if matrix[i][j] == 1:
                print(f"Short between Pin {i+1} and Pin {j+1}.")
                error_found = True
    
    if not error_found:
        print(f"Cable OK - {num_pins} Pins")  # All connections are okay

connectivity_matrix = [[0 for _ in range(num_of_pins)] for _ in range(num_of_pins)]

while True:
    for i in range(num_of_pins):
        turn_on_io(i)
        print("turn_on_io")
        sleep(0.1)
        connectivity_matrix[i] = read_ios()
    oe.value(1)
    print("Matrix after updating row", i, ":")
    for row in connectivity_matrix:
        print(row)
    analyze_matrix(connectivity_matrix)  # Analyze and print results
    print("\n")

    
    # Delay
    sleep(10)














def shift_out(value):
    oe.value(1)  # Disable output
    latch.value(0)  # Prepare latch
    
    # Clear the shift register
    mr.value(1)
    sleep(0.001) 
    mr.value(0)
    sleep(0.001) 
    mr.value(1)

    # Shift out the "high"" value
    for i in range(15, -1, -1):
        ds.value((value >> i) & 1)
        clock.value(0)  
        sleep(0.001)  
        clock.value(1)
    
    latch.value(1)  # Latch to output
    oe.value(0)  # Enable output

def turn_on_io(n):
    shift_out(1 << n)

def analyze_matrix(matrix):
    num_pins = len(matrix)
    error_found = False  # Flag to detect any issues
    
    for i in range(num_pins):
        if matrix[i][i] == 0:  # Check diagonal element
            print(f"Pin {i+1} is disconnected.")
            error_found = True
        for j in range(i+1, num_pins):  # Check upper triangle
            if matrix[i][j] == 1:
                print(f"Short between Pin {i+1} and Pin {j+1}.")
                error_found = True
    
    if not error_found:
        print(f"Cable OK - {num_pins} Pins")  # All connections are okay

connectivity_matrix = [[0 for _ in range(16)] for _ in range(16)]

while True:
    for i in range(16):
        turn_on_io(i)
        sleep(0.1)
        connectivity_matrix[i] = read_ios()
    oe.value(1)
    print("Matrix after updating row", i, ":")
    for row in connectivity_matrix:
        print(row)
    analyze_matrix(connectivity_matrix)  # Analyze and print results
    print("\n")
    
    # Delay 
    sleep(120)
74HC595
74HC595
Loading
cd74hc4067