import utime
import math
# import machine
from machine import Pin
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
from ds1307 import DS1307
I2C_ADDR = 0x27 # Decimal 39
I2C_ADDR2 = 0x68 # Decimal 104
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
SDA_PIN = 0
SCL_PIN = 1
SDA2_PIN = 6
SCL2_PIN = 7
UP_PIN = 2
DOWN_PIN = 3
SELECT_PIN = 4
# print("Running StopWatch")
i2c = I2C(0, sda=machine.Pin(SDA_PIN), scl=machine.Pin(SCL_PIN), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
i2c2 = I2C(1, sda=machine.Pin(SDA2_PIN), scl=machine.Pin(SCL2_PIN), freq=400000)
clock= DS1307(i2c2, I2C_ADDR2)
button_up = Pin(UP_PIN, Pin.IN, Pin.PULL_UP)
button_down = Pin(DOWN_PIN, Pin.IN, Pin.PULL_UP)
button_select = Pin(SELECT_PIN, Pin.IN, Pin.PULL_UP)
weekdays = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]
mode = 0
def nastaveni():
lcd.clear()
lcd.putstr("Settings:")
lcd.move_to(0,1)
lcd.putstr(">Date setting:")
while True:
if (button_down.value()==0 and mode==1):
lcd.move_to(0,1)
lcd.putstr(">Time setting:")
mode = 2
if (button_down.value()==0 and mode==2):
lcd.move_to(0,1)
lcd.putstr(">Alarm setting:")
mode = 3
if (button_down.value()==0 and mode==3):
lcd.move_to(0,1)
lcd.putstr(">Return:")
mode = 4
if (button_select.value()==0 and mode==4):
mode = 0
break
while True:
if (button_select.value()==0 and mode==0):
mode = 1
nastaveni()
elif (mode==0):
# print(clock.datetime())
# lcd.clear()
c = clock.datetime()
weekday = weekdays[c[3]-1]
lcd.move_to(0, 0)
lcd.putstr(weekday)
lcd.move_to(6, 0)
lcd.putstr("{:02d}/{:02d}/{:4d}".format(c[1], c[2], c[0]))
lcd.move_to(4, 1)
lcd.putstr("{:02d}:{:02d}:{:02d}".format(c[4], c[5], c[6]))
# utime.sleep(0.25)