from machine import I2C, Pin
from time import sleep
from i2c_lcd import I2cLcd
import time
import random
from ds1302 import DS1302
I2C_ADDR = 0x27
totalRows = 4
totalColumns = 20
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
device = i2c.scan()
addr = device[0]
num_row = 4
num_col = 20
lcd = I2cLcd(i2c, addr, num_row, num_col)
i2c.writeto(addr, b'\x00\x08')
ds = DS1302(Pin(18),Pin(17),Pin(16))
ds.date_time() # returns the current datetime.
#ds.date_time([2023, 3, 2, 0, 8, 17, 50, 0]) # set datetime.
#ds.hour() # returns hour.
#print(ds.date_time())
while True:
(Y,M,D,day,hr,m,s)=ds.date_time()
if s < 10:
s = "0" + str(s)
if m < 10:
m = "0" + str(m)
if hr < 10:
hr = "0" + str(hr)
if D < 10:
D = "0" + str(D)
if M < 10:
M = "0" + str(M)
lcd.move_to(0,0)
lcd.putstr("Time:")
lcd.move_to(6,0)
lcd.putstr(str(hr) + ":" + str(m) + ":" + str(s))
lcd.move_to(0,1)
lcd.putstr("Date:")
lcd.move_to(6,1)
lcd.putstr(str(D) + "/" + str(M) + "/" + str(Y))