[Issue #2] - adding initial support for SSD1306 displays
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
@@ -9,6 +9,11 @@ import (
|
||||
"machine"
|
||||
"strconv"
|
||||
"time"
|
||||
"image/color"
|
||||
|
||||
"tinygo.org/x/drivers/ssd1306"
|
||||
"tinygo.org/x/tinyfont"
|
||||
"tinygo.org/x/tinyfont/proggy"
|
||||
)
|
||||
|
||||
var StateTopic = fmt.Sprintf("homeassistant/sensor/%s/state", NodeID)
|
||||
@@ -292,10 +297,10 @@ func panicErr(msg string, err error) {
|
||||
func main() {
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// Configure I2C (Pin 16 SDA, Pin 17 SCL -> I2C0)
|
||||
// Bump frequency to 400kHz for snappy display flushes
|
||||
i2c := machine.I2C0
|
||||
err := i2c.Configure(machine.I2CConfig{
|
||||
Frequency: 100 * machine.KHz,
|
||||
Frequency: 400 * machine.KHz,
|
||||
SDA: machine.GPIO16,
|
||||
SCL: machine.GPIO17,
|
||||
})
|
||||
@@ -303,6 +308,18 @@ func main() {
|
||||
panicErr("I2C configuration", err)
|
||||
}
|
||||
|
||||
// Initialize OLED (shares the same I2C bus at 0x3C)
|
||||
oled := ssd1306.NewI2C(i2c)
|
||||
oled.Configure(ssd1306.Config{
|
||||
Address: 0x3C, // Check 0x3D if 0x3C doesn't respond
|
||||
Width: 128,
|
||||
Height: 64,
|
||||
})
|
||||
oled.ClearBuffer()
|
||||
white := color.RGBA{R: 255, G: 255, B: 255, A: 255}
|
||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 16, "Booting...", white)
|
||||
oled.Display()
|
||||
|
||||
bme, err := NewBME280(i2c, BME280Addr)
|
||||
if err != nil {
|
||||
panicErr("BME280 init", err)
|
||||
@@ -338,12 +355,21 @@ func main() {
|
||||
if err != nil {
|
||||
println("BME280 read error:", err.Error())
|
||||
} else {
|
||||
// Publish MQTT
|
||||
stateJSON := fmt.Sprintf(`{"temperature":%.2f,"humidity":%.2f,"pressure":%.2f}`, t, h, p)
|
||||
if err := mqtt.Publish(StateTopic, []byte(stateJSON), false); err != nil {
|
||||
println("MQTT publish error:", err.Error())
|
||||
} else {
|
||||
println(fmt.Sprintf("[%s] Published: %s", NodeName, stateJSON))
|
||||
}
|
||||
|
||||
// Render to OLED
|
||||
oled.ClearBuffer()
|
||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 14, fmt.Sprintf("Node: %s", NodeName), white)
|
||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 30, fmt.Sprintf("Temp: %.1f C", t), white)
|
||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 46, fmt.Sprintf("Hum: %.1f %%", h), white)
|
||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 60, fmt.Sprintf("Pres: %.1f hPa", p), white)
|
||||
oled.Display()
|
||||
}
|
||||
|
||||
time.Sleep(UpdateInterval)
|
||||
|
||||
Reference in New Issue
Block a user