diff --git a/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp b/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp index 5edd7dd..e8e8d7e 100644 --- a/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp +++ b/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp @@ -716,8 +716,9 @@ bool KlipperPrinter::set_target_temperature(PrinterTemperatureDevice device, uns return send_gcode(gcode); } -unsigned char* KlipperPrinter::get_32_32_png_image_thumbnail(const char* gcode_filename) +Thumbnail KlipperPrinter::get_32_32_png_image_thumbnail(const char* gcode_filename) { + Thumbnail thumbnail = {0}; HTTPClient client; configure_http_client(client, "/server/files/thumbnails?filename=", true, 1000); char* img_filename_path = NULL; @@ -731,7 +732,7 @@ unsigned char* KlipperPrinter::get_32_32_png_image_thumbnail(const char* gcode_f catch (...) { LOG_LN("Exception while fetching gcode img location"); - return NULL; + return thumbnail; } if (http_code == 200) @@ -770,7 +771,7 @@ unsigned char* KlipperPrinter::get_32_32_png_image_thumbnail(const char* gcode_f if (img_filename_path == NULL) { - return NULL; + return thumbnail; } client.end(); @@ -785,7 +786,7 @@ unsigned char* KlipperPrinter::get_32_32_png_image_thumbnail(const char* gcode_f catch (...) { LOG_LN("Exception while fetching gcode img"); - return NULL; + return thumbnail; } if (http_code == 200) @@ -794,7 +795,7 @@ unsigned char* KlipperPrinter::get_32_32_png_image_thumbnail(const char* gcode_f if (len <= 0) { LOG_LN("No gcode img data"); - return NULL; + return thumbnail; } data_png = (unsigned char*)malloc(len + 1); @@ -808,12 +809,13 @@ unsigned char* KlipperPrinter::get_32_32_png_image_thumbnail(const char* gcode_f } else { - free(img_filename_path); - return data_png; + thumbnail.png = data_png; + thumbnail.size = len; + thumbnail.success = true; } } } free(img_filename_path); - return NULL; + return thumbnail; } \ No newline at end of file diff --git a/CYD-Klipper/src/core/klipper/klipper_printer_integration.hpp b/CYD-Klipper/src/core/klipper/klipper_printer_integration.hpp index d758c91..e67e518 100644 --- a/CYD-Klipper/src/core/klipper/klipper_printer_integration.hpp +++ b/CYD-Klipper/src/core/klipper/klipper_printer_integration.hpp @@ -47,7 +47,7 @@ class KlipperPrinter : public BasePrinter bool set_power_device_state(const char* device_name, bool state); Files get_files(); bool start_file(const char* filename); - unsigned char* get_32_32_png_image_thumbnail(const char* gcode_filename); + Thumbnail get_32_32_png_image_thumbnail(const char* gcode_filename); bool set_target_temperature(PrinterTemperatureDevice device, unsigned int temperature); bool send_gcode(const char* gcode, bool wait = true); int get_slicer_time_estimate_s(); diff --git a/CYD-Klipper/src/core/printer_integration.hpp b/CYD-Klipper/src/core/printer_integration.hpp index 3381165..1b377af 100644 --- a/CYD-Klipper/src/core/printer_integration.hpp +++ b/CYD-Klipper/src/core/printer_integration.hpp @@ -128,6 +128,12 @@ typedef struct { void* open_panel; // type lv_event_cb_t } PrinterUiPanel; +typedef struct { + bool success; + unsigned int size; + unsigned char* png; +} Thumbnail; + class BasePrinter { protected: @@ -158,7 +164,8 @@ class BasePrinter // Free files externally when done virtual Files get_files() = 0; virtual bool start_file(const char* filename) = 0; - virtual unsigned char* get_32_32_png_image_thumbnail(const char* gcode_filename); + // Free thumbnail externally when done + virtual Thumbnail get_32_32_png_image_thumbnail(const char* gcode_filename); virtual bool set_target_temperature(PrinterTemperatureDevice device, unsigned int temperature) = 0; BasePrinter(unsigned char index); diff --git a/CYD-Klipper/src/ui/gcode_img.cpp b/CYD-Klipper/src/ui/gcode_img.cpp deleted file mode 100644 index bae3057..0000000 --- a/CYD-Klipper/src/ui/gcode_img.cpp +++ /dev/null @@ -1,141 +0,0 @@ -#include "gcode_img.h" -#include "lvgl.h" -#include "ui_utils.h" -#include -#include -#include -#include "../conf/global_config.h" -#include "../core/http_client.h" - -static unsigned char * data_png = NULL; -static char img_filename_path[256] = {0}; -static lv_img_dsc_t img_header = {0}; - -bool has_32_32_gcode_img(const char* filename) -{ - if (filename == NULL){ - LOG_LN("No gcode filename"); - return false; - } - - SETUP_HTTP_CLIENT("/server/files/thumbnails?filename=" + urlEncode(filename)); - int httpCode = 0; - try { - httpCode = client.GET(); - } - catch (...){ - LOG_LN("Exception while fetching gcode img location"); - return false; - } - - if (httpCode == 200) - { - JsonDocument doc; - deserializeJson(doc, client.getStream()); - 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){ - LOG_F(("Found 32x32 PNG gcode img at %s\n", filename)) - strcpy(img_filename_path, chosen_thumb); - return true; - } - } - else - { - LOG_F(("Failed to fetch gcode image data: %d\n", httpCode)) - } - - return false; -} - -lv_obj_t* draw_gcode_img() -{ - clear_img_mem(); - - if (img_filename_path[0] == 0){ - LOG_LN("No gcode img path"); - return NULL; - } - - SETUP_HTTP_CLIENT_FULL("/server/files/gcodes/" + urlEncode(img_filename_path), false, 2000); - - int httpCode = 0; - try { - httpCode = client.GET(); - } - catch (...){ - LOG_LN("Exception while fetching gcode img"); - return NULL; - } - - if (httpCode == 200) - { - size_t len = client.getSize(); - if (len <= 0) - { - LOG_LN("No gcode img data"); - return NULL; - } - - data_png = (unsigned char*)malloc(len + 1); - if (len != client.getStream().readBytes(data_png, len)){ - LOG_LN("Failed to read gcode img data"); - clear_img_mem(); - return NULL; - } - - 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); - - return img; - } - - return NULL; -} - -lv_obj_t* show_gcode_img(const char* filename) -{ - if (filename == NULL){ - LOG_LN("No gcode filename"); - return NULL; - } - - if (!has_32_32_gcode_img(filename)){ - LOG_LN("No 32x32 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 deleted file mode 100644 index ba591a9..0000000 --- a/CYD-Klipper/src/ui/gcode_img.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once -#include "lvgl.h" - -lv_obj_t* show_gcode_img(const char* filename); -bool has_32_32_gcode_img(const char* filename); -void clear_img_mem(); \ No newline at end of file diff --git a/CYD-Klipper/src/ui/panels/files_panel.cpp b/CYD-Klipper/src/ui/panels/files_panel.cpp index e720d8e..87aa77e 100644 --- a/CYD-Klipper/src/ui/panels/files_panel.cpp +++ b/CYD-Klipper/src/ui/panels/files_panel.cpp @@ -6,7 +6,6 @@ #include #include "../ui_utils.h" #include "../../core/lv_setup.h" -#include "../gcode_img.h" #include #include "../../core/printer_integration.hpp" @@ -62,10 +61,26 @@ static void btn_print_file_verify(lv_event_t * e){ lv_label_set_text(label, LV_SYMBOL_OK); lv_obj_center(label); - lv_obj_t* img = show_gcode_img(selected_file); + freeze_request_thread(); + Thumbnail thumbnail = get_current_printer()->get_32_32_png_image_thumbnail(selected_file); + unfreeze_request_thread(); - if (img != NULL){ - lv_obj_set_parent(img, panel); + lv_obj_t * img = NULL; + + if (thumbnail.success) + { + lv_img_dsc_t* img_header = (lv_img_dsc_t*)malloc(sizeof(lv_img_dsc_t)); + lv_obj_on_destroy_free_data(panel, img_header); + + memset(img_header, 0, sizeof(img_header)); + img_header->header.w = 32; + img_header->header.h = 32; + img_header->data_size = thumbnail.size; + img_header->header.cf = LV_IMG_CF_RAW_ALPHA; + img_header->data = thumbnail.png; + + img = lv_img_create(panel); + lv_img_set_src(img, img_header); lv_obj_align(img, LV_ALIGN_TOP_LEFT, 0, 0); lv_obj_t * text_center_panel = lv_create_empty_panel(panel);