[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)
|
||||
}
|
||||
|
||||
// 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
|
||||
// Instantiate nil oled variable
|
||||
var oled *ssd1306.Device
|
||||
// White color for display
|
||||
white := color.RGBA{R: 255, G: 255, B: 255, A: 255}
|
||||
|
||||
// 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()
|
||||
white := color.RGBA{R: 255, G: 255, B: 255, A: 255}
|
||||
tinyfont.WriteLine(oled, &proggy.TinySZ8pt7b, 0, 16, "Booting...", white)
|
||||
oled.Display()
|
||||
} else {
|
||||
println("SSD1306 not found — running headless")
|
||||
}
|
||||
|
||||
bme, err := NewBME280(i2c, BME280Addr)
|
||||
if err != nil {
|
||||
@@ -364,6 +377,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Render to OLED
|
||||
if oled != nil {
|
||||
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)
|
||||
@@ -371,6 +385,7 @@ func main() {
|
||||
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