# LED Matrix Clock with Raspberry Pi Pico
from machine import Pin, I2C, SPI
import utime
import sys, os
from rotary_irq_rp2 import RotaryIRQ
import max7219
import wlan
from sync_time import sync_time_NTP as sync_time
INTERVAL = 1000 # millisecond
I2C_FREQ = 400000
"""
Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
(date(1970, 1, 1) - date(1900, 1, 1)).days * 24*60*60
(70*365+17)*24*60*60 = 2208988800 =>UTC0
28800 = 8*60*60
2208960000 = 2208988800-28800
"""
# Optional UTC+8 offset time (seconds), if not set, UTC0
UTC_OFFSET = 8 * 3600 # 台北 UTC+8;若 RTC 要存 UTC 就設 0
if(sys.platform=="esp32"):
machine_info = os.uname().machine.upper()
if "S3" in machine_info:
print("正在初始化 ESP32-S3 的腳位...")
led_pin = None # 有板載 LED 時填 GPIO 編號,例如 48;沒有就維持 None
button_pin = 0 # BOOT 鍵
I2C_SDA = 5 # GPIO5
I2C_SCL = 4 # GPIO4
else:
print("正在初始化傳統 ESP32 的腳位...")
led_pin = 2 # Initialize LED on GPIO2
button_pin = 35
disp_sck = 18 # default SCK of SPI(1)
disp_mosi = 23 # default MOSI of SPI(1)
disp_miso = 19 # not use
disp_cs = 5
I2C_SDA = 21 # GPIO21
I2C_SCL = 22 # GPIO22
# Initialize I2C
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=I2C_FREQ) #initializing the I2C method for ESP32
# 初始化 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))
elif(sys.platform=="rp2"):
led_pin = "LED" # Initialize LED
button_pin = 21
disp_sck = 6 # default SCK of SPI(0)
disp_mosi = 7 # default MOSI of SPI(0)
disp_miso = 4 # not use
disp_cs = 5
I2C_SDA = 16 # GPIO16
I2C_SCL = 17 # GPIO17
# Initializing the I2C method for pi Pico
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=I2C_FREQ)
# 初始化 SPI
spi = SPI(0, baudrate=10000000, polarity=1, phase=0, sck=Pin(disp_sck), mosi=Pin(disp_mosi))
Rotary_clk=10
Rotary_dt=11
Rotary_SW=12
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)
# Function to handle button press interrupt (optional)
def button_press(pin):
global button_down
button_down = True
# Start Function
if __name__ == "__main__":
print("Hello, " + sys.platform + "!")
# Define GPIO pins for push button
button = Pin(button_pin, Pin.IN, Pin.PULL_UP) # Internal pull-up resistor
# Attach interrupt to the push button pin
button.irq(trigger=Pin.IRQ_FALLING, handler=button_press)
button_down = False
led = Pin(led_pin, Pin.OUT) if led_pin is not None else None
if led:
led.value(0)
SW = Pin(Rotary_SW, Pin.IN, Pin.PULL_UP)
r = RotaryIRQ(pin_num_clk=Rotary_clk,
pin_num_dt=Rotary_dt,
min_val=0,
reverse=False,
range_mode=RotaryIRQ.RANGE_UNBOUNDED)
val_old = r.value()
# ========== CONNECT WIFI ==========
wlan.connect_wifi() # Connecting to WiFi Router
utime.sleep_ms(1000)
# ========== NTP TIME SYNC ==========
led.on()
print("Syncing time from NTP...")
timer = sync_time() # Synchronize the time
hour = "00"
minute = "00"
second = "00"
led.off()
# ========== SPI MAX7219 ==========
display = max7219.Matrix8x8(spi, cs, 8) # 8 matrices
display.brightness(5)
lastTime = utime.ticks_ms()
# ========== MAIN LOOP ==========
try:
while True:
currTime = utime.ticks_ms()
if (currTime - lastTime > INTERVAL):
lastTime = currTime
time_now = utime.localtime(utime.time() + UTC_OFFSET)
hour = "{:02d}".format(time_now[3])
minute = "{:02d}".format(time_now[4])
second = "{:02d}".format(time_now[5])
# SERIAL PRINT
message_str = "TIME: "+ hour + ":" + minute + ":" + second
print(message_str)
# DISPLAY
display.fill(0)
display.text(hour, 0, 0, 1)
draw_colon(18)
display.text(minute, 22, 0, 1)
draw_colon(40)
display.text(second, 44, 0, 1)
display.show()
if (hour == "12") and (minute == "00") and (second == "00"):
# ========== CONNECT WIFI ==========
wlan.connect_wifi() # Connecting to WiFi Router
utime.sleep_ms(1000)
led.on()
timer = sync_time() # Synchronize the time
led.off()
if button_down == True:
# ========== CONNECT WIFI ==========
wlan.connect_wifi() # Connecting to WiFi Router
utime.sleep_ms(1000)
led.on()
timer = sync_time() # Synchronize the time
button_down = False
led.off()
val_new = r.value()
if SW.value() == 0:
print("Button Pressed")
print("Selected Number is:", val_new)
while SW.value() == 0:
continue
if val_old != val_new:
val_old = val_new
print('result =', val_new)
utime.sleep_ms(100)
except KeyboardInterrupt: # 使用者中斷執行(通常是輸入^C)
print("Stop")
Loading
pi-pico-w
pi-pico-w