From f69678f01f25372b9616d1f3c4c8a83e8e7b654b Mon Sep 17 00:00:00 2001 From: gerdlanger <34196043+gerdlanger@users.noreply.github.com> Date: Sun, 6 Oct 2019 19:47:02 +0200 Subject: [PATCH 1/5] Update library.properties --- library.properties | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library.properties b/library.properties index f6a6231..fbeaa28 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=ConfigTool version=1.0 author=Tvde1 -maintainer=Tvde1 +maintainer=gerdlanger sentence=Save config variable and edit them online. paragraph=No more hardcoding. This library will save and load config variables and you can edit them at an endpoint you choose. category=Data Storage -url=https://github.com/Tvde1/ConfigTool -includes=ConfigTool.h \ No newline at end of file +url=https://github.com/gerdlanger/ConfigTool +includes=ConfigTool.h From f6404358f7ac123efb3deaa1b1b19caceab9c0cf Mon Sep 17 00:00:00 2001 From: Gerd Langer Date: Sun, 6 Oct 2019 23:05:25 +0200 Subject: [PATCH 2/5] fix save values using byRef parameters --- .gitignore | 5 ++++- src/ConfigTool.cpp | 11 +++++------ src/ConfigTool.h | 37 ++++++++++++++++++------------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 3c4efe2..ff8bc3f 100644 --- a/.gitignore +++ b/.gitignore @@ -258,4 +258,7 @@ paket-files/ # Python Tools for Visual Studio (PTVS) __pycache__/ -*.pyc \ No newline at end of file +*.pyc + + +*.cpp__ diff --git a/src/ConfigTool.cpp b/src/ConfigTool.cpp index 0de9f3f..1e697ed 100644 --- a/src/ConfigTool.cpp +++ b/src/ConfigTool.cpp @@ -5,9 +5,7 @@ #include "ConfigTool.h" #include -#include #include -#include void ConfigTool::load() { if (!SPIFFS.begin(true)) { @@ -21,7 +19,7 @@ void ConfigTool::load() { DynamicJsonDocument root(ConfigSize); deserializeJson(root, f.readStringUntil('\n')); for (auto item : variables_) { - item.second->deserialize(root); + item.second->deserialize(&root); } f.close(); @@ -31,12 +29,13 @@ void ConfigTool::save() { DynamicJsonDocument root(ConfigSize); for (auto item : variables_) { - item.second->serialize(root); + item.second->serialize(&root); } - - SPIFFS.begin(); + + SPIFFS.begin(true); File f = SPIFFS.open("/config.json", "w"); serializeJson(root, f); + f.close(); } void ConfigTool::reset() { diff --git a/src/ConfigTool.h b/src/ConfigTool.h index a2f1ac7..c616c1a 100644 --- a/src/ConfigTool.h +++ b/src/ConfigTool.h @@ -5,15 +5,14 @@ #ifndef _ConfigTool_h #define _ConfigTool_h -#include #include #include struct BaseVar { String name; - virtual void serialize(JsonDocument) = 0; - virtual void deserialize(JsonDocument) = 0; + virtual void serialize(JsonDocument*) = 0; + virtual void deserialize(JsonDocument*) = 0; virtual void reset() = 0; virtual String toString() = 0; virtual void fromString(String) = 0; @@ -23,9 +22,9 @@ template struct ConfigVar : BaseVar { ConfigVar(String n, T* p) {}; - void deserialize(JsonDocument json) {}; + void deserialize(JsonDocument* json) {}; - void serialize(JsonDocument json) {}; + void serialize(JsonDocument* json) {}; void reset() {}; @@ -44,12 +43,12 @@ struct ConfigVar : BaseVar { defaultValue = *p; }; - void deserialize(JsonDocument json) { - *pointer = String{ json[name] | defaultValue }; + void deserialize(JsonDocument* json) { + *pointer = String{ (*json)[name] | defaultValue }; } - void serialize(JsonDocument json) { - json[name] = *pointer; + void serialize(JsonDocument* json) { + (*json)[name] = *pointer; } void reset() { @@ -75,14 +74,14 @@ struct ConfigVar : BaseVar { defaultValue = *p; }; - void deserialize(JsonDocument json) { - if (!json[name].isNull()) { - *pointer = json[name]; + void deserialize(JsonDocument* json) { + if (!(*json)[name].isNull()) { + *pointer = (*json)[name]; } } - void serialize(JsonDocument json) { - json[name] = *pointer; + void serialize(JsonDocument* json) { + (*json)[name] = *pointer; } void reset() { @@ -108,14 +107,14 @@ struct ConfigVar : BaseVar { defaultValue = *p; }; - void deserialize(JsonDocument json) { - if (!json[name].isNull()) { - *pointer = json[name]; + void deserialize(JsonDocument* json) { + if (!(*json)[name].isNull()) { + *pointer = (*json)[name]; } } - void serialize(JsonDocument json) { - json[name] = *pointer; + void serialize(JsonDocument* json) { + (*json)[name] = *pointer; } void reset() { From cabcd3b6eac26f920fa82824b273150068988260 Mon Sep 17 00:00:00 2001 From: Gerd Langer Date: Mon, 7 Oct 2019 00:52:50 +0200 Subject: [PATCH 3/5] added small test --- .gitignore | 2 +- test/ConfigToolTest/ConfigToolTest.ino | 110 +++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 test/ConfigToolTest/ConfigToolTest.ino diff --git a/.gitignore b/.gitignore index ff8bc3f..aa42daa 100644 --- a/.gitignore +++ b/.gitignore @@ -261,4 +261,4 @@ __pycache__/ *.pyc -*.cpp__ +tmp/* diff --git a/test/ConfigToolTest/ConfigToolTest.ino b/test/ConfigToolTest/ConfigToolTest.ino new file mode 100644 index 0000000..4efb564 --- /dev/null +++ b/test/ConfigToolTest/ConfigToolTest.ino @@ -0,0 +1,110 @@ +/** + * Test functionality of ConfigTool. + * + * TAKE CARE !!! This DELETES exisintg "/config.json" files !!! + * + */ + + +#include +#include + +String config_String_1 = "Default"; +String config_String_2 = "Test"; +int config_int_1 = 100; +int config_int_2 = 200; +bool config_bool_F = false; +bool config_bool_T = true; + +ConfigTool configTool; + +bool dumpConfig(bool check){ + + File file = SPIFFS.open("/config.json"); + if(!file){ + Serial.println("reading - config failed"); + return false; + } + + Serial.println("reading - dumpfile:"); + while(file.available()){ + Serial.write(file.read()); + } + Serial.println(); + // if requested, print the comparison for "zeroVariables": + if (check) Serial.println("{\"String1\":\"-1-\",\"String2\":\"-2-\",\"bool__F\":false,\"bool__T\":false,\"int___1\":0,\"int___2\":0}"); + + return true; +} + +void initVariables() { + configTool.addVariable("String1", &config_String_1); + configTool.addVariable("String2", &config_String_2); + configTool.addVariable("int___1", &config_int_1); + configTool.addVariable("int___2", &config_int_2); + configTool.addVariable("bool__F", &config_bool_F); + configTool.addVariable("bool__T", &config_bool_T); +} + +void zeroVariables() { + config_String_1 = "-1-"; + config_String_2 = "-2-"; + config_int_1 = 0; + config_int_2 = 0; + config_bool_F = false; + config_bool_T = false; +} + +void checkDefaultValues() { + Serial.printf("String1 OK=%d\n", (config_String_1 == "Default")); + Serial.printf("String2 OK=%d\n", (config_String_2 == "Test")); + Serial.printf("int1 OK=%d\n", (config_int_1 == 100)); + Serial.printf("int2 OK=%d\n", (config_int_2 == 200)); + Serial.printf("bool_F OK=%d\n", (config_bool_F == false)); + Serial.printf("bool_T OK=%d\n", (config_bool_T == true)); +} + +void setup(){ + Serial.begin(115200); + Serial.println("Setup begin"); + + if(!SPIFFS.begin(false)){ + Serial.println("setup - SPIFFS begin failed"); + return; + } + + Serial.println("setup - dump at start"); + dumpConfig(true); + + Serial.println("setup - init variables to default and save"); + initVariables(); + configTool.save(); + dumpConfig(false); + + Serial.println("setup - manipulate variables and load again"); + zeroVariables(); + configTool.load(); + checkDefaultValues(); + + Serial.println("setup - manipulate variables and save"); + zeroVariables(); + configTool.save(); + dumpConfig(true); + + Serial.println("setup - reset stored values"); + configTool.reset(); + dumpConfig(false); + checkDefaultValues(); + + Serial.println("setup - again manipulate variables and save"); + zeroVariables(); + configTool.save(); + dumpConfig(true); + + Serial.println( "Setup end" ); + Serial.println( "Test complete" ); +} + +void loop(){ + // NOP +} From 9d53ea68d3babcdc6f1932633a3e9a707b624e04 Mon Sep 17 00:00:00 2001 From: gerdlanger <34196043+gerdlanger@users.noreply.github.com> Date: Mon, 7 Oct 2019 00:58:48 +0200 Subject: [PATCH 4/5] v1.0.1 prep in properties --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index fbeaa28..15242bc 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ConfigTool -version=1.0 +version=1.0.1 author=Tvde1 maintainer=gerdlanger sentence=Save config variable and edit them online. From a8cd787347764ceedab4493ac062441639805485 Mon Sep 17 00:00:00 2001 From: gerdlanger <34196043+gerdlanger@users.noreply.github.com> Date: Mon, 7 Oct 2019 01:04:55 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5e701ef..6c44c33 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # ConfigTool -ESP32 and ESP8266 Arduino library for easy saving and storing config variables. -Also has a handler to edit them via a webserver (ESP8266 Only). +ESP32 Arduino library for easy saving and storing config variables. + +TODO (port this to ESP32): Also has a handler to edit them via a webserver (ESP8266 Only). See [this example](https://github.com/Tvde1/ConfigTool/blob/master/examples/ConfigTool/ConfigTool.ino).