Skip to content

Commit

Permalink
Merge branch 'experimental_load_gcode_img-2' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Mar 2, 2024
2 parents bc3ca38 + 4e6457c commit ea42bf7
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 9 deletions.
14 changes: 7 additions & 7 deletions CYD-Klipper/src/conf/lv_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 (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*/
Expand Down Expand Up @@ -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:
Expand All @@ -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()`*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion CYD-Klipper/src/core/device/ESP32-2432S028R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion CYD-Klipper/src/core/device/ESP32-smartdisplay.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#ifdef CYD_SCREEN_DRIVER_ESP32_SMARTDISPLAY

#include "../screen_driver.h"
Expand Down
1 change: 1 addition & 0 deletions CYD-Klipper/src/core/lv_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ void lv_setup()
screen_timer_setup();
screen_timer_start();
set_screen_brightness();
lv_png_init();
}

bool is_screen_asleep()
Expand Down
146 changes: 146 additions & 0 deletions CYD-Klipper/src/ui/gcode_img.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include "gcode_img.h"
#include "lvgl.h"
#include "ui_utils.h"
#include <esp.h>
#include <ArduinoJson.h>
#include "../conf/global_config.h"
#include <HTTPClient.h>

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<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){
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;
}

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){
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;
}
}
6 changes: 6 additions & 0 deletions CYD-Klipper/src/ui/gcode_img.h
Original file line number Diff line number Diff line change
@@ -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();
10 changes: 10 additions & 0 deletions CYD-Klipper/src/ui/panels/print_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <HTTPClient.h>
#include "../ui_utils.h"
#include "../../core/lv_setup.h"
#include "../gcode_img.h"

FILESYSTEM_FILE* selected_file = NULL;

Expand Down Expand Up @@ -83,6 +84,13 @@ 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);
}
}

void print_panel_init(lv_obj_t* panel){
Expand All @@ -91,6 +99,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);
Expand Down

0 comments on commit ea42bf7

Please sign in to comment.