From 2ac717e5b76cdc80ed8190fb1edfd943ca54e19f Mon Sep 17 00:00:00 2001 From: fvanroie Date: Wed, 26 Feb 2020 21:40:01 +0100 Subject: [PATCH] Memory optimization --- include/lv_conf.h | 4 +-- lib/lv_lib_zifont/lv_zifont.cpp | 22 +++++++-------- src/hasp_button.cpp | 17 ++++++------ src/hasp_mqtt.cpp | 49 +++++++++++++++++++++++---------- 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/include/lv_conf.h b/include/lv_conf.h index 8ee07c81..d16eb35c 100644 --- a/include/lv_conf.h +++ b/include/lv_conf.h @@ -160,13 +160,13 @@ typedef void * lv_group_user_data_t; #define LV_USE_GPU 1 /* 1: Enable file system (might be required for images */ -#define LV_USE_FILESYSTEM 1 +#define LV_USE_FILESYSTEM 0 #if LV_USE_FILESYSTEM /*Declare the type of the user data of file system drivers (can be e.g. `void *`, `int`, `struct`)*/ typedef void * lv_fs_drv_user_data_t; /*File system interface*/ -#define LV_USE_FS_IF 1 +#define LV_USE_FS_IF 0 #if LV_USE_FS_IF # define LV_FS_IF_FATFS '\0' # define LV_FS_IF_PC '\0' diff --git a/lib/lv_lib_zifont/lv_zifont.cpp b/lib/lv_lib_zifont/lv_zifont.cpp index fbc0dda5..aad0a7bf 100644 --- a/lib/lv_lib_zifont/lv_zifont.cpp +++ b/lib/lv_lib_zifont/lv_zifont.cpp @@ -59,16 +59,16 @@ enum zifont_codepage_t8_t { /********************** * STATIC PROTOTYPES **********************/ -static const uint8_t * lv_font_get_bitmap_fmt_zifont(const lv_font_t * font, uint32_t unicode_letter); -static bool lv_font_get_glyph_dsc_fmt_zifont(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, - uint32_t unicode_letter, uint32_t unicode_letter_next); +const uint8_t * IRAM_ATTR lv_font_get_bitmap_fmt_zifont(const lv_font_t * font, uint32_t unicode_letter); +bool IRAM_ATTR lv_font_get_glyph_dsc_fmt_zifont(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, + uint32_t unicode_letter, uint32_t unicode_letter_next); /********************** * STATIC VARIABLES **********************/ uint32_t charInBuffer = 0; // Last Character ID in the Bitmap Buffer // uint8_t filecharBitmap_p[20 * 1024]; -static lv_zifont_char_t lastCharInfo; // Holds the last Glyph DSC +lv_zifont_char_t lastCharInfo; // Holds the last Glyph DSC #if ESP32 // static lv_zifont_char_t charCache[256 - 32]; // glyphID DSC cache @@ -232,7 +232,7 @@ int lv_zifont_font_init(lv_font_t ** font, const char * font_path, uint16_t size * @param unicode_letter an unicode letter which bitmap should be get * @return pointer to the bitmap or NULL if not found */ -static const uint8_t * lv_font_get_bitmap_fmt_zifont(const lv_font_t * font, uint32_t unicode_letter) +const uint8_t * IRAM_ATTR lv_font_get_bitmap_fmt_zifont(const lv_font_t * font, uint32_t unicode_letter) { lv_font_fmt_zifont_dsc_t * fdsc = (lv_font_fmt_zifont_dsc_t *)font->dsc; /* header data struct */ uint32_t glyphID; @@ -297,11 +297,11 @@ static const uint8_t * lv_font_get_bitmap_fmt_zifont(const lv_font_t * font, uin charBitmap_p = (uint8_t *)lv_mem_alloc(size); memset(charBitmap_p, 0, size); // init the bitmap to white - char ch[1]; + char data[256]; file.seek(datapos, SeekSet); - file.readBytes(ch, 1); /* check first byte = bpp */ + file.readBytes(data, 1); /* check first byte = bpp */ - if(ch[0] != 3) { + if(data[0] != 3) { file.close(); lv_mem_free(charInfo); debugPrintln(PSTR("FONT: [ERROR] Character is not 3bpp encoded")); @@ -318,7 +318,7 @@ static const uint8_t * lv_font_get_bitmap_fmt_zifont(const lv_font_t * font, uin uint8_t w = charInfo->width + charInfo->kerningL + charInfo->kerningR; uint16_t fileindex = 0; - char data[256]; + // char data[256]; int len = 1; // while((fileindex < charInfo->length) && len > 0) { //} && !feof(file)) { @@ -404,8 +404,8 @@ static const uint8_t * lv_font_get_bitmap_fmt_zifont(const lv_font_t * font, uin * @return true: descriptor is successfully loaded into `dsc_out`. * false: the letter was not found, no data is loaded to `dsc_out` */ -static bool lv_font_get_glyph_dsc_fmt_zifont(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, - uint32_t unicode_letter, uint32_t unicode_letter_next) +bool IRAM_ATTR lv_font_get_glyph_dsc_fmt_zifont(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, + uint32_t unicode_letter, uint32_t unicode_letter_next) { // ulong startMillis = millis(); lv_font_fmt_zifont_dsc_t * fdsc = (lv_font_fmt_zifont_dsc_t *)font->dsc; /* header data struct */ diff --git a/src/hasp_button.cpp b/src/hasp_button.cpp index 623906da..d916c09e 100644 --- a/src/hasp_button.cpp +++ b/src/hasp_button.cpp @@ -1,27 +1,28 @@ #include "Button.h" +#include "hasp_conf.h" + #include "hasp_button.h" #include "hasp_dispatch.h" -Button * button[3]; // Connect your button between pin 2 and GND +Button * button[HASP_NUM_INPUTS]; // Connect your button between pin 2 and GND void buttonSetup(void) { - button[0] = new Button(2); + // button[0] = new Button(2); button[1] = new Button(3); button[2] = new Button(4); - button[0]->begin(); + // button[0]->begin(); button[1]->begin(); button[2]->begin(); } void buttonLoop(void) { - if(button[2]->toggled()) { - if(button[2]->read() == Button::PRESSED) - Serial.println("Button 3 has been pressed"); - else - Serial.println("Button 3 has been released"); + for(uint8_t i = 0; i < (sizeof button / sizeof *button); i++) { + if(button[i] && button[i]->toggled()) { + dispatchButton(i, button[i]->read() == Button::PRESSED); + } } } diff --git a/src/hasp_mqtt.cpp b/src/hasp_mqtt.cpp index bcfa293e..5da1dd50 100644 --- a/src/hasp_mqtt.cpp +++ b/src/hasp_mqtt.cpp @@ -23,7 +23,6 @@ #include "user_config_override.h" #endif -String mqttClientId((char *)0); // Auto-generated MQTT ClientID /* String mqttGetSubtopic; // MQTT subtopic for incoming commands requesting .val String mqttGetSubtopicJSON; // MQTT object buffer for JSON status when requesting .val @@ -40,6 +39,7 @@ String mqttLightBrightCommandTopic; // MQTT topic for incoming panel backlight d String mqttLightBrightStateTopic; // MQTT topic for outgoing panel backlight dimmer state // String mqttMotionStateTopic; // MQTT topic for outgoing motion sensor state +String mqttClientId((char *)0); // Auto-generated MQTT ClientID String mqttNodeTopic((char *)0); String mqttGroupTopic((char *)0); bool mqttEnabled; @@ -76,9 +76,6 @@ const String mqttLightBrightSubscription = "hasp/" + String(haspGetNodename()) WiFiClient wifiClient; PubSubClient mqttClient(wifiClient); -static char mqttTopic[127]; -static char mqttPayload[254]; - //////////////////////////////////////////////////////////////////////////////////////////////////// // Send changed values OUT @@ -91,6 +88,9 @@ void IRAM_ATTR mqttSendState(const char * subtopic, const char * payload) // light = 0/1 // brightness = 100 + char mqttTopic[127]; + char mqttPayload[127 * 5]; + snprintf_P(mqttTopic, sizeof(mqttTopic), PSTR("%sstate/%s"), mqttNodeTopic.c_str(), subtopic); mqttClient.publish(mqttTopic, payload); debugPrintln(String(F("MQTT OUT: ")) + String(mqttTopic) + " = " + String(payload)); @@ -270,8 +270,11 @@ void mqttCallback(char * topic, byte * payload, unsigned int length) strPayload == F("OFF")) { // catch a dangling LWT from a previous connection if it appears char topicBuffer[127]; snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str()); - debugPrintln(String(F("MQTT: binary_sensor state: [")) + topicBuffer + "] : ON"); mqttClient.publish(topicBuffer, "ON", true); + + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("MQTT: binary_sensor state: [%sstatus] : ON"), + mqttNodeTopic.c_str()); + debugPrintln(topicBuffer); return; } } @@ -280,10 +283,13 @@ void mqttReconnect() { static uint8_t mqttReconnectCount = 0; bool mqttFirstConnect = true; - String nodeName = haspGetNodename(); + String nodeName((char *)0); + nodeName.reserve(127); + nodeName = haspGetNodename(); char topicBuffer[127]; // Generate an MQTT client ID as haspNode + our MAC address + mqttClientId.reserve(127); mqttClientId = nodeName; mqttClientId += F("-"); mqttClientId += wifiGetMacAddress(3, ""); @@ -295,8 +301,9 @@ void mqttReconnect() mqttGroupTopic = topicBuffer; // haspSetPage(0); - debugPrintln(String(F("MQTT: Attempting connection to broker ")) + String(mqttServer.c_str()) + - String(F(" as clientID ")) + mqttClientId); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("MQTT: Attempting connection to broker %s as clientID %s"), + mqttServer.c_str(), mqttClientId.c_str()); + debugPrintln(topicBuffer); // Attempt to connect and set LWT and Clean Session snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str()); @@ -340,30 +347,44 @@ void mqttReconnect() // Subscribe to our incoming topics snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%scommand/#"), mqttGroupTopic.c_str()); if(mqttClient.subscribe(topicBuffer)) { - debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("MQTT: * Subscribed to %scommand/#"), + mqttGroupTopic.c_str()); + debugPrintln(topicBuffer); } + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%scommand/#"), mqttNodeTopic.c_str()); if(mqttClient.subscribe(topicBuffer)) { - debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("MQTT: * Subscribed to %scommand/#"), + mqttNodeTopic.c_str()); + debugPrintln(topicBuffer); } + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%slight/#"), mqttNodeTopic.c_str()); if(mqttClient.subscribe(topicBuffer)) { - debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("MQTT: * Subscribed to %slight/#"), mqttNodeTopic.c_str()); + debugPrintln(topicBuffer); } + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sbrightness/#"), mqttNodeTopic.c_str()); if(mqttClient.subscribe(topicBuffer)) { - debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("MQTT: * Subscribed to %sbrightness/#"), + mqttNodeTopic.c_str()); + debugPrintln(topicBuffer); } snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str()); if(mqttClient.subscribe(topicBuffer)) { - debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("MQTT: * Subscribed to %sstatus"), mqttNodeTopic.c_str()); + debugPrintln(topicBuffer); } // Force any subscribed clients to toggle OFF/ON when we first connect to // make sure we get a full panel refresh at power on. Sending OFF, // "ON" will be sent by the mqttStatusTopic subscription action. - debugPrintln(String(F("MQTT: binary_sensor state: [")) + topicBuffer + "] : " + (mqttFirstConnect ? "OFF" : "ON")); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str()); mqttClient.publish(topicBuffer, mqttFirstConnect ? "OFF" : "ON", true); //, 1); + snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("MQTT: binary_sensor state: [%sstatus] : %s"), + mqttNodeTopic.c_str(), mqttFirstConnect ? PSTR("OFF") : PSTR("ON")); + debugPrintln(topicBuffer); mqttFirstConnect = false; mqttReconnectCount = 0;