from machine import Pin
import utime

buzzer = Pin(15, Pin.OUT)
button_snooze = Pin(1, Pin.IN, Pin.PULL_UP)
button_ok_pin = Pin(28, Pin.IN, Pin.PULL_UP)
button_right_pin = Pin(27, Pin.IN, Pin.PULL_UP)
button_left_pin = Pin(26, Pin.IN, Pin.PULL_UP)
button_home_pin = Pin(22, Pin.IN, Pin.PULL_UP)

# Initialize timer variables
ok_button_timer = 0
snooze_timer = 0
ok_button_pressed_flag = False


# Buzzer on function
def buzzer_on():
    buzzer.value(1)

# Buzzer off function
def buzzer_off():
    buzzer.value(0)

# Function to handle OK button press
def ok_button_pressed():
    global ok_button_timer
    ok_button_timer = 600  # 6 seconds
    global ok_button_pressed_flag
    ok_button_pressed_flag = True

# Snooze button pressed function
def snooze_button_pressed():
    global snooze_timer
    snooze_timer = 500  # 15 seconds

# Function to handle right button press
def button_right_pressed():
    ok_button_pressed()  # Reset OK button timer

# Function to handle left button press
def button_left_pressed():
    if buzzer.value() == 1:
        buzzer.value(0)
    ok_button_pressed()  # Reset OK button timer

# Main function
def main():
    global ok_button_timer
    global snooze_timer
    global ok_button_pressed_flag
    
    while True:
        # Check if OK button is pressed
        if not button_ok_pin.value() and not ok_button_pressed_flag:
            print("OK button pressed")
            ok_button_pressed()
        
        # Check if snooze button is pressed
        if not button_snooze.value():
            if  buzzer.value():
                print("Snooze button pressed")
                buzzer.value(0) 
            snooze_button_pressed()

        # Check if left button is pressed
        if not button_left_pin.value():
            print("Left button pressed")
            button_left_pressed()

        # Check if right button is pressed
        if not button_right_pin.value():
            print("Right button pressed")
            button_right_pressed()

        # Check if OK button timer is active
        if ok_button_timer > 0:
            print("timer")
            print(ok_button_timer)
            ok_button_timer -= 1
            if ok_button_timer == 0:
                buzzer_on()  # Turn on the buzzer when OK button timer expires
        
        # Check if snooze timer is active
        if snooze_timer > 0:
            print(" timer")
            print(snooze_timer)
            snooze_timer -= 1
            if snooze_timer == 0:
                buzzer_on()

        # Check if left or right button is pressed during snooze period
        if (snooze_timer > 0) and (not button_left_pin.value() or not button_right_pin.value()):
            print("Left or right button pressed during snooze")
            ok_button_timer = 600  # Reset OK button timer
            snooze_timer = 0  # Cancel snooze

        utime.sleep_ms(10)

# Call the main function
main()
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT