61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"image/color"
|
|
"machine"
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers/ssd1306"
|
|
"tinygo.org/x/tinyfont"
|
|
"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}
|
|
)
|
|
|
|
func main() {
|
|
// Configure I2C0 using GP4 (SDA) and GP5 (SCL) at 400kHz
|
|
machine.I2C0.Configure(machine.I2CConfig{
|
|
Frequency: 400 * machine.KHz,
|
|
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,
|
|
Width: 128,
|
|
Height: 64,
|
|
})
|
|
|
|
display.ClearDisplay()
|
|
|
|
// 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)
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
|
|
for range ticker.C {
|
|
currentTime = currentTime.Add(1 * time.Second)
|
|
|
|
// Format time and date strings
|
|
timeStr := fmt.Sprintf("%02d:%02d:%02d", currentTime.Hour(), currentTime.Minute(), currentTime.Second())
|
|
dateStr := currentTime.Format("Mon, Jan 02 2006")
|
|
|
|
// Clear screen buffer
|
|
display.ClearBuffer()
|
|
|
|
// Render text to display buffer:
|
|
// WriteLine(target, font, x, y, text, color)
|
|
tinyfont.WriteLine(display, &proggy.TinySZ8pt7b, 16, 14, dateStr, white)
|
|
tinyfont.WriteLine(display, &freemono.Bold12pt7b, 6, 44, timeStr, white)
|
|
|
|
// Push frame buffer to the OLED panel
|
|
display.Display()
|
|
}
|
|
}
|