mirror of
https://github.com/bdring/Grbl_Esp32.git
synced 2025-09-08 05:10:53 +02:00
Made pin parsing more robust. Added some tests
This commit is contained in:
@@ -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.
|
// Initialize pinImplementation first! Callers might want to delete it, and we don't want a random pointer.
|
||||||
pinImplementation = nullptr;
|
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':
|
// Re-use undefined pins happens in 'create':
|
||||||
pinImplementation = new Pins::VoidPinDetail();
|
pinImplementation = new Pins::VoidPinDetail();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the definition: [GPIO].[pinNumber]:[attributes]
|
auto idx = nameStart;
|
||||||
auto nameStart = str.begin();
|
|
||||||
auto idx = nameStart;
|
|
||||||
for (; idx != str.end() && *idx != '.' && *idx != ':'; ++idx) {
|
for (; idx != str.end() && *idx != '.' && *idx != ':'; ++idx) {
|
||||||
*idx = char(::tolower(*idx));
|
*idx = char(::tolower(*idx));
|
||||||
}
|
}
|
||||||
|
|
||||||
String prefix = str.substring(0, int(idx - str.begin()));
|
String prefix = str.substring(0, int(idx - str.begin()));
|
||||||
|
|
||||||
if (idx != str.end()) { // skip '.'
|
if (idx != str.end()) { // skip '.'
|
||||||
@@ -135,6 +140,7 @@ Pin Pin::create(const String& str) {
|
|||||||
#if defined ESP32
|
#if defined ESP32
|
||||||
grbl_sendf(CLIENT_ALL, "Setting up pin failed. Details: %s\r\n", ex.stackTrace.c_str());
|
grbl_sendf(CLIENT_ALL, "Setting up pin failed. Details: %s\r\n", ex.stackTrace.c_str());
|
||||||
#endif
|
#endif
|
||||||
|
(void)ex; // Get rid of compiler warning
|
||||||
|
|
||||||
// RAII safety guard.
|
// RAII safety guard.
|
||||||
if (pinImplementation) {
|
if (pinImplementation) {
|
||||||
|
@@ -248,6 +248,14 @@ namespace Pins {
|
|||||||
Assert(gpio16.name().equals("GPIO.16"), "Name is %s", gpio16.name().c_str());
|
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) {
|
Test(GPIO, ActiveLow) {
|
||||||
GPIONative::initialize();
|
GPIONative::initialize();
|
||||||
PinLookup::ResetAllPins();
|
PinLookup::ResetAllPins();
|
||||||
|
Reference in New Issue
Block a user