import machine
import time
from i2c import *
# Define button pins
button_pin_1 = 17
button_pin_2 = 19
button_pin_3 = 18
button_1 = machine.Pin(button_pin_1, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_2 = machine.Pin(button_pin_2, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_3 = machine.Pin(button_pin_3, machine.Pin.IN, machine.Pin.PULL_DOWN)
last_button_1_state = button_1.value()
last_interrupt_time_1 = time.ticks_ms()
last_button_2_state = button_2.value()
last_interrupt_time_2 = time.ticks_ms()
last_button_3_state = button_3.value()
last_interrupt_time_3 = time.ticks_ms()
# Define modes
modes = ["Normal Clock", "Alarm", "Stopwatch", "International Clock"]
current_mode = 0
# Stopwatch variables
stopwatch_running = False
stopwatch_start_time = 0
stopwatch_elapsed_time = 0
stopwatch_reset_threshold = 10 * 60 * 1000  # 10 minutes in milliseconds
# Initialize RTC
rtc = machine.RTC()
lcd_start()
# Define functions for each mode
def normal_clock():
    current_time = rtc.now()
    time_str = "{:02d}:{:02d}:{:02d}".format(current_time[4], current_time[5], current_time[6])
    cmd_wrt(0x80)
    string_write("Normal Clock    ")
    cmd_wrt(0xC0)
    string_write(time_str)
def alarm():
    # Implement code for Alarm mode
    cmd_wrt(0x80)
    string_write("Alarm           ")
def stopwatch():
    global stopwatch_running, stopwatch_start_time, stopwatch_elapsed_time
    # Check if Button 2 is pressed to start/stop the stopwatch
    if button_2.value() == 1:  # Button is pressed
        if not stopwatch_running:
            # Start the stopwatch
            stopwatch_start_time = time.ticks_ms()
            stopwatch_running = True
        else:
            # Stop the stopwatch
            stopwatch_running = False
            stopwatch_elapsed_time += time.ticks_ms() - stopwatch_start_time
    # Check if the elapsed time exceeds the reset threshold
    if stopwatch_elapsed_time >= stopwatch_reset_threshold:
        stopwatch_elapsed_time = 0
    # Display the elapsed time on the LCD
    cmd_wrt(0x80)
    string_write("Stopwatch       ")
    cmd_wrt(0xC0)
    string_write(format_stopwatch_time(stopwatch_elapsed_time))
def format_stopwatch_time(elapsed_time):
    # Format the elapsed time in minutes:seconds:milliseconds
    minutes, seconds = divmod(elapsed_time // 1000, 60)
    milliseconds = elapsed_time % 1000
    return "{:02d}:{:02d}.{:03d}".format(minutes, seconds, milliseconds)
def international_clock():
    current_time = rtc.now()
    time_str = "{:02d}:{:02d}:{:02d}".format(current_time[4], current_time[5], current_time[6])
    cmd_wrt(0x80)
    string_write("International   ")
    cmd_wrt(0xC0)
    string_write(time_str)
# Function to change mode
def change_mode(pin):
    global current_mode, last_button_1_state, last_interrupt_time_1
    current_time = time.ticks_ms()
    if current_time - last_interrupt_time_1 > 200:  # Adjust the debounce time as needed
        if button_1.value() != last_button_1_state:
            last_button_1_state = button_1.value()
            current_mode = (current_mode + 1) % len(modes)
        last_interrupt_time_1 = current_time
# Attach interrupt for button 1 press
button_1.irq(trigger=machine.Pin.IRQ_FALLING, handler=change_mode)
# Main loop
while True:
    if current_mode == 0:
        normal_clock()
    elif current_mode == 1:
        alarm()
    elif current_mode == 2:
        stopwatch()
    elif current_mode == 3:
        international_clock()
    # Add a short delay to avoid excessive loop iterations
    time.sleep(0.5)