diff --git a/src/hasp_config.cpp b/src/hasp_config.cpp index cf07995e..f7bd53c7 100644 --- a/src/hasp_config.cpp +++ b/src/hasp_config.cpp @@ -9,6 +9,7 @@ #include "hasp_ota.h" #include "hasp_spiffs.h" #include "hasp_telnet.h" +#include "hasp_gpio.h" //#include "hasp_eeprom.h" #include "hasp.h" @@ -242,6 +243,15 @@ void configWriteConfig() writefile = true; } #endif +#if HASP_USE_GPIO > 0 + if(settings[F("gpio")].as().isNull()) settings.createNestedObject(F("gpio")); + changed = gpioGetConfig(settings[F("gpio")]); + if(changed) { + Log.verbose(F("GPIO: Settings changed")); + configOutput(settings[F("gpio")]); + writefile = true; + } +#endif if(settings[F("debug")].as().isNull()) settings.createNestedObject(F("debug")); changed = debugGetConfig(settings[F("debug")]); @@ -367,6 +377,10 @@ void configSetup() #if HASP_USE_HTTP > 0 Log.verbose(F("Loading HTTP settings")); httpSetConfig(settings[F("http")]); +#endif +#if HASP_USE_GPIO > 0 + Log.verbose(F("Loading GPIO settings")); + gpioSetConfig(settings[F("gpio")]); #endif // } Log.notice(F("User configuration loaded")); @@ -386,11 +400,36 @@ void configOutput(const JsonObject & settings) String password((char *)0); password.reserve(128); - password = F("\"pass\":\""); - password += settings[F("pass")].as(); - password += F("\""); - if(password.length() > 2) output.replace(password, passmask); + String pass = F("pass"); + if(!settings[pass].isNull()) { + password = F("\"pass\":\""); + password += settings[pass].as(); + password += F("\""); + output.replace(password, passmask); + } + + if(!settings[F("wifi")][pass].isNull()) { + password = F("\"pass\":\""); + password += settings[F("wifi")][pass].as(); + password += F("\""); + output.replace(password, passmask); + } + + if(!settings[F("mqtt")][pass].isNull()) { + password = F("\"pass\":\""); + password += settings[F("mqtt")][pass].as(); + password += F("\""); + output.replace(password, passmask); + } + + if(!settings[F("http")][pass].isNull()) { + password = F("\"pass\":\""); + password += settings[F("http")][pass].as(); + password += F("\""); + output.replace(password, passmask); + } + Log.trace(F("CONF: %s"), output.c_str()); } @@ -399,7 +438,7 @@ bool configClear() #if defined(STM32F4xx) Log.verbose(F("CONF: Clearing EEPROM")); char buffer[1024 + 128]; - memset(buffer, 1 ,sizeof(buffer)); + memset(buffer, 1, sizeof(buffer)); if(sizeof(buffer) > 0) { uint16_t i; for(i = 0; i < sizeof(buffer); i++) eeprom_buffered_write_byte(i, buffer[i]); diff --git a/src/hasp_config.h b/src/hasp_config.h index cb447c23..2b69bc43 100644 --- a/src/hasp_config.h +++ b/src/hasp_config.h @@ -29,6 +29,7 @@ const char F_GUI_CALIBRATION[] PROGMEM = "calibration"; const char F_GUI_BACKLIGHTPIN[] PROGMEM = "bcklpin"; const char F_GUI_POINTER[] PROGMEM = "pointer"; const char F_DEBUG_TELEPERIOD[] PROGMEM = "teleperiod"; +const char F_GPIO_CONFIG[] PROGMEM = "config"; const char HASP_CONFIG_FILE[] PROGMEM = "/config.json"; diff --git a/src/hasp_gpio.cpp b/src/hasp_gpio.cpp index f6c8afd6..391f56a1 100644 --- a/src/hasp_gpio.cpp +++ b/src/hasp_gpio.cpp @@ -6,6 +6,7 @@ #include "hasp_conf.h" #include "hasp_gpio.h" +#include "hasp_config.h" #include "hasp_dispatch.h" #include "hasp.h" @@ -191,8 +192,8 @@ void gpioSetup() #endif #if defined(ARDUINO_ARCH_ESP32) - // gpioConfig[0] = {D2, 0, HASP_GPIO_SWITCH, INPUT}; - // gpioConfig[1] = {D1, 1, HASP_GPIO_RELAY, OUTPUT}; + gpioConfig[0] = {D2, 0, HASP_GPIO_SWITCH, INPUT}; + gpioConfig[1] = {D1, 1, HASP_GPIO_RELAY, OUTPUT}; // gpioAddButton(D2, INPUT, HIGH, 1); // pinMode(D1, OUTPUT); @@ -293,3 +294,79 @@ void gpio_set_group_outputs(uint8_t groupid, uint8_t eventid) } } } + +//////////////////////////////////////////////////////////////////////////////////////////////////// +bool gpioGetConfig(const JsonObject & settings) +{ + bool changed = false; + + /* Check Gpio array has changed */ + JsonArray array = settings[FPSTR(F_GPIO_CONFIG)].as(); + uint8_t i = 0; + for(JsonVariant v : array) { + if(i < HASP_NUM_GPIO_CONFIG) { + uint32_t cur_val = gpioConfig[i].pin | (gpioConfig[i].group << 8) | (gpioConfig[i].type << 16) | + (gpioConfig[i].gpio_function << 24); + Log.verbose(F("GPIO CONF: %d: %d <=> %d"), i, cur_val, v.as()); + + if(cur_val != v.as()) changed = true; + v.set(cur_val); + } else { + changed = true; + } + i++; + } + + /* Build new Gpio array if the count is not correct */ + if(i != HASP_NUM_GPIO_CONFIG) { + array = settings[FPSTR(F_GPIO_CONFIG)].to(); // Clear JsonArray + for(uint8_t i = 0; i < HASP_NUM_GPIO_CONFIG; i++) { + uint32_t cur_val = gpioConfig[i].pin | (gpioConfig[i].group << 8) | (gpioConfig[i].type << 16) | + (gpioConfig[i].gpio_function << 24); + array.add(cur_val); + } + changed = true; + } + + if(changed) configOutput(settings); + return changed; +} + +/** Set GPIO Configuration. + * + * Read the settings from json and sets the application variables. + * + * @note: data pixel should be formated to uint32_t RGBA. Imagemagick requirements. + * + * @param[in] settings JsonObject with the config settings. + **/ +bool gpioSetConfig(const JsonObject & settings) +{ + configOutput(settings); + bool changed = false; + + if(!settings[FPSTR(F_GPIO_CONFIG)].isNull()) { + bool status = false; + int i = 0; + + JsonArray array = settings[FPSTR(F_GPIO_CONFIG)].as(); + for(JsonVariant v : array) { + uint32_t new_val = v.as(); + + if(i < HASP_NUM_GPIO_CONFIG) { + uint32_t cur_val = gpioConfig[i].pin | (gpioConfig[i].group << 8) | (gpioConfig[i].type << 16) | + (gpioConfig[i].gpio_function << 24); + if(cur_val != new_val) status = true; + + gpioConfig[i].pin = new_val & 0xFF; + gpioConfig[i].group = new_val >> 8 & 0xFF; + gpioConfig[i].type = new_val >> 16 & 0xFF; + gpioConfig[i].gpio_function = new_val >> 24 & 0xFF; + } + i++; + } + changed |= status; + } + + return changed; +} diff --git a/src/hasp_gpio.h b/src/hasp_gpio.h index c9e5ed52..f7fa73af 100644 --- a/src/hasp_gpio.h +++ b/src/hasp_gpio.h @@ -11,6 +11,9 @@ void gpioSetup(void); void IRAM_ATTR gpioLoop(void); void gpio_set_group_outputs(uint8_t groupid, uint8_t eventid); +bool gpioGetConfig(const JsonObject & settings); +bool gpioSetConfig(const JsonObject & settings); + #define HASP_GPIO_FREE 0x00 #define HASP_GPIO_USED 0x01 #define HASP_GPIO_SWITCH 0x02 diff --git a/src/hasp_gui.h b/src/hasp_gui.h index 4e5891dc..66c7b7a0 100644 --- a/src/hasp_gui.h +++ b/src/hasp_gui.h @@ -1,7 +1,7 @@ #ifndef HASP_GUI_H #define HASP_GUI_H -#include "TFT_eSPI.h" +//#include "TFT_eSPI.h" #include "ArduinoJson.h" #include "lvgl.h"