From 09c9e7f066ecc49a0d0a20ccbc8ea09b2eff5283 Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Wed, 17 Feb 2021 05:16:25 +0100 Subject: [PATCH] Add memory functions to device class --- lib/lv_drv_tft_espi/tft_espi_drv.cpp | 57 ++--- src/dev/device.h | 14 +- src/dev/esp32/esp32.cpp | 29 ++- src/dev/esp32/esp32.h | 14 +- src/dev/esp32/lanbonl8.h | 2 +- src/dev/esp32/m5stackcore2.h | 2 +- src/dev/esp8266/esp8266.cpp | 22 +- src/dev/esp8266/esp8266.h | 8 + src/hal/hasp_hal.h | 10 +- src/hasp/hasp_dispatch.cpp | 3 +- src/log/hasp_debug.cpp | 63 ++--- src/svc/hasp_http.cpp | 361 ++++++++++++++------------- 12 files changed, 329 insertions(+), 256 deletions(-) diff --git a/lib/lv_drv_tft_espi/tft_espi_drv.cpp b/lib/lv_drv_tft_espi/tft_espi_drv.cpp index 5f05b867..ebc4136b 100644 --- a/lib/lv_drv_tft_espi/tft_espi_drv.cpp +++ b/lib/lv_drv_tft_espi/tft_espi_drv.cpp @@ -7,23 +7,24 @@ * INCLUDES *********************/ #include "../../src/hal/hasp_hal.h" // for halGpioName() +#include "../../src/dev/device.h" #include "tft_espi_drv.h" #include "ArduinoLog.h" #include "hasp_macro.h" #if USE_TFT_ESPI != 0 - #include - #include "TFT_eSPI.h" +#include +#include "TFT_eSPI.h" - #include LV_DRV_DISP_INCLUDE - #include LV_DRV_DELAY_INCLUDE - #include "bootscreen.h" // Sketch tab header for xbm images +#include LV_DRV_DISP_INCLUDE +#include LV_DRV_DELAY_INCLUDE +#include "bootscreen.h" // Sketch tab header for xbm images - /********************* - * DEFINES - *********************/ - #define TAG_TFT 22 +/********************* + * DEFINES + *********************/ +#define TAG_TFT 22 /********************** * TYPEDEFS @@ -54,11 +55,11 @@ static TFT_eSPI tft; */ void tft_espi_init(uint8_t rotation, bool invert_display) { - #ifdef USE_DMA_TO_TFT +#ifdef USE_DMA_TO_TFT // DMA - should work with STM32F2xx/F4xx/F7xx processors // NOTE: >>>>>> DMA IS FOR SPI DISPLAYS ONLY <<<<<< tft.initDMA(); // Initialise the DMA engine (tested with STM32F446 and STM32F767) - #endif +#endif /* TFT init */ tft.begin(); @@ -77,11 +78,11 @@ void tft_espi_flush(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * c /* Update TFT */ tft.startWrite(); /* Start new TFT transaction */ tft.setWindow(area->x1, area->y1, area->x2, area->y2); /* set the working window */ - #ifdef USE_DMA_TO_TFT +#ifdef USE_DMA_TO_TFT tft.pushPixelsDMA((uint16_t *)color_p, len); /* Write words at once */ - #else +#else tft.pushPixels((uint16_t *)color_p, len); /* Write words at once */ - #endif +#endif tft.endWrite(); /* terminate TFT transaction */ /* Tell lvgl that flushing is done */ @@ -98,7 +99,7 @@ void tft_espi_flush(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * c // tft_espi_flush(x1, y1, x2, y2, color_p); // } - #if defined(TOUCH_CS) +#if defined(TOUCH_CS) void tft_espi_calibrate(uint16_t * calData) { @@ -125,15 +126,15 @@ bool tft_espi_get_touch(int16_t * touchX, int16_t * touchY, uint16_t threshold) { return tft.getTouch((uint16_t *)touchX, (uint16_t *)touchY, threshold); } - #endif +#endif /********************** * STATIC FUNCTIONS **********************/ - #if defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP8266) ADC_MODE(ADC_VCC); // tftShowConfig measures the voltage on the pin - #endif +#endif static void tftOffsetInfo(uint8_t pin, uint8_t x_offset, uint8_t y_offset) { @@ -160,21 +161,21 @@ static void tftShowConfig(TFT_eSPI & tft) tft.getSetup(tftSetup); LOG_VERBOSE(TAG_TFT, F("TFT_eSPI : v%s"), tftSetup.version.c_str()); - #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) LOG_VERBOSE(TAG_TFT, F("Processor : ESP%x"), tftSetup.esp); - #else +#else LOG_VERBOSE(TAG_TFT, F("Processor : STM%x"), tftSetup.esp); - #endif - LOG_VERBOSE(TAG_TFT, F("CPU freq. : %i MHz"), halGetCpuFreqMHz()); +#endif + LOG_VERBOSE(TAG_TFT, F("CPU freq. : %i MHz"), haspDevice.get_cpu_frequency()); - #if defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP8266) LOG_VERBOSE(TAG_TFT, F("Voltage : %2.2f V"), ESP.getVcc() / 918.0); // 918 empirically determined - #endif +#endif LOG_VERBOSE(TAG_TFT, F("Transactns : %s"), (tftSetup.trans == 1) ? PSTR("Yes") : PSTR("No")); LOG_VERBOSE(TAG_TFT, F("Interface : %s"), (tftSetup.serial == 1) ? PSTR("SPI") : PSTR("Parallel")); - #if defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP8266) LOG_VERBOSE(TAG_TFT, F("SPI overlap: %s"), (tftSetup.overlap == 1) ? PSTR("Yes") : PSTR("No")); - #endif +#endif if(tftSetup.tft_driver != 0xE9D) // For ePaper displays the size is defined in the sketch { @@ -201,7 +202,7 @@ static void tftShowConfig(TFT_eSPI & tft) tftPinInfo(F("MISO"), tftSetup.pin_tft_miso); tftPinInfo(F("SCLK"), tftSetup.pin_tft_clk); - #if defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP8266) if(tftSetup.overlap == true) { LOG_VERBOSE(TAG_TFT, F("Overlap selected, following pins MUST be used:")); @@ -212,7 +213,7 @@ static void tftShowConfig(TFT_eSPI & tft) LOG_VERBOSE(TAG_TFT, F("TFT_DC and TFT_RST pins can be tftSetup defined")); } - #endif +#endif tftPinInfo(F("TFT_CS"), tftSetup.pin_tft_cs); tftPinInfo(F("TFT_DC"), tftSetup.pin_tft_dc); diff --git a/src/dev/device.h b/src/dev/device.h index 6350dea6..1fa36ea4 100644 --- a/src/dev/device.h +++ b/src/dev/device.h @@ -39,19 +39,27 @@ class BaseDevice { { return true; } + virtual size_t get_free_max_block() + {} + virtual size_t get_free_heap() + {} + virtual uint8_t get_heap_fragmentation() + {} + virtual uint16_t get_cpu_frequency() + {} }; } // namespace dev #if defined(ESP32) #warning Building for ESP32 Devices -#include "dev/esp32/esp32.h" +#include "esp32/esp32.h" #elif defined(ESP8266) #warning Building for ESP8266 Devices -#include "dev/esp8266/esp8266.h" +#include "esp8266/esp8266.h" #elif defined(STM32F4) #warning Building for STM32F4xx Devices -#include "dev/stm32f4.h" +#include "stm32f4/stm32f4.h" #else #warning Building for Generic Devices using dev::BaseDevice; diff --git a/src/dev/esp32/esp32.cpp b/src/dev/esp32/esp32.cpp index b317aafa..d4f6a9ef 100644 --- a/src/dev/esp32/esp32.cpp +++ b/src/dev/esp32/esp32.cpp @@ -4,8 +4,8 @@ #include #include "esp_system.h" -#include "dev/device.h" -#include "dev/esp32/esp32.h" +#include "../device.h" +#include "esp32.h" #include "driver/adc.h" #include "esp_adc_cal.h" @@ -69,6 +69,31 @@ void Esp32Device::update_backlight() } } +size_t Esp32Device::get_free_max_block() +{ + return ESP.getMaxAllocHeap(); +} + +size_t Esp32Device::get_free_heap() +{ + return ESP.getFreeHeap(); +} + +uint8_t Esp32Device::get_heap_fragmentation() +{ + uint32_t free = ESP.getFreeHeap(); + if(free) { + return (int8_t)(100.00f - (float)ESP.getMaxAllocHeap() * 100.00f / (float)free); + } else { + return 100; // no free memory + } +} + +uint16_t Esp32Device::get_cpu_frequency() +{ + return ESP.getCpuFreqMHz(); +} + } // namespace dev #if defined(LANBONL8) diff --git a/src/dev/esp32/esp32.h b/src/dev/esp32/esp32.h index 3c9bd449..7c950867 100644 --- a/src/dev/esp32/esp32.h +++ b/src/dev/esp32/esp32.h @@ -4,7 +4,7 @@ #ifndef HASP_DEVICE_ESP32_H #define HASP_DEVICE_ESP32_H -#include "dev/device.h" +#include "../device.h" #if defined(ESP32) @@ -25,6 +25,14 @@ class Esp32Device : public BaseDevice { bool get_backlight_power() override; + size_t get_free_max_block() override; + + size_t get_free_heap() override; + + uint8_t get_heap_fragmentation() override; + + uint16_t get_cpu_frequency() override; + private: uint8_t backlight_pin; uint8_t backlight_level; @@ -39,10 +47,10 @@ using dev::Esp32Device; #if defined(LANBONL8) #warning Building for Lanbon L8 -#include "dev/esp32/lanbonl8.h" +#include "lanbonl8.h" #elif defined(M5STACK) #warning Building for M5Stack core2 -#include "dev/esp32/m5stackcore2.h" +#include "m5stackcore2.h" #else extern dev::Esp32Device haspDevice; #endif diff --git a/src/dev/esp32/lanbonl8.h b/src/dev/esp32/lanbonl8.h index 0c1ffc53..8cf07a5f 100644 --- a/src/dev/esp32/lanbonl8.h +++ b/src/dev/esp32/lanbonl8.h @@ -4,7 +4,7 @@ #ifndef HASP_DEVICE_LANBONL8_H #define HASP_DEVICE_LANBONL8_H -#include "dev/esp32/esp32.h" +#include "esp32.h" #if defined(LANBONL8) diff --git a/src/dev/esp32/m5stackcore2.h b/src/dev/esp32/m5stackcore2.h index 8060530a..12187b5d 100644 --- a/src/dev/esp32/m5stackcore2.h +++ b/src/dev/esp32/m5stackcore2.h @@ -6,7 +6,7 @@ #if defined(M5STACK) -#include "dev/esp32/esp32.h" +#include "esp32.h" namespace dev { diff --git a/src/dev/esp8266/esp8266.cpp b/src/dev/esp8266/esp8266.cpp index 029139eb..95aa5795 100644 --- a/src/dev/esp8266/esp8266.cpp +++ b/src/dev/esp8266/esp8266.cpp @@ -3,7 +3,7 @@ #include "Arduino.h" #include -#include "dev/esp8266/esp8266.h" +#include "esp8266.h" #include "hasp_conf.h" #include "hasp_debug.h" @@ -59,6 +59,26 @@ void Esp8266Device::update_backlight() analogWrite(backlight_pin, backlight_power ? map(backlight_level, 0, 100, 0, 1023) : 0); } +size_t Esp8266Device::get_free_max_block() +{ + return ESP.getMaxFreeBlockSize(); +} + +size_t Esp8266Device::get_free_heap(void) +{ + return ESP.getFreeHeap(); +} + +uint8_t Esp8266Device::get_heap_fragmentation() +{ + return ESP.getHeapFragmentation(); +} + +uint16_t Esp8266Device::get_cpu_frequency() +{ + return ESP.getCpuFreqMHz(); +} + } // namespace dev dev::Esp8266Device haspDevice; diff --git a/src/dev/esp8266/esp8266.h b/src/dev/esp8266/esp8266.h index e90b486a..29a57ef3 100644 --- a/src/dev/esp8266/esp8266.h +++ b/src/dev/esp8266/esp8266.h @@ -26,6 +26,14 @@ class Esp8266Device : public BaseDevice { bool get_backlight_power() override; + size_t get_free_max_block() override; + + size_t get_free_heap() override; + + uint8_t get_heap_fragmentation() override; + + uint16_t get_cpu_frequency() override; + private: uint8_t backlight_pin; uint8_t backlight_level; diff --git a/src/hal/hasp_hal.h b/src/hal/hasp_hal.h index 5c735bab..0ccd03df 100644 --- a/src/hal/hasp_hal.h +++ b/src/hal/hasp_hal.h @@ -6,15 +6,15 @@ #include -void halRestartMcu(void); -uint8_t halGetHeapFragmentation(void); +// void halRestartMcu(void); String halGetResetInfo(void); -size_t halGetMaxFreeBlock(void); -size_t halGetFreeHeap(void); +// uint8_t halGetHeapFragmentation(void); +// size_t halGetMaxFreeBlock(void); +// size_t halGetFreeHeap(void); String halGetCoreVersion(void); String halGetChipModel(); String halGetMacAddress(int start, const char * seperator); -uint16_t halGetCpuFreqMHz(void); +// uint16_t halGetCpuFreqMHz(void); String halDisplayDriverName(void); String halGpioName(uint8_t gpio); diff --git a/src/hasp/hasp_dispatch.cpp b/src/hasp/hasp_dispatch.cpp index 2931e518..77fd108a 100644 --- a/src/hasp/hasp_dispatch.cpp +++ b/src/hasp/hasp_dispatch.cpp @@ -875,7 +875,8 @@ void dispatch_reboot(bool saveConfig) Serial.flush(); #endif - halRestartMcu(); + // halRestartMcu(); + haspDevice.reboot(); } void dispatch_current_state() diff --git a/src/log/hasp_debug.cpp b/src/log/hasp_debug.cpp index 1aece68b..a7183f3a 100644 --- a/src/log/hasp_debug.cpp +++ b/src/log/hasp_debug.cpp @@ -19,15 +19,16 @@ //#include "time.h" #if defined(ARDUINO_ARCH_ESP8266) - #include // sntp_servermode_dhcp() - #include - #include +#include // sntp_servermode_dhcp() +#include +#include #elif defined(ARDUINO_ARCH_ESP32) - #include - #include +#include +#include #endif #include "hasp_conf.h" +#include "dev/device.h" #include "hal/hasp_hal.h" #include "hasp_debug.h" @@ -37,27 +38,27 @@ #include "hasp/hasp.h" #ifdef USE_CONFIG_OVERRIDE - #include "user_config_override.h" +#include "user_config_override.h" #endif #ifndef SERIAL_SPEED - #define SERIAL_SPEED 115200 +#define SERIAL_SPEED 115200 #endif #if HASP_USE_SYSLOG > 0 - #include +#include - #ifndef SYSLOG_SERVER - #define SYSLOG_SERVER "" - #endif +#ifndef SYSLOG_SERVER +#define SYSLOG_SERVER "" +#endif - #ifndef SYSLOG_PORT - #define SYSLOG_PORT 514 - #endif +#ifndef SYSLOG_PORT +#define SYSLOG_PORT 514 +#endif - #ifndef APP_NAME - #define APP_NAME "HASP" - #endif +#ifndef APP_NAME +#define APP_NAME "HASP" +#endif // variables for debug stream writer // static String debugOutput((char *)0); @@ -72,7 +73,7 @@ uint8_t debugSyslogProtocol = 0; // A UDP instance to let us send and receive packets over UDP WiFiUDP * syslogClient; - #define SYSLOG_PROTO_IETF 0 +#define SYSLOG_PROTO_IETF 0 // Create a new syslog instance with LOG_KERN facility // Syslog syslog(syslogClient, SYSLOG_SERVER, SYSLOG_PORT, MQTT_CLIENT, APP_NAME, LOG_KERN); @@ -207,7 +208,7 @@ bool debugGetConfig(const JsonObject & settings) if(debugTelePeriod != settings[FPSTR(FP_DEBUG_TELEPERIOD)].as()) changed = true; settings[FPSTR(FP_DEBUG_TELEPERIOD)] = debugTelePeriod; - #if HASP_USE_SYSLOG > 0 +#if HASP_USE_SYSLOG > 0 if(strcmp(debugSyslogHost, settings[FPSTR(FP_CONFIG_HOST)].as().c_str()) != 0) changed = true; settings[FPSTR(FP_CONFIG_HOST)] = debugSyslogHost; @@ -219,7 +220,7 @@ bool debugGetConfig(const JsonObject & settings) if(debugSyslogFacility != settings[FPSTR(FP_CONFIG_LOG)].as()) changed = true; settings[FPSTR(FP_CONFIG_LOG)] = debugSyslogFacility; - #endif +#endif if(changed) configOutput(settings, TAG_DEBG); return changed; @@ -244,8 +245,8 @@ bool debugSetConfig(const JsonObject & settings) /* Teleperiod Settings*/ changed |= configSet(debugTelePeriod, settings[FPSTR(FP_DEBUG_TELEPERIOD)], F("debugTelePeriod")); - /* Syslog Settings*/ - #if HASP_USE_SYSLOG > 0 +/* Syslog Settings*/ +#if HASP_USE_SYSLOG > 0 if(!settings[FPSTR(FP_CONFIG_HOST)].isNull()) { changed |= strcmp(debugSyslogHost, settings[FPSTR(FP_CONFIG_HOST)]) != 0; strncpy(debugSyslogHost, settings[FPSTR(FP_CONFIG_HOST)], sizeof(debugSyslogHost)); @@ -253,7 +254,7 @@ bool debugSetConfig(const JsonObject & settings) changed |= configSet(debugSyslogPort, settings[FPSTR(FP_CONFIG_PORT)], F("debugSyslogPort")); changed |= configSet(debugSyslogProtocol, settings[FPSTR(FP_CONFIG_PROTOCOL)], F("debugSyslogProtocol")); changed |= configSet(debugSyslogFacility, settings[FPSTR(FP_CONFIG_LOG)], F("debugSyslogFacility")); - #endif +#endif return changed; } @@ -369,9 +370,9 @@ static void debugPrintTimestamp(int level, Print * _logOutput) static void debugPrintHaspMemory(int level, Print * _logOutput) { - size_t maxfree = halGetMaxFreeBlock(); - uint32_t totalfree = halGetFreeHeap(); - uint8_t frag = halGetHeapFragmentation(); + size_t maxfree = haspDevice.get_free_max_block(); + size_t totalfree = haspDevice.get_free_heap(); + uint8_t frag = haspDevice.get_heap_fragmentation(); /* Print HASP Memory Info */ if(debugAnsiCodes) { @@ -411,7 +412,7 @@ static void debugPrintPriority(int level, Print * _logOutput) // } switch(level) { - case LOG_LEVEL_FATAL...LOG_LEVEL_ERROR: + case LOG_LEVEL_FATAL ... LOG_LEVEL_ERROR: debugSendAnsiCode(F(TERM_COLOR_RED), _logOutput); break; case LOG_LEVEL_WARNING: @@ -572,9 +573,9 @@ void debugPrintPrefix(uint8_t tag, int level, Print * _logOutput) } debugPrintHaspMemory(level, _logOutput); - #if LV_MEM_CUSTOM == 0 +#if LV_MEM_CUSTOM == 0 debugPrintLvglMemory(level, _logOutput); - #endif +#endif } return; } @@ -637,10 +638,10 @@ void debugPreSetup(JsonObject settings) if(baudrate >= 9600u) { /* the baudrates are stored divided by 10 */ #if defined(STM32F4xx) - #ifndef STM32_SERIAL1 // Define what Serial port to use for log output +#ifndef STM32_SERIAL1 // Define what Serial port to use for log output Serial.setRx(PA3); // User Serial2 Serial.setTx(PA2); - #endif +#endif #endif Serial.begin(baudrate); /* prepare for possible serial debug */ delay(10); diff --git a/src/svc/hasp_http.cpp b/src/svc/hasp_http.cpp index 194805ef..f4751797 100644 --- a/src/svc/hasp_http.cpp +++ b/src/svc/hasp_http.cpp @@ -7,10 +7,11 @@ #include "lvgl.h" #if defined(ARDUINO_ARCH_ESP32) - #include "Update.h" +#include "Update.h" #endif #include "hasp_conf.h" +#include "dev/device.h" #include "hasp_gui.h" #include "hal/hasp_hal.h" @@ -23,9 +24,9 @@ #if HASP_USE_HTTP > 0 - #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) File fsUploadFile; - #endif +#endif //////////////////////////////////////////////////////////////////////////////////////////////////// bool webServerStarted = false; @@ -36,31 +37,31 @@ bool webServerStarted = false; // char httpPassword[32] = ""; hasp_http_config_t http_config; - #define HTTP_PAGE_SIZE (6 * 256) +#define HTTP_PAGE_SIZE (6 * 256) - #if defined(STM32F4xx) && HASP_USE_ETHERNET > 0 - #include +#if defined(STM32F4xx) && HASP_USE_ETHERNET > 0 +#include EthernetWebServer webServer(80); - #endif +#endif - #if defined(STM32F4xx) && HASP_USE_WIFI > 0 - #include +#if defined(STM32F4xx) && HASP_USE_WIFI > 0 +#include // #include EthernetWebServer webServer(80); - #endif +#endif - #if defined(ARDUINO_ARCH_ESP8266) - #include "StringStream.h" - #include - #include +#if defined(ARDUINO_ARCH_ESP8266) +#include "StringStream.h" +#include +#include ESP8266WebServer webServer(80); - #endif +#endif - #if defined(ARDUINO_ARCH_ESP32) - #include - #include +#if defined(ARDUINO_ARCH_ESP32) +#include +#include WebServer webServer(80); - #endif // ESP32 +#endif // ESP32 HTTPUpload * upload; @@ -112,12 +113,11 @@ const char HTTP_FOOTER[] PROGMEM = " by Francis Van Roie"; // // Default link to compiled Nextion firmware images // String lcdFirmwareUrl = "http://haswitchplate.com/update/HASwitchPlate.tft"; - #if HASP_USE_MQTT > 0 +#if HASP_USE_MQTT > 0 extern char mqttNodeName[16]; - #else +#else char mqttNodeName[3] = "na"; - #endif - +#endif //////////////////////////////////////////////////////////////////////////////////////////////////// String getOption(int value, String label, bool selected) @@ -185,13 +185,13 @@ bool httpIsAuthenticated(const __FlashStringHelper * fstr_page) } } - #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) LOG_TRACE(TAG_HTTP, F("Sending %S page to client connected from: %s"), fstr_page, - webServer.client().remoteIP().toString().c_str()); - #else - // LOG_INFO(TAG_HTTP,F("Sending %s page to client connected from: %s"), page, - // String(webServer.client().remoteIP()).c_str()); - #endif + webServer.client().remoteIP().toString().c_str()); +#else + // LOG_INFO(TAG_HTTP,F("Sending %s page to client connected from: %s"), page, + // String(webServer.client().remoteIP()).c_str()); +#endif return true; } @@ -201,15 +201,15 @@ void webSendFooter() char buffer[16]; haspGetVersion(buffer, sizeof(buffer)); - #if defined(STM32F4xx) +#if defined(STM32F4xx) webServer.sendContent(HTTP_END); webServer.sendContent(buffer); webServer.sendContent(HTTP_FOOTER); - #else +#else webServer.sendContent_P(HTTP_END); webServer.sendContent(buffer); webServer.sendContent_P(HTTP_FOOTER); - #endif +#endif } void webSendPage(char * nodename, uint32_t httpdatalength, bool gohome = false) @@ -235,29 +235,29 @@ void webSendPage(char * nodename, uint32_t httpdatalength, bool gohome = false) } webServer.setContentLength(contentLength + httpdatalength); - #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) webServer.send_P(200, PSTR("text/html"), HTTP_DOCTYPE); // 122 - #else +#else webServer.send(200, ("text/html"), HTTP_DOCTYPE); // 122 - #endif +#endif snprintf_P(buffer, sizeof(buffer), HTTP_HEADER, nodename); webServer.sendContent(buffer); // 17-2+len } - #if defined(STM32F4xx) +#if defined(STM32F4xx) webServer.sendContent(HTTP_SCRIPT); // 131 webServer.sendContent(HTTP_STYLE); // 487 // webServer.sendContent(HASP_STYLE); // 145 if(gohome) webServer.sendContent(HTTP_META_GO_BACK); // 47 webServer.sendContent(HTTP_HEADER_END); // 80 - #else +#else webServer.sendContent_P(HTTP_SCRIPT); // 131 webServer.sendContent_P(HTTP_STYLE); // 487 // webServer.sendContent_P(HASP_STYLE); // 145 if(gohome) webServer.sendContent_P(HTTP_META_GO_BACK); // 47 webServer.sendContent_P(HTTP_HEADER_END); // 80 - #endif +#endif } void saveConfig() @@ -272,10 +272,10 @@ void saveConfig() if(save == String(PSTR("hasp"))) { haspSetConfig(settings.as()); - #if HASP_USE_MQTT > 0 +#if HASP_USE_MQTT > 0 } else if(save == String(PSTR("mqtt"))) { mqttSetConfig(settings.as()); - #endif +#endif } else if(save == String(PSTR("gui"))) { settings[FPSTR(FP_GUI_POINTER)] = webServer.hasArg(PSTR("cur")); @@ -291,10 +291,10 @@ void saveConfig() // Password might have changed if(!httpIsAuthenticated(F("config"))) return; - #if HASP_USE_WIFI > 0 +#if HASP_USE_WIFI > 0 } else if(save == String(PSTR("wifi"))) { wifiSetConfig(settings.as()); - #endif +#endif } } } @@ -326,13 +326,13 @@ void webHandleRoot() httpMessage += F("

