"""
Copyright (c) 2022 Gary Sims
MIT License
SPDX-License-Identifier: MIT
"""
import max7219
from time import sleep
from machine import RTC
from ds1307 import DS1307
import network
import time
from machine import Pin, RTC, SPI, I2C
from machine_i2c_lcd import I2cLcd
import urequests
clock_style = 1 # 1 = 3 col, 2 = 6 col BDC, 3 = length style
def binary_at(disp, b, x):
y = 7
while y > 1:
bit = b & 0x01
if bit == 1:
disp.pixel(x, y, 1)
y = y - 1
b = b >> 1
def bcd_at(disp, b, x):
d1 = b // 10
d2 = b % 10
print(b, d1, d2)
binary_at(display, d1, x)
binary_at(display, d2, x+1)
def len_at(disp, b, x):
d1 = b // 10
d2 = b % 10
disp.vline(x, 8-d1, d1, 1)
if d2==9:
disp.pixel(x,0,1)
disp.vline(x+1, 8-d2, d2, 1)
mode = Pin(15, Pin.IN, Pin.PULL_DOWN)
#led.off()
#Matrix init
i2c_rtc = I2C(0,scl = Pin(1),sda = Pin(0),freq = 100000)
i2c_lcd = I2C(1,scl = Pin(7),sda = Pin(6),freq = 100000)
lcd = I2cLcd(i2c_lcd, 0x27, 2, 16)
spi = SPI(0,sck=Pin(2),mosi=Pin(3))
cs = Pin(5, Pin.OUT)
display = max7219.Matrix8x8(spi, cs, 1)
display.brightness(10)
display.fill(0)
display.show()
#led.on()
rtc = DS1307(i2c_rtc)
#print(rtc.datetime())
#force_sync_counter = 0
while True:
#led.toggle()
Y, m, D, W, H, M, S, SS = rtc.datetime()
#print(H, M, S)
if mode.value() == 0 :
display.fill(0)
if clock_style == 1:
binary_at(display, H, 0)
binary_at(display, M, 2)
binary_at(display, S, 4)
elif clock_style == 2:
bcd_at(display, H, 0)
bcd_at(display, M, 3)
bcd_at(display, S, 6)
else:
len_at(display, H, 0)
len_at(display, M, 3)
len_at(display, S, 6)
display.show()
else :
lcd.move_to(0,0)
lcd.putstr("Time:")
lcd.move_to(6,0)
lcd.putstr(str(H) + ":" + 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))
#sleep(1)