From 4b769ff862fe6aa5b1a0582750250d0cc950201e Mon Sep 17 00:00:00 2001 From: Derek Smithson Date: Tue, 11 Apr 2023 21:49:45 -0700 Subject: [PATCH] fix: catching display init error when not available --- .gitignore | 2 ++ .../Models/DisplayRepository.cs | 36 +++++++++++++------ .../Models/RelayRepository.cs | 24 ++++++++++--- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 0969583..0d55e56 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs +.vscode/ + # Build results [Dd]ebug/ [Dd]ebugPublic/ diff --git a/src/Linux/SpyderTallyControllerLinux/SpyderTallyControllerWebApp/Models/DisplayRepository.cs b/src/Linux/SpyderTallyControllerLinux/SpyderTallyControllerWebApp/Models/DisplayRepository.cs index 6e05104..3954026 100644 --- a/src/Linux/SpyderTallyControllerLinux/SpyderTallyControllerWebApp/Models/DisplayRepository.cs +++ b/src/Linux/SpyderTallyControllerLinux/SpyderTallyControllerWebApp/Models/DisplayRepository.cs @@ -32,20 +32,31 @@ public class DisplayRepository : IDisplayRepository public DisplayRepository(IRelayRepository relayRepository) { + try + { + //Initialize our display + i2c = I2cDevice.Create(new I2cConnectionSettings(1, 0x27)); + driver = new Pcf8574(i2c); + lcd = new Lcd1602(registerSelectPin: 0, + enablePin: 2, + dataPins: new int[] { 4, 5, 6, 7 }, + backlightPin: 3, + backlightBrightness: 0.1f, + readWritePin: 1, + controller: new GpioController(PinNumberingScheme.Logical, driver)); + } + catch(Exception ex) + { + i2c = null; + driver = null; + lcd = null; + Console.WriteLine($"{ex.GetType().Name} occurred while trying to open display: {ex.Message}. Display will be unavailable"); + return; + } + this.relayRepository = relayRepository; this.relayRepository.RelayStatusChanged += RelayRepository_RelayStatusChanged; - //Initialize our display - i2c = I2cDevice.Create(new I2cConnectionSettings(1, 0x27)); - driver = new Pcf8574(i2c); - lcd = new Lcd1602(registerSelectPin: 0, - enablePin: 2, - dataPins: new int[] { 4, 5, 6, 7 }, - backlightPin: 3, - backlightBrightness: 0.1f, - readWritePin: 1, - controller: new GpioController(PinNumberingScheme.Logical, driver)); - //Create filled 0 charcater byte[] filledCircle = new byte[] { @@ -119,6 +130,9 @@ public void SetText(string line1, string line2) public void UpdateDisplay(bool blinkCursorOnly = false) { + if(lcd == null) + return; + lock (displayLock) { if (!blinkCursorOnly) diff --git a/src/Linux/SpyderTallyControllerLinux/SpyderTallyControllerWebApp/Models/RelayRepository.cs b/src/Linux/SpyderTallyControllerLinux/SpyderTallyControllerWebApp/Models/RelayRepository.cs index 9ca8881..d2af692 100644 --- a/src/Linux/SpyderTallyControllerLinux/SpyderTallyControllerWebApp/Models/RelayRepository.cs +++ b/src/Linux/SpyderTallyControllerLinux/SpyderTallyControllerWebApp/Models/RelayRepository.cs @@ -20,11 +20,27 @@ public RelayRepository(IConfigurationRepository configurationRepository) relayStatus = new bool[deviceConfiguration.TallyCount]; - //Initialize GPIO pins - gpioController = new GpioController(); - foreach(var gpioPin in deviceConfiguration.TallyGpioPinAssignments) + // Initialize GPIO pins + try { - gpioController.OpenPin(gpioPin.Value, PinMode.Output, PinValue.Low); + gpioController = new GpioController(); + foreach (var gpioPin in deviceConfiguration.TallyGpioPinAssignments) + { + try + { + gpioController.OpenPin(gpioPin.Value, PinMode.Output, PinValue.Low); + } + catch (Exception ex) + { + Console.WriteLine($"Failed to open and configure GPIO pin {gpioPin.Value}: {ex.Message}"); + // Handle the situation when a specific GPIO pin fails to open, e.g., continue without it + } + } + } + catch (Exception ex) + { + Console.WriteLine($"Failed to initialize GpioController: {ex.Message}"); + // Handle the situation when GpioController fails to initialize, e.g., continue without GPIO control } }