-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first ConfigUtils version published in pio lib
- Loading branch information
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <Arduino.h> | ||
#include <ArduinoJson.h> | ||
|
||
#include <ConfigUtils.h> | ||
|
||
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); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <Arduino.h> | ||
#include <ArduinoJson.h> | ||
|
||
#include <ConfigUtils.h> | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <ArduinoJson.h> | ||
|
||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "ConfigUtils.h" | ||
#include <FS.h> | ||
#include <SPIFFS.h> | ||
|
||
#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() | ||
} | ||
|