Use new image system

This commit is contained in:
suchmememanyskill
2024-10-27 17:15:46 +01:00
parent be0bf0fc71
commit 75bb334b09
6 changed files with 38 additions and 161 deletions

View File

@@ -716,8 +716,9 @@ bool KlipperPrinter::set_target_temperature(PrinterTemperatureDevice device, uns
return send_gcode(gcode); 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; HTTPClient client;
configure_http_client(client, "/server/files/thumbnails?filename=", true, 1000); configure_http_client(client, "/server/files/thumbnails?filename=", true, 1000);
char* img_filename_path = NULL; char* img_filename_path = NULL;
@@ -731,7 +732,7 @@ unsigned char* KlipperPrinter::get_32_32_png_image_thumbnail(const char* gcode_f
catch (...) catch (...)
{ {
LOG_LN("Exception while fetching gcode img location"); LOG_LN("Exception while fetching gcode img location");
return NULL; return thumbnail;
} }
if (http_code == 200) 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) if (img_filename_path == NULL)
{ {
return NULL; return thumbnail;
} }
client.end(); client.end();
@@ -785,7 +786,7 @@ unsigned char* KlipperPrinter::get_32_32_png_image_thumbnail(const char* gcode_f
catch (...) catch (...)
{ {
LOG_LN("Exception while fetching gcode img"); LOG_LN("Exception while fetching gcode img");
return NULL; return thumbnail;
} }
if (http_code == 200) if (http_code == 200)
@@ -794,7 +795,7 @@ unsigned char* KlipperPrinter::get_32_32_png_image_thumbnail(const char* gcode_f
if (len <= 0) if (len <= 0)
{ {
LOG_LN("No gcode img data"); LOG_LN("No gcode img data");
return NULL; return thumbnail;
} }
data_png = (unsigned char*)malloc(len + 1); 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 else
{ {
free(img_filename_path); thumbnail.png = data_png;
return data_png; thumbnail.size = len;
thumbnail.success = true;
} }
} }
} }
free(img_filename_path); free(img_filename_path);
return NULL; return thumbnail;
} }

View File

@@ -47,7 +47,7 @@ class KlipperPrinter : public BasePrinter
bool set_power_device_state(const char* device_name, bool state); bool set_power_device_state(const char* device_name, bool state);
Files get_files(); Files get_files();
bool start_file(const char* filename); 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 set_target_temperature(PrinterTemperatureDevice device, unsigned int temperature);
bool send_gcode(const char* gcode, bool wait = true); bool send_gcode(const char* gcode, bool wait = true);
int get_slicer_time_estimate_s(); int get_slicer_time_estimate_s();

View File

@@ -128,6 +128,12 @@ typedef struct {
void* open_panel; // type lv_event_cb_t void* open_panel; // type lv_event_cb_t
} PrinterUiPanel; } PrinterUiPanel;
typedef struct {
bool success;
unsigned int size;
unsigned char* png;
} Thumbnail;
class BasePrinter class BasePrinter
{ {
protected: protected:
@@ -158,7 +164,8 @@ class BasePrinter
// Free files externally when done // Free files externally when done
virtual Files get_files() = 0; virtual Files get_files() = 0;
virtual bool start_file(const char* filename) = 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; virtual bool set_target_temperature(PrinterTemperatureDevice device, unsigned int temperature) = 0;
BasePrinter(unsigned char index); BasePrinter(unsigned char index);

View File

@@ -1,141 +0,0 @@
#include "gcode_img.h"
#include "lvgl.h"
#include "ui_utils.h"
#include <Esp.h>
#include <UrlEncode.h>
#include <ArduinoJson.h>
#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<JsonArray>();
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;
}
}

View File

@@ -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();

View File

@@ -6,7 +6,6 @@
#include <HardwareSerial.h> #include <HardwareSerial.h>
#include "../ui_utils.h" #include "../ui_utils.h"
#include "../../core/lv_setup.h" #include "../../core/lv_setup.h"
#include "../gcode_img.h"
#include <UrlEncode.h> #include <UrlEncode.h>
#include "../../core/printer_integration.hpp" #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_label_set_text(label, LV_SYMBOL_OK);
lv_obj_center(label); 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_t * img = NULL;
lv_obj_set_parent(img, panel);
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_align(img, LV_ALIGN_TOP_LEFT, 0, 0);
lv_obj_t * text_center_panel = lv_create_empty_panel(panel); lv_obj_t * text_center_panel = lv_create_empty_panel(panel);