diff --git a/Grbl_Esp32/src/Configuration/Parser.cpp b/Grbl_Esp32/src/Configuration/Parser.cpp index 29546abf..30f944fb 100644 --- a/Grbl_Esp32/src/Configuration/Parser.cpp +++ b/Grbl_Esp32/src/Configuration/Parser.cpp @@ -163,6 +163,6 @@ namespace Configuration { break; } } - return e->value; + return e->value; // Terminal value is default. } } diff --git a/Grbl_Esp32/src/EnumItem.h b/Grbl_Esp32/src/EnumItem.h index 242dc84b..00a71e4a 100644 --- a/Grbl_Esp32/src/EnumItem.h +++ b/Grbl_Esp32/src/EnumItem.h @@ -1,7 +1,19 @@ #pragma once +// Usage: +// +// EnumItem stepTypes[] = { +// { ST_TIMED, "Timed" }, { ST_RMT, "RMT" }, { ST_I2S_STATIC, "I2S_static" }, { ST_I2S_STREAM, "I2S_stream" }, EnumItem(ST_RMT) +// }; + struct EnumItem { - EnumItem() : value(0), name(nullptr) {} + // Used for brace initialization + EnumItem() {} + + // Set enumItem with a default value as last item in the EnumItem array. This is the terminator. + EnumItem(int defaultValue) : value(defaultValue), name(nullptr) {} + + // Other items are here. EnumItem(int val, const char* n) : value(val), name(n) {} int value; diff --git a/Grbl_Esp32/src/Stepper.cpp b/Grbl_Esp32/src/Stepper.cpp index a88bdf34..239d9714 100644 --- a/Grbl_Esp32/src/Stepper.cpp +++ b/Grbl_Esp32/src/Stepper.cpp @@ -127,7 +127,7 @@ typedef struct { static st_prep_t prep; EnumItem stepTypes[] = { - { ST_TIMED, "Timed" }, { ST_RMT, "RMT" }, { ST_I2S_STATIC, "I2S_static" }, { ST_I2S_STREAM, "I2S_stream" }, EnumItem() + { ST_TIMED, "Timed" }, { ST_RMT, "RMT" }, { ST_I2S_STATIC, "I2S_static" }, { ST_I2S_STREAM, "I2S_stream" }, EnumItem(ST_RMT) }; /* "The Stepper Driver Interrupt" - This timer interrupt is the workhorse of Grbl. Grbl employs diff --git a/Grbl_Esp32/src/StringRange.h b/Grbl_Esp32/src/StringRange.h index 00c1578a..06029e45 100644 --- a/Grbl_Esp32/src/StringRange.h +++ b/Grbl_Esp32/src/StringRange.h @@ -67,7 +67,7 @@ public: bool equals(const char* o) const { const char* c = start_; const char* oc = o; - for (; *c != '\0' && *oc != '\0' && tolower(*c) == tolower(*oc); ++c, ++oc) {} + for (; c != end_ && *oc != '\0' && tolower(*c) == tolower(*oc); ++c, ++oc) {} return c == end_ && *oc == '\0'; } diff --git a/Grbl_Esp32/test/Configuration/YamlTreeBuilder.cpp b/Grbl_Esp32/test/Configuration/YamlTreeBuilder.cpp index fd0731a1..7a120e9b 100644 --- a/Grbl_Esp32/test/Configuration/YamlTreeBuilder.cpp +++ b/Grbl_Esp32/test/Configuration/YamlTreeBuilder.cpp @@ -34,6 +34,25 @@ namespace Configuration { } }; + enum stepper_id_t { + ST_TIMED = 0, + ST_RMT, + ST_I2S_STREAM, + ST_I2S_STATIC, + }; + + EnumItem stepTypes[] = { + { ST_TIMED, "Timed" }, { ST_RMT, "RMT" }, { ST_I2S_STATIC, "I2S_static" }, { ST_I2S_STREAM, "I2S_stream" }, EnumItem(ST_RMT) + }; + + class TestBasicEnum : public Configurable { + public: + int value; + + void validate() const {} + void handle(HandlerBase& handler) { handler.handle("type", value, stepTypes); } + }; + class TestHierarchical : public Configurable { public: TestBasic* n1 = nullptr; @@ -196,4 +215,33 @@ namespace Configuration { } Assert(test.foo == 2); } + + // ST_TIMED, "Timed" }, { ST_RMT, "RMT" }, { ST_I2S_STATIC, "I2S_static" }, { ST_I2S_STREAM, "I2S_stream" }, EnumItem() + Test(YamlTreeBuilder, Enum1) { + { + const char* config = "type: Timed\n"; + TestBasicEnum test; + Helper::Parse(config, test); + Assert(test.value == int(ST_TIMED)); + } + + { + const char* config = "type: RMT\n"; + TestBasicEnum test; + Helper::Parse(config, test); + Assert(test.value == int(ST_RMT)); + } + { + const char* config = "type: I2S_static\n"; + TestBasicEnum test; + Helper::Parse(config, test); + Assert(test.value == int(ST_I2S_STATIC)); + } + { + const char* config = "type: I2S_stream\n"; + TestBasicEnum test; + Helper::Parse(config, test); + Assert(test.value == int(ST_I2S_STREAM)); + } + } }