"); - #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 +#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 if(HASP_FS.exists(F("/edit.htm.gz"))) { httpMessage += F("

"); } - #endif +#endif httpMessage += F("

"); @@ -455,15 +455,15 @@ void webHandleAbout() F("

ArduinoLog

Copyright© 2017,2018 Thijs Elenbaas, MrRobot62, rahuldeo2047, NOX73, " "dhylands, Josha blemasle, mfalkvidd"); httpMessage += FPSTR(MIT_LICENSE); - #if HASP_USE_SYSLOG > 0 - // Replaced with WiFiUDP client - // httpMessage += F("

Syslog

Copyright© 2016 Martin Sloup"); - // httpMessage += FPSTR(MIT_LICENSE); - #endif - #if HASP_USE_QRCODE > 0 +#if HASP_USE_SYSLOG > 0 + // Replaced with WiFiUDP client + // httpMessage += F("

Syslog

Copyright© 2016 Martin Sloup"); + // httpMessage += FPSTR(MIT_LICENSE); +#endif +#if HASP_USE_QRCODE > 0 httpMessage += F("

QR Code generator

Copyright© Project Nayuki"); httpMessage += FPSTR(MIT_LICENSE); - #endif +#endif httpMessage += F("

AceButton

Copyright© 2018 Brian T. Park"); httpMessage += FPSTR(MIT_LICENSE); @@ -527,12 +527,12 @@ void webHandleInfo() httpMessage += F("s"); httpMessage += F("
Free Memory: "); - Utilities::format_bytes(halGetFreeHeap(), size_buf, sizeof(size_buf)); + Utilities::format_bytes(haspDevice.get_free_heap(), size_buf, sizeof(size_buf)); httpMessage += size_buf; httpMessage += F("
Memory Fragmentation: "); - httpMessage += String(halGetHeapFragmentation()); + httpMessage += String(haspDevice.get_heap_fragmentation()); - #if ARDUINO_ARCH_ESP32 +#if ARDUINO_ARCH_ESP32 if(psramFound()) { httpMessage += F("
Free PSRam: "); Utilities::format_bytes(ESP.getFreePsram(), size_buf, sizeof(size_buf)); @@ -541,7 +541,7 @@ void webHandleInfo() Utilities::format_bytes(ESP.getPsramSize(), size_buf, sizeof(size_buf)); httpMessage += size_buf; } - #endif +#endif /* LVGL Stats */ lv_mem_monitor_t mem_mon; @@ -562,7 +562,7 @@ void webHandleInfo() httpMessage += String(haspGetPage()); /* Wifi Stats */ - #if HASP_USE_WIFI > 0 +#if HASP_USE_WIFI > 0 httpMessage += F("

