it does work with a structure and arrays in it. this is fine

This commit is contained in:
Marcus 2024-10-24 02:24:23 +02:00
parent bf1e25d04c
commit 99d598e05d
3 changed files with 67 additions and 55 deletions

View file

@ -156,33 +156,33 @@ void loop() {
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"));
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;
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;
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"));
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;
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;
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
SaveConfig();

View file

@ -100,7 +100,7 @@ struct Config_WiFi {
* Config System
*/
//struct Config_System_Outputs {
struct Config_System_Output {
//byte type;
//char name[32] = "lol";
//byte gpio = 15;
@ -110,7 +110,16 @@ struct Config_WiFi {
//byte ip[4];
//char ip_path[32];
//bool enabled;
//};
byte type[Max_Outputs];
char name[Max_Outputs][32];
byte gpio[Max_Outputs];
bool gpio_invert[Max_Outputs];
bool gpio_pwm[Max_Outputs];
char i2c[Max_Outputs][8];
byte ip[Max_Outputs][4];
char ip_path[Max_Outputs][32];
bool enabled[Max_Outputs];
};
/* main System struct */
struct Config_System {
@ -138,16 +147,16 @@ struct Config_System {
* - 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];
// 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_Output output;
};

View file

@ -205,19 +205,23 @@ bool LoadConfig() {
config.system.httpLogSerial = objSystem["httpLogSerial"];
config.system.schedulerInterval = objSystem["schedulerInterval"];
JsonObject objSystemOutput = objSystem["output"][0];
/* System Outputs */
for(byte i=0; i < Max_Outputs; i++) {
config.system.output_type[i] = objSystem["output_type"][i];
strlcpy(config.system.output_name[i], objSystem["output_name"][i], sizeof(config.system.output_name[i]));
config.system.output_gpio[i] = objSystem["output_gpio"][i];
config.system.output_gpio_invert[i] = objSystem["output_gpio_invert"][i];
config.system.output_gpio_pwm[i] = objSystem["output_gpio_pwm"][i];
strlcpy(config.system.output_i2c[i], objSystem["output_i2c"][i], sizeof(config.system.output_i2c[i]));
for(byte j=0; j < 4 ; j++) {
config.system.output_ip[i][j] = objSystem["output_ip"][i][j];
if(objSystemOutput["type"][i] > 0) {
config.system.output.type[i] = objSystemOutput["type"][i];
strlcpy(config.system.output.name[i], objSystemOutput["name"][i], sizeof(config.system.output.name[i]));
config.system.output.gpio[i] = objSystemOutput["gpio"][i];
config.system.output.gpio_invert[i] = objSystemOutput["gpio_invert"][i];
config.system.output.gpio_pwm[i] = objSystemOutput["gpio_pwm"][i];
strlcpy(config.system.output.i2c[i], objSystemOutput["i2c"][i], sizeof(config.system.output.i2c[i]));
for(byte j=0; j < 4 ; j++) {
config.system.output.ip[i][j] = objSystemOutput["ip"][i][j];
}
strlcpy(config.system.output.ip_path[i], objSystemOutput["ip_path"][i], sizeof(config.system.output.ip_path[i]));
config.system.output.enabled[i] = objSystemOutput["enabled"][i];
}
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 */
@ -280,23 +284,22 @@ bool SaveConfig(bool writeToSerial = false) {
objSystem["schedulerInterval"] = config.system.schedulerInterval;
/* System Outputs */
JsonObject objSystemOutput = objSystem["output"].add<JsonObject>();
for(byte i=0; i < Max_Outputs; i++) {
if(config.system.output.type[i] > 0) {
objSystemOutput["type"][i] = config.system.output.type[i];
objSystemOutput["name"][i] = config.system.output.name[i];
objSystemOutput["gpio"][i] = config.system.output.gpio[i];
objSystemOutput["gpio_invert"][i] = config.system.output.gpio_invert[i];
objSystemOutput["gpio_pwm"][i] = config.system.output.gpio_pwm[i];
objSystemOutput["i2c"][i] = config.system.output.i2c[i];
objSystem["output_type"][i] = config.system.output_type[i];
objSystem["output_name"][i] = config.system.output_name[i];
objSystem["output_gpio"][i] = config.system.output_gpio[i];
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++) {
objSystem["output_ip"][i][j] = config.system.output_ip[i][j];
for(byte j=0; j < 4 ; j++) {
objSystemOutput["ip"][i][j] = config.system.output.ip[i][j];
}
objSystemOutput["ip_path"][i] = config.system.output.ip_path[i];
objSystemOutput["enabled"][i] = config.system.output.enabled[i];
}
objSystem["output_ip_path"][i] = config.system.output_ip_path[i];
objSystem["output_enabled"][i] = config.system.output_enabled[i];
}