"""
Author: Turapov I.A.
Group: K0709-22/3
Device: Digit Calendar
"""
import time
from machine import I2C, Pin
from ds1307 import DS1307
from pico_i2c_lcd import I2cLcd
# Time
I2C_ADDR = 0x68 # DEC 104, HEX 0x68
i2c = I2C(1, scl=Pin(19), sda=Pin(18), freq=400000)
ds1307 = DS1307(addr=I2C_ADDR, i2c=i2c)
# Screen (above)
i2c_bus_a = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
i2c_address_a = i2c_bus_a.scan()[0]
lcd_display_a = I2cLcd(i2c_bus_a, i2c_address_a, 4, 20)
# Screen (under)
i2c_bus_u = I2C(1, sda=Pin(2), scl=Pin(3), freq=400000)
i2c_address_u = i2c_bus_u.scan()[0]
lcd_display_u = I2cLcd(i2c_bus_u, i2c_address_u, 4, 20)
button_up = Pin(5, Pin.IN)
button_down = Pin(4, Pin.IN)
# Constats
months = (
'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December',
)
moths_days = (
31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31
)
weekdays = (
'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday',
)
def get_timedate(lcd_above, lcd_under):
"""Display the current weekend."""
day = ds1307.datetime[2] # today
# Display weekdays
lcd_above.move_to(0, 1)
count = 0
for wday in weekdays:
count += 1
if count > 3: # For display_under
lcd_under.putstr(f"{wday}\n")
continue
lcd_above.putstr(f"{wday}\n")
# Check current weekday above
count = -1
for cell in range(1, 4):
count += 1
lcd_above.move_to(17, cell)
if weekdays[ds1307.datetime[6]-2] == weekdays[count]:
current_day = ['above', 18, cell]
lcd_above.putstr("[X]")
lcd_above.move_to(10, cell)
lcd_above.putstr(f"{day}")
continue
lcd_above.putstr("[ ]")
# lcd_above.move_to(10, cell)
# Check current weekday under
count = 2
for cell in range(4):
count += 1
lcd_under.move_to(17, cell)
if weekdays[ds1307.datetime[6]-2] == weekdays[count]:
current_day = ['under', 18, cell]
lcd_under.putstr("[X]")
lcd_under.move_to(10, cell)
lcd_under.putstr(f"{day}")
continue
lcd_under.putstr("[ ]")
time.sleep(1)
return current_day
def update_time(lcd):
"""Get and set current timedate."""
year = ds1307.datetime[0]
month = months[ds1307.datetime[1]-1]
hours = ds1307.datetime[3]
minutes = ds1307.datetime[4]
seconds = ds1307.datetime[5]
lcd.move_to(0, 0)
lcd.putstr(f"0{hours}" if hours < 10 else f"{hours}")
lcd.putstr(":")
lcd.putstr(f"0{minutes}" if minutes < 10 else f"{minutes}")
lcd.putstr(":")
lcd.putstr(f"0{seconds} " if seconds < 10 else f"{seconds} ")
lcd.putstr(f"{month} {year}")
def button_action(lcd_above, lcd_under, x):
"""Switch weekend and date with two buttons."""
# x - [display position, cell x position, cell y position]
print("="*20)
print(f"Button_up: {button_up.value()}")
print(f"Button_down: {button_down.value()}")
if button_up.value():
time.sleep(0.05)
if x[0] == 'under':
lcd_under.move_to(x[1], x[2])
lcd_under.putstr(" ")
x[2] = x[2] - 1
if x[2] < 0:
x[0] = 'above'
x[2] = 3
if x[0] == 'under':
lcd_under.move_to(x[1], x[2])
lcd_under.putstr("X")
if x[0] == 'above':
lcd_above.move_to(x[1], x[2])
lcd_above.putstr(" ")
x[2] = x[2] - 1
lcd_above.move_to(x[1], x[2])
lcd_above.putstr("X")
if button_down.value():
time.sleep(0.05)
if x[0] == 'under':
lcd_under.move_to(x[1], x[2])
lcd_under.putstr(" ")
x[1] = x[1] + 1
lcd_under.move_to(x[1], x[2])
lcd_under.putstr("X")
if x[0] == 'above':
lcd_above.move_to(x[1], x[2])
lcd_above.putstr(" ")
x[2] = x[2] + 1
if x[2] > 3:
x[0] = 'under'
x[2] = 0
if x[0] == 'above':
lcd_above.move_to(x[1], x[2])
lcd_above.putstr("X")
def main():
"""Main function."""
update_time(lcd_display_a)
target = get_timedate(lcd_display_a, lcd_display_u)
while True:
update_time(lcd_display_a)
button_action(lcd_display_a, lcd_display_u, target)
if __name__ == '__main__':
main()