| from machine import Pin, SPI import network, ntptime, time SSID = "Wi-Fiネットワーク名" PASSWORD = "パスワード" BLACK = 0x0000 spi = SPI( 0, baudrate=10000000, polarity=1, phase=1, sck=Pin(18), mosi=Pin(19), miso=None ) dc = Pin(21, Pin.OUT) rst = Pin(20, Pin.OUT) cs = Pin(17, Pin.OUT) # ---- SPI立ち上がり安定化 ---- def spi_warmup(): cs(0); dc(1) spi.write(b"\x00") cs(1) time.sleep_ms(2) def cmd(c): cs(0); dc(0) spi.write(bytearray([c])) cs(1) def data(d): cs(0); dc(1) spi.write(bytearray([d])) cs(1) def reset_display(): rst(0); time.sleep_ms(80) rst(1); time.sleep_ms(150) def init(): cmd(0x11); time.sleep_ms(120) cmd(0x36); data(0xC0) cmd(0x3A); data(0x55) cmd(0x21) cmd(0x29); time.sleep_ms(20) def init_stable(): spi_warmup() reset_display() time.sleep_ms(60) init() time.sleep_ms(60) init() def window(x0, y0, x1, y1): cmd(0x2A) data(x0 >> 8); data(x0 & 255) data(x1 >> 8); data(x1 & 255) cmd(0x2B) data(y0 >> 8); data(y0 & 255) data(y1 >> 8); data(y1 & 255) cmd(0x2C) def fill(color): window(0, 0, 239, 319) hi = color >> 8 lo = color & 255 buf = bytearray(512) for i in range(0, 512, 2): buf[i] = hi buf[i+1] = lo pixels = 240 * 320 cs(0); dc(1) for _ in range(pixels // 256): spi.write(buf) cs(1) def draw_raw_135x240(filename, x, y): w = 135 h = 240 window(x, y, x + w - 1, y + h - 1) cs(0); dc(1) with open(filename, "rb") as f: while True: chunk = f.read(512) if not chunk: break spi.write(chunk) cs(1) # ===== Wi-Fi / NTP ===== def connect_wifi(): wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.config(pm=0xa11140) wlan.connect(SSID, PASSWORD) print("Connecting Wi-Fi...") while not wlan.isconnected(): time.sleep(0.1) print("Connected:", wlan.ifconfig()) def sync_time(): ntptime.host = "ntp.nict.jp" for _ in range(5): try: ntptime.settime() print("NTP synced") return except: time.sleep(1) print("NTP failed") JST = 9 * 3600 def get_digits(): t = time.localtime(time.time() + JST) h, m, s = t[3], t[4], t[5] return { "H10": h // 10, "H1": h % 10, "M10": m // 10, "M1": m % 10, "S10": s // 10, "S1": s % 10 } center_x = (240 - 135) // 2 center_y = (320 - 240) // 2 + 36 def show_digit(d): if d is None: draw_raw_135x240("digit_blank.raw", center_x, center_y) else: draw_raw_135x240(f"digit{d}.raw", center_x, center_y) sequence = [ ("H10", 0.7), ("H1", 0.7), ("BLANK", 0.3), ("M10", 0.7), ("M1", 0.7), ("BLANK", 0.3), ("S10", 0.7), ("S1", 0.7), ("BLANK", 0.8), ] def main(): print("INIT...") init_stable() fill(BLACK) draw_raw_135x240("digit0.raw", center_x, center_y) print("INIT DONE") connect_wifi() sync_time() while True: digits = get_digits() for kind, dur in sequence: if kind == "BLANK": show_digit(None) else: show_digit(digits[kind]) time.sleep(dur) main() |