Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import network
- import ntptime
- import utime
- import framebuf
- from machine import Pin, SPI
- print("=== BOOT main.py ===")
- # ——— Wi-Fi & NTP ———
- SSID = "##"
- PASSWORD = "##"
- ntptime.host = "91.189.89.198"
- TZ_OFFSET = 0
- wlan = network.WLAN(network.STA_IF)
- offline_mode = False
- def connect_wifi():
- wlan.active(True)
- wlan.connect(SSID, PASSWORD)
- print("Connecting to Wi-Fi", end="")
- for _ in range(15):
- if wlan.isconnected(): break
- utime.sleep(1); print(".", end="")
- print()
- if not wlan.isconnected():
- raise RuntimeError("Wi-Fi failed")
- print("Wi-Fi up, IP:", wlan.ifconfig()[0])
- def ensure_wifi():
- if not wlan.isconnected():
- print("Wi-Fi dropped, reconnecting…")
- try:
- connect_wifi()
- except Exception as e:
- print("Reconnect failed:", e)
- # ——— EPD driver ———
- EPD_PW, EPD_PH = 104, 212
- EPD_LW, EPD_LH = EPD_PH, 104
- RST_PIN, DC_PIN, CS_PIN, BUSY_PIN = 12, 8, 9, 13
- class EPD:
- def __init__(self):
- self.rst = Pin(RST_PIN, Pin.OUT)
- self.dc = Pin(DC_PIN, Pin.OUT)
- self.cs = Pin(CS_PIN, Pin.OUT)
- self.busy = Pin(BUSY_PIN, Pin.IN, Pin.PULL_UP)
- self.spi = SPI(1)
- self.spi.init(baudrate=4_000_000, polarity=0, phase=0)
- buf = EPD_PW*EPD_PH//8
- self.bw = bytearray(buf)
- self.rd = bytearray(buf)
- self.fb_black = framebuf.FrameBuffer(self.bw, EPD_PW, EPD_PH, framebuf.MONO_HLSB)
- self.fb_red = framebuf.FrameBuffer(self.rd, EPD_PW, EPD_PH, framebuf.MONO_HLSB)
- lbuf = EPD_LW*EPD_LH//8
- self.lb = bytearray(lbuf)
- self.lr = bytearray(lbuf)
- self.lfb_b = framebuf.FrameBuffer(self.lb, EPD_LW, EPD_LH, framebuf.MONO_VLSB)
- self.lfb_r = framebuf.FrameBuffer(self.lr, EPD_LW, EPD_LH, framebuf.MONO_VLSB)
- self._init_panel()
- def _write(self, dc, data):
- self.dc.value(dc); self.cs.value(0)
- self.spi.write(data if isinstance(data,(bytes,bytearray)) else bytearray(data))
- self.cs.value(1)
- def cmd(self, c): self._write(0, [c])
- def data(self,b): self._write(1, b)
- def _busy(self):
- self.cmd(0x71)
- while not self.busy.value():
- utime.sleep_ms(10); self.cmd(0x71)
- def _init_panel(self):
- self.rst.value(1); utime.sleep_ms(50)
- self.rst.value(0); utime.sleep_ms(2)
- self.rst.value(1); utime.sleep_ms(50)
- self.cmd(0x04); self._busy()
- self.cmd(0x00); self.data([0x0F,0x89])
- self.cmd(0x61); self.data([EPD_PW, (EPD_PH>>8)&0xFF, EPD_PH&0xFF])
- self.cmd(0x50); self.data([0x77])
- def _rotate(self):
- self.fb_black.fill(1); self.fb_red.fill(1)
- for x in range(EPD_LW):
- for y in range(EPD_LH):
- if self.lfb_b.pixel(x,y)==0:
- self.fb_black.pixel(y, EPD_LW-1-x, 0)
- if self.lfb_r.pixel(x,y)==0:
- self.fb_red.pixel( y, EPD_LW-1-x, 0)
- def display(self):
- self._rotate()
- self.cmd(0x10); self.data(self.bw)
- self.cmd(0x13); self.data(self.rd)
- self.cmd(0x12); self._busy()
- def clear(self):
- w = EPD_PW*EPD_PH//8
- self.cmd(0x10); self.data([0xFF]*w)
- self.cmd(0x13); self.data([0xFF]*w)
- self.cmd(0x12); self._busy()
- self.lfb_b.fill(1); self.lfb_r.fill(1)
- def draw_text2x(self, fb, txt, x, y, col):
- w,h = len(txt)*8, 8
- tmp = bytearray(w*h//8)
- tf = framebuf.FrameBuffer(tmp, w, h, framebuf.MONO_HLSB)
- tf.fill(1); tf.text(txt,0,0,0)
- for px in range(w):
- for py in range(h):
- if tf.pixel(px,py)==0:
- fb.pixel(x+px*2, y+py*2, col)
- fb.pixel(x+px*2+1, y+py*2, col)
- fb.pixel(x+px*2, y+py*2+1, col)
- fb.pixel(x+px*2+1, y+py*2+1, col)
- def update_dynamic(epd, hh, mm, ss, y0, margin, th_main, th_beat):
- # HH:MM
- t = "{:02d}:{:02d}".format(hh, mm)
- w = len(t)*16; x = (EPD_LW - w)//2
- epd.lfb_b.fill_rect(x, y0, w, th_main, 1)
- epd.draw_text2x(epd.lfb_b, t, x, y0, 0)
- # BMT-beats
- bmt_h = (hh - 1) % 24
- secs = bmt_h*3600 + mm*60 + ss
- beats = int(secs/86.4)
- bt = "@{:03d}".format(beats)
- w2 = len(bt)*16; x2 = (EPD_LW - w2)//2
- y2 = y0 + th_main + margin
- epd.lfb_b.fill_rect(x2, y2, w2, th_beat, 1)
- epd.draw_text2x(epd.lfb_r, "@", x2, y2, 0)
- epd.draw_text2x(epd.lfb_b, "{:03d}".format(beats), x2+16, y2, 0)
- if __name__=="__main__":
- # try Wi-Fi + NTP with pause
- try:
- connect_wifi()
- print("Waiting 5 s for network…"); utime.sleep(5)
- print("Sync NTP…", end="")
- ntptime.settime()
- print(" OK")
- except Exception as e:
- print("Wi-Fi/NTP failed:", e)
- print("Falling back offline in 5 s…"); utime.sleep(5)
- offline_mode = True
- start_ticks = utime.ticks_ms()
- epd = EPD()
- # layout
- margin = 4; th_main = 16; th_beat = 16
- y0 = (EPD_LH - (th_main+margin+th_beat))//2
- # draw static UI
- epd.clear()
- for i in range(4):
- o=6+i; epd.lfb_b.rect(o,o,EPD_LW-2*o,EPD_LH-2*o,0)
- for i in range(2):
- o=10+i; epd.lfb_r.rect(o,o,EPD_LW-2*o,EPD_LH-2*o,0)
- # "Amsterdam"
- am="Amsterdam"
- x_am=(EPD_LW-len(am)*8)//2; y_am=y0-margin-8
- epd.lfb_b.text(am,x_am,y_am,0)
- # lower logo
- dot=16; logo_w=dot+4+len("beat")*8
- lx=(EPD_LW-logo_w)//2
- ly=y0+th_main+margin+th_beat+margin
- epd.lfb_r.fill_rect(lx,ly,dot,dot,0)
- epd.lfb_b.text("beat",lx+dot+4,ly+(dot-8)//2,0)
- # main loop
- while True:
- if offline_mode:
- secs = utime.ticks_diff(utime.ticks_ms(), start_ticks)//1000
- hh=(12+secs//3600)%24; mm=(secs//60)%60; ss=secs%60
- else:
- ensure_wifi()
- tm = utime.localtime(utime.time()+TZ_OFFSET)
- hh,mm,ss = tm[3],tm[4],tm[5]
- update_dynamic(epd, hh, mm, ss, y0, margin, th_main, th_beat)
- epd.display()
- utime.sleep(60 - ss)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement