From 8e150fca3ae242d4a5c74023ab5faacd545f392c Mon Sep 17 00:00:00 2001 From: fvanroie Date: Sun, 22 Nov 2020 16:59:35 +0100 Subject: [PATCH] Code cleanup --- include/lv_conf.h | 7 ------- src/hasp.h | 6 +++--- src/hasp_config.cpp | 8 ++++---- src/hasp_config.h | 34 +++++++++++++++++++++------------- src/hasp_debug.cpp | 37 ++++++++++++++++++++----------------- src/hasp_debug.h | 14 ++++++-------- src/hasp_dispatch.h | 22 ++++++++++++++-------- src/hasp_filesystem.cpp | 10 +++++++--- src/hasp_gpio.cpp | 2 +- src/hasp_gui.cpp | 24 ++++++++++++++---------- src/hasp_gui.h | 24 ++++++++---------------- src/hasp_hal.cpp | 2 +- src/hasp_hal.h | 2 +- src/hasp_http.cpp | 18 +++++++++--------- src/hasp_http.h | 6 ++++-- src/hasp_mdns.h | 2 ++ src/hasp_mqtt.cpp | 8 +++----- src/hasp_mqtt.h | 2 +- src/hasp_ota.cpp | 8 ++++---- src/hasp_ota.h | 5 ++++- src/hasp_telnet.cpp | 2 +- src/hasp_wifi.cpp | 12 +++++++----- src/main.cpp | 21 +++++++++++---------- 23 files changed, 146 insertions(+), 130 deletions(-) diff --git a/include/lv_conf.h b/include/lv_conf.h index f480fe3f..257b49e3 100644 --- a/include/lv_conf.h +++ b/include/lv_conf.h @@ -458,13 +458,6 @@ typedef void* lv_font_user_data_t; #endif /*LV_SPRINTF_CUSTOM*/ -//*********************** 7.0 BETA defines, Remove when final version is ready !! -#define LV_USE_TA 1 // TEMPORARY FIX for 7.0 beta -#define LV_USE_DDLIST 1 // TEMPORARY FIX for 7.0 beta -# define LV_DSLIST_DEF_ANIM_TIME 200 -#define LV_USE_BTNM 1 // TEMPORARY FIX for 7.0 beta - - /*=================== * LV_OBJ SETTINGS *==================*/ diff --git a/src/hasp.h b/src/hasp.h index 603271a7..f1d0a99b 100644 --- a/src/hasp.h +++ b/src/hasp.h @@ -49,6 +49,8 @@ enum hasp_event_t { // even = released, odd = pressed */ void haspSetup(); void haspLoop(void); +void haspReconnect(void); +void haspDisconnect(void); lv_obj_t * get_page_obj(uint8_t pageid); bool get_page_id(lv_obj_t * obj, uint8_t * pageid); @@ -63,10 +65,8 @@ void haspBackground(uint16_t pageid, uint16_t imageid); void hasp_set_group_objects(uint8_t groupid, uint8_t eventid, lv_obj_t * src_obj); -void haspNewObject(const JsonObject & config, uint8_t & saved_page_id); +// void haspNewObject(const JsonObject & config, uint8_t & saved_page_id); -void haspReconnect(void); -void haspDisconnect(void); void haspWakeUp(void); void haspProgressVal(uint8_t val); diff --git a/src/hasp_config.cpp b/src/hasp_config.cpp index 7634f7d4..95ed3bd4 100644 --- a/src/hasp_config.cpp +++ b/src/hasp_config.cpp @@ -370,11 +370,11 @@ void configSetup() } //#if HASP_USE_SPIFFS > 0 - Log.notice(TAG_DEBG, F("Loading debug settings")); + Log.trace(TAG_DEBG, F("Loading debug settings")); debugSetConfig(settings[F("debug")]); - Log.notice(TAG_GPIO, F("Loading GUI settings")); + Log.trace(TAG_GPIO, F("Loading GUI settings")); guiSetConfig(settings[F("gui")]); - Log.notice(TAG_HASP, F("Loading HASP settings")); + Log.trace(TAG_HASP, F("Loading HASP settings")); haspSetConfig(settings[F("hasp")]); // otaGetConfig(settings[F("ota")]); @@ -458,7 +458,7 @@ void configOutput(const JsonObject & settings, uint8_t tag) Log.verbose(tag, output.c_str()); } -bool configClear() +bool configClearEeprom() { #if defined(STM32F4xx) Log.notice(TAG_CONF, F("Clearing EEPROM")); diff --git a/src/hasp_config.h b/src/hasp_config.h index 55c86d10..c6f2a37d 100644 --- a/src/hasp_config.h +++ b/src/hasp_config.h @@ -7,6 +7,27 @@ #include "ArduinoJson.h" #include "hasp_debug.h" // for TAG_CONF +/* ===== Default Event Processors ===== */ +void configSetup(void); +void configLoop(void); +void configEverySecond(void); +void configStart(void); +void configStop(void); + +/* ===== Special Event Processors ===== */ +void configWriteConfig(void); +void configOutput(const JsonObject & settings, uint8_t tag = TAG_CONF); +bool configClearEeprom(void); + +/* ===== Getter and Setter Functions ===== */ +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); + +/* ===== Read/Write Configuration ===== */ +void configSetConfig(JsonObject & settings); +void configGetConfig(JsonDocument & settings); + /* json keys used in the configfile */ const char F_CONFIG_STARTPAGE[] PROGMEM = "startpage"; const char F_CONFIG_STARTDIM[] PROGMEM = "startdim"; @@ -37,17 +58,4 @@ const char F_GPIO_CONFIG[] PROGMEM = "config"; const char HASP_CONFIG_FILE[] PROGMEM = "/config.json"; -void configSetup(); -void configStop(void); - -void configSetConfig(JsonObject & settings); -void configGetConfig(JsonDocument & settings); -void configWriteConfig(); -void configOutput(const JsonObject & settings, uint8_t tag = TAG_CONF); - -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 configClear(); - #endif \ No newline at end of file diff --git a/src/hasp_debug.cpp b/src/hasp_debug.cpp index ad1fe812..1159517b 100644 --- a/src/hasp_debug.cpp +++ b/src/hasp_debug.cpp @@ -109,7 +109,7 @@ unsigned long debugLastMillis = 0; uint16_t debugTelePeriod = 300; // Send the HASP header and version to the output device specified -void debugHaspHeader(Print * output) +void debugPrintHaspHeader(Print * output) { if(debugAnsiCodes) output->println(TERM_COLOR_YELLOW); output->print(F("" @@ -559,19 +559,19 @@ void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput) debugSendAnsiCode(F(TERM_CLEAR_LINE), _logOutput); debugPrintTimestamp(level, _logOutput); debugPrintHaspMemory(level, _logOutput); + #if LV_MEM_CUSTOM == 0 debugPrintLvglMemory(level, _logOutput); #endif - switch(tag) { - case TAG_MQTT_PUB: - debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput); - break; - case TAG_MQTT_RCV: - debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput); - break; - default: - debugPrintPriority(level, _logOutput); + + if(tag == TAG_MQTT_PUB && level == LOG_LEVEL_NOTICE) { + debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput); + } else if(tag == TAG_MQTT_RCV && level == LOG_LEVEL_NOTICE) { + debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput); + } else { + debugPrintPriority(level, _logOutput); } + _logOutput->print(F(" ")); debugPrintTag(tag, _logOutput); _logOutput->print(F(": ")); @@ -627,7 +627,7 @@ void debugPreSetup(JsonObject settings) // Print Header Serial.println(); - debugHaspHeader(&Serial); + debugPrintHaspHeader(&Serial); // Serial.println(debugHaspHeader()); // Serial.println(); Serial.flush(); @@ -637,15 +637,18 @@ void debugPreSetup(JsonObject settings) } #if LV_USE_LOG != 0 -static uint32_t lastDbgLine; -static uint16_t lastDbgFree; -void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const char * funcname, const char * descr) +void debugLvglLogEvent(lv_log_level_t level, const char * file, uint32_t line, const char * funcname, + const char * descr) { + /* used for duplicate detection */ + static uint32_t lastDbgLine; + static uint16_t lastDbgFreeMem; + lv_mem_monitor_t mem_mon; lv_mem_monitor(&mem_mon); /* Reduce the number of reepeated debug message */ - if(line != lastDbgLine || mem_mon.free_biggest_size != lastDbgFree) { + if(line != lastDbgLine || mem_mon.free_biggest_size != lastDbgFreeMem) { switch(level) { case LV_LOG_LEVEL_TRACE: Log.verbose(TAG_LVGL, descr); @@ -659,8 +662,8 @@ void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const cha default: Log.notice(TAG_LVGL, descr); } - lastDbgLine = line; - lastDbgFree = mem_mon.free_biggest_size; + lastDbgLine = line; + lastDbgFreeMem = mem_mon.free_biggest_size; } } #endif diff --git a/src/hasp_debug.h b/src/hasp_debug.h index 7d6dfd73..398c972e 100644 --- a/src/hasp_debug.h +++ b/src/hasp_debug.h @@ -7,8 +7,7 @@ #include "ArduinoJson.h" #include "lvgl.h" -void debugHaspHeader(Print * output); - +/* ===== Default Event Processors ===== */ void debugPreSetup(JsonObject settings); void debugSetup(); void debugLoop(void); @@ -16,21 +15,20 @@ void debugEverySecond(void); void debugStart(void); void debugStop(void); +/* ===== Special Event Processors ===== */ +void debugLvglLogEvent(lv_log_level_t level, const char * file, uint32_t line, const char * funcname, const char * descr); +void debugPrintHaspHeader(Print * output); void debugStartSyslog(void); void debugStopSyslog(void); +// void syslogSend(uint8_t log, const char * debugText); -void serialPrintln(String & debugText, uint8_t level); -void serialPrintln(const char * debugText, uint8_t level); - -void syslogSend(uint8_t log, const char * debugText); - +/* ===== Read/Write Configuration ===== */ bool debugGetConfig(const JsonObject & settings); bool debugSetConfig(const JsonObject & settings); // void debugPrintPrefix(int level, Print * _logOutput); // void debugPrintSuffix(int level, Print * _logOutput); // void debugSendOuput(const char * buffer); -void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const char * funcname, const char * descr); enum { TAG_MAIN = 0, diff --git a/src/hasp_dispatch.h b/src/hasp_dispatch.h index cbc6907b..869f0c1e 100644 --- a/src/hasp_dispatch.h +++ b/src/hasp_dispatch.h @@ -8,31 +8,31 @@ #define LOG_CMND_CTR "CMND: " -bool is_true(const char * s); - +/* ===== Default Event Processors ===== */ void dispatchSetup(void); void dispatchLoop(void); +void dispatchEverySecond(void); +void dispatchStart(void); +void dispatchStop(void); -// void dispatchCommand(const char * topic, const char * payload); // intenal +/* ===== Special Event Processors ===== */ void dispatchConfig(const char * topic, const char * payload); void dispatchTopicPayload(const char * topic, const char * payload); void dispatchTextLine(const char * cmnd); - -// void dispatchParseJson(char * strPayload); -// void dispatchParseJsonl(char * strPayload); void dispatchParseJsonl(Stream & stream); +void dispatchClearPage(const char * page); void dispatchPage(const char * page); void dispatchPageNext(); void dispatchPagePrev(); -void dispatchClearPage(const char * page); void dispatchDim(const char * level); void dispatchBacklight(const char * payload); void dispatchWebUpdate(const char * espOtaUrl); -void dispatch_output_idle_state(const char * state); void dispatchReboot(bool saveConfig); + +void dispatch_output_idle_state(const char * state); void dispatch_output_statusupdate(void); void dispatch_button(uint8_t id, const char * event); @@ -41,9 +41,15 @@ void dispatch_send_object_event(uint8_t pageid, uint8_t objid, uint8_t eventid); void dispatch_send_group_event(uint8_t groupid, uint8_t eventid, bool update_hasp); bool dispatch_get_event_state(uint8_t eventid); +bool is_true(const char * s); void IRAM_ATTR dispatch_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, const char * attribute, const char * data); +/* ===== Getter and Setter Functions ===== */ + +/* ===== Read/Write Configuration ===== */ + +/* ===== Structs and Constants ===== */ struct haspCommand_t { void (*func)(const char *); diff --git a/src/hasp_filesystem.cpp b/src/hasp_filesystem.cpp index 16d5f6c8..16e73f04 100644 --- a/src/hasp_filesystem.cpp +++ b/src/hasp_filesystem.cpp @@ -95,9 +95,13 @@ void filesystemList() #endif } -bool filesystemSetup() +bool filesystemSetup(void) { // no SPIFFS settings, as settings depend on SPIFFS + // no Logging, because it depends on the configuration file + + // Logging is defered until debugging has started + // FS success or failure is printed at that time ! #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 #if defined(ARDUINO_ARCH_ESP8266) @@ -105,10 +109,10 @@ bool filesystemSetup() #else if(!HASP_FS.begin(true)) { #endif - Log.error(TAG_FILE, F("SPI flash init failed. Unable to mount FS.")); + // Log.error(TAG_FILE, F("SPI flash init failed. Unable to mount FS.")); return false; } else { - Log.trace(TAG_FILE, F("SPI Flash FS mounted")); + // Log.trace(TAG_FILE, F("SPI Flash FS mounted")); return true; } #endif diff --git a/src/hasp_gpio.cpp b/src/hasp_gpio.cpp index 3202d2b9..adb5c692 100644 --- a/src/hasp_gpio.cpp +++ b/src/hasp_gpio.cpp @@ -21,7 +21,7 @@ static AceButton * button[HASP_NUM_INPUTS]; // An array of button pins, led pins, and the led states. Cannot be const // because ledState is mutable. hasp_gpio_config_t gpioConfig[HASP_NUM_GPIO_CONFIG] = { - {2, 8, INPUT, LOW}, {3, 9, OUTPUT, LOW}, {4, 10, INPUT, HIGH}, {5, 11, OUTPUT, LOW}, {6, 12, INPUT, LOW}, +// {2, 8, INPUT, LOW}, {3, 9, OUTPUT, LOW}, {4, 10, INPUT, HIGH}, {5, 11, OUTPUT, LOW}, {6, 12, INPUT, LOW}, }; #if defined(ARDUINO_ARCH_ESP32) diff --git a/src/hasp_gui.cpp b/src/hasp_gui.cpp index 3650b471..005813e0 100644 --- a/src/hasp_gui.cpp +++ b/src/hasp_gui.cpp @@ -15,7 +15,7 @@ #include "tft_espi_drv.h" #endif -// Touch Driver +// Select Touch Driver //#include "indev/XPT2046_alt_drv.h" #include "indev/XPT2046.h" @@ -69,13 +69,14 @@ static uint16_t guiSleepTime2 = 120; // 1 second resolution static uint8_t guiSleeping = HASP_SLEEP_OFF; static uint8_t guiTickPeriod = 20; static uint8_t guiRotation = TFT_ROTATION; -#if ESP32 > 0 || ESP8266 > 0 +// static TFT_eSPI tft; // = TFT_eSPI(); /* TFT instance */ +static uint16_t calData[5] = {0, 65535, 0, 65535, 0}; + +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) static Ticker tick; /* timer for interrupt handler */ #else static Ticker tick(lv_tick_handler, LVGL_TICK_PERIOD); // guiTickPeriod); #endif -// static TFT_eSPI tft; // = TFT_eSPI(); /* TFT instance */ -static uint16_t calData[5] = {0, 65535, 0, 65535, 0}; bool guiCheckSleep() { @@ -136,7 +137,6 @@ static void IRAM_ATTR my_flush_cb(lv_disp_drv_t * disp, const lv_area_t * area, /* Interrupt driven periodic handler */ static void ICACHE_RAM_ATTR lv_tick_handler(void) { - // Serial.print("."); lv_tick_inc(LVGL_TICK_PERIOD); } @@ -562,7 +562,7 @@ void guiSetup() #if LV_USE_LOG != 0 Log.notice(TAG_LVGL, F("Registering lvgl logging handler")); - lv_log_register_print_cb(debugLvgl); /* register print function for debugging */ + lv_log_register_print_cb(debugLvglLogEvent); /* register print function for debugging */ #endif /* Initialize the display driver */ @@ -639,19 +639,23 @@ void guiSetup() void IRAM_ATTR guiLoop() { + lv_task_handler(); // process animations + #if defined(STM32F4xx) tick.update(); #endif - lv_task_handler(); - - guiCheckSleep(); - #if TOUCH_DRIVER == 1 touch.loop(); #endif } +void guiEverySecond(void) +{ + // check if we went to sleep, wake up is handled in the event handlers + guiCheckSleep(); +} + void guiStart() { /*Initialize the graphics library's tick*/ diff --git a/src/hasp_gui.h b/src/hasp_gui.h index deea91e6..651a1a8e 100644 --- a/src/hasp_gui.h +++ b/src/hasp_gui.h @@ -4,41 +4,33 @@ #ifndef HASP_GUI_H #define HASP_GUI_H -//#include "TFT_eSPI.h" #include "ArduinoJson.h" - #include "lvgl.h" #define HASP_SLEEP_OFF 0 #define HASP_SLEEP_SHORT 1 #define HASP_SLEEP_LONG 2 -// #if defined(ARDUINO_ARCH_ESP8266) -// #include -// void guiTakeScreenshot(ESP8266WebServer & client); -// #endif - -// #if defined(ARDUINO_ARCH_ESP32) -// #include -// void guiTakeScreenshot(WebServer & client); -// #endif // ESP32 - -void guiTakeScreenshot(); - +/* ===== Default Event Processors ===== */ void guiSetup(); -void guiStart(void); void guiLoop(void); +void guiEverySecond(void); +void guiStart(void); void guiStop(void); +/* ===== Special Event Processors ===== */ void guiCalibrate(); -void guiTakeScreenshot(const char * pFileName); +void guiTakeScreenshot(const char * pFileName); // to file +void guiTakeScreenshot(); // webclient +/* ===== Getter and Setter Functions ===== */ void guiSetDim(int8_t level); int8_t guiGetDim(void); void guiSetBacklight(bool lighton); bool guiGetBacklight(); bool guiCheckSleep(); +/* ===== Read/Write Configuration ===== */ bool guiGetConfig(const JsonObject & settings); bool guiSetConfig(const JsonObject & settings); diff --git a/src/hasp_hal.cpp b/src/hasp_hal.cpp index c51f384a..26f21b5b 100644 --- a/src/hasp_hal.cpp +++ b/src/hasp_hal.cpp @@ -88,7 +88,7 @@ String esp32ResetReason(uint8_t cpuid) } #endif -void halRestart(void) +void halRestartMcu(void) { #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) ESP.restart(); diff --git a/src/hasp_hal.h b/src/hasp_hal.h index caf04e84..895bbfa1 100644 --- a/src/hasp_hal.h +++ b/src/hasp_hal.h @@ -6,7 +6,7 @@ #include -void halRestart(void); +void halRestartMcu(void); uint8_t halGetHeapFragmentation(void); String halGetResetInfo(void); size_t halGetMaxFreeBlock(void); diff --git a/src/hasp_http.cpp b/src/hasp_http.cpp index ca9e3297..568802d9 100644 --- a/src/hasp_http.cpp +++ b/src/hasp_http.cpp @@ -130,7 +130,7 @@ bool httpIsAuthenticated(const __FlashStringHelper * fstr_page) #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) Log.notice(TAG_HTTP, F("Sending %S page to client connected from: %s"), fstr_page, - webServer.client().remoteIP().toString().c_str()); + webServer.client().remoteIP().toString().c_str()); #else // Log.trace(TAG_HTTP,F("Sending %s page to client connected from: %s"), page, // String(webServer.client().remoteIP()).c_str()); @@ -1642,7 +1642,7 @@ void httpHandleResetConfig() httpMessage += F("
"); if(resetConfirmed) { // User has confirmed, so reset everything - bool formatted = configClear(); + bool formatted = configClearEeprom(); if(formatted) { httpMessage += F("Resetting all saved settings and restarting device"); } else { @@ -1677,7 +1677,7 @@ void httpHandleResetConfig() } } -void webStart() +void httpStart() { webServer.begin(); webServerStarted = true; @@ -1688,7 +1688,7 @@ void webStart() Log.trace(TAG_HTTP, F("Server started @ http://%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); #else Log.trace(TAG_HTTP, F("Server started @ http://%s"), - (WiFi.getMode() != WIFI_STA ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); + (WiFi.getMode() != WIFI_STA ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); #endif #else IPAddress ip; @@ -1701,7 +1701,7 @@ void webStart() #endif } -void webStop() +void httpStop() { webServer.stop(); webServerStarted = false; @@ -1817,7 +1817,7 @@ void httpSetup() webServer.collectHeaders(headerkeys, headerkeyssize); Log.trace(TAG_HTTP, F("Setup Complete")); - webStart(); + // webStart(); Wait for network connection } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1826,13 +1826,13 @@ void httpReconnect() if(!httpEnable) return; if(webServerStarted) { - webStop(); + httpStop(); } else #if HASP_USE_WIFI > 0 && !defined(STM32F4xx) if(WiFi.status() == WL_CONNECTED || WiFi.getMode() != WIFI_STA) #endif { - webStart(); + httpStart(); } } @@ -1845,7 +1845,7 @@ void httpLoop() //////////////////////////////////////////////////////////////////////////////////////////////////// void httpEvery5Seconds() { - if(httpEnable && !webServerStarted) httpReconnect(); + // if(httpEnable && !webServerStarted) httpReconnect(); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/hasp_http.h b/src/hasp_http.h index 82d78fcc..bfc6277f 100644 --- a/src/hasp_http.h +++ b/src/hasp_http.h @@ -10,9 +10,11 @@ void httpSetup(); void httpLoop(void); void httpEvery5Seconds(void); -void httpReconnect(void); +// void httpReconnect(void); +void httpStart(void); +void httpStop(void); -size_t httpClientWrite(const uint8_t *buf, size_t size); // Screenshot Write Data +size_t httpClientWrite(const uint8_t * buf, size_t size); // Screenshot Write Data bool httpGetConfig(const JsonObject & settings); bool httpSetConfig(const JsonObject & settings); diff --git a/src/hasp_mdns.h b/src/hasp_mdns.h index ad56e12b..eefc8484 100644 --- a/src/hasp_mdns.h +++ b/src/hasp_mdns.h @@ -6,11 +6,13 @@ #include "ArduinoJson.h" +/* ===== Default Event Processors ===== */ void mdnsSetup(); void mdnsLoop(void); void mdnsStart(void); void mdnsStop(void); +/* ===== Read/Write Configuration ===== */ bool mdnsGetConfig(const JsonObject & settings); bool mdnsSetConfig(const JsonObject & settings); diff --git a/src/hasp_mqtt.cpp b/src/hasp_mqtt.cpp index c62b9b54..f071b350 100644 --- a/src/hasp_mqtt.cpp +++ b/src/hasp_mqtt.cpp @@ -323,7 +323,7 @@ void mqttSubscribeTo(const char * format, const char * data) } } -void mqttReconnect() +void mqttStart() { char buffer[128]; char mqttClientId[64]; @@ -342,7 +342,7 @@ void mqttReconnect() // Attempt to connect and set LWT and Clean Session snprintf_P(buffer, sizeof(buffer), PSTR("%sstatus"), mqttNodeTopic); - if(!mqttClient.connect(mqttClientId, mqttUser, mqttPassword, buffer, 0, false, "OFF", true)) { + if(!mqttClient.connect(mqttClientId, mqttUser, mqttPassword, buffer, 0, false, "OFF", true)) { // Literal String // Retry until we give up and restart after connectTimeout seconds mqttReconnectCount++; @@ -398,12 +398,10 @@ void mqttReconnect() mqttCommandTopic = prefix + F("/page"); mqttGroupCommandTopic = "hasp/" + mqttGroupName + "/page"; - mqttSensorTopic = prefix + F("/sensor"); mqttLightCommandTopic = prefix + F("/light/switch"); mqttLightStateTopic = prefix + F("/light/state"); mqttLightBrightCommandTopic = prefix + F("/brightness/set"); mqttLightBrightStateTopic = prefix + F("/brightness/state"); - mqttMotionStateTopic = prefix + F("/motion/state"); */ // Set keepAlive, cleanSession, timeout @@ -469,7 +467,7 @@ void mqttLoop() void mqttEvery5Seconds(bool networkIsConnected) { - if(mqttEnabled && networkIsConnected && !mqttClient.connected()) mqttReconnect(); + if(mqttEnabled && networkIsConnected && !mqttClient.connected()) mqttStart(); } String mqttGetNodename() diff --git a/src/hasp_mqtt.h b/src/hasp_mqtt.h index e362965c..64e9cc04 100644 --- a/src/hasp_mqtt.h +++ b/src/hasp_mqtt.h @@ -9,8 +9,8 @@ void mqttSetup(); void mqttLoop(); void mqttEvery5Seconds(bool wifiIsConnected); +void mqttStart(); void mqttStop(); -void mqttReconnect(); void IRAM_ATTR mqtt_send_state(const __FlashStringHelper * subtopic, const char * payload); void mqtt_send_input(uint8_t id, const char * payload); diff --git a/src/hasp_ota.cpp b/src/hasp_ota.cpp index 11a1eb52..d5848e1d 100644 --- a/src/hasp_ota.cpp +++ b/src/hasp_ota.cpp @@ -32,13 +32,13 @@ std::string otaUrl = "http://ota.netwize.be"; int16_t otaPort = HASP_OTA_PORT; int8_t otaPrecentageComplete = -1; -void otaProgress() +static inline void otaProgress(void) { Log.verbose(TAG_OTA, F("%s update in progress... %3u%"), (ArduinoOTA.getCommand() == U_FLASH ? PSTR("Firmware") : PSTR("Filesystem")), otaPrecentageComplete); } -void otaSetup() +void otaSetup(void) { if(strlen(otaUrl.c_str())) { Log.trace(TAG_OTA, otaUrl.c_str()); @@ -126,12 +126,12 @@ void otaSetup() } } -void otaLoop() +void otaLoop(void) { ArduinoOTA.handle(); } -void otaEverySecond() +void otaEverySecond(void) { if(otaPrecentageComplete >= 0) otaProgress(); } diff --git a/src/hasp_ota.h b/src/hasp_ota.h index 06229d3c..9d023c1f 100644 --- a/src/hasp_ota.h +++ b/src/hasp_ota.h @@ -8,9 +8,12 @@ #include "ArduinoJson.h" -void otaSetup(); +/* ===== Default Event Processors ===== */ +void otaSetup(void); void otaLoop(void); void otaEverySecond(void); + +/* ===== Special Event Processors ===== */ void otaHttpUpdate(const char * espOtaUrl); #endif diff --git a/src/hasp_telnet.cpp b/src/hasp_telnet.cpp index c9f0cc6b..2e1f317b 100644 --- a/src/hasp_telnet.cpp +++ b/src/hasp_telnet.cpp @@ -60,7 +60,7 @@ void telnetClientDisconnect() void telnetClientLogon() { telnetClient.println(); - debugHaspHeader(&telnetClient); + debugPrintHaspHeader(&telnetClient); // telnetClient.println(debugHaspHeader().c_str()); // Send version header telnetLoginState = TELNET_AUTHENTICATED; // User and Pass are correct telnetLoginAttempt = 0; // Reset attempt counter diff --git a/src/hasp_wifi.cpp b/src/hasp_wifi.cpp index 323a6cac..79d8ddac 100644 --- a/src/hasp_wifi.cpp +++ b/src/hasp_wifi.cpp @@ -66,11 +66,11 @@ static void wifiConnected(IPAddress ipaddress) Log.verbose(TAG_WIFI, F("Connected = %s"), WiFi.status() == WL_CONNECTED ? PSTR("yes") : PSTR("no")); haspProgressVal(255); - debugStartSyslog(); - // mqttReconnect(); - // httpReconnect(); - mdnsStart(); haspReconnect(); + debugStartSyslog(); + //mqttStart(); + httpStart(); + mdnsStart(); } static void wifiDisconnected(const char * ssid, uint8_t reason) @@ -81,6 +81,8 @@ static void wifiDisconnected(const char * ssid, uint8_t reason) haspProgressMsg(F("Wifi Disconnected")); debugStopSyslog(); + mqttStop(); + httpStop(); mdnsStop(); if(wifiReconnectCounter > 33) { @@ -356,7 +358,7 @@ bool wifiShowAP(char * ssid, char * pass) return true; } -void wifiReconnect() +static void wifiReconnect(void) { WiFi.disconnect(true); #if defined(ARDUINO_ARCH_ESP8266) diff --git a/src/main.cpp b/src/main.cpp index dbf1f0f5..6fc2e37a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,9 +26,9 @@ void setup() eepromSetup(); // Don't start at boot, only at write #endif -#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 - filesystemSetup(); -#endif +// #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 +// filesystemSetup(); // Done in configSetup() +// #endif #if HASP_USE_SDCARD > 0 sdcardSetup(); @@ -154,13 +154,14 @@ void loop() /* Timer Loop */ if(millis() - mainLastLoopTime >= 1000) { - /* Run Every Second */ + /* Runs Every Second */ #if HASP_USE_OTA > 0 - otaEverySecond(); + otaEverySecond(); // progressbar #endif - debugEverySecond(); + guiEverySecond(); // sleep timer + debugEverySecond(); // statusupdate - /* Run Every 5 Seconds */ + /* Runs Every 5 Seconds */ if(mainLoopCounter == 0 || mainLoopCounter == 5) { #if HASP_USE_WIFI > 0 isConnected = wifiEvery5Seconds(); @@ -171,11 +172,11 @@ void loop() #endif #if HASP_USE_HTTP > 0 - httpEvery5Seconds(); + // httpEvery5Seconds(); #endif #if HASP_USE_MQTT > 0 - mqttEvery5Seconds(isConnected); + mqttEvery5Seconds(isConnected); #endif } @@ -188,5 +189,5 @@ void loop() mainLastLoopTime += 1000; } - delay(2); + delay(3); } \ No newline at end of file