From 1cca0873fd23340c099b59af2e9bf0e305ffd538 Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Mon, 7 Jun 2021 16:11:10 +0200 Subject: [PATCH 1/4] Test raw image over http --- include/hasp_conf.h | 12 +++ platformio.ini | 6 ++ src/hasp/hasp_attribute.cpp | 98 +++++++++++++++++++++- src/hasp_gui.cpp | 27 ++++++ user_setups/linux_sdl/linux_sdl_64bits.ini | 6 ++ user_setups/win32/windows_sdl_64bits.ini | 6 ++ 6 files changed, 153 insertions(+), 2 deletions(-) diff --git a/include/hasp_conf.h b/include/hasp_conf.h index 8d9aabbc..0a5b8e56 100644 --- a/include/hasp_conf.h +++ b/include/hasp_conf.h @@ -106,6 +106,18 @@ #define HASP_USE_PNGDECODE 0 #endif +#ifndef HASP_USE_BMPDECODE +#define HASP_USE_BMPDECODE 0 +#endif + +#ifndef HASP_USE_GIFDECODE +#define HASP_USE_GIFDECODE 0 +#endif + +#ifndef HASP_USE_JPGDECODE +#define HASP_USE_JPGDECODE 0 +#endif + #ifndef HASP_NUM_GPIO_CONFIG #define HASP_NUM_GPIO_CONFIG 8 #endif diff --git a/platformio.ini b/platformio.ini index 34fd4d57..d2305c7c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -134,6 +134,9 @@ build_flags = -D HASP_USE_CONFIG=1 ; Native application, not library -D LV_LOG_TRACE_TIMER=1 -D HASP_USE_PNGDECODE=1 + -D HASP_USE_BMPDECODE=0 + -D HASP_USE_JPGDECODE=0 + -D HASP_USE_GIFDECODE=0 ; -- LittleFS build options ------------------------ ;-D CONFIG_LITTLEFS_FOR_IDF_3_2 ; obsolete in IDF 3.3 @@ -151,6 +154,9 @@ lib_ignore = lib_deps = LittleFS_esp32 git+https://github.com/lvgl/lv_lib_png.git#release/v7 + #git+https://github.com/lvgl/lv_lib_bmp.git#release/v7 + #git+https://github.com/lvgl/lv_lib_gif.git#release/v7 + git+https://github.com/lvgl/lv_lib_split_jpg.git ps_ram = -DBOARD_HAS_PSRAM diff --git a/src/hasp/hasp_attribute.cpp b/src/hasp/hasp_attribute.cpp index 39da7789..25df6e20 100644 --- a/src/hasp/hasp_attribute.cpp +++ b/src/hasp/hasp_attribute.cpp @@ -8,6 +8,21 @@ #include "hasplib.h" #include "hasp_attribute_helper.h" +/*** Image Improvement ***/ +#if defined(ARDUINO_ARCH_ESP8266) +#include +#endif + +#if defined(ARDUINO_ARCH_ESP32) +#include +#endif + +#if HASP_USE_PNGDECODE > 0 +#include "lv_png.h" +#include "lodepng.h" +#endif +/*** Image Improvement ***/ + LV_FONT_DECLARE(unscii_8_icon); extern const char** btnmatrix_default_map; // memory pointer to lvgl default btnmatrix map @@ -948,8 +963,87 @@ static hasp_attribute_type_t special_attribute_src(lv_obj_t* obj, const char* pa if(!obj_check_type(obj, LV_HASP_IMAGE)) return HASP_ATTR_TYPE_NOT_FOUND; if(update) { - lv_img_cache_invalidate_src(lv_img_get_src(obj)); - lv_img_set_src(obj, payload); + const void* src = lv_img_get_src(obj); + lv_img_src_t src_type = lv_img_src_get_type(src); + lv_img_cache_invalidate_src(src); + + if(src_type == LV_IMG_SRC_VARIABLE) { + lv_img_dsc_t* img_dsc = (lv_img_dsc_t*)src; + free((uint8_t*)img_dsc->data); + lv_mem_free(img_dsc); + } + + if(payload != strstr_P(payload, PSTR("http://"))) { + lv_img_set_src(obj, payload); + } else { + HTTPClient http; + http.begin(payload); + int httpCode = http.GET(); + if(httpCode == HTTP_CODE_OK) { + int total = http.getSize(); + int len = total; + int read = 0; + lv_img_dsc_t* img_dsc = (lv_img_dsc_t*)lv_mem_alloc(sizeof(lv_img_dsc_t)); + uint8_t* img_buf = (uint8_t*)(len > 0 ? malloc(len) : NULL); + + LOG_VERBOSE(TAG_ATTR, "HTTP OK: buffer created of %d bytes", len); + + if(img_dsc && img_buf && len > sizeof(lv_img_header_t)) { // total size must be larger then header size + + Stream* stream = http.getStreamPtr(); + while(http.connected() && (len > 0 || len == -1)) { + size_t size = stream->available(); + int c = 0; + + if(size) { + if(read == 0 && size >= sizeof(lv_img_header_t)) { // read 4-byte header first + c = stream->readBytes((uint8_t*)img_dsc, sizeof(lv_img_header_t)); + LOG_VERBOSE(TAG_ATTR, D_BULLET "HEADER READ: %d bytes", c); + } else if(read != 0) { // header has been read + c = stream->readBytes(img_buf + read - sizeof(lv_img_header_t), size); + LOG_VERBOSE(TAG_ATTR, D_BULLET "HTTP READ: %d bytes", c); + } + + if(len > 0) { + len -= c; + read += c; + } + } + delay(1); + } + LOG_VERBOSE(TAG_ATTR, D_BULLET "HTTP DONE: %d bytes", read); + + img_dsc->data_size = total - 4; + img_dsc->data = img_buf; + lv_img_set_src(obj, img_dsc); + + // /*Decode the PNG image*/ + // unsigned char* png_decoded; /*Will be pointer to the decoded image*/ + // uint32_t png_width; /*Will be the width of the decoded image*/ + // uint32_t png_height; /*Will be the width of the decoded image*/ + + // /*Decode the loaded image in ARGB8888 */ + // uint32_t error = lodepng_decode32(&png_decoded, &png_width, &png_height, img_buf, (size_t)total); + + // if(error) { + // LOG_ERROR(TAG_ATTR, "error %u: %s\n", error, lodepng_error_text(error)); + // } else { + // img_dsc->header.always_zero = 0; /*It must be zero*/ + // img_dsc->header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA; /*Set the color format*/ + // img_dsc->header.w = png_width; + // img_dsc->header.h = png_height; + // img_dsc->data_size = png_width * png_height * 3; + // img_dsc->data = png_decoded; + // lv_img_set_src(obj, img_dsc); + // } + + } else { + LOG_WARNING(TAG_ATTR, "image buffer creation failed %d", len); + } + } else { + LOG_WARNING(TAG_ATTR, "HTTP result %d", httpCode); + } + } } else { switch(lv_img_src_get_type(obj)) { case LV_IMG_SRC_FILE: diff --git a/src/hasp_gui.cpp b/src/hasp_gui.cpp index fde91c86..3c4e87e8 100644 --- a/src/hasp_gui.cpp +++ b/src/hasp_gui.cpp @@ -31,6 +31,18 @@ #include "lv_png.h" #endif +#if HASP_USE_BMPDECODE > 0 +#include "lv_bmp.h" +#endif + +#if HASP_USE_GIFDECODE > 0 +#include "lv_gif.h" +#endif + +#if HASP_USE_JPGDECODE > 0 +#include "lv_sjpg.h" +#endif + #define BACKLIGHT_CHANNEL 0 // pwm channel 0-15 #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 @@ -267,6 +279,21 @@ void guiSetup() lv_png_init(); #endif + /* Initialize BMP decoder */ +#if HASP_USE_BMPDECODE > 0 + lv_bmp_init(); +#endif + + /* Initialize GIF decoder */ +#if HASP_USE_GIFDECODE > 0 + // lv_gif_init(); +#endif + + /* Initialize JPG decoder */ +#if HASP_USE_JPGDECODE > 0 + lv_split_jpeg_init(); +#endif + #ifdef USE_DMA_TO_TFT LOG_VERBOSE(TAG_GUI, F("DMA : " D_SETTING_ENABLED)); #else diff --git a/user_setups/linux_sdl/linux_sdl_64bits.ini b/user_setups/linux_sdl/linux_sdl_64bits.ini index 9584f7a6..76fd32ca 100644 --- a/user_setups/linux_sdl/linux_sdl_64bits.ini +++ b/user_setups/linux_sdl/linux_sdl_64bits.ini @@ -29,6 +29,9 @@ build_flags = -D HASP_USE_CONFIG=0 ; Standalone application, as library -D HASP_USE_DEBUG=1 -D HASP_USE_PNGDECODE=1 + -D HASP_USE_BMPDECODE=1 + -D HASP_USE_GIFDECODE=1 + -D HASP_USE_JPGDECODE=1 -D HASP_USE_MQTT=1 -D MQTT_MAX_PACKET_SIZE=2048 -D HASP_ATTRIBUTE_FAST_MEM= @@ -62,6 +65,9 @@ lib_deps = bblanchon/ArduinoJson@^6.17.2 ; Json(l) parser https://github.com/fvanroie/lv_drivers git+https://github.com/lvgl/lv_lib_png.git#release/v7 + git+https://github.com/lvgl/lv_lib_bmp.git#release/v7 + git+https://github.com/lvgl/gif.git#release/v7 + git+https://github.com/lvgl/lv_lib_split_jpg.git lib_ignore = paho diff --git a/user_setups/win32/windows_sdl_64bits.ini b/user_setups/win32/windows_sdl_64bits.ini index edfe3417..dcf950d4 100644 --- a/user_setups/win32/windows_sdl_64bits.ini +++ b/user_setups/win32/windows_sdl_64bits.ini @@ -29,6 +29,9 @@ build_flags = -D HASP_USE_CONFIG=0 ; Standalone application, as library -D HASP_USE_DEBUG=1 -D HASP_USE_PNGDECODE=1 + -D HASP_USE_BMPDECODE=0 + -D HASP_USE_GIFDECODE=0 + -D HASP_USE_JPGDECODE=1 -D HASP_USE_MQTT=1 -D MQTT_MAX_PACKET_SIZE=2048 -D HASP_ATTRIBUTE_FAST_MEM= @@ -84,6 +87,9 @@ lib_deps = bblanchon/ArduinoJson@^6.17.2 ; Json(l) parser https://github.com/fvanroie/lv_drivers git+https://github.com/lvgl/lv_lib_png.git#release/v7 + #git+https://github.com/lvgl/lv_lib_bmp.git#release/v7 + #git+https://github.com/lvgl/gif.git#release/v7 + git+https://github.com/lvgl/lv_lib_split_jpg.git lib_ignore = paho From aa2a97090b03bc06ae292ac888ef96f8963eb7c3 Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Sun, 13 Jun 2021 02:05:49 +0200 Subject: [PATCH 2/4] Use PSram and more debug info --- src/hasp/hasp_attribute.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/hasp/hasp_attribute.cpp b/src/hasp/hasp_attribute.cpp index 25df6e20..96a8eb36 100644 --- a/src/hasp/hasp_attribute.cpp +++ b/src/hasp/hasp_attribute.cpp @@ -20,6 +20,7 @@ #if HASP_USE_PNGDECODE > 0 #include "lv_png.h" #include "lodepng.h" +#include "hasp_png.h" #endif /*** Image Improvement ***/ @@ -969,7 +970,7 @@ static hasp_attribute_type_t special_attribute_src(lv_obj_t* obj, const char* pa if(src_type == LV_IMG_SRC_VARIABLE) { lv_img_dsc_t* img_dsc = (lv_img_dsc_t*)src; - free((uint8_t*)img_dsc->data); + lodepng_free((uint8_t*)img_dsc->data); lv_mem_free(img_dsc); } @@ -984,7 +985,7 @@ static hasp_attribute_type_t special_attribute_src(lv_obj_t* obj, const char* pa int len = total; int read = 0; lv_img_dsc_t* img_dsc = (lv_img_dsc_t*)lv_mem_alloc(sizeof(lv_img_dsc_t)); - uint8_t* img_buf = (uint8_t*)(len > 0 ? malloc(len) : NULL); + uint8_t* img_buf = (uint8_t*)(len > 0 ? lodepng_malloc(len) : NULL); LOG_VERBOSE(TAG_ATTR, "HTTP OK: buffer created of %d bytes", len); @@ -998,7 +999,8 @@ static hasp_attribute_type_t special_attribute_src(lv_obj_t* obj, const char* pa if(size) { if(read == 0 && size >= sizeof(lv_img_header_t)) { // read 4-byte header first c = stream->readBytes((uint8_t*)img_dsc, sizeof(lv_img_header_t)); - LOG_VERBOSE(TAG_ATTR, D_BULLET "HEADER READ: %d bytes", c); + LOG_VERBOSE(TAG_ATTR, D_BULLET "HEADER READ: %d bytes w=%d h=%d", c, + img_dsc->header.w, img_dsc->header.h); } else if(read != 0) { // header has been read c = stream->readBytes(img_buf + read - sizeof(lv_img_header_t), size); LOG_VERBOSE(TAG_ATTR, D_BULLET "HTTP READ: %d bytes", c); @@ -1011,7 +1013,8 @@ static hasp_attribute_type_t special_attribute_src(lv_obj_t* obj, const char* pa } delay(1); } - LOG_VERBOSE(TAG_ATTR, D_BULLET "HTTP DONE: %d bytes", read); + LOG_VERBOSE(TAG_ATTR, D_BULLET "HTTP TOTAL READ: %d bytes, %d expected", read, + img_dsc->header.w * img_dsc->header.h * 2 + 4); img_dsc->data_size = total - 4; img_dsc->data = img_buf; From e86cb437c44e0bfefd3c28ad3602bf9b9dd8d3fd Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Sun, 13 Jun 2021 15:59:31 +0200 Subject: [PATCH 3/4] Fix lv_lib_split_jpg lvgl.h include issue --- platformio.ini | 2 +- user_setups/linux_sdl/linux_sdl_64bits.ini | 2 +- user_setups/win32/windows_sdl_64bits.ini | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/platformio.ini b/platformio.ini index d2305c7c..77b0c305 100644 --- a/platformio.ini +++ b/platformio.ini @@ -156,7 +156,7 @@ lib_deps = git+https://github.com/lvgl/lv_lib_png.git#release/v7 #git+https://github.com/lvgl/lv_lib_bmp.git#release/v7 #git+https://github.com/lvgl/lv_lib_gif.git#release/v7 - git+https://github.com/lvgl/lv_lib_split_jpg.git + git+https://github.com/fvanroie/lv_lib_split_jpg.git ps_ram = -DBOARD_HAS_PSRAM diff --git a/user_setups/linux_sdl/linux_sdl_64bits.ini b/user_setups/linux_sdl/linux_sdl_64bits.ini index 76fd32ca..926ac341 100644 --- a/user_setups/linux_sdl/linux_sdl_64bits.ini +++ b/user_setups/linux_sdl/linux_sdl_64bits.ini @@ -67,7 +67,7 @@ lib_deps = git+https://github.com/lvgl/lv_lib_png.git#release/v7 git+https://github.com/lvgl/lv_lib_bmp.git#release/v7 git+https://github.com/lvgl/gif.git#release/v7 - git+https://github.com/lvgl/lv_lib_split_jpg.git + git+https://github.com/fvanroie/lv_lib_split_jpg.git lib_ignore = paho diff --git a/user_setups/win32/windows_sdl_64bits.ini b/user_setups/win32/windows_sdl_64bits.ini index dcf950d4..69974f04 100644 --- a/user_setups/win32/windows_sdl_64bits.ini +++ b/user_setups/win32/windows_sdl_64bits.ini @@ -89,7 +89,7 @@ lib_deps = git+https://github.com/lvgl/lv_lib_png.git#release/v7 #git+https://github.com/lvgl/lv_lib_bmp.git#release/v7 #git+https://github.com/lvgl/gif.git#release/v7 - git+https://github.com/lvgl/lv_lib_split_jpg.git + git+https://github.com/fvanroie/lv_lib_split_jpg.git lib_ignore = paho From 3eed71febe0747ddac0363411d8ea6c7994ae522 Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Sun, 13 Jun 2021 20:47:01 +0200 Subject: [PATCH 4/4] Zero image buffer --- src/hasp/hasp_attribute.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/hasp/hasp_attribute.cpp b/src/hasp/hasp_attribute.cpp index 96a8eb36..dceec191 100644 --- a/src/hasp/hasp_attribute.cpp +++ b/src/hasp/hasp_attribute.cpp @@ -990,6 +990,7 @@ static hasp_attribute_type_t special_attribute_src(lv_obj_t* obj, const char* pa LOG_VERBOSE(TAG_ATTR, "HTTP OK: buffer created of %d bytes", len); if(img_dsc && img_buf && len > sizeof(lv_img_header_t)) { // total size must be larger then header size + memset(img_buf, 0, len); Stream* stream = http.getStreamPtr(); while(http.connected() && (len > 0 || len == -1)) {