From ab7e281c1ca89ebed615b048e1805630cf470ed6 Mon Sep 17 00:00:00 2001 From: morg Date: Mon, 30 Sep 2024 09:51:54 +0200 Subject: [PATCH] Adds a few checks before trying to init a serial device. Closes #2247 --- .../LEDStripController/LEDStripController.h | 4 +- .../LEDStripControllerDetect.cpp | 44 ++++++++++++++++--- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Controllers/LEDStripController/LEDStripController.h b/Controllers/LEDStripController/LEDStripController.h index 2dff0383..8167c47e 100644 --- a/Controllers/LEDStripController/LEDStripController.h +++ b/Controllers/LEDStripController/LEDStripController.h @@ -41,8 +41,8 @@ struct LEDStripDevice { std::string name; std::string port; - unsigned int baud; - unsigned int num_leds; + unsigned int baud = 0; + unsigned int num_leds = 0; led_protocol protocol; }; diff --git a/Controllers/LEDStripController/LEDStripControllerDetect.cpp b/Controllers/LEDStripController/LEDStripControllerDetect.cpp index 213e1fe5..89f2775a 100644 --- a/Controllers/LEDStripController/LEDStripControllerDetect.cpp +++ b/Controllers/LEDStripController/LEDStripControllerDetect.cpp @@ -13,6 +13,7 @@ #include "LEDStripController.h" #include "RGBController_LEDStrip.h" #include "SettingsManager.h" +#include "LogManager.h" /******************************************************************************************\ * * @@ -39,16 +40,17 @@ void DetectLEDStripControllers() { for(unsigned int device_idx = 0; device_idx < ledstrip_settings["devices"].size(); device_idx++) { - /*-------------------------------------------------*\ - | Default to the Keyboard Visualizer protocol | - \*-------------------------------------------------*/ - dev.name = "LED Strip"; - dev.protocol = LED_PROTOCOL_KEYBOARD_VISUALIZER; - if(ledstrip_settings["devices"][device_idx].contains("name")) { dev.name = ledstrip_settings["devices"][device_idx]["name"]; } + else + { + /*-------------------------------------------------*\ + | Default name | + \*-------------------------------------------------*/ + dev.name = "LED Strip"; + } if(ledstrip_settings["devices"][device_idx].contains("port")) { @@ -85,6 +87,36 @@ void DetectLEDStripControllers() { dev.protocol = LED_PROTOCOL_BASIC_I2C; } + else + { + LOG_WARNING("[LEDStripController] '%s' is not a valid value for protocol", protocol_string.c_str()); + return; + } + } + else + { + /*-------------------------------------------------*\ + | Default to the Keyboard Visualizer protocol | + \*-------------------------------------------------*/ + dev.protocol = LED_PROTOCOL_KEYBOARD_VISUALIZER; + } + + if(dev.port.empty()) + { + LOG_WARNING("[LEDStripController] port value cannot be left empty."); + return; + } + + if(dev.baud <= 0) + { + LOG_WARNING("[LEDStripController] baud value cannot be left empty."); + return; + } + + if(dev.num_leds <= 0) + { + LOG_WARNING("[LEDStripController] num_leds value cannot be left empty."); + return; } std::string value = dev.port + "," + std::to_string(dev.baud) + "," + std::to_string(dev.num_leds);