import machine
import utime

button1 = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_DOWN)
button2 = machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_DOWN)
buzzer = machine.Pin(15, machine.Pin.OUT)

led_pins = [16, 17, 18, 19, 20, 21]
leds = [machine.Pin(pin, machine.Pin.OUT) for pin in led_pins]

red_pins = [16, 19]
green_pins = [17, 20]
yellow_pins = [18, 21]

red_leds = [machine.Pin(pin, machine.Pin.OUT) for pin in red_pins]
green_leds = [machine.Pin(pin, machine.Pin.OUT) for pin in green_pins]
yellow_leds = [machine.Pin(pin, machine.Pin.OUT) for pin in yellow_pins]

light_case = 0

def button1_handler(pin):
    global light_case
    print("You pressed the button1!")
    buzzer.toggle() 
    utime.sleep_ms(150)
    buzzer.toggle()
    if light_case == 0:
        light_case = 1
    else:
        light_case = 0
    utime.sleep_ms(150)

def button2_handler(pin):
    global light_case
    print("You pressed the button2!") 
    buzzer.toggle() 
    utime.sleep_ms(150)
    buzzer.toggle()
    utime.sleep_ms(150)
    buzzer.toggle()
    utime.sleep_ms(150)
    buzzer.toggle()
    light_case += 1
    if light_case > 3:
        light_case = 1
    utime.sleep_ms(150)

button1.irq(trigger=machine.Pin.IRQ_RISING, handler=button1_handler)
button2.irq(trigger=machine.Pin.IRQ_RISING, handler=button2_handler)

def run_cross_blink():
    for i in range(len(leds)):
        if i % 2 == 0:
            leds[i].on()
        else:
            leds[i].off()
    utime.sleep(0.5)  # 保持状态的时间
    for i in range(len(leds)):
        if i % 2 == 0:
            leds[i].off()
        else:
            leds[i].on()
    utime.sleep(0.5)  # 保持状态的时间

def run_scattering():
    for led in leds:
        led.on()
        utime.sleep_ms(100)  # 点亮时间

    for led in leds:
        led.off()
        utime.sleep_ms(100)  # 熄灭时间

def run_color_cycle():
    # 红色灯亮，绿色灯灭，黄色灯灭
    for red_led in red_leds:
        red_led.on()
    for green_led in green_leds:
        green_led.off()
    for yellow_led in yellow_leds:
        yellow_led.off()
    utime.sleep(0.5)  # 灯亮持续时间

    # 红色灯灭，绿色灯亮，黄色灯灭
    for red_led in red_leds:
        red_led.off()
    for green_led in green_leds:
        green_led.on()
    for yellow_led in yellow_leds:
        yellow_led.off()
    utime.sleep(0.5)  # 灯亮持续时间

    # 红色灯灭，绿色灯灭，黄色灯亮
    for red_led in red_leds:
        red_led.off()
    for green_led in green_leds:
        green_led.off()
    for yellow_led in yellow_leds:
        yellow_led.on()
    utime.sleep(0.5)  # 灯亮持续时间

while True:
    if light_case == 1:
        run_scattering()
    elif light_case == 2:
        run_cross_blink()
    elif light_case == 3:
        run_color_cycle()
    else:
        for led in leds:
            led.off()