From 270029fd7521c0a6848ad471b8e3b0aa756018be Mon Sep 17 00:00:00 2001 From: Stefan de Bruijn Date: Mon, 26 Oct 2020 09:23:45 +0100 Subject: [PATCH] Made pin parsing more robust. Added some tests --- Grbl_Esp32/src/Pin.cpp | 14 ++++++++++---- Grbl_Esp32/test/Pins/GPIO.cpp | 8 ++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Grbl_Esp32/src/Pin.cpp b/Grbl_Esp32/src/Pin.cpp index 5dff21f1..d56c2003 100644 --- a/Grbl_Esp32/src/Pin.cpp +++ b/Grbl_Esp32/src/Pin.cpp @@ -18,18 +18,23 @@ bool Pin::parse(String str, Pins::PinDetail*& pinImplementation) { // Initialize pinImplementation first! Callers might want to delete it, and we don't want a random pointer. pinImplementation = nullptr; - if (str == "") { + // Parse the definition: [GPIO].[pinNumber]:[attributes] + + // Skip whitespaces at the start + auto nameStart = str.begin(); + for (; nameStart != str.end() && ::isspace(*nameStart); ++nameStart) {} + + if (nameStart == str.end()) { // Re-use undefined pins happens in 'create': pinImplementation = new Pins::VoidPinDetail(); return true; } - // Parse the definition: [GPIO].[pinNumber]:[attributes] - auto nameStart = str.begin(); - auto idx = nameStart; + auto idx = nameStart; for (; idx != str.end() && *idx != '.' && *idx != ':'; ++idx) { *idx = char(::tolower(*idx)); } + String prefix = str.substring(0, int(idx - str.begin())); if (idx != str.end()) { // skip '.' @@ -135,6 +140,7 @@ Pin Pin::create(const String& str) { #if defined ESP32 grbl_sendf(CLIENT_ALL, "Setting up pin failed. Details: %s\r\n", ex.stackTrace.c_str()); #endif + (void)ex; // Get rid of compiler warning // RAII safety guard. if (pinImplementation) { diff --git a/Grbl_Esp32/test/Pins/GPIO.cpp b/Grbl_Esp32/test/Pins/GPIO.cpp index 54a41600..d784744f 100644 --- a/Grbl_Esp32/test/Pins/GPIO.cpp +++ b/Grbl_Esp32/test/Pins/GPIO.cpp @@ -248,6 +248,14 @@ namespace Pins { Assert(gpio16.name().equals("GPIO.16"), "Name is %s", gpio16.name().c_str()); } + Test(GPIO, NameCaseSensitivity) { + GPIONative::initialize(); + PinLookup::ResetAllPins(); + + Pin gpio16 = Pin::create("GpIo.16"); + Assert(gpio16.name().equals("GPIO.16"), "Name is %s", gpio16.name().c_str()); + } + Test(GPIO, ActiveLow) { GPIONative::initialize(); PinLookup::ResetAllPins();