import serial
import time

# ----------------------------
# USB SERIAL PORT (your device)
# ----------------------------
PORT = "/dev/serial/by-id/usb-MicroPython_Board_in_FS_mode_de6258c0c3143639-if00"
BAUD = 115200

# ----------------------------
# Actions (replace with your real logic)
# ----------------------------

def button_a(): print("A")
def button_b(): print("B")
def button_c(): print("C")
def button_d(): print("D")
def button_e(): print("E")
def button_f(): print("F")

def button_close(): print("CLOSE")
def button_menu(): print("MENU")
def button_left(): print("LEFT")
def button_right(): print("RIGHT")
def button_up(): print("UP")
def button_down(): print("DOWN")
def button_pb(): print("PB")

# ----------------------------
# Command map
# ----------------------------

commands = {
    "A": button_a,
    "B": button_b,
    "C": button_c,
    "D": button_d,
    "E": button_e,
    "F": button_f,
    "CLOSE": button_close,
    "MENU": button_menu,
    "LEFT": button_left,
    "RIGHT": button_right,
    "UP": button_up,
    "DOWN": button_down,
    "PB": button_pb,
}

# ----------------------------
# Connect to Pico
# ----------------------------

print("Connecting to Pico...")

ser = serial.Serial(PORT, BAUD, timeout=1)
time.sleep(2)  # allow Pico reset

print("Connected to Pico!")

# ----------------------------
# Main loop
# ----------------------------

while True:
    try:
        line = ser.readline().decode("utf-8").strip().upper()

        if line:
            if line in commands:
                commands[line]()
            else:
                print("Unknown command:", line)

    except Exception as e:
        print("Error:", e)
        time.sleep(0.1)