# LED Matrix Clock with Raspberry Pi Pico
from machine import Pin, SPI
import utime
import sys
import network
import ntptime
import max7219
import wlan
INTERVAL = 1000 # millisecond
# Timezone offset (India = UTC +8:00)
UTC_OFFSET = 28800 # 8 * 60 * 60
if(sys.platform=="esp32"):
disp_sck = 18 # default SCK of SPI(1)
disp_mosi = 23 # default MOSI of SPI(1)
disp_miso = 19 # not use
disp_cs = 5
# 初始化 SPI
spi = SPI(1, baudrate=10000000, polarity=1, phase=0, sck=Pin(disp_sck), mosi=Pin(disp_mosi), miso=Pin(disp_miso))
# spi = SPI(1, baudrate=10000000, sck=Pin(4), mosi=Pin(2))
cs = Pin(disp_cs, Pin.OUT)
elif(sys.platform=="rp2"):
disp_sck = 2 # default SCK of SPI(0)
disp_mosi = 3 # default MOSI of SPI(0)
disp_miso = 4 # not use
disp_cs = 5
spi = SPI(0, baudrate=10000000, polarity=1, phase=0, sck=Pin(disp_sck), mosi=Pin(disp_mosi))
# 初始化 SPI
cs = Pin(disp_cs, Pin.OUT)
# Colon draw function
def draw_colon(x):
display.pixel(x, 2, 1)
display.pixel(x, 5, 1)
display.pixel(x+1, 2, 1)
display.pixel(x+1, 5, 1)
# Start Function
if __name__ == '__main__':
# ========== CONNECT WIFI ==========
wlan.connect_wifi() # Connecting to WiFi Router
# ========== NTP TIME SYNC ==========
print("Syncing time from NTP...")
ntptime.settime() # UTC time
# ========== SPI MAX7219 ==========
display = max7219.Matrix8x8(spi, cs, 8) # 8 matrices
display.brightness(5)
lastTime = utime.ticks_ms()
# ========== MAIN LOOP ==========
while True:
try:
currTime = utime.ticks_ms()
if (currTime - lastTime > INTERVAL):
lastTime = currTime
t = utime.time() + UTC_OFFSET
tm = utime.localtime(t)
hh = "{:02d}".format(tm[3])
mm = "{:02d}".format(tm[4])
ss = "{:02d}".format(tm[5])
# SERIAL PRINT
message_str = "TIME: "+ hh + ":" + mm + ":" + ss
print(message_str)
# DISPLAY
display.fill(0)
display.text(hh, 0, 0, 1)
draw_colon(18)
display.text(mm, 22, 0, 1)
draw_colon(40)
display.text(ss, 44, 0, 1)
display.show()
utime.sleep_ms(100)
except KeyboardInterrupt: # 使用者中斷執行(通常是輸入^C)
pass