From 35e2d1a470dafb874959a473610989449a41f7e9 Mon Sep 17 00:00:00 2001 From: wassfila Date: Sun, 6 Sep 2020 20:13:28 +0200 Subject: [PATCH] first ConfigUtils version published in pio lib --- README.md | 27 ++++++++++++++++ examples/main.ino | 23 +++++++++++++ include/ConfigUtils.h | 9 ++++++ library.json | 19 +++++++++++ src/ConfigUtils.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 examples/main.ino create mode 100644 include/ConfigUtils.h create mode 100644 library.json create mode 100644 src/ConfigUtils.cpp diff --git a/README.md b/README.md index d15a97b..fdeb958 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,29 @@ # ConfigUtils A tiny helper library to wrap dependencies and provide a one liner to load json config files + +# Code example +```c++ +#include +#include + +#include + +DynamicJsonDocument config(5*1024);//5 KB + +void setup() { + + Serial.begin(115200); + + //required 'data' folder with 'config.json' + //flash the file system with the platformio command : + //>pio run -t uploadfs + load_json(config,"/config.json"); + //now you can configure any component with json + //below just a pretty print + serializeJsonPretty(config, Serial); +} + +void loop() { + delay(10000); +} +``` \ No newline at end of file diff --git a/examples/main.ino b/examples/main.ino new file mode 100644 index 0000000..4963327 --- /dev/null +++ b/examples/main.ino @@ -0,0 +1,23 @@ +#include +#include + +#include + +DynamicJsonDocument config(5*1024);//5 KB + +void setup() { + + Serial.begin(115200); + + //required 'data' folder with 'config.json' + //flash the file system with the platformio command : + //>pio run -t uploadfs + load_json(config,"/config.json"); + //now you can configure any component with json + //below just a pretty print + serializeJsonPretty(config, Serial); +} + +void loop() { + delay(10000); +} diff --git a/include/ConfigUtils.h b/include/ConfigUtils.h new file mode 100644 index 0000000..ecdbfbd --- /dev/null +++ b/include/ConfigUtils.h @@ -0,0 +1,9 @@ +#include + +bool load_config(DynamicJsonDocument &config,bool verbose=false); +bool save_config(DynamicJsonDocument &config); + +bool load_json(DynamicJsonDocument &config,const char* FileName,bool verbose=false); +bool save_json(DynamicJsonDocument &config,const char* FileName); + +void timelog(String Text); diff --git a/library.json b/library.json new file mode 100644 index 0000000..3b0c4a2 --- /dev/null +++ b/library.json @@ -0,0 +1,19 @@ +{ + "name": "ConfigUtils", + "version" : "1.0.0", + "description" : "A tiny helper library to wrap dependencies and provide a one liner to load json config files", + "keywords" : "json, Arduinojson, spiffs, config", + "license" : "MIT", + "repository":{ + "type" : "git", + "url" : "https://github.com/ESP32Home/ConfigUtils.git" + }, + "authors":{ + "name" : "wassfila", + "url": "https://github.com/wassfila" + }, + "dependencies":{ + }, + "frameworks" : "arduino", + "platforms" : "espressif32" +} diff --git a/src/ConfigUtils.cpp b/src/ConfigUtils.cpp new file mode 100644 index 0000000..a92420c --- /dev/null +++ b/src/ConfigUtils.cpp @@ -0,0 +1,75 @@ +#include "ConfigUtils.h" +#include +#include + +#define FORMAT_SPIFFS_IF_FAILED true +static bool ready = false; + +bool spiffs_init(){ + if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){ + Serial.println("SPIFFS Mount Failed"); + return false; + } + ready = true; + return true; +} + +bool load_json(DynamicJsonDocument &config,const char* FileName,bool verbose){ + if(!ready){ + if(!spiffs_init()){ + return false; + } + } + File file = SPIFFS.open(FileName,"r"); + + DeserializationError error = deserializeJson(config, file); + if (error){ + Serial.println(F("Failed to read json file")); + file.close(); + return false; + }else{ + if(verbose){ + Serial.println(); + Serial.println("Loaded json :"); + serializeJsonPretty(config, Serial); + Serial.println(); + } + file.close(); + return true; + } +} + +bool save_json(DynamicJsonDocument &config,const char* FileName){ + if(!ready){ + if(!spiffs_init()){ + return false; + } + } + File file = SPIFFS.open(FileName,"w"); + file.close(); + return true; +} + +bool load_config(DynamicJsonDocument &config,bool verbose){ + bool result = load_json(config,"/config.json"); + if(verbose){ + if(result){ + Serial.println(); + Serial.println("Loaded configuration :"); + serializeJsonPretty(config, Serial); + Serial.println(); + }else{ + Serial.println("Failed to load configuration"); + } + } + return result; +} + +bool save_config(DynamicJsonDocument &config){ + return save_json(config,"/config.json"); +} + +void timelog(String Text){ + Serial.println(String(millis())+" : "+Text);//micros() +} +