"""
Reference: https://blog.jmaker.com.tw/arduino-ds1302-lcd/
Arduino大字型LCD時鐘,使用DS1302模組
"""
from machine import Pin, I2C
import utime
import sys, os
import ds1307
import lcd_big_api
import wlan
from sync_time import sync_time_NTP as sync_time
I2C_FREQ = 400000
# 配置控制GPIO
if(sys.platform=="esp8266"):
led_pin = 2 # GPIO2(D4)
button_pin = 4 # GPIO4(D2)
I2C_SDA = 4 # GPIO4(D2)
I2C_SCL = 5 # GIPO5(D1)
# Initializing the I2C method for ESP8266
i2c = I2C(scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=I2C_FREQ)
elif(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
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
elif(sys.platform=="rp2"):
led_pin = "LED" # Initialize LED
button_pin = 21
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)
# Function to handle button press interrupt (optional)
def button_press(pin):
global button_down
button_down = True
#Timer Function Callback
def timerFunc0(t):
global TimeDone
TimeDone = 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)
wlan.connect_wifi() # Connecting to WiFi Router
utime.sleep_ms(1000)
# 自動偵測 I2C 設備位址
devices = i2c.scan() # I2C scanning
if len(devices) != 0:
print('Number of I2C devices found=',len(devices))
for device in devices:
print("Device Hexadecimel Address= ",hex(device))
else:
raise OSError("No i2c device found!")
# 預設 LCD I2C 位址為 0x27 或 0x3F
lcd_addr = 0x27 if 0x27 in devices else 0x3F
lcd = lcd_big_api.I2cLcd(i2c, lcd_addr, 2, 16)
rtc = ds1307.DS1307(i2c)
print("Hello, Pi Pico!")
now = (2018, 3, 24, 6, 13, 45, 21, 0)
rtc.datetime(now)
lcd.clear()
led.on()
timer = sync_time() # Synchronize the time
if timer != None:
rtc.datetime((timer[0], timer[1], timer[2], timer[6] + 1, timer[3], timer[4], timer[5], 0))
print(rtc.datetime())
led.off()
# 載入自訂大數字字形
for i, pattern in enumerate(lcd_big_api.custom_patterns):
lcd.custom_char(i, pattern)
last_sec = -1
try:
while True:
# year, month, day, hour, minute, sec = rtc.datetime()
time_now = rtc.datetime()
year = time_now[0]
month = time_now[1]
day = time_now[2]
hour = time_now[4]
minute = time_now[5]
sec = time_now[6]
# 僅每秒更新一次 LCD,避免畫面閃爍
if sec != last_sec:
last_sec = sec
# 排版:[HH] : [MM] [SS]
# 1602 LCD 排版 (共 16 格):
# Col 0~2: 時(十位)
# Col 3~5: 時(個位)
# Col 6: 冒號 ':'
# Col 7~9: 分(十位)
# Col 10~12: 分(個位)
# Col 14~15: 秒數(小字體)
lcd_big_api.draw_big_digit(lcd, 0, hour // 10)
lcd_big_api.draw_big_digit(lcd, 3, hour % 10)
# 顯示閃爍的中間冒號
lcd.move_to(6, 0)
lcd.write_char(ord(':') if sec % 2 == 0 else 32)
lcd.move_to(6, 1)
lcd.write_char(ord(':') if sec % 2 == 0 else 32)
lcd_big_api.draw_big_digit(lcd, 7, minute // 10)
lcd_big_api.draw_big_digit(lcd, 10, minute % 10)
# 右側顯示小字體秒數
lcd.move_to(14, 0)
lcd.write_char(ord('0') + sec // 10)
lcd.move_to(15, 0)
lcd.write_char(ord('0') + sec % 10)
if button_down == True:
timer = sync_time() # Synchronize the time
if timer != None:
rtc.datetime((timer[0], timer[1], timer[2], timer[6] + 1, timer[3], timer[4], timer[5], 0))
button_down = False
utime.sleep_ms(100)
except KeyboardInterrupt: # 使用者中斷執行(通常是輸入^C)
print("Stop")
Loading
pi-pico-w
pi-pico-w