from machine import Pin, I2C, SPI
import time
import ili9341
# ---------------- DS1307 ----------------
DS1307_ADDR = 0x68
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=100000)
def bcd_to_dec(b):
return (b >> 4) * 10 + (b & 0x0F)
def read_ds1307():
d = i2c.readfrom_mem(DS1307_ADDR, 0x00, 7)
return (
bcd_to_dec(d[2] & 0x3F), # hour
bcd_to_dec(d[1]), # min
bcd_to_dec(d[0] & 0x7F), # sec
bcd_to_dec(d[4]), # day
bcd_to_dec(d[5]), # month
bcd_to_dec(d[6]) + 2000 # year
)
# ---------------- ILI9341 ----------------
spi = SPI(
0,
baudrate=20000000,
polarity=0,
phase=0,
sck=Pin(2),
mosi=Pin(3)
)
display = ili9341.ILI9341(
spi,
cs=Pin(5, Pin.OUT),
dc=Pin(7, Pin.OUT),
rst=Pin(6, Pin.OUT),
w=240,
h=320
)
BLACK = ili9341.color565(0, 0, 0)
WHITE = ili9341.color565(255, 255, 255)
CYAN = ili9341.color565(0, 255, 255)
display.fill(BLACK)
# ---------------- SIMPLE FONT (5x7) ----------------
FONT = {
'0': [0x3E,0x51,0x49,0x45,0x3E],
'1': [0x00,0x42,0x7F,0x40,0x00],
'2': [0x42,0x61,0x51,0x49,0x46],
'3': [0x21,0x41,0x45,0x4B,0x31],
'4': [0x18,0x14,0x12,0x7F,0x10],
'5': [0x27,0x45,0x45,0x45,0x39],
'6': [0x3C,0x4A,0x49,0x49,0x30],
'7': [0x01,0x71,0x09,0x05,0x03],
'8': [0x36,0x49,0x49,0x49,0x36],
'9': [0x06,0x49,0x49,0x29,0x1E],
':': [0x00,0x36,0x36,0x00,0x00],
'-': [0x08,0x08,0x08,0x08,0x08]
}
def draw_char(x, y, ch, color, scale):
if ch not in FONT:
return
bitmap = FONT[ch]
for col in range(5):
line = bitmap[col]
for row in range(7):
if line & (1 << row):
for dx in range(scale):
for dy in range(scale):
display.pixel(
x + col*scale + dx,
y + row*scale + dy,
color
)
def draw_text(x, y, text, color, scale):
for ch in text:
draw_char(x, y, ch, color, scale)
x += 6 * scale
# ---------------- MAIN LOOP ----------------
blink = True
last_time = ""
while True:
h, m, s, d, mo, y = read_ds1307()
time_str = f"{h:02d}:{m:02d}:{s:02d}"
date_str = f"{d:02d}-{mo:02d}-{y}"
if time_str != last_time:
display.fill(BLACK)
# Blinking colon
if not blink:
time_str = time_str.replace(":", " ")
# Draw time (BIG)
draw_text(20, 110, time_str, WHITE, scale=3)
# Draw date (SMALL)
draw_text(45, 180, date_str, CYAN, scale=2)
blink = not blink
last_time = time_str
Loading
pi-pico-w
pi-pico-w