Skip to content

Commit

Permalink
first ConfigUtils version published in pio lib
Browse files Browse the repository at this point in the history
  • Loading branch information
wassfila committed Sep 6, 2020
1 parent 6134ce2 commit 35e2d1a
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
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);
}
```
23 changes: 23 additions & 0 deletions examples/main.ino
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);
}
9 changes: 9 additions & 0 deletions include/ConfigUtils.h
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);
19 changes: 19 additions & 0 deletions library.json
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"
}
75 changes: 75 additions & 0 deletions src/ConfigUtils.cpp
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()
}

0 comments on commit 35e2d1a

Please sign in to comment.