From 6a9023eb8d269476658069247120a05159f53bc4 Mon Sep 17 00:00:00 2001 From: Sims <38142618+suchmememanyskill@users.noreply.github.com> Date: Thu, 24 Oct 2024 18:18:15 +0200 Subject: [PATCH] Implement klipper custom ui panels --- .../klipper/klipper_printer_integration.cpp | 13 +- .../klipper/klipper_printer_integration.hpp | 8 +- .../core/klipper/klipper_printer_panels.cpp | 185 ++++++++++++++++++ CYD-Klipper/src/core/printer_integration.hpp | 5 +- CYD-Klipper/src/ui/ui_utils.cpp | 2 +- CYD-Klipper/src/ui/ui_utils.h | 2 +- 6 files changed, 204 insertions(+), 11 deletions(-) create mode 100644 CYD-Klipper/src/core/klipper/klipper_printer_panels.cpp diff --git a/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp b/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp index 11e15f5..8709e2d 100644 --- a/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp +++ b/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp @@ -61,7 +61,7 @@ bool KlipperPrinter::send_gcode(const char *gcode, bool wait) bool KlipperPrinter::move_printer(const char* axis, float amount, bool relative) { if (!printer_data.homed_axis || printer_data.state == PrinterStatePrinting) - return true; + return false; char gcode[64]; const char* extra = (amount > 0) ? "+" : ""; @@ -86,6 +86,7 @@ bool KlipperPrinter::move_printer(const char* axis, float amount, bool relative) send_gcode(gcode); lock_absolute_relative_mode_swap = 2; + return true; } bool KlipperPrinter::execute_feature(PrinterFeatures feature) @@ -155,8 +156,9 @@ bool KlipperPrinter::execute_feature(PrinterFeatures feature) return send_gcode("M104 S0\nM140 S0"); default: LOG_F(("Unsupported printer feature %d", feature)); - return false; } + + return false; } bool KlipperPrinter::connect() @@ -453,6 +455,8 @@ PrinterDataMinimal KlipperPrinter::fetch_min() data.state = PrinterStateOffline; data.power_devices = get_power_devices_count(); } + + return data; } void KlipperPrinter::disconnect() @@ -686,8 +690,9 @@ bool KlipperPrinter::start_file(const char *filename) HTTPClient client; configure_http_client(client, "/printer/print/start?filename=" + urlEncode(filename), false, 1000); - int httpCode = client.POST(""); - LOG_F(("Print start: HTTP %d\n", httpCode)) + int http_code = client.POST(""); + LOG_F(("Print start: HTTP %d\n", http_code)) + return http_code == 200; } bool KlipperPrinter::set_target_temperature(PrinterTemperatureDevice device, float temperature) diff --git a/CYD-Klipper/src/core/klipper/klipper_printer_integration.hpp b/CYD-Klipper/src/core/klipper/klipper_printer_integration.hpp index 25c552b..445c652 100644 --- a/CYD-Klipper/src/core/klipper/klipper_printer_integration.hpp +++ b/CYD-Klipper/src/core/klipper/klipper_printer_integration.hpp @@ -2,16 +2,17 @@ #include "../printer_integration.hpp" -class KlipperPrinter : BasePrinter +class KlipperPrinter : public BasePrinter { private: unsigned char lock_absolute_relative_mode_swap{}; unsigned char klipper_request_consecutive_fail_count{}; unsigned int slicer_estimated_print_time_s{}; unsigned int last_slicer_time_query{}; - float gcode_offset[3]{}; public: + float gcode_offset[3]{}; + KlipperPrinter(int index) : BasePrinter(index) { supported_features = PrinterFeatureRestart @@ -28,6 +29,8 @@ class KlipperPrinter : BasePrinter supported_temperature_devices = PrinterTemperatureDeviceBed | PrinterTemperatureDeviceNozzle1; + + init_ui_panels(); } bool move_printer(const char* axis, float amount, bool relative); @@ -49,4 +52,5 @@ class KlipperPrinter : BasePrinter bool send_gcode(const char* gcode, bool wait = true); int get_slicer_time_estimate_s(); void configure_http_client(HTTPClient &client, String url_part, bool stream, int timeout); + void init_ui_panels(); }; \ No newline at end of file diff --git a/CYD-Klipper/src/core/klipper/klipper_printer_panels.cpp b/CYD-Klipper/src/core/klipper/klipper_printer_panels.cpp new file mode 100644 index 0000000..04226e1 --- /dev/null +++ b/CYD-Klipper/src/core/klipper/klipper_printer_panels.cpp @@ -0,0 +1,185 @@ +#include "klipper_printer_integration.hpp" +#include "lvgl.h" +#include "../../ui/ui_utils.h" +#include + + +static void set_fan_speed_text(lv_event_t * e) { + lv_obj_t * label = lv_event_get_target(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + char data[16]; + sprintf(data, "Fan: %.0f%%", printer->printer_data.fan_speed * 100); + lv_label_set_text(label, data); +} + +static void set_fan_speed(lv_event_t * e){ + int speed = (int)lv_event_get_user_data(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + char gcode[16]; + sprintf(gcode, "M106 S%d", speed); + printer->send_gcode(gcode); +} + +const char* fan_speeds[] = { "0%", "15%", "25%", "35%" }; +const int fan_speeds_values[] = { 0, 38, 64, 90 }; + +const char* fan_speeds_2[] = { "50%", "75%", "100%"}; +const int fan_speeds_values_2[] = { 128, 192, 255 }; + +lv_button_column_t fan_speed_columns[] = { + { set_fan_speed, fan_speeds, (const void**)fan_speeds_values, 4}, + { set_fan_speed, fan_speeds_2, (const void**)fan_speeds_values_2, 3} +}; + +static void set_zoffset_text(lv_event_t * e) { + lv_obj_t * label = lv_event_get_target(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + char data[24]; + sprintf(data, "Z Offset: %.03f", printer->gcode_offset[2]); + lv_label_set_text(label, data); +} + +static void set_zoffset_text_ex(lv_event_t * e) { + lv_obj_t * label = lv_event_get_target(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + char data[32]; + sprintf(data, "Z Offset: %.03f, Z: %.03f", printer->gcode_offset[2], printer->printer_data.position[2]); + lv_label_set_text(label, data); +} + +static void set_zoffset(lv_event_t * e){ + char* offset = (char*)lv_event_get_user_data(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + + char gcode[48]; + sprintf(gcode, "SET_GCODE_OFFSET Z_ADJUST=%s MOVE=1", offset); + printer->send_gcode(gcode); +} + +static void set_z(lv_event_t * e){ + void* ptr = lv_event_get_user_data(e); + float value = *(float *)(&ptr); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + + if (value < 0) { + printer->send_gcode("SET_GCODE_OFFSET Z=0 MOVE=1"); + return; + } + + printer->move_printer("Z", value, false); +} + +const char* zoffsets[] = { "-0.01", "-0.025", "-0.05", "-0.2" }; +const char* zoffsets_2[] = { "+0.01", "+0.025", "+0.05", "+0.2" }; +const char* zabs[] = { "Z=0", "Z=0.1", "Z=1", "Clear" }; +const float zabsvalues[] = { 0, 0.1f, 1.0f, -1.0f }; + +lv_button_column_t zoffset_columns[] = { + { set_zoffset, zoffsets, (const void**)zoffsets, 4}, + { set_zoffset, zoffsets_2, (const void**)zoffsets_2, 4}, + { set_z, zabs, (const void**)zabsvalues, 4} +}; + +static void set_speed_mult_text(lv_event_t * e){ + lv_obj_t * label = lv_event_get_target(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + char data[16]; + sprintf(data, "Speed: %.0f%%", printer->printer_data.speed_mult * 100); + lv_label_set_text(label, data); +} + +static void set_speed_mult(lv_event_t * e){ + int speed = (int)lv_event_get_user_data(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + char gcode[16]; + sprintf(gcode, "M220 S%d", speed); + printer->send_gcode(gcode); +} + +static void set_speed_mult_offset(lv_event_t * e){ + int speed = (int)lv_event_get_user_data(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + float result = printer->printer_data.speed_mult * 100 + speed; + printer->printer_data.speed_mult = result / 100; + char gcode[16]; + sprintf(gcode, "M220 S%.0f", result); + printer->send_gcode(gcode); +} + +const char* speed_presets[] = { "50%", "100%", "150%", "200%" }; +const int speed_presets_values[] = { 50, 100, 150, 200 }; +const char* speed_presets_minus[] = { "-1%", "-5%", "-10%", "-25%" }; +const int speed_presets_minus_values[] = { -1, -5, -10, -25 }; +const char* speed_presets_plus[] = { "+1%", "+5%", "+10%", "+25%" }; +const int speed_presets_plus_values[] = { 1, 5, 10, 25 }; + +lv_button_column_t speed_mult_columns[] = { + { set_speed_mult, speed_presets, (const void**)speed_presets_values, 4}, + { set_speed_mult_offset, speed_presets_minus, (const void**)speed_presets_minus_values, 4}, + { set_speed_mult_offset, speed_presets_plus, (const void**)speed_presets_plus_values, 4} +}; + +static void set_extrude_mult_text(lv_event_t * e){ + lv_obj_t * label = lv_event_get_target(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + char data[16]; + sprintf(data, "Flow: %.0f%%", printer->printer_data.extrude_mult * 100); + lv_label_set_text(label, data); +} + +static void set_extrude_mult(lv_event_t * e){ + int speed = (int)lv_event_get_user_data(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + char gcode[16]; + sprintf(gcode, "M221 S%d", speed); + printer->send_gcode(gcode); +} + +static void set_extrude_mult_offset(lv_event_t * e){ + int speed = (int)lv_event_get_user_data(e); + KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref + float result = printer->printer_data.extrude_mult * 100 + speed; + printer->printer_data.extrude_mult = result / 100; + char gcode[16]; + sprintf(gcode, "M221 S%.0f", result); + printer->send_gcode(gcode); +} + +const char* extrude_presets[] = { "95%", "100%", "105%", "110%" }; +const int extrude_presets_values[] = { 95, 100, 105, 110 }; +const char* extrude_offset[] = { "+5%", "+1%", "-1%", "-5%" }; +const int extrude_offset_values[] = { 5, 1, -1, -5 }; + +lv_button_column_t extrude_mult_columns[] = { + { set_extrude_mult, extrude_presets, (const void**)extrude_presets_values, 4}, + { set_extrude_mult_offset, extrude_offset, (const void**)extrude_offset_values, 4} +}; + +static void open_fan_speed_panel(lv_event_t * e){ + lv_create_fullscreen_button_matrix_popup(lv_scr_act(), set_fan_speed_text, fan_speed_columns, 2); +} + +static void open_zoffset_panel(lv_event_t * e){ + lv_create_fullscreen_button_matrix_popup(lv_scr_act(), set_zoffset_text_ex, zoffset_columns, (get_current_printer()->printer_data.state == PrinterStateIdle) ? 3 : 2); +} + +static void open_speed_mult_panel(lv_event_t * e){ + lv_create_fullscreen_button_matrix_popup(lv_scr_act(), set_speed_mult_text, speed_mult_columns, 3); +} + +static void open_extrude_mult_panel(lv_event_t * e){ + lv_create_fullscreen_button_matrix_popup(lv_scr_act(), set_extrude_mult_text, extrude_mult_columns, 2); +} + +static PrinterUiPanel klipper_ui_panels[4] { + { .set_label = set_fan_speed_text, .open_panel = open_fan_speed_panel }, + { .set_label = set_zoffset_text, .open_panel = open_zoffset_panel }, + { .set_label = set_speed_mult_text, .open_panel = open_speed_mult_panel }, + { .set_label = set_extrude_mult_text, .open_panel = open_extrude_mult_panel } +}; + +void KlipperPrinter::init_ui_panels() +{ + custom_menus_count = 4; + custom_menus = klipper_ui_panels; +} \ No newline at end of file diff --git a/CYD-Klipper/src/core/printer_integration.hpp b/CYD-Klipper/src/core/printer_integration.hpp index 540b212..8847a9a 100644 --- a/CYD-Klipper/src/core/printer_integration.hpp +++ b/CYD-Klipper/src/core/printer_integration.hpp @@ -1,5 +1,4 @@ #pragma once -#include "lvgl.h" #include "../conf/global_config.h" #define BIT(x) 1 << x @@ -125,8 +124,8 @@ typedef struct { } Files; typedef struct { - lv_event_cb_t set_label; - lv_event_cb_t open_panel; + void* set_label; // type lv_event_cb_t + void* open_panel; // type lv_event_cb_t } PrinterUiPanel; class BasePrinter diff --git a/CYD-Klipper/src/ui/ui_utils.cpp b/CYD-Klipper/src/ui/ui_utils.cpp index eff5efa..12c9053 100644 --- a/CYD-Klipper/src/ui/ui_utils.cpp +++ b/CYD-Klipper/src/ui/ui_utils.cpp @@ -40,7 +40,7 @@ void destroy_event_free_data(lv_event_t * e) free(data); } -void on_destroy_free_data(lv_obj_t * element, void* ptr) +void lv_on_destroy_free_data(lv_obj_t * element, void* ptr) { lv_obj_add_event_cb(element, destroy_event_free_data, LV_EVENT_DELETE, ptr); } diff --git a/CYD-Klipper/src/ui/ui_utils.h b/CYD-Klipper/src/ui/ui_utils.h index e50ead5..32790b3 100644 --- a/CYD-Klipper/src/ui/ui_utils.h +++ b/CYD-Klipper/src/ui/ui_utils.h @@ -38,7 +38,7 @@ void lv_layout_flex_column(lv_obj_t* obj, lv_flex_align_t allign = LV_FLEX_ALIGN void lv_layout_flex_row(lv_obj_t* obj, lv_flex_align_t allign = LV_FLEX_ALIGN_START, lv_coord_t pad_column = CYD_SCREEN_GAP_PX, lv_coord_t pad_row = CYD_SCREEN_GAP_PX); void lv_create_fullscreen_button_matrix_popup(lv_obj_t * root, lv_event_cb_t title, lv_button_column_t* columns, int column_count); void destroy_event_user_data(lv_event_t * e); -void on_destroy_free_data(lv_obj_t * element, void* ptr); +void lv_on_destroy_free_data(lv_obj_t * element, void* ptr); void lv_create_keyboard_text_entry(lv_event_cb_t keyboard_callback, const char* title = NULL, lv_keyboard_mode_t keyboard_mode = LV_KEYBOARD_MODE_NUMBER, lv_coord_t width = CYD_SCREEN_PANEL_WIDTH_PX / 2, uint8_t max_length = 3, const char* fill_text = "", bool contain_in_panel= true); void lv_create_custom_menu_entry(const char* label_text, lv_obj_t* object, lv_obj_t* root_panel, bool set_height = true, const char * comment = NULL); void lv_create_custom_menu_button(const char *label_text, lv_obj_t* root_panel, lv_event_cb_t on_click, const char *btn_text, void * user_data = NULL, const char * comment = NULL);