import machine
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
import time

debug = machine.Pin(0, machine.Pin.OUT)
light = machine.Pin(17, machine.Pin.OUT)
Enter = machine.Pin(15, machine.Pin.IN)
Back = machine.Pin(2, machine.Pin.IN)
up = Pin(16, Pin.IN, Pin.PULL_UP)
down = Pin(4, Pin.IN, Pin.PULL_UP)

I2C_ADDR = 39
totalRows = 4
totalColumns = 20

i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)

menutext = [" ","countdown","stop watch","custom add on","calc","files","flash light"," "]
lcd.clear()

counter = 0
final_output = 0
print_counter = 0

light.on()

print_counter = 0
counter += 1
final_output = counter + 3
lcd.clear()
lightvalue = True
mainloop = True

for n in menutext[counter:final_output]:
    lcd.move_to(0,print_counter)
    print_counter += 1
    if print_counter == 2:
        lcd.putstr(f"{n} <")
    else:
        lcd.putstr(n)

def countdown():
    print("countdown mode")
    mainloop = False
    lcd.clear()
    for i in range(5 + 1):
        lcd.clear()
        lcd.putstr(f"Countdown: {-(i - 5)}")
        time.sleep(1)
    lcd.clear()
    lcd.putstr("Press down to contineu")
    mainloop = True

while mainloop:
    
    if down.value() == 0 and button_prev_state == 1:
        #print("up")
        if not counter >= 5:
            print_counter = 0
            counter += 1
            final_output = counter + 3
            lcd.clear()
            for n in menutext[counter:final_output]:
                lcd.move_to(0,print_counter)
                print_counter += 1
                if print_counter == 2:
                    lcd.putstr(f"{n} <")
                else:
                    lcd.putstr(n)
        print(counter)
    elif up.value() == 0 and button_prev_state2 == 1:
        #print("down")
        if not counter <= 0:
            print_counter = 0
            counter -= 1
            final_output = counter + 3
            lcd.clear()
            for n in menutext[counter:final_output]:
                lcd.move_to(0,print_counter)
                print_counter += 1
                if print_counter == 2:
                    lcd.putstr(f"{n} <")
                else:
                    lcd.putstr(n)
        print(counter)
    elif Enter.value() == 0 and button_prev_state3 == 1:
        print("Enter")
        if counter == 5:
            if lightvalue:
                light.off()
                lightvalue = False
            else:
                light.on()
                lightvalue = True
            print("light")
        if counter == 0:
            print(countdown)
            countdown()
            
    button_prev_state3 = Enter.value()
    button_prev_state = up.value()
    button_prev_state2 = down.value()
    time.sleep_ms(20)
$abcdeabcde151015202530fghijfghij
$abcdeabcde151015202530fghijfghij