SSID: "); httpMessage += String(WiFi.SSID()); httpMessage += F("
Signal Strength: "); @@ -582,7 +582,7 @@ void webHandleInfo() } else { httpMessage += F("Very Bad)"); } - #if defined(STM32F4xx) +#if defined(STM32F4xx) byte mac[6]; WiFi.macAddress(mac); char macAddress[16]; @@ -594,7 +594,7 @@ void webHandleInfo() httpMessage += String(WiFi.gatewayIP()); httpMessage += F("
MAC Address: "); httpMessage += String(macAddress); - #else +#else httpMessage += F("
IP Address: "); httpMessage += String(WiFi.localIP().toString()); httpMessage += F("
Gateway: "); @@ -603,10 +603,10 @@ void webHandleInfo() httpMessage += String(WiFi.dnsIP().toString()); httpMessage += F("
MAC Address: "); httpMessage += String(WiFi.macAddress()); - #endif - #endif - #if HASP_USE_ETHERNET > 0 - #if defined(ARDUINO_ARCH_ESP32) +#endif +#endif +#if HASP_USE_ETHERNET > 0 +#if defined(ARDUINO_ARCH_ESP32) httpMessage += F("

Ethernet: "); httpMessage += String(ETH.linkSpeed()); httpMessage += F(" Mbps"); @@ -621,10 +621,10 @@ void webHandleInfo() httpMessage += String(ETH.dnsIP().toString()); httpMessage += F("
MAC Address: "); httpMessage += String(ETH.macAddress()); - #endif - #endif - /* Mqtt Stats */ - #if HASP_USE_MQTT > 0 +#endif +#endif +/* Mqtt Stats */ +#if HASP_USE_MQTT > 0 httpMessage += F("

