From 1adb966ee1466a6439bc1df63a7812054e0a6f15 Mon Sep 17 00:00:00 2001 From: suchmememanyskill <38142618+suchmememanyskill@users.noreply.github.com> Date: Sat, 2 Mar 2024 21:05:16 +0100 Subject: [PATCH 1/3] Image load round 2 (slightly less unstable) --- CYD-Klipper/src/conf/lv_conf.h | 16 +- .../src/core/device/ESP32-smartdisplay.cpp | 1 - CYD-Klipper/src/core/lv_setup.cpp | 1 + CYD-Klipper/src/ui/gcode_img.cpp | 149 ++++++++++++++++++ CYD-Klipper/src/ui/gcode_img.h | 6 + CYD-Klipper/src/ui/panels/print_panel.cpp | 9 ++ 6 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 CYD-Klipper/src/ui/gcode_img.cpp create mode 100644 CYD-Klipper/src/ui/gcode_img.h diff --git a/CYD-Klipper/src/conf/lv_conf.h b/CYD-Klipper/src/conf/lv_conf.h index 76bbcbe..a404d70 100644 --- a/CYD-Klipper/src/conf/lv_conf.h +++ b/CYD-Klipper/src/conf/lv_conf.h @@ -49,7 +49,7 @@ #define LV_MEM_CUSTOM 0 #if LV_MEM_CUSTOM == 0 /*Size of the memory available for `lv_mem_alloc()` in bytes (>= 2kB)*/ - #define LV_MEM_SIZE (32U * 1024U) /*[bytes]*/ + #define LV_MEM_SIZE (48U * 1024U) /*[bytes]*/ /*Set an address for the memory pool instead of allocating it as a normal array. Can be in external SRAM too.*/ #define LV_MEM_ADR 0 /*0: unused*/ @@ -194,7 +194,7 @@ *-----------*/ /*Enable the log module*/ -#define LV_USE_LOG 1 +#define LV_USE_LOG 0 #if LV_USE_LOG /*How important log should be added: @@ -204,7 +204,7 @@ *LV_LOG_LEVEL_ERROR Only critical issue, when the system may fail *LV_LOG_LEVEL_USER Only logs added by the user *LV_LOG_LEVEL_NONE Do not log anything*/ - #define LV_LOG_LEVEL LV_LOG_LEVEL_USER + #define LV_LOG_LEVEL LV_LOG_LEVEL_NONE /*1: Print the log with 'printf'; *0: User need to register a callback with `lv_log_register_print_cb()`*/ @@ -254,7 +254,7 @@ /*1: Show the used memory and the memory fragmentation * Requires LV_MEM_CUSTOM = 0*/ -#define LV_USE_MEM_MONITOR 0 +#define LV_USE_MEM_MONITOR 1 #if LV_USE_MEM_MONITOR #define LV_USE_MEM_MONITOR_POS LV_ALIGN_BOTTOM_LEFT #endif @@ -607,7 +607,7 @@ #endif /*PNG decoder library*/ -#define LV_USE_PNG 0 +#define LV_USE_PNG 1 /*BMP decoder library*/ #define LV_USE_BMP 0 @@ -678,16 +678,16 @@ ====================*/ /*Show some widget. It might be required to increase `LV_MEM_SIZE` */ -#define LV_USE_DEMO_WIDGETS 1 +#define LV_USE_DEMO_WIDGETS 0 #if LV_USE_DEMO_WIDGETS #define LV_DEMO_WIDGETS_SLIDESHOW 1 #endif /*Demonstrate the usage of encoder and keyboard*/ -#define LV_USE_DEMO_KEYPAD_AND_ENCODER 1 +#define LV_USE_DEMO_KEYPAD_AND_ENCODER 0 /*Benchmark your system*/ -#define LV_USE_DEMO_BENCHMARK 1 +#define LV_USE_DEMO_BENCHMARK 0 /*Stress test for LVGL*/ #define LV_USE_DEMO_STRESS 0 diff --git a/CYD-Klipper/src/core/device/ESP32-smartdisplay.cpp b/CYD-Klipper/src/core/device/ESP32-smartdisplay.cpp index 5e29e05..3ca7b3a 100644 --- a/CYD-Klipper/src/core/device/ESP32-smartdisplay.cpp +++ b/CYD-Klipper/src/core/device/ESP32-smartdisplay.cpp @@ -1,4 +1,3 @@ - #ifdef CYD_SCREEN_DRIVER_ESP32_SMARTDISPLAY #include "../screen_driver.h" diff --git a/CYD-Klipper/src/core/lv_setup.cpp b/CYD-Klipper/src/core/lv_setup.cpp index 5b2d53c..176916b 100644 --- a/CYD-Klipper/src/core/lv_setup.cpp +++ b/CYD-Klipper/src/core/lv_setup.cpp @@ -271,6 +271,7 @@ void lv_setup() screen_timer_setup(); screen_timer_start(); set_screen_brightness(); + lv_png_init(); } bool is_screen_asleep() diff --git a/CYD-Klipper/src/ui/gcode_img.cpp b/CYD-Klipper/src/ui/gcode_img.cpp new file mode 100644 index 0000000..be238a0 --- /dev/null +++ b/CYD-Klipper/src/ui/gcode_img.cpp @@ -0,0 +1,149 @@ +#include "gcode_img.h" +#include "lvgl.h" +#include "ui_utils.h" +#include +#include +#include "../conf/global_config.h" +#include + +static unsigned char * data_png = NULL; +static char img_filename_path[256] = {0}; +static lv_img_dsc_t img_header = {0}; + +static void close_button_click(lv_event_t * e){ + lv_obj_t * root = (lv_obj_t *)lv_event_get_user_data(e); + lv_obj_del(root); + clear_img_mem(); +} + +bool has_128_128_gcode(const char* filename) +{ + if (filename == NULL){ + Serial.println("No gcode filename"); + return false; + } + + String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/server/files/thumbnails?filename=" + String(filename); + HTTPClient client; + int httpCode = 0; + try { + client.begin(url.c_str()); + httpCode = client.GET(); + } + catch (...){ + Serial.println("Exception while fetching gcode img location"); + return {0}; + } + + if (httpCode == 200) + { + String payload = client.getString(); + JsonDocument doc; + deserializeJson(doc, payload); + auto result = doc["result"].as(); + const char* chosen_thumb = NULL; + + for (auto file : result){ + int width = file["width"]; + int height = file["height"]; + int size = file["size"]; + const char* thumbnail = file["thumbnail_path"]; + + if (width != height || width != 32) + continue; + + if (strcmp(thumbnail + strlen(thumbnail) - 4, ".png")) + continue; + + chosen_thumb = thumbnail; + break; + } + + if (chosen_thumb != NULL){ + Serial.printf("Found 128x128 PNG gcode img at %s\n", filename); + strcpy(img_filename_path, chosen_thumb); + return true; + } + } + + return false; +} + +lv_obj_t* draw_gcode_img() +{ + clear_img_mem(); + + if (img_filename_path[0] == 0){ + Serial.println("No gcode img path"); + return NULL; + } + + HTTPClient client; + int httpCode = 0; + try { + String img_url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/server/files/gcodes/" + String(img_filename_path); + client.begin(img_url); + httpCode = client.GET(); + } + catch (...){ + Serial.println("Exception while fetching gcode img"); + return NULL; + } + + if (httpCode == 200) + { + size_t len = client.getSize(); + if (len <= 0) + { + Serial.println("No gcode img data"); + return NULL; + } + + data_png = (unsigned char*)malloc(len + 1); + if (len != client.getStream().readBytes(data_png, len)){ + Serial.println("Failed to read gcode img data"); + clear_img_mem(); + return NULL; + } + + Serial.println((char*)data_png); + + memset(&img_header, 0, sizeof(img_header)); + img_header.header.w = 32; + img_header.header.h = 32; + img_header.data_size = len; + img_header.header.cf = LV_IMG_CF_RAW_ALPHA; + img_header.data = data_png; + + lv_obj_t * img = lv_img_create(lv_scr_act()); + lv_img_set_src(img, &img_header); + //lv_img_set_zoom(img, 512); + + return img; + } + + return NULL; +} + +lv_obj_t* show_gcode_img(const char* filename) +{ + if (filename == NULL){ + Serial.println("No gcode filename"); + return NULL; + } + + if (!has_128_128_gcode(filename)){ + Serial.println("No 128x128 gcode img found"); + return NULL; + } + + return draw_gcode_img(); +} + +void clear_img_mem() +{ + if (data_png != NULL){ + free(data_png); + data_png = NULL; + } +} \ No newline at end of file diff --git a/CYD-Klipper/src/ui/gcode_img.h b/CYD-Klipper/src/ui/gcode_img.h new file mode 100644 index 0000000..75f7074 --- /dev/null +++ b/CYD-Klipper/src/ui/gcode_img.h @@ -0,0 +1,6 @@ +#pragma once +#include "lvgl.h" + +lv_obj_t* show_gcode_img(const char* filename); +bool has_128_128_gcode(const char* filename); +void clear_img_mem(); \ No newline at end of file diff --git a/CYD-Klipper/src/ui/panels/print_panel.cpp b/CYD-Klipper/src/ui/panels/print_panel.cpp index e51d88f..af1f576 100644 --- a/CYD-Klipper/src/ui/panels/print_panel.cpp +++ b/CYD-Klipper/src/ui/panels/print_panel.cpp @@ -7,6 +7,7 @@ #include #include "../ui_utils.h" #include "../../core/lv_setup.h" +#include "../gcode_img.h" FILESYSTEM_FILE* selected_file = NULL; @@ -83,6 +84,14 @@ static void btn_print_file_verify(lv_event_t * e){ label = lv_label_create(btn); lv_label_set_text(label, LV_SYMBOL_OK); lv_obj_center(label); + + lv_obj_t* img = show_gcode_img(selected_file->name); + + if (img != NULL){ + lv_obj_set_parent(img, panel); + lv_obj_align(img, LV_ALIGN_BOTTOM_MID, 0, 0); + //lv_obj_set_size(img, CYD_SCREEN_MIN_BUTTON_WIDTH_PX * button_size_mult, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX * button_size_mult); + } } void print_panel_init(lv_obj_t* panel){ From 639eb50371429ebce4091bbc2b4aac8e1fcf657e Mon Sep 17 00:00:00 2001 From: suchmememanyskill <38142618+suchmememanyskill@users.noreply.github.com> Date: Sat, 2 Mar 2024 21:16:31 +0100 Subject: [PATCH 2/3] A --- CYD-Klipper/src/conf/lv_conf.h | 2 +- CYD-Klipper/src/ui/gcode_img.cpp | 2 -- CYD-Klipper/src/ui/panels/print_panel.cpp | 2 ++ 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CYD-Klipper/src/conf/lv_conf.h b/CYD-Klipper/src/conf/lv_conf.h index a404d70..6b6ab9c 100644 --- a/CYD-Klipper/src/conf/lv_conf.h +++ b/CYD-Klipper/src/conf/lv_conf.h @@ -49,7 +49,7 @@ #define LV_MEM_CUSTOM 0 #if LV_MEM_CUSTOM == 0 /*Size of the memory available for `lv_mem_alloc()` in bytes (>= 2kB)*/ - #define LV_MEM_SIZE (48U * 1024U) /*[bytes]*/ + #define LV_MEM_SIZE (41U * 1024U) /*[bytes]*/ /*Set an address for the memory pool instead of allocating it as a normal array. Can be in external SRAM too.*/ #define LV_MEM_ADR 0 /*0: unused*/ diff --git a/CYD-Klipper/src/ui/gcode_img.cpp b/CYD-Klipper/src/ui/gcode_img.cpp index be238a0..507b948 100644 --- a/CYD-Klipper/src/ui/gcode_img.cpp +++ b/CYD-Klipper/src/ui/gcode_img.cpp @@ -106,8 +106,6 @@ lv_obj_t* draw_gcode_img() return NULL; } - Serial.println((char*)data_png); - memset(&img_header, 0, sizeof(img_header)); img_header.header.w = 32; img_header.header.h = 32; diff --git a/CYD-Klipper/src/ui/panels/print_panel.cpp b/CYD-Klipper/src/ui/panels/print_panel.cpp index af1f576..ffade1a 100644 --- a/CYD-Klipper/src/ui/panels/print_panel.cpp +++ b/CYD-Klipper/src/ui/panels/print_panel.cpp @@ -100,6 +100,8 @@ void print_panel_init(lv_obj_t* panel){ return; } + clear_img_mem(); + lv_obj_t * list = lv_list_create(panel); lv_obj_set_style_radius(list, 0, 0); lv_obj_set_style_border_width(list, 0, 0); From 4e6457c7295fdcb052f7afe3f8156814016a1328 Mon Sep 17 00:00:00 2001 From: suchmememanyskill <38142618+suchmememanyskill@users.noreply.github.com> Date: Sat, 2 Mar 2024 22:02:20 +0100 Subject: [PATCH 3/3] Misc fixes --- CYD-Klipper/src/conf/lv_conf.h | 4 ++-- CYD-Klipper/src/core/device/ESP32-2432S028R.cpp | 2 +- CYD-Klipper/src/ui/gcode_img.cpp | 1 - CYD-Klipper/src/ui/panels/print_panel.cpp | 1 - 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/CYD-Klipper/src/conf/lv_conf.h b/CYD-Klipper/src/conf/lv_conf.h index 6b6ab9c..9c14919 100644 --- a/CYD-Klipper/src/conf/lv_conf.h +++ b/CYD-Klipper/src/conf/lv_conf.h @@ -49,7 +49,7 @@ #define LV_MEM_CUSTOM 0 #if LV_MEM_CUSTOM == 0 /*Size of the memory available for `lv_mem_alloc()` in bytes (>= 2kB)*/ - #define LV_MEM_SIZE (41U * 1024U) /*[bytes]*/ + #define LV_MEM_SIZE (40U * 1024U) /*[bytes]*/ /*Set an address for the memory pool instead of allocating it as a normal array. Can be in external SRAM too.*/ #define LV_MEM_ADR 0 /*0: unused*/ @@ -254,7 +254,7 @@ /*1: Show the used memory and the memory fragmentation * Requires LV_MEM_CUSTOM = 0*/ -#define LV_USE_MEM_MONITOR 1 +#define LV_USE_MEM_MONITOR 0 #if LV_USE_MEM_MONITOR #define LV_USE_MEM_MONITOR_POS LV_ALIGN_BOTTOM_LEFT #endif diff --git a/CYD-Klipper/src/core/device/ESP32-2432S028R.cpp b/CYD-Klipper/src/core/device/ESP32-2432S028R.cpp index d2734d8..65f234f 100644 --- a/CYD-Klipper/src/core/device/ESP32-2432S028R.cpp +++ b/CYD-Klipper/src/core/device/ESP32-2432S028R.cpp @@ -86,7 +86,7 @@ void screen_setup() tft.setRotation(global_config.rotateScreen ? 3 : 1); tft.fillScreen(TFT_BLACK); set_invert_display(); - + set_screen_brightness(); touchscreen_spi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS); touchscreen.begin(touchscreen_spi); diff --git a/CYD-Klipper/src/ui/gcode_img.cpp b/CYD-Klipper/src/ui/gcode_img.cpp index 507b948..ad1ab07 100644 --- a/CYD-Klipper/src/ui/gcode_img.cpp +++ b/CYD-Klipper/src/ui/gcode_img.cpp @@ -115,7 +115,6 @@ lv_obj_t* draw_gcode_img() lv_obj_t * img = lv_img_create(lv_scr_act()); lv_img_set_src(img, &img_header); - //lv_img_set_zoom(img, 512); return img; } diff --git a/CYD-Klipper/src/ui/panels/print_panel.cpp b/CYD-Klipper/src/ui/panels/print_panel.cpp index ffade1a..357861e 100644 --- a/CYD-Klipper/src/ui/panels/print_panel.cpp +++ b/CYD-Klipper/src/ui/panels/print_panel.cpp @@ -90,7 +90,6 @@ static void btn_print_file_verify(lv_event_t * e){ if (img != NULL){ lv_obj_set_parent(img, panel); lv_obj_align(img, LV_ALIGN_BOTTOM_MID, 0, 0); - //lv_obj_set_size(img, CYD_SCREEN_MIN_BUTTON_WIDTH_PX * button_size_mult, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX * button_size_mult); } }