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
lcd_start()
# Define functions for each mode
def normal_clock():
    # Implement code for Normal Clock mode
    cmd_wrt(0x80)
    string_write("Normal Clock    ")
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():
    # Implement code for International Clock mode
    cmd_wrt(0x80)
    string_write("International   ")
# 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)
from datetime import datetime, timedelta
import pytz  # Make sure to install this module using: pip install pytz
# Define time zones
malaysia_timezone = pytz.timezone('Asia/Kuala_Lumpur')
london_timezone = pytz.timezone('Europe/London')
# Get current time
current_time = datetime.now()
# Convert to Malaysia time
malaysia_time = current_time.astimezone(malaysia_timezone)
# Convert to London time
london_time = current_time.astimezone(london_timezone)
# Print the results
print("Current Time in Malaysia:", malaysia_time.strftime('%Y-%m-%d %H:%M:%S %Z'))
print("Current Time in London:", london_time.strftime('%Y-%m-%d %H:%M:%S %Z'))