MQTT Status: "); if(mqttIsConnected()) { // Check MQTT connection httpMessage += F("Connected"); @@ -642,16 +642,16 @@ void webHandleInfo() httpMessage += mqttClientId; } - #endif // MQTT +#endif // MQTT /* ESP Stats */ httpMessage += F("

MCU Model: "); httpMessage += halGetChipModel(); httpMessage += F("
CPU Frequency: "); - httpMessage += String(halGetCpuFreqMHz()); + httpMessage += String(haspDevice.get_cpu_frequency()); httpMessage += F("MHz"); - #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) httpMessage += F("
Flash Chip Size: "); Utilities::format_bytes(ESP.getFlashChipSize(), size_buf, sizeof(size_buf)); httpMessage += size_buf; @@ -663,14 +663,14 @@ void webHandleInfo() httpMessage += F("
Free Program Space: "); Utilities::format_bytes(ESP.getFreeSketchSpace(), size_buf, sizeof(size_buf)); httpMessage += size_buf; - #endif +#endif //#if defined(ARDUINO_ARCH_ESP32) // httpMessage += F("
ESP SDK version: "); // httpMessage += String(ESP.getSdkVersion()); //#else httpMessage += F("
Core version: "); - httpMessage += String(halGetCoreVersion()); + httpMessage += halGetCoreVersion(); //#endif httpMessage += F("
Last Reset: "); httpMessage += halGetResetInfo(); @@ -768,20 +768,20 @@ void webUploadProgress() haspProgressVal(t); } - #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) static inline void webUpdatePrintError() { - #if defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP8266) String output((char *)0); output.reserve(128); StringStream stream((String &)output); Update.printError(stream); // ESP8266 only has printError() LOG_ERROR(TAG_HTTP, output.c_str()); haspProgressMsg(output.c_str()); - #elif defined(ARDUINO_ARCH_ESP32) +#elif defined(ARDUINO_ARCH_ESP32) LOG_ERROR(TAG_HTTP, Update.errorString()); // ESP32 has errorString() haspProgressMsg(Update.errorString()); - #endif +#endif } void webUpdateReboot() @@ -839,9 +839,9 @@ void webHandleFirmwareUpload() } } } - #endif +#endif - #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 +#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 bool handleFileRead(String path) { if(!httpIsAuthenticated(F("fileread"))) return false; @@ -973,7 +973,7 @@ void handleFileList() LOG_TRACE(TAG_HTTP, F("handleFileList: %s"), path.c_str()); path.clear(); - #if defined(ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_ARCH_ESP32) File root = HASP_FS.open("/", FILE_READ); File file = root.openNextFile(); String output = "["; @@ -998,7 +998,7 @@ void handleFileList() } output += "]"; webServer.send(200, PSTR("text/json"), output); - #elif defined(ARDUINO_ARCH_ESP8266) +#elif defined(ARDUINO_ARCH_ESP8266) Dir dir = HASP_FS.openDir(path); String output = "["; while(dir.next()) { @@ -1020,24 +1020,24 @@ void handleFileList() } output += "]"; webServer.send(200, PSTR("text/json"), output); - #endif +#endif } - #endif +#endif - //////////////////////////////////////////////////////////////////////////////////////////////////// - #if HASP_USE_CONFIG > 0 +//////////////////////////////////////////////////////////////////////////////////////////////////// +#if HASP_USE_CONFIG > 0 void webHandleConfig() { // http://plate01/config if(!httpIsAuthenticated(F("config"))) return; saveConfig(); - // Reboot after saving wifi config in AP mode - #if HASP_USE_WIFI > 0 && !defined(STM32F4xx) +// Reboot after saving wifi config in AP mode +#if HASP_USE_WIFI > 0 && !defined(STM32F4xx) if(WiFi.getMode() != WIFI_STA) { httpHandleReboot(); } - #endif +#endif { String httpMessage((char *)0); @@ -1046,15 +1046,15 @@ void webHandleConfig() httpMessage += httpGetNodename(); httpMessage += F("


