package main import ( "fmt" "image/color" "machine" "sync" "time" "tinygo.org/x/drivers/ssd1306" "tinygo.org/x/tinyfont" "tinygo.org/x/tinyfont/freemono" "tinygo.org/x/tinyfont/proggy" ) var ( white = color.RGBA{R: 255, G: 255, B: 255, A: 255} black = color.RGBA{R: 0, G: 0, B: 0, A: 255} mu sync.Mutex currentTime time.Time ) func main() { // Configure I2C0 with your pins: GP16 (SDA) and GP17 (SCL) machine.I2C0.Configure(machine.I2CConfig{ Frequency: 400 * machine.KHz, SDA: machine.GP16, SCL: machine.GP17, }) display := ssd1306.NewI2C(machine.I2C0) display.Configure(ssd1306.Config{ Address: 0x3C, Width: 128, Height: 64, }) // 1. Splash / Connection screen display.ClearBuffer() tinyfont.WriteLine(display, &proggy.TinySZ8pt7b, 8, 32, "Connecting Wi-Fi...", white) display.Display() // 2. Initialize Wi-Fi hardware and lneto stack netStack, err := InitNetwork(WifiSSID, WifiPass) if err != nil { display.ClearBuffer() tinyfont.WriteLine(display, &proggy.TinySZ8pt7b, 8, 32, "Wi-Fi Error!", white) display.Display() println("Wi-Fi init error:", err.Error()) for {} } // 3. NTP Fetch Status display.ClearBuffer() tinyfont.WriteLine(display, &proggy.TinySZ8pt7b, 8, 32, "Syncing NTP...", white) display.Display() syncedTime, err := netStack.FetchNTP(NtpServer) if err != nil { println("Initial NTP sync error:", err.Error()) currentTime = time.Date(2026, time.September, 7, 12, 0, 0, 0, time.UTC) } else { currentTime = syncedTime } // 4. Background NTP re-sync routine (every 1 hour) go func() { resyncTicker := time.NewTicker(1 * time.Hour) for range resyncTicker.C { if t, err := netStack.FetchNTP(NtpServer); err == nil { mu.Lock() currentTime = t mu.Unlock() } } }() // 5. 1Hz UI rendering loop ticker := time.NewTicker(1 * time.Second) for range ticker.C { mu.Lock() currentTime = currentTime.Add(1 * time.Second) now := currentTime mu.Unlock() // Optional: Apply timezone offset if needed (e.g. now = now.Add(-4 * time.Hour)) timeStr := fmt.Sprintf("%02d:%02d:%02d", now.Hour(), now.Minute(), now.Second()) dateStr := now.Format("Mon, Jan 02 2006") display.ClearBuffer() // Top: Date with Proggy tinyfont.WriteLine(display, &proggy.TinySZ8pt7b, 16, 14, dateStr, white) // Center: Time with Freemono Bold 12pt tinyfont.WriteLine(display, &freemono.Bold12pt7b, 6, 44, timeStr, white) display.Display() } }