diff --git a/CYD-Klipper/src/core/data_setup.cpp b/CYD-Klipper/src/core/data_setup.cpp index e64a732..89e2f5a 100644 --- a/CYD-Klipper/src/core/data_setup.cpp +++ b/CYD-Klipper/src/core/data_setup.cpp @@ -313,7 +313,7 @@ void fetch_printer_data_minimal() delay(10); HTTPClient client; - configure_http_client(client, "http://" + String(config->klipper_host) + ":" + String(config->klipper_port) + "/printer/objects/query?webhooks&print_stats&virtual_sdcard", true, 1000); + configure_http_client(client, get_full_url("/printer/objects/query?webhooks&print_stats&virtual_sdcard", config), true, 1000); freeze_request_thread(); int httpCode = client.GET(); @@ -370,8 +370,6 @@ void fetch_printer_data_minimal() } } } - - Serial.printf("Record: %d %f %d\n", data[i].online, data[i].print_progress, data[i].state); } else { diff --git a/CYD-Klipper/src/core/http_client.cpp b/CYD-Klipper/src/core/http_client.cpp index 42edba3..4aeeb09 100644 --- a/CYD-Klipper/src/core/http_client.cpp +++ b/CYD-Klipper/src/core/http_client.cpp @@ -1,5 +1,9 @@ #include "http_client.h" -#include "../conf/global_config.h" + +String get_full_url(String url_part, PRINTER_CONFIG * config) +{ + return "http://" + String(config->klipper_host) + ":" + String(config->klipper_port) + url_part; +} String get_full_url(String url_part) { diff --git a/CYD-Klipper/src/core/http_client.h b/CYD-Klipper/src/core/http_client.h index d5c6d61..a3115ac 100644 --- a/CYD-Klipper/src/core/http_client.h +++ b/CYD-Klipper/src/core/http_client.h @@ -1,8 +1,10 @@ #pragma once #include +#include "../conf/global_config.h" String get_full_url(String url_part); +String get_full_url(String url_part, PRINTER_CONFIG * config); void configure_http_client(HTTPClient &client, String url, bool stream = true, int timeout = 1000); diff --git a/CYD-Klipper/src/ui/ip_setup.cpp b/CYD-Klipper/src/ui/ip_setup.cpp index d741fd4..ac1644b 100644 --- a/CYD-Klipper/src/ui/ip_setup.cpp +++ b/CYD-Klipper/src/ui/ip_setup.cpp @@ -7,6 +7,7 @@ #include "../core/macros_query.h" #include "panels/panel.h" #include "../core/http_client.h" +#include "switch_printer.h" bool connect_ok = false; lv_obj_t * hostEntry; @@ -161,6 +162,8 @@ void redraw_connect_screen(){ lv_label_set_text(btn_label, "Power Devices"); lv_obj_center(btn_label); } + + draw_switch_printer_button(); } static bool auth_entry_done = false; diff --git a/CYD-Klipper/src/ui/main_ui.cpp b/CYD-Klipper/src/ui/main_ui.cpp index 8191d5c..d02219d 100644 --- a/CYD-Klipper/src/ui/main_ui.cpp +++ b/CYD-Klipper/src/ui/main_ui.cpp @@ -8,6 +8,7 @@ #include "panels/panel.h" #include "../core/macros_query.h" #include "../core/lv_setup.h" +#include "switch_printer.h" char extruder_temp_buff[20]; char bed_temp_buff[20]; @@ -90,6 +91,8 @@ void error_ui(){ lv_label_set_text(label, "Devices"); lv_obj_center(label); } + + draw_switch_printer_button(); } void check_if_screen_needs_to_be_disabled(){ diff --git a/CYD-Klipper/src/ui/panels/settings_panel.cpp b/CYD-Klipper/src/ui/panels/settings_panel.cpp index 8db8ce3..1072fc8 100644 --- a/CYD-Klipper/src/ui/panels/settings_panel.cpp +++ b/CYD-Klipper/src/ui/panels/settings_panel.cpp @@ -173,25 +173,19 @@ void settings_panel_init(lv_obj_t* panel){ lv_create_custom_menu_switch("Multi Printer Mode", panel, multi_printer_switch, global_config.multi_printer_mode); lv_create_custom_menu_switch("Auto Update", panel, auto_ota_update_switch, global_config.auto_ota_update); - - - lv_obj_t *label = lv_label_create(panel); - lv_label_set_text(label, REPO_VERSION " "); - lv_create_custom_menu_entry("Version", label, panel, false); + lv_create_custom_menu_label("Version", panel, REPO_VERSION " "); if (ota_has_update()){ lv_obj_t *btn = lv_btn_create(panel); lv_obj_add_event_cb(btn, btn_ota_do_update, LV_EVENT_CLICKED, NULL); - label = lv_label_create(btn); + lv_obj_t *label = lv_label_create(btn); lv_label_set_text_fmt(label, "Update to %s", ota_new_version_name().c_str()); lv_obj_center(label); lv_create_custom_menu_entry("Device", btn, panel); } else { - label = lv_label_create(panel); - lv_label_set_text(label, ARDUINO_BOARD " "); - lv_create_custom_menu_entry("Device", label, panel, false); + lv_create_custom_menu_label("Device", panel, ARDUINO_BOARD " "); } } \ No newline at end of file diff --git a/CYD-Klipper/src/ui/switch_printer.cpp b/CYD-Klipper/src/ui/switch_printer.cpp new file mode 100644 index 0000000..34c5684 --- /dev/null +++ b/CYD-Klipper/src/ui/switch_printer.cpp @@ -0,0 +1,88 @@ +#include "switch_printer.h" +#include "lvgl.h" +#include "../conf/global_config.h" +#include "ui_utils.h" +#include "../core/http_client.h" +#include "../core/lv_setup.h" +#include "../core/macros_query.h" + +static void btn_switch_printer(lv_event_t *e){ + lv_obj_t *btn = lv_event_get_target(e); + PRINTER_CONFIG * config = (PRINTER_CONFIG*)lv_event_get_user_data(e); + int index = config - global_config.printer_config; + + set_printer_config_index(index); + set_color_scheme(); + _macros_query_internal(); + _power_devices_query_internal(); + + lv_obj_del(lv_obj_get_parent(lv_obj_get_parent(btn))); +} + +void switch_printer_init() { + lv_obj_t * parent = lv_create_empty_panel(lv_scr_act()); + lv_obj_set_style_bg_opa(parent, LV_OPA_100, 0); + lv_obj_align(parent, LV_ALIGN_TOP_RIGHT, 0, 0); + lv_obj_set_size(parent, CYD_SCREEN_WIDTH_PX, CYD_SCREEN_HEIGHT_PX); + lv_layout_flex_column(parent); + + lv_obj_set_size(lv_create_empty_panel(parent), 0, 0); + + auto width = CYD_SCREEN_WIDTH_PX - CYD_SCREEN_GAP_PX * 2; + + lv_obj_t * btn = lv_btn_create(parent); + lv_obj_set_size(btn, width, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX); + lv_obj_add_event_cb(btn, destroy_event_user_data, LV_EVENT_CLICKED, parent); + + lv_obj_t * label = lv_label_create(btn); + lv_label_set_text(label, LV_SYMBOL_CLOSE " Close"); + lv_obj_center(label); + + for (int i = 0; i < PRINTER_CONFIG_COUNT; i++){ + PRINTER_CONFIG * config = &global_config.printer_config[i]; + const char* printer_name = (config->printer_name[0] == 0) ? config->klipper_host : config->printer_name; + + if (config == get_current_printer_config()) + { + lv_create_custom_menu_label(printer_name, parent, "Active"); + continue; + } + + if (config->ip_configured) { + HTTPClient client; + configure_http_client(client, get_full_url("/printer/objects/query?webhooks&print_stats&virtual_sdcard", config), true, 1000); + + + int httpCode = client.GET(); + if (httpCode == 200) + { + lv_create_custom_menu_button(printer_name, parent, btn_switch_printer, "Switch", config); + } + else + { + lv_create_custom_menu_label(printer_name, parent, "Offline"); + } + } + } +} + +static void show_switch_printer_screen(lv_event_t * e){ + switch_printer_init(); +} + +void draw_switch_printer_button() +{ + if (!global_config.multi_printer_mode) + { + return; + } + + lv_obj_t * btn = lv_btn_create(lv_scr_act()); + lv_obj_set_size(btn, CYD_SCREEN_MIN_BUTTON_WIDTH_PX, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX); + lv_obj_align(btn, LV_ALIGN_TOP_RIGHT, -CYD_SCREEN_GAP_PX, CYD_SCREEN_GAP_PX); + lv_obj_add_event_cb(btn, show_switch_printer_screen, LV_EVENT_CLICKED, NULL); + + lv_obj_t * label = lv_label_create(btn); + lv_label_set_text(label, LV_SYMBOL_HOME); + lv_obj_center(label); +} \ No newline at end of file diff --git a/CYD-Klipper/src/ui/switch_printer.h b/CYD-Klipper/src/ui/switch_printer.h new file mode 100644 index 0000000..eb2d8a0 --- /dev/null +++ b/CYD-Klipper/src/ui/switch_printer.h @@ -0,0 +1,4 @@ +#pragma once + +void switch_printer_init(); +void draw_switch_printer_button(); \ No newline at end of file diff --git a/CYD-Klipper/src/ui/ui_utils.cpp b/CYD-Klipper/src/ui/ui_utils.cpp index 98606b4..c43592b 100644 --- a/CYD-Klipper/src/ui/ui_utils.cpp +++ b/CYD-Klipper/src/ui/ui_utils.cpp @@ -192,4 +192,11 @@ void lv_create_custom_menu_dropdown(const char *label_text, lv_obj_t *root_panel lv_obj_add_event_cb(dropdown, on_change, LV_EVENT_VALUE_CHANGED, user_data); lv_create_custom_menu_entry(label_text, dropdown, root_panel, true); +} + +void lv_create_custom_menu_label(const char *label_text, lv_obj_t* root_panel, const char *text) +{ + lv_obj_t * label = lv_label_create(lv_scr_act()); + lv_label_set_text(label, text); + lv_create_custom_menu_entry(label_text, label, root_panel, false); } \ No newline at end of file diff --git a/CYD-Klipper/src/ui/ui_utils.h b/CYD-Klipper/src/ui/ui_utils.h index 7bfcba1..295a2cf 100644 --- a/CYD-Klipper/src/ui/ui_utils.h +++ b/CYD-Klipper/src/ui/ui_utils.h @@ -42,4 +42,5 @@ void lv_create_keyboard_text_entry(lv_event_cb_t keyboard_callback, lv_keyboard_ void lv_create_custom_menu_entry(const char* label_text, lv_obj_t* object, lv_obj_t* root_panel, bool set_height = true); 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); void lv_create_custom_menu_switch(const char *label_text, lv_obj_t* root_panel, lv_event_cb_t on_toggle, bool state, void * user_data = NULL); -void lv_create_custom_menu_dropdown(const char *label_text, lv_obj_t *root_panel, lv_event_cb_t on_change, const char *options, int index, void * user_data = NULL); \ No newline at end of file +void lv_create_custom_menu_dropdown(const char *label_text, lv_obj_t *root_panel, lv_event_cb_t on_change, const char *options, int index, void * user_data = NULL); +void lv_create_custom_menu_label(const char *label_text, lv_obj_t* root_panel, const char *text); \ No newline at end of file