"); - #if HASP_USE_WIFI > 0 +#if HASP_USE_WIFI > 0 httpMessage += F("

"); - #endif +#endif - #if HASP_USE_MQTT > 0 +#if HASP_USE_MQTT > 0 httpMessage += F("

"); - #endif +#endif httpMessage += F("

"); @@ -1062,14 +1062,14 @@ void webHandleConfig() httpMessage += F("

"); - // httpMessage += - // F("

"); + // httpMessage += + // F("

"); - #if HASP_USE_GPIO > 0 +#if HASP_USE_GPIO > 0 httpMessage += F("

"); - #endif +#endif httpMessage += F("

"); @@ -1087,8 +1087,8 @@ void webHandleConfig() webSendFooter(); } - //////////////////////////////////////////////////////////////////////////////////////////////////// - #if HASP_USE_MQTT > 0 +//////////////////////////////////////////////////////////////////////////////////////////////////// +#if HASP_USE_MQTT > 0 void webHandleMqttConfig() { // http://plate01/config/mqtt if(!httpIsAuthenticated(F("config/mqtt"))) return; @@ -1129,7 +1129,8 @@ void webHandleMqttConfig() F("'>

"); add_form_button(httpMessage, F("↩ " D_HTTP_CONFIGURATION), F("/config"), F("")); - // httpMessage += PSTR("

