From cbc1f161c1fbb58fbdaeb227326a35f934573d5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Rasmussen?= Date: Sat, 15 Jun 2024 19:30:52 +0200 Subject: [PATCH] Handle LCD errors gracefully --- cmd/fermentord/loop.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/cmd/fermentord/loop.go b/cmd/fermentord/loop.go index a7155ec..821fd73 100644 --- a/cmd/fermentord/loop.go +++ b/cmd/fermentord/loop.go @@ -27,12 +27,15 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream) { config := configuration.Global() // Display + var hasDisplay bool display, err := lcd.NewLCD(config.I2C.Bus) if err != nil { hub.CaptureException(err) - log.Fatal(err) + log.Printf("Error initializing LCD: %v", err) + } else { + hasDisplay = true + defer display.Close() } - defer display.Close() // Controller ctrl := controllers.NewChamberController(config) @@ -60,8 +63,12 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream) { } defer gpio.Close() - wg.Add(5) - go display.Run(ctx, wg) + if hasDisplay { + wg.Add(1) + go display.Run(ctx, wg) + } + + wg.Add(4) go ctrl.Run(ctx, wg) go ingest.Run(ctx, wg) go temperature.PollSensors(ctx, wg, 1*time.Second, config.Sensors.Weight) @@ -77,7 +84,9 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream) { case reading := <-temperature.Reading: ctrl.SetTemperature(reading) ingest.AddReading(reading) - display.SetTemperature(reading) + if hasDisplay { + display.SetTemperature(reading) + } case <-temperature.RequestReset: log.Print("Powering down one wire bus for 20 seconds") @@ -111,7 +120,9 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream) { case state := <-ctrl.C: gpioSetState(state, gpio, config) ingest.AddState(state) - display.SetState(state) + if hasDisplay { + display.SetState(state) + } case c := <-chConfigChange: config = c