Skip to content

Commit

Permalink
Merge pull request #13 from dsmithson/fixCrashOnNoDisplay
Browse files Browse the repository at this point in the history
fix: catching display init error when not available
  • Loading branch information
dsmithson authored Apr 12, 2023
2 parents 1a11073 + 4b769ff commit fd201b3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

.vscode/

# Build results
[Dd]ebug/
[Dd]ebugPublic/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
{
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit fd201b3

Please sign in to comment.