"); webSendPage(httpGetNodename(), httpMessage.length(), false); @@ -1138,7 +1139,7 @@ void webHandleMqttConfig() // httpMessage.clear(); webSendFooter(); } - #endif +#endif //////////////////////////////////////////////////////////////////////////////////////////////////// void webHandleGuiConfig() @@ -1190,7 +1191,7 @@ void webHandleGuiConfig() int8_t bcklpin = settings[FPSTR(FP_GUI_BACKLIGHTPIN)].as(); httpMessage += F("

Backlight Control

"); add_button(httpMessage, F(D_HTTP_SAVE_SETTINGS), F("name='save' value='gui'")); close_form(httpMessage); - // httpMessage += - // F("

"); + // httpMessage += + // F("

"); - #if TOUCH_DRIVER == 2046 && defined(TOUCH_CS) +#if TOUCH_DRIVER == 2046 && defined(TOUCH_CS) add_form_button(httpMessage, F(D_HTTP_CALIBRATE), F("/config/gui"), F("name='action' value='calibrate'")); - // httpMessage += PSTR("

"); - #endif +// httpMessage += PSTR("

"); +#endif add_form_button(httpMessage, F("↩ " D_HTTP_CONFIGURATION), F("/config"), F("")); @@ -1235,8 +1236,8 @@ void webHandleGuiConfig() if(webServer.hasArg(F("action"))) dispatch_text_line(webServer.arg(F("action")).c_str()); } - //////////////////////////////////////////////////////////////////////////////////////////////////// - #if HASP_USE_WIFI > 0 +//////////////////////////////////////////////////////////////////////////////////////////////////// +#if HASP_USE_WIFI > 0 void webHandleWifiConfig() { // http://plate01/config/wifi if(!httpIsAuthenticated(F("config/wifi"))) return; @@ -1262,28 +1263,28 @@ void webHandleWifiConfig() httpMessage += F("'>

