putting output config into json

i could not apply the structure of configSystemOutputs into json, so i had to
switch to use arrays instead. it works, but looks not that nice as a struct.
This commit is contained in:
Marcus 2024-10-24 01:45:06 +02:00
parent 4c9c280d45
commit d68d0fbf79
3 changed files with 94 additions and 48 deletions

View file

@ -151,6 +151,39 @@ void loop() {
if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) { if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) {
Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json and set config.system.httpLogSerial to true"); Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json and set config.system.httpLogSerial to true");
config.system.httpLogSerial = true; config.system.httpLogSerial = true;
// testing output save stuff
Serial.println(":: [LOOP] save output 0");
byte i = 0;
config.system.output_type[i] = 1;
strlcpy(config.system.output_name[i], "bla", sizeof("bla"));
config.system.output_gpio[i] = 3;
config.system.output_gpio_invert[i] = false;
config.system.output_gpio_pwm[i] = false;
strlcpy(config.system.output_i2c[i], "0x3", sizeof("0x3"));
for(byte j=0; j < 4 ; j++) {
config.system.output_ip[i][j] = j + 3;
}
strlcpy(config.system.output_ip_path[i], "/asd?foo=lol", sizeof("/asd?foo=lol"));
config.system.output_enabled[i] = true;
Serial.println(":: [LOOP] save output 1");
i = 1;
config.system.output_type[i] = 2;
strlcpy(config.system.output_name[i], "lol", sizeof("lol"));
config.system.output_gpio[i] = 5;
config.system.output_gpio_invert[i] = true;
config.system.output_gpio_pwm[i] = true;
strlcpy(config.system.output_i2c[i], "0x69", sizeof("0x69"));
for(byte j=0; j < 4 ; j++) {
config.system.output_ip[i][j] = j + 23;
}
strlcpy(config.system.output_ip_path[i], "/asd?foo=lol", sizeof("/asd?foo=lol"));
config.system.output_enabled[i] = true;
// save config to littlefs as json // save config to littlefs as json
SaveConfig(); SaveConfig();
// only print json to serial // only print json to serial

View file

