From a8c94fe207f2f581d26b7d8e12fef7127b9de423 Mon Sep 17 00:00:00 2001 From: suchmememanyskill <38142618+suchmememanyskill@users.noreply.github.com> Date: Tue, 12 Mar 2024 21:08:48 +0100 Subject: [PATCH] Refactor + Power menu in printer menu --- CYD-Klipper/src/core/data_setup.cpp | 6 +- CYD-Klipper/src/core/data_setup.h | 1 + CYD-Klipper/src/core/macros_query.cpp | 142 ++++++++++++++------ CYD-Klipper/src/core/macros_query.h | 15 ++- CYD-Klipper/src/ui/ip_setup.cpp | 32 ++--- CYD-Klipper/src/ui/macros.cpp | 75 +++++++++++ CYD-Klipper/src/ui/macros.h | 9 ++ CYD-Klipper/src/ui/main_ui.cpp | 5 +- CYD-Klipper/src/ui/panels/macros_panel.cpp | 40 +----- CYD-Klipper/src/ui/panels/panel.h | 3 +- CYD-Klipper/src/ui/panels/printer_panel.cpp | 45 ++++++- CYD-Klipper/src/ui/switch_printer.cpp | 2 - 12 files changed, 253 insertions(+), 122 deletions(-) create mode 100644 CYD-Klipper/src/ui/macros.cpp create mode 100644 CYD-Klipper/src/ui/macros.h diff --git a/CYD-Klipper/src/core/data_setup.cpp b/CYD-Klipper/src/core/data_setup.cpp index 69ec09b..73c3efc 100644 --- a/CYD-Klipper/src/core/data_setup.cpp +++ b/CYD-Klipper/src/core/data_setup.cpp @@ -8,6 +8,7 @@ #include #include "http_client.h" #include "../ui/ui_utils.h" +#include "macros_query.h" const char *printer_state_messages[] = { "Error", @@ -325,6 +326,7 @@ void fetch_printer_data_minimal() if (httpCode == 200) { data[i].online = true; + data[i].power_devices = 0; JsonDocument doc; deserializeJson(doc, client.getStream()); auto status = doc["result"]["status"]; @@ -377,7 +379,8 @@ void fetch_printer_data_minimal() } else { - printer_minimal->online = false; + data[i].online = false; + data[i].power_devices = power_devices_count(config); unfreeze_request_thread(); } } @@ -420,7 +423,6 @@ void data_setup() printer.print_filename = filename_buff; fetch_printer_data(); - macros_query_setup(); freeze_render_thread(); xTaskCreatePinnedToCore(data_loop_background, "data_loop_background", 5000, NULL, 2, &background_loop, 0); } diff --git a/CYD-Klipper/src/core/data_setup.h b/CYD-Klipper/src/core/data_setup.h index f7071cb..f6c5bde 100644 --- a/CYD-Klipper/src/core/data_setup.h +++ b/CYD-Klipper/src/core/data_setup.h @@ -41,6 +41,7 @@ typedef struct _PrinterMinimal { bool online; unsigned char state; float print_progress; // 0 -> 1 + unsigned int power_devices; } PrinterMinimal; extern Printer printer; diff --git a/CYD-Klipper/src/core/macros_query.cpp b/CYD-Klipper/src/core/macros_query.cpp index f194c1e..f3998b3 100644 --- a/CYD-Klipper/src/core/macros_query.cpp +++ b/CYD-Klipper/src/core/macros_query.cpp @@ -1,7 +1,6 @@ #include "lvgl.h" #include "macros_query.h" #include "./data_setup.h" -#include "../conf/global_config.h" #include #include #include "http_client.h" @@ -11,12 +10,15 @@ static int macros_count = 0; static char* power_devices[16] = {0}; static bool power_device_states[16] = {0}; -static int power_devices_count = 0; +static unsigned int stored_power_devices_count = 0; -void _macros_query_internal(){ - SETUP_HTTP_CLIENT("/printer/gcode/help") +MACROSQUERY macros_query(PRINTER_CONFIG * config) +{ + HTTPClient client; + configure_http_client(client, get_full_url("/printer/gcode/help", config), true, 1000); int httpCode = client.GET(); + if (httpCode == 200){ JsonDocument doc; deserializeJson(doc, client.getStream()); @@ -37,76 +39,132 @@ void _macros_query_internal(){ macros[macros_count++] = macro; } } - } -} -void power_devices_clear(){ - for (int i = 0; i < power_devices_count; i++){ - free(power_devices[i]); + return {(const char**)macros, (unsigned int)macros_count}; } + else { + return {NULL, 0}; + } +} - power_devices_count = 0; +MACROSQUERY macros_query() +{ + return macros_query(get_current_printer_config()); } -void _power_devices_query_internal(){ - SETUP_HTTP_CLIENT("/machine/device_power/devices") +unsigned int macro_count(PRINTER_CONFIG * config) +{ + HTTPClient client; + configure_http_client(client, get_full_url("/printer/gcode/help", config), true, 1000); int httpCode = client.GET(); - if (httpCode == 200 || httpCode == 404 || httpCode == 500){ - power_devices_clear(); + if (httpCode == 200){ + JsonDocument doc; + deserializeJson(doc, client.getStream()); + auto result = doc["result"].as(); + + unsigned int count = 0; + + for (JsonPair i : result){ + const char *value = i.value().as().c_str(); + if (strcmp(value, "CYD_SCREEN_MACRO") == 0) { + count++; + } + } + + return count; } + else { + return 0; + } +} + +unsigned int macro_count() +{ + return macro_count(get_current_printer_config()); +} + +POWERQUERY power_devices_query(PRINTER_CONFIG * config) +{ + HTTPClient client; + configure_http_client(client, get_full_url("/machine/device_power/devices", config), true, 1000); + + int httpCode = client.GET(); if (httpCode == 200){ JsonDocument doc; deserializeJson(doc, client.getStream()); auto result = doc["result"]["devices"].as(); + for (int i = 0; i < stored_power_devices_count; i++){ + free(power_devices[i]); + } + + stored_power_devices_count = 0; + for (auto i : result){ const char * device_name = i["device"]; const char * device_state = i["status"]; - power_devices[power_devices_count] = (char*)malloc(strlen(device_name) + 1); - strcpy(power_devices[power_devices_count], device_name); - power_device_states[power_devices_count] = strcmp(device_state, "on") == 0; - power_devices_count++; + power_devices[stored_power_devices_count] = (char*)malloc(strlen(device_name) + 1); + strcpy(power_devices[stored_power_devices_count], device_name); + power_device_states[stored_power_devices_count] = strcmp(device_state, "on") == 0; + stored_power_devices_count++; } - } -} -static void on_state_change(void * s, lv_msg_t * m) { - if (printer.state == PRINTER_STATE_ERROR || printer.state == PRINTER_STATE_PAUSED){ - return; + return {(const char**)power_devices, (const bool*)power_device_states, (unsigned int)stored_power_devices_count}; + } + else { + return {NULL, NULL, 0}; } +} - _macros_query_internal(); - _power_devices_query_internal(); +POWERQUERY power_devices_query() +{ + return power_devices_query(get_current_printer_config()); } -bool set_power_state(const char* device_name, bool state) { - SETUP_HTTP_CLIENT("/machine/device_power/device?device=" + urlEncode(device_name) + "&action=" + (state ? "on" : "off")); +unsigned int power_devices_count(PRINTER_CONFIG * config) +{ + HTTPClient client; + configure_http_client(client, get_full_url("/machine/device_power/devices", config), true, 1000); + + int httpCode = client.GET(); + + if (httpCode == 200){ + JsonDocument doc; + deserializeJson(doc, client.getStream()); + auto result = doc["result"]["devices"].as(); - if (client.POST("") != 200) - return false; + unsigned int count = 0; - for (int i = 0; i < power_devices_count; i++){ - if (strcmp(power_devices[i], device_name) == 0){ - power_device_states[i] = state; - return true; + for (auto i : result){ + count++; } - } - return true; + return count; + } + else { + return 0; + } } -MACROSQUERY macros_query() { - return {(const char**)macros, (unsigned int)macros_count}; +unsigned int power_devices_count() +{ + return power_devices_count(get_current_printer_config()); } -POWERQUERY power_devices_query() { - return {(const char**)power_devices, (const bool*)power_device_states, (unsigned int)power_devices_count}; + + +bool set_power_state(const char* device_name, bool state, PRINTER_CONFIG * config) +{ + HTTPClient client; + configure_http_client(client, get_full_url("/machine/device_power/device?device=" + urlEncode(device_name) + "&action=" + (state ? "on" : "off"), config), true, 1000); + + return client.POST("") == 200; } -void macros_query_setup(){ - lv_msg_subscribe(DATA_PRINTER_STATE, on_state_change, NULL); - on_state_change(NULL, NULL); +bool set_power_state(const char* device_name, bool state) +{ + return set_power_state(device_name, state, get_current_printer_config()); } \ No newline at end of file diff --git a/CYD-Klipper/src/core/macros_query.h b/CYD-Klipper/src/core/macros_query.h index 40788b4..13ae98a 100644 --- a/CYD-Klipper/src/core/macros_query.h +++ b/CYD-Klipper/src/core/macros_query.h @@ -1,5 +1,7 @@ #pragma once +#include "../conf/global_config.h" + typedef struct { const char** macros; uint32_t count; @@ -11,10 +13,13 @@ typedef struct { uint32_t count; } POWERQUERY; +MACROSQUERY macros_query(PRINTER_CONFIG * config); MACROSQUERY macros_query(); +unsigned int macro_count(PRINTER_CONFIG * config); +unsigned int macro_count(); +POWERQUERY power_devices_query(PRINTER_CONFIG * config); POWERQUERY power_devices_query(); -void macros_query_setup(); -bool set_power_state(const char* device_name, bool state); -void _power_devices_query_internal(); -void _macros_query_internal(); -void power_devices_clear(); \ No newline at end of file +unsigned int power_devices_count(PRINTER_CONFIG * config); +unsigned int power_devices_count(); +bool set_power_state(const char* device_name, bool state, PRINTER_CONFIG * config); +bool set_power_state(const char* device_name, bool state); \ No newline at end of file diff --git a/CYD-Klipper/src/ui/ip_setup.cpp b/CYD-Klipper/src/ui/ip_setup.cpp index ac1644b..8c0ce2a 100644 --- a/CYD-Klipper/src/ui/ip_setup.cpp +++ b/CYD-Klipper/src/ui/ip_setup.cpp @@ -8,8 +8,10 @@ #include "panels/panel.h" #include "../core/http_client.h" #include "switch_printer.h" +#include "macros.h" bool connect_ok = false; +int prev_power_device_count = 0; lv_obj_t * hostEntry; lv_obj_t * portEntry; lv_obj_t * label = NULL; @@ -116,21 +118,7 @@ static void reset_btn_event_handler(lv_event_t * e){ } static void power_devices_button(lv_event_t * e) { - lv_obj_t * panel = lv_create_empty_panel(lv_scr_act()); - lv_obj_set_style_bg_opa(panel, LV_OPA_COVER, 0); - lv_layout_flex_column(panel); - lv_obj_set_size(panel, CYD_SCREEN_WIDTH_PX, CYD_SCREEN_HEIGHT_PX - CYD_SCREEN_GAP_PX); - lv_obj_align(panel, LV_ALIGN_TOP_LEFT, 0, CYD_SCREEN_GAP_PX); - - lv_obj_t * button = lv_btn_create(panel); - lv_obj_set_size(button, CYD_SCREEN_WIDTH_PX - CYD_SCREEN_GAP_PX * 2, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX); - lv_obj_add_event_cb(button, destroy_event_user_data, LV_EVENT_CLICKED, panel); - - lv_obj_t * label = lv_label_create(button); - lv_label_set_text(label, LV_SYMBOL_CLOSE " Close"); - lv_obj_center(label); - - macros_panel_add_power_devices_to_panel(panel, power_devices_query()); + macros_draw_power_fullscreen(); } void redraw_connect_screen(){ @@ -153,7 +141,7 @@ void redraw_connect_screen(){ lv_label_set_text(btn_label, "Reset"); lv_obj_center(btn_label); - if (power_devices_query().count >= 1){ + if (prev_power_device_count >= 1){ lv_obj_t * power_devices_btn = lv_btn_create(button_row); lv_obj_add_event_cb(power_devices_btn, power_devices_button, LV_EVENT_CLICKED, NULL); lv_obj_set_height(power_devices_btn, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX); @@ -292,7 +280,7 @@ int retry_count = 0; void ip_init(){ connect_ok = false; retry_count = 0; - int prev_power_device_count = 0; + prev_power_device_count = 0; ip_init_inner(); @@ -312,13 +300,12 @@ void ip_init(){ lv_label_set_text(label, retry_count_text.c_str()); } - if (status != CONNECT_AUTH_REQUIRED) - _power_devices_query_internal(); - else + if (status == CONNECT_AUTH_REQUIRED) handle_auth_entry(); - if (power_devices_query().count != prev_power_device_count) { - prev_power_device_count = power_devices_query().count; + unsigned int power_device_count = power_devices_count(); + if (power_device_count != prev_power_device_count) { + prev_power_device_count = power_device_count; redraw_connect_screen(); } } @@ -328,7 +315,6 @@ void ip_init(){ void ip_ok(){ if (klipper_request_consecutive_fail_count > 5){ freeze_request_thread(); - power_devices_clear(); ip_init(); unfreeze_request_thread(); klipper_request_consecutive_fail_count = 0; diff --git a/CYD-Klipper/src/ui/macros.cpp b/CYD-Klipper/src/ui/macros.cpp new file mode 100644 index 0000000..b5e9987 --- /dev/null +++ b/CYD-Klipper/src/ui/macros.cpp @@ -0,0 +1,75 @@ +#include "macros.h" +#include "ui_utils.h" +#include +#include "../core/data_setup.h" + +PRINTER_CONFIG * curernt_config = NULL; + +static void btn_press(lv_event_t * e){ + lv_obj_t * btn = lv_event_get_target(e); + const char* macro = (const char*)lv_event_get_user_data(e); + Serial.printf("Macro: %s\n", macro); + send_gcode(false, macro); +} + +void macros_add_macros_to_panel(lv_obj_t * root_panel, MACROSQUERY query) +{ + for (int i = 0; i < query.count; i++){ + const char* macro = query.macros[i]; + lv_create_custom_menu_button(macro, root_panel, btn_press, "Run", (void*)macro); + } +} + +static void power_device_toggle(lv_event_t * e) +{ + auto state = lv_obj_get_state(lv_event_get_target(e)); + bool checked = (state & LV_STATE_CHECKED == LV_STATE_CHECKED); + const char* power_device_name = (const char*)lv_event_get_user_data(e); + Serial.printf("Power Device: %s, State: %d -> %d\n", power_device_name, !checked, checked); + + if (curernt_config != NULL) + set_power_state(power_device_name, checked, curernt_config); +} + +void macros_add_power_devices_to_panel(lv_obj_t * root_panel, POWERQUERY query) +{ + for (int i = 0; i < query.count; i++){ + const char* power_device_name = query.power_devices[i]; + const bool power_device_state = query.power_states[i]; + lv_create_custom_menu_switch(power_device_name, root_panel, power_device_toggle, power_device_state, (void*)power_device_name); + } +} + +void macros_draw_power_fullscreen(PRINTER_CONFIG * config) +{ + curernt_config = config; + + 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); + + MACROSQUERY query = macros_query(config); + POWERQUERY power = power_devices_query(config); + + macros_add_macros_to_panel(parent, query); + macros_add_power_devices_to_panel(parent, power); +} + +void macros_draw_power_fullscreen() +{ + macros_draw_power_fullscreen(get_current_printer_config()); +} \ No newline at end of file diff --git a/CYD-Klipper/src/ui/macros.h b/CYD-Klipper/src/ui/macros.h new file mode 100644 index 0000000..c5040be --- /dev/null +++ b/CYD-Klipper/src/ui/macros.h @@ -0,0 +1,9 @@ +#pragma once + +#include "lvgl.h" +#include "../core/macros_query.h" + +void macros_add_macros_to_panel(lv_obj_t * root_panel, MACROSQUERY query); +void macros_add_power_devices_to_panel(lv_obj_t * root_panel, POWERQUERY query); +void macros_draw_power_fullscreen(PRINTER_CONFIG * config); +void macros_draw_power_fullscreen(); \ No newline at end of file diff --git a/CYD-Klipper/src/ui/main_ui.cpp b/CYD-Klipper/src/ui/main_ui.cpp index d02219d..3f3a77f 100644 --- a/CYD-Klipper/src/ui/main_ui.cpp +++ b/CYD-Klipper/src/ui/main_ui.cpp @@ -9,6 +9,7 @@ #include "../core/macros_query.h" #include "../core/lv_setup.h" #include "switch_printer.h" +#include "macros.h" char extruder_temp_buff[20]; char bed_temp_buff[20]; @@ -37,7 +38,7 @@ void error_ui_macros_open(lv_event_t * e){ lv_label_set_text(label, LV_SYMBOL_CLOSE " Close"); lv_obj_center(label); - macros_panel_add_power_devices_to_panel(panel, power_devices_query()); + macros_add_power_devices_to_panel(panel, power_devices_query()); } void error_ui(){ @@ -81,7 +82,7 @@ void error_ui(){ lv_label_set_text(label, "FW Restart"); lv_obj_center(label); - if (power_devices_query().count >= 1){ + if (power_devices_count() >= 1){ btn = lv_btn_create(button_row); lv_obj_set_height(btn, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX); lv_obj_add_event_cb(btn, error_ui_macros_open, LV_EVENT_CLICKED, NULL); diff --git a/CYD-Klipper/src/ui/panels/macros_panel.cpp b/CYD-Klipper/src/ui/panels/macros_panel.cpp index 0928252..ec4784c 100644 --- a/CYD-Klipper/src/ui/panels/macros_panel.cpp +++ b/CYD-Klipper/src/ui/panels/macros_panel.cpp @@ -1,49 +1,15 @@ -#include "lvgl.h" +#include "../macros.h" #include "panel.h" #include "../nav_buttons.h" #include "../../core/data_setup.h" -#include "../../core/macros_query.h" #include "../../conf/global_config.h" #include "../ui_utils.h" #include -const static lv_point_t line_points[] = { {0, 0}, {(short int)((CYD_SCREEN_PANEL_WIDTH_PX - CYD_SCREEN_GAP_PX * 2) * 0.85f), 0} }; - -static void btn_press(lv_event_t * e){ - lv_obj_t * btn = lv_event_get_target(e); - const char* macro = (const char*)lv_event_get_user_data(e); - Serial.printf("Macro: %s\n", macro); - send_gcode(false, macro); -} - static void btn_goto_settings(lv_event_t * e){ nav_buttons_setup(3); } -void macros_panel_add_macros_to_panel(lv_obj_t * root_panel, MACROSQUERY query){ - for (int i = 0; i < query.count; i++){ - const char* macro = query.macros[i]; - lv_create_custom_menu_button(macro, root_panel, btn_press, "Run", (void*)macro); - } -} - -static void power_device_toggle(lv_event_t * e){ - auto state = lv_obj_get_state(lv_event_get_target(e)); - bool checked = (state & LV_STATE_CHECKED == LV_STATE_CHECKED); - const char* power_device_name = (const char*)lv_event_get_user_data(e); - Serial.printf("Power Device: %s, State: %d -> %d\n", power_device_name, !checked, checked); - - set_power_state(power_device_name, checked); -} - -void macros_panel_add_power_devices_to_panel(lv_obj_t * root_panel, POWERQUERY query){ - for (int i = 0; i < query.count; i++){ - const char* power_device_name = query.power_devices[i]; - const bool power_device_state = query.power_states[i]; - lv_create_custom_menu_switch(power_device_name, root_panel, power_device_toggle, power_device_state, (void*)power_device_name); - } -} - void macros_panel_init(lv_obj_t* panel) { lv_obj_t * btn = lv_btn_create(panel); lv_obj_add_event_cb(btn, btn_goto_settings, LV_EVENT_CLICKED, NULL); @@ -69,6 +35,6 @@ void macros_panel_init(lv_obj_t* panel) { lv_obj_align(root_panel, LV_ALIGN_TOP_MID, 0, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX + CYD_SCREEN_GAP_PX * 2); lv_layout_flex_column(root_panel); - macros_panel_add_power_devices_to_panel(root_panel, power); - macros_panel_add_macros_to_panel(root_panel, query); + macros_add_power_devices_to_panel(root_panel, power); + macros_add_macros_to_panel(root_panel, query); } \ No newline at end of file diff --git a/CYD-Klipper/src/ui/panels/panel.h b/CYD-Klipper/src/ui/panels/panel.h index a18095c..a6c1aa8 100644 --- a/CYD-Klipper/src/ui/panels/panel.h +++ b/CYD-Klipper/src/ui/panels/panel.h @@ -10,5 +10,4 @@ void move_panel_init(lv_obj_t* panel); void progress_panel_init(lv_obj_t* panel); void macros_panel_init(lv_obj_t* panel); void stats_panel_init(lv_obj_t* panel); -void printer_panel_init(lv_obj_t* panel); -void macros_panel_add_power_devices_to_panel(lv_obj_t * panel, POWERQUERY query); \ No newline at end of file +void printer_panel_init(lv_obj_t* panel); \ No newline at end of file diff --git a/CYD-Klipper/src/ui/panels/printer_panel.cpp b/CYD-Klipper/src/ui/panels/printer_panel.cpp index 68148f5..427be34 100644 --- a/CYD-Klipper/src/ui/panels/printer_panel.cpp +++ b/CYD-Klipper/src/ui/panels/printer_panel.cpp @@ -7,6 +7,7 @@ #include "../nav_buttons.h" #include "../../core/macros_query.h" #include "../switch_printer.h" +#include "../macros.h" const char * printer_status[] = { "Error", @@ -83,34 +84,56 @@ static void update_printer_percentage_text(lv_event_t * e) } } -static void btn_disable_if_controlled(lv_event_t * e) +static void update_printer_control_button_text(lv_event_t * e) +{ + lv_obj_t * label = lv_event_get_target(e); + PRINTER_CONFIG * config = (PRINTER_CONFIG*)lv_event_get_user_data(e); + int index = config - global_config.printer_config; + PrinterMinimal * printer = &printer_minimal[index]; + + if (!printer->online && printer->power_devices > 0) + { + lv_label_set_text(label, "Power"); + } + else + { + lv_label_set_text(label, "Control"); + } +} + +static void btn_enable_delete(lv_event_t * e) { lv_obj_t * btn = lv_event_get_target(e); PRINTER_CONFIG * config = (PRINTER_CONFIG*)lv_event_get_user_data(e); if (config == get_current_printer_config()) { + // Disable lv_obj_add_state(btn, LV_STATE_DISABLED); } else { + // Enable lv_obj_clear_state(btn, LV_STATE_DISABLED); } } -static void btn_disable_if_controlled_or_offline(lv_event_t * e) +static void btn_enable_control(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; PrinterMinimal * printer = &printer_minimal[index]; - if (config == get_current_printer_config() || !printer->online) + if (config == get_current_printer_config() || (!printer->online && printer->power_devices < 0)) { + // Disable lv_obj_add_state(btn, LV_STATE_DISABLED); + } - else + else { + // Enable lv_obj_clear_state(btn, LV_STATE_DISABLED); } } @@ -155,6 +178,13 @@ static void btn_printer_activate(lv_event_t * e) lv_obj_t * label = lv_event_get_target(e); PRINTER_CONFIG * config = (PRINTER_CONFIG*)lv_event_get_user_data(e); int index = config - global_config.printer_config; + PrinterMinimal * printer = &printer_minimal[index]; + + if (!printer->online) + { + macros_draw_power_fullscreen(config); + return; + } switch_printer(index); lv_msg_send(DATA_PRINTER_MINIMAL, NULL); @@ -203,7 +233,7 @@ void create_printer_ui(PRINTER_CONFIG * config, lv_obj_t * root) lv_obj_t * btn = lv_btn_create(button_row); lv_obj_set_flex_grow(btn, 1); lv_obj_add_event_cb(btn, btn_printer_delete, LV_EVENT_CLICKED, config); - lv_obj_add_event_cb(btn, btn_disable_if_controlled, LV_EVENT_MSG_RECEIVED, config); + lv_obj_add_event_cb(btn, btn_enable_delete, LV_EVENT_MSG_RECEIVED, config); lv_msg_subsribe_obj(DATA_PRINTER_MINIMAL, btn, config); label = lv_label_create(btn); @@ -221,12 +251,13 @@ void create_printer_ui(PRINTER_CONFIG * config, lv_obj_t * root) btn = lv_btn_create(button_row); lv_obj_set_flex_grow(btn, 2); lv_obj_add_event_cb(btn, btn_printer_activate, LV_EVENT_CLICKED, config); - lv_obj_add_event_cb(btn, btn_disable_if_controlled_or_offline, LV_EVENT_MSG_RECEIVED, config); + lv_obj_add_event_cb(btn, btn_enable_control, LV_EVENT_MSG_RECEIVED, config); lv_msg_subsribe_obj(DATA_PRINTER_MINIMAL, btn, config); label = lv_label_create(btn); - lv_label_set_text(label, "Control"); lv_obj_center(label); + lv_obj_add_event_cb(label, update_printer_control_button_text, LV_EVENT_MSG_RECEIVED, config); + lv_msg_subsribe_obj(DATA_PRINTER_MINIMAL, label, config); lv_obj_t * line = lv_line_create(root); lv_line_set_points(line, line_points, 2); diff --git a/CYD-Klipper/src/ui/switch_printer.cpp b/CYD-Klipper/src/ui/switch_printer.cpp index f344334..20a8982 100644 --- a/CYD-Klipper/src/ui/switch_printer.cpp +++ b/CYD-Klipper/src/ui/switch_printer.cpp @@ -12,8 +12,6 @@ void switch_printer(int index) set_printer_config_index(index); set_color_scheme(); set_invert_display(); - _macros_query_internal(); - _power_devices_query_internal(); } static void btn_switch_printer(lv_event_t *e){