"); - #if HASP_USE_WIFI > 0 && !defined(STM32F4xx) +#if HASP_USE_WIFI > 0 && !defined(STM32F4xx) if(WiFi.getMode() == WIFI_STA) { add_form_button(httpMessage, F("↩ " D_HTTP_CONFIGURATION), F("/config"), F("")); // httpMessage += PSTR("

"); } - #endif +#endif webSendPage(httpGetNodename(), httpMessage.length(), false); webServer.sendContent(httpMessage); - #if defined(STM32F4xx) +#if defined(STM32F4xx) httpMessage = ""; - #else +#else httpMessage.clear(); - #endif +#endif webSendFooter(); } - #endif +#endif - //////////////////////////////////////////////////////////////////////////////////////////////////// - #if HASP_USE_HTTP > 0 +//////////////////////////////////////////////////////////////////////////////////////////////////// +#if HASP_USE_HTTP > 0 void webHandleHttpConfig() { // http://plate01/config/http if(!httpIsAuthenticated(F("config/http"))) return; @@ -1340,10 +1341,10 @@ void webHandleHttpConfig() // httpMessage.clear(); webSendFooter(); } - #endif +#endif - //////////////////////////////////////////////////////////////////////////////////////////////////// - #if defined(HASP_USE_GPIO) && (HASP_USE_GPIO > 0) +//////////////////////////////////////////////////////////////////////////////////////////////////// +#if defined(HASP_USE_GPIO) && (HASP_USE_GPIO > 0) void webHandleGpioConfig() { // http://plate01/config/gpio if(!httpIsAuthenticated(F("config/gpio"))) return; @@ -1571,7 +1572,7 @@ void webHandleGpioOptions() if(webServer.hasArg(F("action"))) dispatch_text_line(webServer.arg(F("action")).c_str()); // Security check } - #endif // HASP_USE_GPIO +#endif // HASP_USE_GPIO //////////////////////////////////////////////////////////////////////////////////////////////////// void webHandleDebugConfig() @@ -1604,7 +1605,7 @@ void webHandleDebugConfig() httpMessage += settings[FPSTR(FP_DEBUG_TELEPERIOD)].as(); httpMessage += F("'>

"); - #if HASP_USE_SYSLOG > 0 +#if HASP_USE_SYSLOG > 0 httpMessage += F("Syslog Hostame (optional)() == 1) httpMessage += F(" checked"); httpMessage += F(">BSD (RFC 3164)"); - #endif +#endif httpMessage += F("

"); @@ -1664,24 +1665,24 @@ void webHandleHaspConfig() httpMessage += F("

UI Theme (required)
"); httpMessage += F("Hue

"); httpMessage += F("

Default Font

"); httpMessage += F("

