[Issue #2] - adding code to verify a display is connected before attempting to use it
This commit is contained in:
@@ -308,17 +308,30 @@ func main() {
|
|||||||
panicErr("I2C configuration", err)
|
panicErr("I2C configuration", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize OLED (shares the same I2C bus at 0x3C)
|
// Instantiate nil oled variable
|
||||||
oled := ssd1306.NewI2C(i2c)
|
var oled *ssd1306.Device
|
||||||
oled.Configure(ssd1306.Config{
|
// White color for display
|
||||||
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}
|
white := color.RGBA{R: 255, G: 255, B: 255, A: 255}
|
||||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 16, "Booting...", white)
|
|
||||||
oled.Display()
|
// Probe address 0x3C with a zero-byte or 1-byte read
|
||||||
|
var dummy [1]byte
|
||||||
|
if err := i2c.ReadRegister(0x3C, 0x00, dummy[:]); err == nil {
|
||||||
|
println("SSD1306 detected at 0x3C")
|
||||||
|
dev := ssd1306.NewI2C(i2c)
|
||||||
|
dev.Configure(ssd1306.Config{
|
||||||
|
Address: 0x3C,
|
||||||
|
Width: 128,
|
||||||
|
Height: 64,
|
||||||
|
})
|
||||||
|
oled = dev
|
||||||
|
|
||||||
|
// Run some initial clearing and add booting text to the screen
|
||||||
|
oled.ClearBuffer()
|
||||||
|
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 16, "Booting...", white)
|
||||||
|
oled.Display()
|
||||||
|
} else {
|
||||||
|
println("SSD1306 not found — running headless")
|
||||||
|
}
|
||||||
|
|
||||||
bme, err := NewBME280(i2c, BME280Addr)
|
bme, err := NewBME280(i2c, BME280Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -364,12 +377,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render to OLED
|
// Render to OLED
|
||||||
oled.ClearBuffer()
|
if oled != nil {
|
||||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 14, fmt.Sprintf("Node: %s", NodeName), white)
|
oled.ClearBuffer()
|
||||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 30, fmt.Sprintf("Temp: %.1f C", t), white)
|
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 14, fmt.Sprintf("Node: %s", NodeName), white)
|
||||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 46, fmt.Sprintf("Hum: %.1f %%", h), white)
|
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 30, fmt.Sprintf("Temp: %.1f C", t), white)
|
||||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 60, fmt.Sprintf("Pres: %.1f hPa", p), white)
|
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 46, fmt.Sprintf("Hum: %.1f %%", h), white)
|
||||||
oled.Display()
|
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 60, fmt.Sprintf("Pres: %.1f hPa", p), white)
|
||||||
|
oled.Display()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(UpdateInterval)
|
time.Sleep(UpdateInterval)
|
||||||
|
|||||||
Reference in New Issue
Block a user