import machine import utime import network import ntptime spi = machine.SPI(1, baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(10), mosi=machine.Pin(11), miso=machine.Pin(8) ) latch = machine.Pin(9, machine.Pin.OUT) digit_bits = [8, 9, 10, 11] WIFI_SSID = "SSID" WIFI_PASSWORD = "PASS" CHAR_MAP = { ' ': 0b00000000, '0': 0b00111111, '1': 0b00000110, '2': 0b01011011, '3': 0b01001111, '4': 0b01100110, '5': 0b01101101, '6': 0b01111101, '7': 0b00000111, '8': 0b01111111, '9': 0b01101111, 'A': 0b01110111, 'B': 0b01111100, 'C': 0b00111001, 'D': 0b01011110, 'E': 0b01111001, 'F': 0b01110001, 'G': 0b01111011, 'H': 0b01110110, 'I': 0b00110000, 'L': 0b00111000, 'O': 0b00111111, 'P': 0b01110011, 'S': 0b01101101, 'T': 0b01111000, 'U': 0b00111110, '-': 0b01000000, '_': 0b00001000 } def write16(value): hb = (value >> 8) & 0xFF lb = value & 0xFF latch.value(0) spi.write(bytes([hb, lb])) latch.value(1) def connect_wifi(): wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): wlan.connect(WIFI_SSID, WIFI_PASSWORD) for _ in range(10): if wlan.isconnected(): break utime.sleep(1) else: print("WiFi connect failed") return False print("Connected:", wlan.ifconfig()) return True def sync_time(): try: ntptime.settime() print("NTP sync done") except: print("NTP sync failed") def get_sydney_time(): rtc = machine.RTC() (year, month, day, weekday, hour, minute, second, subsecs) = rtc.datetime() hour = (hour + 10) % 24 return year, month, day, hour, minute, second def encode_char(c, dot=False): seg = CHAR_MAP.get(c, 0) if dot: seg |= 0b10000000 return seg # Main setup if connect_wifi(): sync_time() else: print("WiFi failed, running without NTP sync") mode = 0 last_mode_switch = utime.ticks_ms() MODE_INTERVAL = 5000 # 5 sec for switching display mode last_ntp_sync = utime.ticks_ms() NTP_SYNC_INTERVAL = 86400000 # 24 hours in ms digit_index = 0 display_chars = [' ', ' ', ' ', ' '] display_dots = [False, False, False, False] while True: now = utime.ticks_ms() # Every 24 hours re-sync NTP time if utime.ticks_diff(now, last_ntp_sync) > NTP_SYNC_INTERVAL: if connect_wifi(): sync_time() last_ntp_sync = now # Switch display mode every 5 seconds if utime.ticks_diff(now, last_mode_switch) >= MODE_INTERVAL: mode = 1 - mode last_mode_switch = now year, month, day, hour, minute, second = get_sydney_time() if mode == 0: h_str = f"{hour:02d}" m_str = f"{minute:02d}" display_chars = [h_str[0], h_str[1], m_str[0], m_str[1]] display_dots = [False, True, False, False] else: d_str = f"{day:02d}" mo_str = f"{month:02d}" display_chars = [d_str[0], d_str[1], mo_str[0], mo_str[1]] display_dots = [False, True, False, False] # Multiplex one digit at a time for smooth display c = display_chars[digit_index] dot = display_dots[digit_index] seg = encode_char(c, dot) digit_select = ~(1 << digit_bits[digit_index]) & 0x0F00 word = seg | digit_select write16(word) digit_index = (digit_index + 1) % 4 utime.sleep_us(2000)