Skip to content

Commit 35e2d1a

Browse files
committed
first ConfigUtils version published in pio lib
1 parent 6134ce2 commit 35e2d1a

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# ConfigUtils
22
A tiny helper library to wrap dependencies and provide a one liner to load json config files
3+
4+
# Code example
5+
```c++
6+
#include <Arduino.h>
7+
#include <ArduinoJson.h>
8+
9+
#include <ConfigUtils.h>
10+
11+
DynamicJsonDocument config(5*1024);//5 KB
12+
13+
void setup() {
14+
15+
Serial.begin(115200);
16+
17+
//required 'data' folder with 'config.json'
18+
//flash the file system with the platformio command :
19+
//>pio run -t uploadfs
20+
load_json(config,"/config.json");
21+
//now you can configure any component with json
22+
//below just a pretty print
23+
serializeJsonPretty(config, Serial);
24+
}
25+
26+
void loop() {
27+
delay(10000);
28+
}
29+
```

examples/main.ino

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <Arduino.h>
2+
#include <ArduinoJson.h>
3+
4+
#include <ConfigUtils.h>
5+
6+
DynamicJsonDocument config(5*1024);//5 KB
7+
8+
void setup() {
9+
10+
Serial.begin(115200);
11+
12+
//required 'data' folder with 'config.json'
13+
//flash the file system with the platformio command :
14+
//>pio run -t uploadfs
15+
load_json(config,"/config.json");
16+
//now you can configure any component with json
17+
//below just a pretty print
18+
serializeJsonPretty(config, Serial);
19+
}
20+
21+
void loop() {
22+
delay(10000);
23+
}

include/ConfigUtils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <ArduinoJson.h>
2+
3+
bool load_config(DynamicJsonDocument &config,bool verbose=false);
4+
bool save_config(DynamicJsonDocument &config);
5+
6+
bool load_json(DynamicJsonDocument &config,const char* FileName,bool verbose=false);
7+
bool save_json(DynamicJsonDocument &config,const char* FileName);
8+
9+
void timelog(String Text);

library.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "ConfigUtils",
3+
"version" : "1.0.0",
4+
"description" : "A tiny helper library to wrap dependencies and provide a one liner to load json config files",
5+
"keywords" : "json, Arduinojson, spiffs, config",
6+
"license" : "MIT",
7+
"repository":{
8+
"type" : "git",
9+
"url" : "https://github.com/ESP32Home/ConfigUtils.git"
10+
},
11+
"authors":{
12+
"name" : "wassfila",
13+
"url": "https://github.com/wassfila"
14+
},
15+
"dependencies":{
16+
},
17+
"frameworks" : "arduino",
18+
"platforms" : "espressif32"
19+
}

src/ConfigUtils.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "ConfigUtils.h"
2+
#include <FS.h>
3+
#include <SPIFFS.h>
4+
5+
#define FORMAT_SPIFFS_IF_FAILED true
6+
static bool ready = false;
7+
8+
bool spiffs_init(){
9+
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
10+
Serial.println("SPIFFS Mount Failed");
11+
return false;
12+
}
13+
ready = true;
14+
return true;
15+
}
16+
17+
bool load_json(DynamicJsonDocument &config,const char* FileName,bool verbose){
18+
if(!ready){
19+
if(!spiffs_init()){
20+
return false;
21+
}
22+
}
23+
File file = SPIFFS.open(FileName,"r");
24+
25+
DeserializationError error = deserializeJson(config, file);
26+
if (error){
27+
Serial.println(F("Failed to read json file"));
28+
file.close();
29+
return false;
30+
}else{
31+
if(verbose){
32+
Serial.println();
33+
Serial.println("Loaded json :");
34+
serializeJsonPretty(config, Serial);
35+
Serial.println();
36+
}
37+
file.close();
38+
return true;
39+
}
40+
}
41+
42+
bool save_json(DynamicJsonDocument &config,const char* FileName){
43+
if(!ready){
44+
if(!spiffs_init()){
45+
return false;
46+
}
47+
}
48+
File file = SPIFFS.open(FileName,"w");
49+
file.close();
50+
return true;
51+
}
52+
53+
bool load_config(DynamicJsonDocument &config,bool verbose){
54+
bool result = load_json(config,"/config.json");
55+
if(verbose){
56+
if(result){
57+
Serial.println();
58+
Serial.println("Loaded configuration :");
59+
serializeJsonPretty(config, Serial);
60+
Serial.println();
61+
}else{
62+
Serial.println("Failed to load configuration");
63+
}
64+
}
65+
return result;
66+
}
67+
68+
bool save_config(DynamicJsonDocument &config){
69+
return save_json(config,"/config.json");
70+
}
71+
72+
void timelog(String Text){
73+
Serial.println(String(millis())+" : "+Text);//micros()
74+
}
75+

0 commit comments

Comments
 (0)