Added networking stack (mostly copied from pico-weather) and added NTP sync
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
2026-09-07 18:01:31 -04:00
parent b9195b33bc
commit 1b03ed6789
5 changed files with 385 additions and 17 deletions
+56 -16
View File
@@ -4,28 +4,31 @@ 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"
"tinygo.org/x/tinyfont/freemono"
)
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 using GP4 (SDA) and GP5 (SCL) at 400kHz
// 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,
SDA: machine.GP16,
SCL: machine.GP17,
})
// Initialize the display (default I2C address for SSD1306 is usually 0x3C)
display := ssd1306.NewI2C(machine.I2C0)
display.Configure(ssd1306.Config{
Address: 0x3C,
@@ -33,28 +36,65 @@ func main() {
Height: 64,
})
display.ClearDisplay()
// 1. Splash / Connection screen
display.ClearBuffer()
tinyfont.WriteLine(display, &proggy.TinySZ8pt7b, 8, 32, "Connecting Wi-Fi...", white)
display.Display()
// Base time for clock tracking (update to current time or set via NTP)
currentTime := time.Date(2026, time.September, 7, 12, 0, 0, 0, time.UTC)
// 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()
// Format time and date strings
timeStr := fmt.Sprintf("%02d:%02d:%02d", currentTime.Hour(), currentTime.Minute(), currentTime.Second())
dateStr := currentTime.Format("Mon, Jan 02 2006")
// 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")
// Clear screen buffer
display.ClearBuffer()
// Render text to display buffer:
// WriteLine(target, font, x, y, text, color)
// 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)
// Push frame buffer to the OLED panel
display.Display()
}
}