Startup Layout (optional) 0 || HASP_USE_LITTLEFS > 0 +#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 if(handleFileRead(webServer.uri())) return; - #endif +#endif - #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) LOG_TRACE(TAG_HTTP, F("Sending 404 to client connected from: %s"), - webServer.client().remoteIP().toString().c_str()); - #else + webServer.client().remoteIP().toString().c_str()); +#else // LOG_TRACE(TAG_HTTP,F("Sending 404 to client connected from: %s"), String(webServer.client().remoteIP()).c_str()); - #endif +#endif String httpMessage((char *)0); httpMessage.reserve(HTTP_PAGE_SIZE); @@ -1828,8 +1829,8 @@ void httpHandleEspFirmware() // espStartOta(webServer.arg("espFirmware")); } - //////////////////////////////////////////////////////////////////////////////////////////////////// - #if HASP_USE_CONFIG > 0 +//////////////////////////////////////////////////////////////////////////////////////////////////// +#if HASP_USE_CONFIG > 0 void webHandleSaveConfig() { if(!httpIsAuthenticated(F("saveConfig"))) return; @@ -1888,30 +1889,30 @@ void httpHandleResetConfig() dispatch_reboot(false); // Do not save the current config } } - #endif // HASP_USE_CONFIG +#endif // HASP_USE_CONFIG void httpStart() { webServer.begin(); webServerStarted = true; - #if HASP_USE_WIFI > 0 - #if defined(STM32F4xx) +#if HASP_USE_WIFI > 0 +#if defined(STM32F4xx) IPAddress ip; ip = WiFi.localIP(); LOG_INFO(TAG_HTTP, F(D_SERVICE_STARTED " @ http://%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); - #else +#else LOG_INFO(TAG_HTTP, F(D_SERVICE_STARTED " @ http://%s"), - (WiFi.getMode() != WIFI_STA ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); - #endif - #else + (WiFi.getMode() != WIFI_STA ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); +#endif +#else IPAddress ip; - #if defined(ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_ARCH_ESP32) ip = ETH.localIP(); - #else +#else ip = Ethernet.localIP(); - #endif +#endif LOG_INFO(TAG_HTTP, F(D_SERVICE_STARTED " @ http://%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); - #endif +#endif } void httpStop() @@ -1935,19 +1936,19 @@ void httpSetup() webServer.on(F("/about"), webHandleAbout); webServer.onNotFound(httpHandleNotFound); - #if HASP_USE_WIFI > 0 - #if !defined(STM32F4xx) +#if HASP_USE_WIFI > 0 +#if !defined(STM32F4xx) - #if HASP_USE_CONFIG > 0 +#if HASP_USE_CONFIG > 0 if(WiFi.getMode() != WIFI_STA) { LOG_TRACE(TAG_HTTP, F("Wifi access point")); webServer.on(F("/"), webHandleWifiConfig); return; } - #endif - #endif - #endif +#endif +#endif +#endif webServer.on(F("/page/"), []() { String pageid = webServer.arg(F("page")); @@ -1955,7 +1956,7 @@ void httpSetup() haspSetPage(pageid.toInt()); }); - #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 +#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 webServer.on(F("/list"), HTTP_GET, handleFileList); // load editor webServer.on(F("/edit"), HTTP_GET, []() { @@ -1976,7 +1977,7 @@ void httpSetup() LOG_VERBOSE(TAG_HTTP, F("Headers: %d"), webServer.headers()); }, handleFileUpload); - #endif +#endif webServer.on(F("/"), webHandleRoot); webServer.on(F("/info"), webHandleInfo); @@ -1984,27 +1985,27 @@ void httpSetup() webServer.on(F("/firmware"), webHandleFirmware); webServer.on(F("/reboot"), httpHandleReboot); - #if HASP_USE_CONFIG > 0 +#if HASP_USE_CONFIG > 0 webServer.on(F("/config/hasp"), webHandleHaspConfig); webServer.on(F("/config/http"), webHandleHttpConfig); webServer.on(F("/config/gui"), webHandleGuiConfig); webServer.on(F("/config/debug"), webHandleDebugConfig); - #if HASP_USE_MQTT > 0 +#if HASP_USE_MQTT > 0 webServer.on(F("/config/mqtt"), webHandleMqttConfig); - #endif - #if HASP_USE_WIFI > 0 +#endif +#if HASP_USE_WIFI > 0 webServer.on(F("/config/wifi"), webHandleWifiConfig); - #endif - #if HASP_USE_GPIO > 0 +#endif +#if HASP_USE_GPIO > 0 webServer.on(F("/config/gpio"), webHandleGpioConfig); webServer.on(F("/config/gpio/options"), webHandleGpioOptions); - #endif +#endif webServer.on(F("/saveConfig"), webHandleSaveConfig); webServer.on(F("/resetConfig"), httpHandleResetConfig); webServer.on(F("/config"), webHandleConfig); - #endif // HASP_USE_CONFIG +#endif // HASP_USE_CONFIG - #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) webServer.on( F("/update"), HTTP_POST, []() { @@ -2013,7 +2014,7 @@ void httpSetup() }, webHandleFirmwareUpload); webServer.on(F("/espfirmware"), httpHandleEspFirmware); - #endif +#endif LOG_INFO(TAG_HTTP, F(D_SERVICE_STARTED)); // webStart(); Wait for network connection @@ -2027,9 +2028,9 @@ void httpReconnect() if(webServerStarted) { httpStop(); } else - #if HASP_USE_WIFI > 0 && !defined(STM32F4xx) +#if HASP_USE_WIFI > 0 && !defined(STM32F4xx) if(WiFi.status() == WL_CONNECTED || WiFi.getMode() != WIFI_STA) - #endif +#endif { httpStart(); } @@ -2048,7 +2049,7 @@ void httpEvery5Seconds() } //////////////////////////////////////////////////////////////////////////////////////////////////// - #if HASP_USE_CONFIG > 0 +#if HASP_USE_CONFIG > 0 bool httpGetConfig(const JsonObject & settings) { bool changed = false; @@ -2095,7 +2096,7 @@ bool httpSetConfig(const JsonObject & settings) return changed; } - #endif // HASP_USE_CONFIG +#endif // HASP_USE_CONFIG size_t httpClientWrite(const uint8_t * buf, size_t size) {