@ -99,34 +99,18 @@ struct Config_WiFi {
/* /*
* Config System * Config System
*/ */
/* //struct Config_System_Outputs {
* Config System Outputs //byte type;
* //char name[32] = "lol";
* - type: output type like GPIO, I2C, URL //byte gpio = 15;
* 1 - GPIO //bool gpio_invert;
* 2 - I2C //bool gpio_pwm;
* 3 - URL //char i2c[8];
* - gpio: which gpio is used //byte ip[4];
* - gpio_invert: invert gpio output //char ip_path[32];
* - gpio_pwm: enable pwm for output //bool enabled;
* - i2c: //};
* - ip: ip to smart plug (tasmota e.g.)
* - ip_path: uri path
* - enabled: enable output
*
*/
struct Config_System_Outputs {
byte type;
char name[32];
byte gpio;
bool gpio_invert;
bool gpio_pwm;
char i2c[8];
byte ip[4];
char ip_path[32];
bool enabled;
};
/* main System struct */ /* main System struct */
struct Config_System { struct Config_System {
@ -137,7 +121,33 @@ struct Config_System {
char httpPass[32]; char httpPass[32];
bool httpLogSerial; bool httpLogSerial;
unsigned short schedulerInterval = 1000; unsigned short schedulerInterval = 1000;
Config_System_Outputs outputs[Max_Outputs];
/*
* Config System Outputs
*
* - output_type: output type like GPIO, I2C, URL
* 1 - GPIO
* 2 - I2C
* 3 - URL
* - output_gpio: which gpio is used
* - output_gpio_invert: invert gpio output
* - output_gpio_pwm: enable pwm for output
* - output_i2c:
* - output_ip: ip to smart plug (tasmota e.g.)
* - output_ip_path: uri path
* - output_enabled: enable output
*
*/
byte output_type[Max_Outputs];
char output_name[Max_Outputs][32];
byte output_gpio[Max_Outputs];
bool output_gpio_invert[Max_Outputs];
bool output_gpio_pwm[Max_Outputs];
char output_i2c[Max_Outputs][8];
byte output_ip[Max_Outputs][4];
char output_ip_path[Max_Outputs][32];
bool output_enabled[Max_Outputs];
//Config_System_Outputs outputs[Max_Outputs];
}; };

View file

@ -207,17 +207,17 @@ bool LoadConfig() {
/* System Outputs */ /* System Outputs */
for(byte i=0; i < Max_Outputs; i++) { for(byte i=0; i < Max_Outputs; i++) {
JsonObject objSystemOutputs = doc["system"]["outputs"][i]; config.system.output_type[i] = objSystem["output_type"][i];
config.system.outputs[i].type = objSystemOutputs["type"]; strlcpy(config.system.output_name[i], objSystem["output_name"][i], sizeof(config.system.output_name[i]));
strlcpy(config.system.outputs[i].name, objSystemOutputs["name"], sizeof(config.system.outputs[i].name)); config.system.output_gpio[i] = objSystem["output_gpio"][i];
config.system.outputs[i].gpio = objSystemOutputs["gpio"]; config.system.output_gpio_invert[i] = objSystem["output_gpio_invert"][i];
config.system.outputs[i].gpio_invert = objSystemOutputs["gpio_invert"]; config.system.output_gpio_pwm[i] = objSystem["output_gpio_pwm"][i];
config.system.outputs[i].gpio_pwm = objSystemOutputs["gpio_pwm"]; strlcpy(config.system.output_i2c[i], objSystem["output_i2c"][i], sizeof(config.system.output_i2c[i]));
strlcpy(config.system.outputs[i].name, objSystemOutputs["name"], sizeof(config.system.outputs[i].i2c));
for(byte j=0; j < 4 ; j++) { for(byte j=0; j < 4 ; j++) {
config.system.outputs[i].ip[j] = objSystemOutputs["ip"][j]; config.system.output_ip[i][j] = objSystem["output_ip"][i][j];
} }
config.system.outputs[i].enabled = objSystemOutputs["enabled"]; strlcpy(config.system.output_ip_path[i], objSystem["output_ip_path"][i], sizeof(config.system.output_ip_path[i]));
config.system.output_enabled[i] = objSystem["output_enabled"][i];
} }
/* Grow */ /* Grow */
@ -280,19 +280,22 @@ bool SaveConfig(bool writeToSerial = false) {
objSystem["schedulerInterval"] = config.system.schedulerInterval; objSystem["schedulerInterval"] = config.system.schedulerInterval;
/* System Outputs */ /* System Outputs */
JsonObject objSystemOutputs = doc["system"]["outputs"].add<JsonObject>();
for(byte i=0; i < Max_Outputs; i++) { for(byte i=0; i < Max_Outputs; i++) {
objSystemOutputs["type"] = config.system.outputs[i].type;
objSystemOutputs["name"] = config.system.outputs[i].name;
objSystemOutputs["gpio"] = config.system.outputs[i].gpio; objSystem["output_type"][i] = config.system.output_type[i];
objSystemOutputs["gpio_invert"] = config.system.outputs[i].gpio_invert; objSystem["output_name"][i] = config.system.output_name[i];
objSystemOutputs["gpio_pwm"] = config.system.outputs[i].gpio_pwm; objSystem["output_gpio"][i] = config.system.output_gpio[i];
objSystemOutputs["i2c"] = config.system.outputs[i].i2c; objSystem["output_gpio_invert"][i] = config.system.output_gpio_invert[i];
objSystem["output_gpio_pwm"][i] = config.system.output_gpio_pwm[i];
objSystem["output_i2c"][i] = config.system.output_i2c[i];
for(byte j=0; j < 4 ; j++) { for(byte j=0; j < 4 ; j++) {
objSystemOutputs["ip"][j] = config.system.outputs[i].ip[j]; objSystem["output_ip"][j] = config.system.output_ip[j];
} }
objSystemOutputs["ip_path"] = config.system.outputs[i].ip_path; objSystem["output_ip_path"][i] = config.system.output_ip_path[i];
objSystemOutputs["enabled"] = config.system.outputs[i].enabled; objSystem["output_enabled"][i] = config.system.output_enabled[i];
} }