From 68b68af7151c5751813e19eabf6a66d72016d3e4 Mon Sep 17 00:00:00 2001 From: Sims <38142618+suchmememanyskill@users.noreply.github.com> Date: Sun, 15 Dec 2024 00:40:11 +0100 Subject: [PATCH] Add octoprint panels --- .../octoprint_printer_integration.cpp | 8 +- .../octoprint_printer_integration.hpp | 7 +- .../octoprint/octoprint_printer_panels.cpp | 111 ++++++++++++++++++ 3 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 CYD-Klipper/src/core/octoprint/octoprint_printer_panels.cpp diff --git a/CYD-Klipper/src/core/octoprint/octoprint_printer_integration.cpp b/CYD-Klipper/src/core/octoprint/octoprint_printer_integration.cpp index acacc04..a3f3f4f 100644 --- a/CYD-Klipper/src/core/octoprint/octoprint_printer_integration.cpp +++ b/CYD-Klipper/src/core/octoprint/octoprint_printer_integration.cpp @@ -26,7 +26,7 @@ void configure_http_client(HTTPClient &client, String url_part, bool stream, int } } -bool OctoPrinter::get_request(const char* endpoint, int timeout_ms, bool stream) +bool OctoPrinter::get_request(const char* endpoint, int timeout_ms) { HTTPClient client; @@ -35,12 +35,12 @@ bool OctoPrinter::get_request(const char* endpoint, int timeout_ms, bool stream) timeout_ms = 500; } - configure_http_client(client, endpoint, stream, timeout_ms, printer_config); + configure_http_client(client, endpoint, false, timeout_ms, printer_config); int result = client.GET(); return result >= 200 && result < 300; } -bool OctoPrinter::post_request(const char* endpoint, const char* body, int timeout_ms, bool stream) +bool OctoPrinter::post_request(const char* endpoint, const char* body, int timeout_ms) { HTTPClient client; @@ -49,7 +49,7 @@ bool OctoPrinter::post_request(const char* endpoint, const char* body, int timeo timeout_ms = 500; } - configure_http_client(client, endpoint, stream, timeout_ms, printer_config); + configure_http_client(client, endpoint, false, timeout_ms, printer_config); if (body[0] == '{' || body[0] == '[') { diff --git a/CYD-Klipper/src/core/octoprint/octoprint_printer_integration.hpp b/CYD-Klipper/src/core/octoprint/octoprint_printer_integration.hpp index 5f12513..0c0901c 100644 --- a/CYD-Klipper/src/core/octoprint/octoprint_printer_integration.hpp +++ b/CYD-Klipper/src/core/octoprint/octoprint_printer_integration.hpp @@ -19,8 +19,8 @@ class OctoPrinter : public BasePrinter void parse_error(JsonDocument& in); void OctoPrinter::parse_file_list(JsonDocument &in, std::list &files, int fetch_limit); - bool get_request(const char* endpoint, int timeout_ms = 1000, bool stream = true); - bool post_request(const char* endpoint, const char* body, int timeout_ms = 1000, bool stream = false); + bool get_request(const char* endpoint, int timeout_ms = 1000); + void init_ui_panels(); public: OctoPrinter(int index) : BasePrinter(index) @@ -39,8 +39,11 @@ class OctoPrinter : public BasePrinter | PrinterTemperatureDeviceNozzle1; printer_data.error_screen_features = PrinterFeatureRetryError; + + init_ui_panels(); } + bool post_request(const char* endpoint, const char* body, int timeout_ms = 1000); bool move_printer(const char* axis, float amount, bool relative); bool execute_feature(PrinterFeatures feature); bool connect(); diff --git a/CYD-Klipper/src/core/octoprint/octoprint_printer_panels.cpp b/CYD-Klipper/src/core/octoprint/octoprint_printer_panels.cpp new file mode 100644 index 0000000..f778eb8 --- /dev/null +++ b/CYD-Klipper/src/core/octoprint/octoprint_printer_panels.cpp @@ -0,0 +1,111 @@ +#include "octoprint_printer_integration.hpp" +#include "lvgl.h" +#include "../../ui/ui_utils.h" +#include + +const char* COMMAND_EXTRUDE_MULT = "{\"command\":\"flowrate\",\"factor\":%d}"; + +#define OCTO_TIMEOUT_POPUP_MESSAGES 4000 + +static void set_fan_speed_text(lv_event_t * e) +{ + lv_obj_t * label = lv_event_get_target(e); + lv_label_set_text(label, "Fan"); +} + +static void set_speed_mult_text(lv_event_t * e) +{ + lv_obj_t * label = lv_event_get_target(e); + lv_label_set_text(label, "Speed"); +} + +static void set_extruder_mult_text(lv_event_t * e) +{ + lv_obj_t * label = lv_event_get_target(e); + lv_label_set_text(label, "Flowrate"); +} + +bool get_range(lv_event_t * e, int min, int max, int* out) +{ + char buff[64]; + lv_obj_t * ta = lv_event_get_target(e); + lv_obj_t * kb = (lv_obj_t *)lv_event_get_user_data(e); + const char * txt = lv_textarea_get_text(ta); + + if (txt == NULL || *txt == '\0') + { + return false; + } + + int parsed_input = atoi(txt); + if (parsed_input < min || parsed_input > max) + { + sprintf(buff, "Value out of range (%d -> %d)", min, max); + lv_create_popup_message(buff, OCTO_TIMEOUT_POPUP_MESSAGES); + return false; + } + + *out = parsed_input; + return true; +} + +static void set_fan_speed(lv_event_t * e) +{ + int fan_speed = 0; + if (get_range(e, 0, 100, &fan_speed)) + { + int actual_fan_speed = fan_speed * 255 / 100; + char buff[16]; + sprintf(buff, "M106 S%d", actual_fan_speed); + ((OctoPrinter*)get_current_printer())->send_gcode(buff); + } +} + +static void open_fan_speed_keypad(lv_event_t * e) +{ + lv_create_keyboard_text_entry(set_fan_speed, "New fan speed %", LV_KEYBOARD_MODE_NUMBER); +} + +static void set_speed_mult(lv_event_t * e) +{ + int speed_mult = 0; + if (get_range(e, 50, 300, &speed_mult)) + { + char buff[16]; + sprintf(buff, "M220 S%d", speed_mult); + ((OctoPrinter*)get_current_printer())->send_gcode(buff); + } +} + +static void open_speed_mult_keypad(lv_event_t * e) +{ + lv_create_keyboard_text_entry(set_speed_mult, "New speed multiplier %", LV_KEYBOARD_MODE_NUMBER); +} + +static void set_extrude_mult(lv_event_t * e) +{ + int extrude_mult = 0; + if (get_range(e, 75, 125, &extrude_mult)) + { + char buff[64]; + sprintf(buff, COMMAND_EXTRUDE_MULT, extrude_mult); + ((OctoPrinter*)get_current_printer())->post_request("/api/printer/tool", buff); + } +} + +static void open_extrude_mult_keypad(lv_event_t * e) +{ + lv_create_keyboard_text_entry(set_extrude_mult, "New extrude multiplier %", LV_KEYBOARD_MODE_NUMBER); +} + +static PrinterUiPanel klipper_ui_panels[4] { + { .set_label = (void*)set_fan_speed_text, .open_panel = (void*)open_fan_speed_keypad }, + { .set_label = (void*)set_speed_mult_text, .open_panel = (void*)open_speed_mult_keypad }, + { .set_label = (void*)set_extruder_mult_text, .open_panel = (void*)open_extrude_mult_keypad }, +}; + +void OctoPrinter::init_ui_panels() +{ + custom_menus_count = 4; + custom_menus = klipper_ui_panels; +} \ No newline at end of file