From 8858c71087e66391bd9f423cd7ab4b151be744fd 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 | 2 +- internal/lcd/hd44780.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/fermentord/loop.go b/cmd/fermentord/loop.go index a7155ec..64609bf 100644 --- a/cmd/fermentord/loop.go +++ b/cmd/fermentord/loop.go @@ -30,7 +30,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream) { display, err := lcd.NewLCD(config.I2C.Bus) if err != nil { hub.CaptureException(err) - log.Fatal(err) + log.Printf("Error initializing LCD: %v", err) } defer display.Close() diff --git a/internal/lcd/hd44780.go b/internal/lcd/hd44780.go index b426cae..1b5e05b 100644 --- a/internal/lcd/hd44780.go +++ b/internal/lcd/hd44780.go @@ -26,6 +26,7 @@ type LCD struct { chSetpoint chan float64 lastUpdate time.Time hub *sentry.Hub + isOpen bool } func NewLCD(bus int) (*LCD, error) { @@ -68,6 +69,8 @@ func NewLCD(bus int) (*LCD, error) { return nil, err } + p.isOpen = true + return p, nil } @@ -175,6 +178,10 @@ func (p *LCD) Run(ctx context.Context, wg *sync.WaitGroup) { } func (p *LCD) update() error { + if !p.isOpen { + return nil + } + if time.Since(p.lastUpdate) < 3*time.Second { return nil }