1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-01 02:21:46 +02:00

Fixed bug in enumitem parsing / string equals. Added unit test for yaml enums. Added some comments.

This commit is contained in:
Stefan de Bruijn
2021-06-06 19:13:39 +02:00
parent 26f40444ad
commit 11433470dd
5 changed files with 64 additions and 4 deletions

View File

@@ -163,6 +163,6 @@ namespace Configuration {
break;
}
}
return e->value;
return e->value; // Terminal value is default.
}
}

View File

@@ -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;

View File

@@ -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

View File

@@ -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';
}

View File

@@ -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));
}
}
}