From 6ccd10dbe034af50f19c9b9597516bbc48287e6d Mon Sep 17 00:00:00 2001 From: fvanroie Date: Tue, 9 May 2023 16:13:39 +0200 Subject: [PATCH] Revert F() and move char* to FlashStringHelper --- src/hasp/hasp.cpp | 26 +++++----- src/hasp/hasp.h | 6 +-- src/hasp_config.cpp | 104 ++++++++++++++++++++++++++++++++++---- src/hasp_config.h | 12 ++--- src/hasp_debug.cpp | 16 +++--- src/sys/svc/hasp_http.cpp | 27 +++++++++- 6 files changed, 149 insertions(+), 42 deletions(-) diff --git a/src/hasp/hasp.cpp b/src/hasp/hasp.cpp index 7f541b32..e80582be 100644 --- a/src/hasp/hasp.cpp +++ b/src/hasp/hasp.cpp @@ -437,13 +437,13 @@ void haspProgressMsg(const char* msg) } */ } -// #ifdef ARDUINO -// // Sets the value string of the global progress bar -// void haspProgressMsg(const __FlashStringHelper* msg) -// { -// haspProgressMsg(String(msg).c_str()); -// } -// #endif +#ifdef ARDUINO +// Sets the value string of the global progress bar +void haspProgressMsg(const __FlashStringHelper* msg) +{ + haspProgressMsg(String(msg).c_str()); +} +#endif /*Add a custom apply callback*/ static void custom_font_apply_cb(lv_theme_t* th, lv_obj_t* obj, lv_theme_style_t name) @@ -829,20 +829,20 @@ bool haspSetConfig(const JsonObject& settings) JsonVariant color_str; bool changed = false; - changed |= configSet(haspStartPage, settings[FPSTR(FP_CONFIG_STARTPAGE)], F("haspStartPage")); - changed |= configSet(haspStartDim, settings[FPSTR(FP_CONFIG_STARTDIM)], F("haspStartDim")); + changed |= configSet(haspStartPage, settings[FPSTR(FP_CONFIG_STARTPAGE)], "haspStartPage"); + changed |= configSet(haspStartDim, settings[FPSTR(FP_CONFIG_STARTDIM)], "haspStartDim"); { // Theme related settings // Set from Hue first bool theme_changed = false; - theme_changed |= configSet(haspThemeId, settings[FPSTR(FP_CONFIG_THEME)], F("haspThemeId")); - theme_changed |= configSet(haspThemeHue, settings[FPSTR(FP_CONFIG_HUE)], F("haspThemeHue")); + theme_changed |= configSet(haspThemeId, settings[FPSTR(FP_CONFIG_THEME)], "haspThemeId"); + theme_changed |= configSet(haspThemeHue, settings[FPSTR(FP_CONFIG_HUE)], "haspThemeHue"); color_primary = lv_color_hsv_to_rgb(haspThemeHue, 100, 100); color_secondary = lv_color_hsv_to_rgb(20, 60, 100); // Check for color1 and color2 - theme_changed |= configSet(color_primary, settings[FPSTR(FP_CONFIG_COLOR1)], F("haspColor1")); - theme_changed |= configSet(color_secondary, settings[FPSTR(FP_CONFIG_COLOR2)], F("haspColor2")); + theme_changed |= configSet(color_primary, settings[FPSTR(FP_CONFIG_COLOR1)], "haspColor1"); + theme_changed |= configSet(color_secondary, settings[FPSTR(FP_CONFIG_COLOR2)], "haspColor2"); changed |= theme_changed; // if(theme_changed) hasp_set_theme(haspThemeId); // LVGL is not inited at config load time diff --git a/src/hasp/hasp.h b/src/hasp/hasp.h index c67dca11..4b9b5217 100644 --- a/src/hasp/hasp.h +++ b/src/hasp/hasp.h @@ -105,8 +105,8 @@ void hasp_set_theme(uint8_t themeid); #endif void haspProgressMsg(const char* msg); -// #ifdef ARDUINO -// void haspProgressMsg(const __FlashStringHelper* msg); -// #endif +#ifdef ARDUINO +void haspProgressMsg(const __FlashStringHelper* msg); +#endif #endif /*HASP_H*/ diff --git a/src/hasp_config.cpp b/src/hasp_config.cpp index 1573299e..61811a83 100644 --- a/src/hasp_config.cpp +++ b/src/hasp_config.cpp @@ -10,12 +10,12 @@ #include "hasp_gui.h" #include "hal/hasp_hal.h" -//#include "hasp_ota.h" included in conf -//#include "hasp_filesystem.h" included in conf -//#include "hasp_telnet.h" included in conf -//#include "hasp_gpio.h" included in conf +// #include "hasp_ota.h" included in conf +// #include "hasp_filesystem.h" included in conf +// #include "hasp_telnet.h" included in conf +// #include "hasp_gpio.h" included in conf -//#include "hasp_eeprom.h" +// #include "hasp_eeprom.h" #if HASP_USE_EEPROM > 0 #include "EEPROM.h" @@ -29,13 +29,94 @@ extern uint32_t dispatchLastMillis; extern gui_conf_t gui_settings; extern dispatch_conf_t dispatch_settings; -void confDebugSet(const char* fstr_name) +void confDebugSet(const __FlashStringHelper* fstr_name) { /*char buffer[128]; snprintf_P(buffer, sizeof(buffer), PSTR(" * %s set"), name); debugPrintln(buffer);*/ LOG_VERBOSE(TAG_CONF, F(D_BULLET "%S set"), fstr_name); } +void confDebugSet(const char* fstr_name) +{ + /*char buffer[128]; + snprintf_P(buffer, sizeof(buffer), PSTR(" * %s set"), name); + debugPrintln(buffer);*/ + LOG_VERBOSE(TAG_CONF, F(D_BULLET "%s set"), fstr_name); +} + +bool configSet(bool& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name) +{ + if(!setting.isNull()) { + bool val = setting.as(); + if(value != val) { + confDebugSet(fstr_name); + value = val; + return true; + } + } + return false; +} +bool configSet(int8_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name) +{ + if(!setting.isNull()) { + int8_t val = setting.as(); + if(value != val) { + confDebugSet(fstr_name); + value = val; + return true; + } + } + return false; +} +bool configSet(uint8_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name) +{ + if(!setting.isNull()) { + uint8_t val = setting.as(); + if(value != val) { + confDebugSet(fstr_name); + value = val; + return true; + } + } + return false; +} +bool configSet(uint16_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name) +{ + if(!setting.isNull()) { + uint16_t val = setting.as(); + if(value != val) { + confDebugSet(fstr_name); + value = val; + return true; + } + } + return false; +} +bool configSet(int32_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name) +{ + if(!setting.isNull()) { + int32_t val = setting.as(); + if(value != val) { + confDebugSet(fstr_name); + value = val; + return true; + } + } + return false; +} +bool configSet(lv_color_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name) +{ + lv_color32_t c32; + if(!setting.isNull() && Parser::haspPayloadToColor(setting.as(), c32)) { + lv_color_t val = lv_color_make(c32.ch.red, c32.ch.green, c32.ch.blue); + if(value.full != val.full) { + confDebugSet(fstr_name); + value = val; + return true; + } + } + return false; +} bool configSet(bool& value, const JsonVariant& setting, const char* fstr_name) { @@ -110,6 +191,7 @@ bool configSet(lv_color_t& value, const JsonVariant& setting, const char* fstr_n } return false; } + void configSetupDebug(JsonDocument& settings) { debugSetup(settings[FPSTR(FP_DEBUG)]); @@ -118,7 +200,7 @@ void configSetupDebug(JsonDocument& settings) void configStorePasswords(JsonDocument& settings, String& wifiPass, String& mqttPass, String& httpPass) { - const char* pass = F("pass"); + const char* pass = ("pass"); wifiPass = settings[FPSTR(FP_WIFI)][pass].as(); mqttPass = settings[FPSTR(FP_MQTT)][pass].as(); @@ -127,7 +209,7 @@ void configStorePasswords(JsonDocument& settings, String& wifiPass, String& mqtt void configRestorePasswords(JsonDocument& settings, String& wifiPass, String& mqttPass, String& httpPass) { - const char* pass = F("pass"); + const char* pass = ("pass"); if(!settings[FPSTR(FP_WIFI)][pass].isNull()) settings[FPSTR(FP_WIFI)][pass] = wifiPass; if(!settings[FPSTR(FP_MQTT)][pass].isNull()) settings[FPSTR(FP_MQTT)][pass] = mqttPass; @@ -287,7 +369,7 @@ void configWrite() bool writefile = false; bool changed = false; - const char* module; + const __FlashStringHelper* module; #if HASP_USE_WIFI > 0 module = FPSTR(FP_WIFI); @@ -453,7 +535,7 @@ void configSetup() configRead(settings, true); } - //#if HASP_USE_SPIFFS > 0 + // #if HASP_USE_SPIFFS > 0 LOG_INFO(TAG_DEBG, F("Loading debug settings")); debugSetConfig(settings[FPSTR(FP_DEBUG)]); LOG_INFO(TAG_GPIO, F("Loading GUI settings")); @@ -494,7 +576,7 @@ void configSetup() LOG_INFO(TAG_CONF, F(D_CONFIG_LOADED)); } - //#endif + // #endif } void configLoop(void) diff --git a/src/hasp_config.h b/src/hasp_config.h index eda3d400..3e465a95 100644 --- a/src/hasp_config.h +++ b/src/hasp_config.h @@ -23,12 +23,12 @@ void configOutput(const JsonObject& settings, uint8_t tag); bool configClearEeprom(void); /* ===== Getter and Setter Functions ===== */ -// bool configSet(bool& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); -// bool configSet(int8_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); -// bool configSet(uint8_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); -// bool configSet(uint16_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); -// bool configSet(int32_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); -// bool configSet(lv_color_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); +bool configSet(bool& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); +bool configSet(int8_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); +bool configSet(uint8_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); +bool configSet(uint16_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); +bool configSet(int32_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); +bool configSet(lv_color_t& value, const JsonVariant& setting, const __FlashStringHelper* fstr_name); bool configSet(bool& value, const JsonVariant& setting, const char* fstr_name); bool configSet(int8_t& value, const JsonVariant& setting, const char* fstr_name); bool configSet(uint8_t& value, const JsonVariant& setting, const char* fstr_name); diff --git a/src/hasp_debug.cpp b/src/hasp_debug.cpp index 408997d8..fef0989a 100644 --- a/src/hasp_debug.cpp +++ b/src/hasp_debug.cpp @@ -25,14 +25,14 @@ bool debugAnsiCodes = false; -// inline void debugSendAnsiCode(const __FlashStringHelper* code, Print* _logOutput) -// { -// #ifdef ARDUINO -// if(debugAnsiCodes) _logOutput->print(code); -// #else -// if(debugAnsiCodes) debug_print(_logOutput, code); -// #endif -// } +inline void debugSendAnsiCode(const __FlashStringHelper* code, Print* _logOutput) +{ +#ifdef ARDUINO + if(debugAnsiCodes) _logOutput->print(code); +#else + if(debugAnsiCodes) debug_print(_logOutput, code); +#endif +} inline void debugSendAnsiCode(const char* code, Print* _logOutput) { diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index 058846af..984cf52e 100644 --- a/src/sys/svc/hasp_http.cpp +++ b/src/sys/svc/hasp_http.cpp @@ -169,6 +169,15 @@ static void add_form_button(String& str, const char* label, const char* action) str += ""; } +static void add_form_button(String& str, const char* label, const __FlashStringHelper* action) +{ + str += ""; + str += label; + str += ""; +} + static String http_get_content_type(const String& path) { char buffer[sizeof(mime::mimeTable[0].mimeType)]; @@ -208,6 +217,22 @@ bool http_is_authenticated() return true; } +// Check authentication and create Log entry +bool http_is_authenticated(const __FlashStringHelper* notused) +{ + if(!http_is_authenticated()) return false; + +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) + LOG_VERBOSE(TAG_HTTP, D_HTTP_SENDING_PAGE, webServer.uri().c_str(), + webServer.client().remoteIP().toString().c_str()); +#else + // LOG_INFO(TAG_HTTP,D_HTTP_SENDING_PAGE, page, + // String(webServer.client().remoteIP()).c_str()); +#endif + + return true; +} + // Check authentication and create Log entry bool http_is_authenticated(const char* notused) { @@ -640,7 +665,7 @@ static void webHandleApi() } settings = doc.to(); - const char* module; + const __FlashStringHelper* module; module = FPSTR(FP_HASP); settings.createNestedObject(module);