from machine import Pin, I2C
import time
import ssd1306

# ESP32 Pin assignment 
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
button = Pin(27, Pin.IN, Pin.PULL_UP)

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

mode = "first"

def button_state_read():
    return not button.value() # Assuming `button.value()` returns 0 or 1, adjust if needed

def main():
    button_held = False
    press_start_time = None

    while True:
        if button_state_read():  # Button is pressed
            if not button_held:
                # Button was not previously held, so this is a new press
                press_start_time = time.time()
                button_held = True
        else:  # Button is not pressed
            if button_held:
                # Button was held but now is released
                press_end_time = time.time()
                duration = press_end_time - press_start_time
                button_held = False

                oled.fill(0)

                if duration >= 2:
                    if mode = "first":
                        mode = "second"
                    elif mode = "second":
                        mode = "first"

                    oled.text(f"Mode: {mode}", 0, 0)  # Display the updated mode
                else:
                    oled.text("Not change", 0, 0)  # Display message if button was not held long enough
                
                oled.show()  # Update the display with new content
        time.sleep(0.01)  # Small delay to avoid rapid polling

main()