import machine
import time
from utime import sleep

# Constants for button, LED, and input counts
button_count = 3
led_count = 9
input_count = 4

# GPIO pin numbers for buttons and LEDs
button_start_gp = 16
led_start_gp = 7

# Initialize timestamp for button press tracking
previous_button_timestamp = 0
keys_pressed = []

def PinNumber(pin):
# Extract the numeric pin value from the pin object
# Example: Pin(GPIO17, mode=IN, pull=PULL_DOWN) -> Numeric value: 17
    return int(str(pin)[8:11].rstrip(","))

def interrupt_handler(pin):

    #Interrupt handler for button presses.
    global previous_button_timestamp

    current_button_timestamp = time.ticks_ms()
    button_press_diff = current_button_timestamp - previous_button_timestamp
    if button_press_diff > 200:
        previous_button_timestamp = current_button_timestamp
        keys_pressed.append(pin)
        print(f'key press: {PinNumber(pin) - button_start_gp}')

def main():
    sleep(0.01)
    print('Program starting')
    global keys_pressed
    global previous_button_timestamp
    Password_len = 0

    # Initialize pins for multiplexer (s0, s1, mux_in)
    s0 = machine.Pin(27, machine.Pin.OUT)
    s1 = machine.Pin(28, machine.Pin.OUT)
    mux_in = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_DOWN)

    # Initialize button pins and set up interrupts
    buttons = []
    for button_index in range(0, button_count):
        buttons.append(machine.Pin(button_start_gp + button_index, machine.Pin.IN, machine.Pin.PULL_DOWN))
        buttons[-1].irq(trigger=machine.Pin.IRQ_FALLING, handler=interrupt_handler)
    
    # Define a password sequence using specific buttons
    Password = [buttons[0], buttons[2], buttons[1]]
    Password_len = len(Password)

    # Initialize output pins for LEDs
    Output_Pins = []
    for output_id in range(0, led_count):
        Output_Pins.append(machine.Pin(led_start_gp + output_id, machine.Pin.OUT))

    previous_device = -1

    while True:
        # Read binary code from multiplexer
        Binary_code = 0
        for Selector_Value in range(input_count):
            s0.value(Selector_Value % 2)
            s1.value(Selector_Value // 2)
            sleep(0.02)
            Binary_code += (pow(2, Selector_Value) * mux_in.value())

        # Print selected output device based on binary code
        if previous_device != Binary_code:
            previous_device = Binary_code
            print(f'selected output: {previous_device}')
        sleep(0.1)

        # Check if enough keys have been pressed for password entry
        if len(keys_pressed) >= Password_len:
            # Implement password validation logic here
            if keys_pressed[:Password_len] == Password:
                print('correct passcode')
                if Binary_code < led_count:
                    print(f'toggling: {Binary_code}')
                    Output_Pins[Binary_code].toggle()
                else:
                    print(f'invalid output: {Binary_code}, ' + \
                    f'valid range: 0-{len(Output_Pins) - 1}, doing nothing')
            else:
                print('wrong passcode')
            print('')
            keys_pressed = keys_pressed[Password_len:]



if __name__ == "__main__":
    main()


$abcdeabcde151015202530354045505560fghijfghij
$abcdeabcde151015202530fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT