From ccc58177998273c854bcfbacae3334501a7c74bc Mon Sep 17 00:00:00 2001 From: suchmememanyskill <38142618+suchmememanyskill@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:57:18 +0100 Subject: [PATCH] More fixes --- CYD-Klipper/src/conf/global_config.cpp | 4 ++- CYD-Klipper/src/core/data_setup.cpp | 5 ++- .../klipper/klipper_printer_integration.cpp | 19 ++++++----- CYD-Klipper/src/core/lv_setup.cpp | 1 + CYD-Klipper/src/core/printer_integration.cpp | 5 +-- CYD-Klipper/src/core/printer_integration.hpp | 2 +- CYD-Klipper/src/ui/ip_setup.cpp | 16 +++++++-- CYD-Klipper/src/ui/nav_buttons.cpp | 33 +++++++++++++++---- CYD-Klipper/src/ui/panels/printer_panel.cpp | 17 +++++----- CYD-Klipper/src/ui/panels/settings_panel.cpp | 3 +- 10 files changed, 70 insertions(+), 35 deletions(-) diff --git a/CYD-Klipper/src/conf/global_config.cpp b/CYD-Klipper/src/conf/global_config.cpp index 91b42b0..c1e0e4c 100644 --- a/CYD-Klipper/src/conf/global_config.cpp +++ b/CYD-Klipper/src/conf/global_config.cpp @@ -70,6 +70,7 @@ void global_config_add_new_printer() int free_index = get_printer_config_free_index(); if (free_index <= -1) { + LOG_LN("No available slot for new printer"); return; } @@ -77,6 +78,7 @@ void global_config_add_new_printer() PrinterConfiguration* new_config = &global_config.printer_config[free_index]; new_config->raw = old_config->raw; + new_config->setup_complete = false; new_config->ip_configured = false; new_config->auth_configured = false; @@ -99,7 +101,7 @@ void global_config_add_new_printer() new_config->printer_move_z_steps[i] = old_config->printer_move_z_steps[i]; } - write_global_config(); + global_config_set_printer(free_index); ESP.restart(); } diff --git a/CYD-Klipper/src/core/data_setup.cpp b/CYD-Klipper/src/core/data_setup.cpp index fdcb89c..4cdb4d6 100644 --- a/CYD-Klipper/src/core/data_setup.cpp +++ b/CYD-Klipper/src/core/data_setup.cpp @@ -61,18 +61,17 @@ void fetch_printer_data() void fetch_printer_data_minimal() { - PrinterDataMinimal* data = (PrinterDataMinimal*)malloc(sizeof(PrinterDataMinimal) * get_printer_count()); + PrinterDataMinimal data[PRINTER_CONFIG_COUNT] = {{}}; for (int i = 0; i < get_printer_count(); i++) { freeze_request_thread(); BasePrinter* printer = get_printer(i); unfreeze_request_thread(); - *(data + i) = printer->fetch_min(); + data[i] = printer->fetch_min(); } freeze_render_thread(); announce_printer_data_minimal(data); unfreeze_render_thread(); - free(data); } void data_loop() diff --git a/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp b/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp index 29844ae..ae60526 100644 --- a/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp +++ b/CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp @@ -371,9 +371,10 @@ bool KlipperPrinter::fetch() PrinterDataMinimal KlipperPrinter::fetch_min() { - PrinterDataMinimal data = {0}; + PrinterDataMinimal data = {}; + data.success = false; - if (!printer_config->ip_configured) + if (!printer_config->setup_complete) { data.state = PrinterStateOffline; return data; @@ -387,7 +388,7 @@ PrinterDataMinimal KlipperPrinter::fetch_min() int http_code = client.GET(); if (http_code == 200) { - data.state = PrinterStateIdle; + data.state = PrinterState::PrinterStateIdle; data.power_devices = get_power_devices_count(); JsonDocument doc; @@ -400,7 +401,7 @@ PrinterDataMinimal KlipperPrinter::fetch_min() if (strcmp(state, "shutdown") == 0) { - data.state = PrinterStateError; + data.state = PrinterState::PrinterStateError; } } @@ -417,26 +418,26 @@ PrinterDataMinimal KlipperPrinter::fetch_min() if (state == nullptr) { - data.state = PrinterStateError; + data.state = PrinterState::PrinterStateError; } else if (strcmp(state, "printing") == 0) { - data.state = PrinterStatePrinting; + data.state = PrinterState::PrinterStatePrinting; } else if (strcmp(state, "paused") == 0) { - data.state = PrinterStatePaused; + data.state = PrinterState::PrinterStatePaused; } else if (strcmp(state, "complete") == 0 || strcmp(state, "cancelled") == 0 || strcmp(state, "standby") == 0) { - data.state = PrinterStateIdle; + data.state = PrinterState::PrinterStateIdle; } } } } else { - data.state = PrinterStateOffline; + data.state = PrinterState::PrinterStateOffline; data.power_devices = get_power_devices_count(); } diff --git a/CYD-Klipper/src/core/lv_setup.cpp b/CYD-Klipper/src/core/lv_setup.cpp index e478bd5..d094c8f 100644 --- a/CYD-Klipper/src/core/lv_setup.cpp +++ b/CYD-Klipper/src/core/lv_setup.cpp @@ -72,6 +72,7 @@ void lv_touch_intercept(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) } data->state = LV_INDEV_STATE_REL; + delay(300); // Some screens don't debounce their signal properly } screen_timer_wake(); diff --git a/CYD-Klipper/src/core/printer_integration.cpp b/CYD-Klipper/src/core/printer_integration.cpp index 9168da4..8b9565f 100644 --- a/CYD-Klipper/src/core/printer_integration.cpp +++ b/CYD-Klipper/src/core/printer_integration.cpp @@ -72,10 +72,11 @@ PrinterData* BasePrinter::AnnouncePrinterData() void initialize_printers(BasePrinter** printers, unsigned char total) { + LOG_F(("Initializing %d printers\n", total)) printer_data_copy = (PrinterData*)malloc(sizeof(PrinterData)); - minimal_data_copy = (PrinterDataMinimal*)malloc(sizeof(PrinterDataMinimal) * total_printers); + minimal_data_copy = (PrinterDataMinimal*)malloc(sizeof(PrinterDataMinimal) * total); memset(printer_data_copy, 0, sizeof(PrinterData)); - memset(minimal_data_copy, 0, sizeof(PrinterDataMinimal) * total_printers); + memset(minimal_data_copy, 0, sizeof(PrinterDataMinimal) * total); registered_printers = printers; total_printers = total; } diff --git a/CYD-Klipper/src/core/printer_integration.hpp b/CYD-Klipper/src/core/printer_integration.hpp index e03be81..2e6b3cb 100644 --- a/CYD-Klipper/src/core/printer_integration.hpp +++ b/CYD-Klipper/src/core/printer_integration.hpp @@ -97,7 +97,7 @@ typedef struct _PrinterData { } PrinterData; typedef struct { - unsigned char state; + PrinterState state; float print_progress; // 0 -> 1 unsigned int power_devices; bool success; diff --git a/CYD-Klipper/src/ui/ip_setup.cpp b/CYD-Klipper/src/ui/ip_setup.cpp index f7e22f2..e6990e3 100644 --- a/CYD-Klipper/src/ui/ip_setup.cpp +++ b/CYD-Klipper/src/ui/ip_setup.cpp @@ -198,15 +198,27 @@ void show_ip_entry() lv_textarea_set_one_line(hostEntry, true); lv_obj_add_flag(hostEntry, LV_OBJ_FLAG_USER_1); lv_textarea_set_max_length(hostEntry, 63); - lv_textarea_set_text(hostEntry, ""); lv_obj_set_flex_grow(hostEntry, 3); portEntry = lv_textarea_create(textbow_row); lv_textarea_set_one_line(portEntry, true); lv_textarea_set_max_length(portEntry, 5); - lv_textarea_set_text(portEntry, "80"); + lv_obj_set_flex_grow(portEntry, 1); + if (global_config.printer_config[global_config.printer_index].ip_configured) + { + char buff[7] = {0}; + sprintf(buff, "%d", global_config.printer_config[global_config.printer_index].klipper_port); + lv_textarea_set_text(hostEntry, global_config.printer_config[global_config.printer_index].klipper_host); + lv_textarea_set_text(portEntry, buff); + } + else + { + lv_textarea_set_text(hostEntry, ""); + lv_textarea_set_text(portEntry, "80"); + } + lv_obj_t * keyboard = lv_keyboard_create(root); lv_keyboard_set_map(keyboard, LV_KEYBOARD_MODE_USER_1, kb_map, kb_ctrl); lv_obj_add_event_cb(hostEntry, keyboard_event_ip_entry, LV_EVENT_ALL, keyboard); diff --git a/CYD-Klipper/src/ui/nav_buttons.cpp b/CYD-Klipper/src/ui/nav_buttons.cpp index bc2fcc8..7a517ff 100644 --- a/CYD-Klipper/src/ui/nav_buttons.cpp +++ b/CYD-Klipper/src/ui/nav_buttons.cpp @@ -7,15 +7,11 @@ #include "../core/printer_integration.hpp" static lv_style_t nav_button_style; - -static char temp_buffer[10]; -static char z_pos_buffer[10]; -static char time_buffer[10]; - static lv_style_t nav_button_text_style; static void update_printer_data_z_pos(lv_event_t * e) { lv_obj_t * label = lv_event_get_target(e); + char z_pos_buffer[10]; sprintf(z_pos_buffer, "Z%.2f", get_current_printer_data()->position[2]); lv_label_set_text(label, z_pos_buffer); @@ -23,6 +19,7 @@ static void update_printer_data_z_pos(lv_event_t * e) { static void update_printer_data_temp(lv_event_t * e) { lv_obj_t * label = lv_event_get_target(e); + char temp_buffer[10]; sprintf(temp_buffer, "%.0f/%.0f", get_current_printer_data()->temperatures[PrinterTemperatureDeviceIndex::PrinterTemperatureDeviceIndexNozzle1], get_current_printer_data()->temperatures[PrinterTemperatureDeviceIndex::PrinterTemperatureDeviceIndexBed]); lv_label_set_text(label, temp_buffer); @@ -30,6 +27,7 @@ static void update_printer_data_temp(lv_event_t * e) { static void update_printer_data_time(lv_event_t * e){ lv_obj_t * label = lv_event_get_target(e); + char time_buffer[10]; if (get_current_printer_data()->state == PrinterState::PrinterStateIdle){ lv_label_set_text(label, "Idle"); @@ -57,6 +55,29 @@ static void update_printer_data_time(lv_event_t * e){ lv_label_set_text(label, time_buffer); } +static void update_multi_printer_label(lv_event_t * e) { + lv_obj_t * label = lv_event_get_target(e); + + int idle_count = 0; + for (int i = 0; i < get_printer_count(); i++) + { + PrinterDataMinimal* data = get_printer_data_minimal(i); + if (data->state == PrinterState::PrinterStateIdle) + { + idle_count++; + } + } + + if (idle_count > 0) + { + lv_label_set_text_fmt(label, "%d idle", idle_count); + } + else + { + lv_label_set_text(label, "Printer"); + } +} + static void btn_click_files(lv_event_t * e){ nav_buttons_setup(PANEL_FILES); } @@ -168,7 +189,7 @@ void nav_buttons_setup(PANEL_TYPE active_panel){ if (global_config.multi_printer_mode) { // Printers - create_button(LV_SYMBOL_HOME, "Printer", btn_click_printer, NULL, root_panel); + create_button(LV_SYMBOL_HOME, "Printer", btn_click_printer, update_multi_printer_label, root_panel); } lv_obj_t * panel = lv_create_empty_panel(lv_scr_act()); diff --git a/CYD-Klipper/src/ui/panels/printer_panel.cpp b/CYD-Klipper/src/ui/panels/printer_panel.cpp index 765be06..71e8778 100644 --- a/CYD-Klipper/src/ui/panels/printer_panel.cpp +++ b/CYD-Klipper/src/ui/panels/printer_panel.cpp @@ -170,7 +170,9 @@ static void btn_printer_secondary(lv_event_t * e) static void btn_printer_rename(lv_event_t * e) { - keyboard_config = (PrinterConfiguration*)lv_event_get_user_data(e); + int config_index = (int)lv_event_get_user_data(e); + BasePrinter* printer = get_printer(config_index); + keyboard_config = printer->printer_config; lv_create_keyboard_text_entry(keyboard_callback, "Rename Printer", LV_KEYBOARD_MODE_TEXT_LOWER, CYD_SCREEN_WIDTH_PX * 0.75, 24, keyboard_config->printer_name, false); } @@ -196,9 +198,8 @@ static void btn_printer_add(lv_event_t * e) global_config_add_new_printer(); } -void create_printer_ui(PrinterConfiguration * config, lv_obj_t * root) +void create_printer_ui(int index, lv_obj_t * root) { - int index = config - global_config.printer_config; auto width = CYD_SCREEN_PANEL_WIDTH_PX - CYD_SCREEN_GAP_PX * 2; lv_obj_t * data_row_name = lv_create_empty_panel(root); @@ -279,15 +280,12 @@ void printer_panel_init(lv_obj_t* panel) lv_obj_set_size(lv_create_empty_panel(inner_panel), 0, 0); - for (int i = 0; i < PRINTER_CONFIG_COUNT; i++){ - PrinterConfiguration * config = &global_config.printer_config[i]; - if (config->ip_configured) { - create_printer_ui(&global_config.printer_config[i], inner_panel); - } + for (int i = 0; i < get_printer_count(); i++){ + create_printer_ui(i, inner_panel); } // Add Printer Button - if (get_printer_count() == PRINTER_CONFIG_COUNT){ + if (get_printer_count() < PRINTER_CONFIG_COUNT){ lv_obj_t * btn = lv_btn_create(inner_panel); lv_obj_set_size(btn, CYD_SCREEN_PANEL_WIDTH_PX - CYD_SCREEN_GAP_PX * 2, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX); lv_obj_add_event_cb(btn, btn_printer_add, LV_EVENT_CLICKED, NULL); @@ -298,4 +296,5 @@ void printer_panel_init(lv_obj_t* panel) } lv_obj_set_size(lv_create_empty_panel(inner_panel), 0, 0); + lv_msg_send(DATA_PRINTER_MINIMAL, NULL); } \ No newline at end of file diff --git a/CYD-Klipper/src/ui/panels/settings_panel.cpp b/CYD-Klipper/src/ui/panels/settings_panel.cpp index 8e867aa..f114497 100644 --- a/CYD-Klipper/src/ui/panels/settings_panel.cpp +++ b/CYD-Klipper/src/ui/panels/settings_panel.cpp @@ -39,8 +39,7 @@ static void reset_wifi_click(lv_event_t * e){ } static void reset_ip_click(lv_event_t * e){ - get_current_printer()->printer_config->ip_configured = false; - get_current_printer()->printer_config->auth_configured = false; + get_current_printer()->printer_config->setup_complete = false; write_global_config(); ESP.restart(); }