diff --git a/CYD-Klipper/src/conf/global_config.cpp b/CYD-Klipper/src/conf/global_config.cpp index 613e635..91b42b0 100644 --- a/CYD-Klipper/src/conf/global_config.cpp +++ b/CYD-Klipper/src/conf/global_config.cpp @@ -46,11 +46,11 @@ PrinterConfiguration* get_current_printer_config() return &global_config.printer_config[global_config.printer_index]; } -int get_printer_config_count() +int global_config_get_printer_config_count() { int count = 0; for (int i = 0; i < PRINTER_CONFIG_COUNT; i++) { - if (global_config.printer_config[i].ip_configured) + if (global_config.printer_config[i].setup_complete) count++; } return count; @@ -59,12 +59,72 @@ int get_printer_config_count() int get_printer_config_free_index() { for (int i = 0; i < PRINTER_CONFIG_COUNT; i++) { - if (!global_config.printer_config[i].ip_configured) + if (!global_config.printer_config[i].setup_complete) return i; } return -1; } +void global_config_add_new_printer() +{ + int free_index = get_printer_config_free_index(); + if (free_index <= -1) + { + return; + } + + PrinterConfiguration* old_config = &global_config.printer_config[global_config.printer_index]; + PrinterConfiguration* new_config = &global_config.printer_config[free_index]; + + new_config->raw = old_config->raw; + new_config->ip_configured = false; + new_config->auth_configured = false; + + new_config->printer_name[0] = 0; + new_config->klipper_host[0] = 0; + new_config->klipper_auth[0] = 0; + new_config->klipper_port = 0; + + new_config->color_scheme = old_config->color_scheme; + + // TODO: Replace with memcpy + for (int i = 0; i < 3; i++){ + new_config->hotend_presets[i] = old_config->hotend_presets[i]; + new_config->bed_presets[i] = old_config->bed_presets[i]; + } + + for (int i = 0; i < 3; i++){ + new_config->printer_move_x_steps[i] = old_config->printer_move_x_steps[i]; + new_config->printer_move_y_steps[i] = old_config->printer_move_y_steps[i]; + new_config->printer_move_z_steps[i] = old_config->printer_move_z_steps[i]; + } + + write_global_config(); + ESP.restart(); +} + +void global_config_set_printer(int idx) +{ + if (idx < 0 || idx >= PRINTER_CONFIG_COUNT) + return; + + global_config.printer_index = idx; + write_global_config(); +} + +void global_config_delete_printer(int idx) +{ + if (global_config.printer_index == idx) + { + return; + } + + PrinterConfiguration* config = &global_config.printer_config[idx]; + config->setup_complete = false; + write_global_config(); + ESP.restart(); +} + void set_printer_config_index(int index) { if (index < 0 || index >= PRINTER_CONFIG_COUNT) diff --git a/CYD-Klipper/src/conf/global_config.h b/CYD-Klipper/src/conf/global_config.h index c9a7c06..2dcf3c1 100644 --- a/CYD-Klipper/src/conf/global_config.h +++ b/CYD-Klipper/src/conf/global_config.h @@ -3,7 +3,7 @@ #include "lvgl.h" -#define CONFIG_VERSION 6 +#define CONFIG_VERSION 7 #define PRINTER_CONFIG_COUNT 8 #define DISPLAY_SECRETS 0 @@ -32,17 +32,17 @@ typedef struct { unsigned int raw; struct { // Internal + bool setup_complete : 1; bool ip_configured : 1; bool auth_configured : 1; + bool custom_filament_move_macros : 1; + PrinterType printer_type : 3; // External bool light_mode : 1; bool invert_colors : 1; unsigned char remaining_time_calc_mode : 2; unsigned char show_stats_on_progress_panel : 2; - - bool custom_filament_move_macros : 1; - PrinterType printer_type : 3; }; }; @@ -122,8 +122,12 @@ void write_global_config(); void verify_version(); void load_global_config(); +void global_config_add_new_printer(); +void global_config_set_printer(int idx); +void global_config_delete_printer(int idx); + //PRINTER_CONFIG* get_current_printer_config(); -//int get_printer_config_count(); +int global_config_get_printer_config_count(); //void set_printer_config_index(int index); //int get_printer_config_free_index(); diff --git a/CYD-Klipper/src/core/data_setup.cpp b/CYD-Klipper/src/core/data_setup.cpp index 89053b5..fdcb89c 100644 --- a/CYD-Klipper/src/core/data_setup.cpp +++ b/CYD-Klipper/src/core/data_setup.cpp @@ -3,6 +3,7 @@ #include #include #include "printer_integration.hpp" +#include "klipper/klipper_printer_integration.hpp" SemaphoreHandle_t freezeRenderThreadSemaphore, freezeRequestThreadSemaphore; const long data_update_interval = 780; @@ -101,6 +102,22 @@ TaskHandle_t background_loop; void data_setup() { + BasePrinter** available_printers = (BasePrinter**)malloc(sizeof(BasePrinter*) * PRINTER_CONFIG_COUNT); + int count = 0; + + for (int i = 0; i < PRINTER_CONFIG_COUNT; i++) + { + if (global_config.printer_config[i].setup_complete) + { + switch (global_config.printer_config[i].printer_type) + { + case PrinterType::PrinterTypeKlipper: + available_printers[count++] = new KlipperPrinter(i); + } + } + } + + initialize_printers(available_printers, count); semaphore_init(); fetch_printer_data(); freeze_render_thread(); diff --git a/CYD-Klipper/src/core/klipper/klipper_printer_panels.cpp b/CYD-Klipper/src/core/klipper/klipper_printer_panels.cpp index 2e1a27f..de9e394 100644 --- a/CYD-Klipper/src/core/klipper/klipper_printer_panels.cpp +++ b/CYD-Klipper/src/core/klipper/klipper_printer_panels.cpp @@ -3,7 +3,6 @@ #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); char data[16]; diff --git a/CYD-Klipper/src/core/printer_integration.cpp b/CYD-Klipper/src/core/printer_integration.cpp index 725369c..5130862 100644 --- a/CYD-Klipper/src/core/printer_integration.cpp +++ b/CYD-Klipper/src/core/printer_integration.cpp @@ -4,13 +4,14 @@ unsigned char current_printer_index = 0; unsigned char total_printers; -BasePrinter* registered_printers; +BasePrinter** registered_printers; PrinterDataMinimal* minimal_data_copy; PrinterData* printer_data_copy; BasePrinter::BasePrinter(unsigned char index) { config_index = index; + printer_config = &global_config.printer_config[index]; printer_data.state_message = (char*)malloc(1); printer_data.print_filename = (char*)malloc(1); @@ -54,9 +55,13 @@ PrinterData* BasePrinter::AnnouncePrinterData() lv_msg_send(DATA_PRINTER_DATA, get_current_printer()); } -void initialize_printers() +void initialize_printers(BasePrinter** printers, unsigned char total) { printer_data_copy = (PrinterData*)malloc(sizeof(PrinterData)); + minimal_data_copy = (PrinterDataMinimal*)malloc(sizeof(PrinterDataMinimal) * total_printers); + memset(minimal_data_copy, 0, sizeof(PrinterDataMinimal) * total_printers); + registered_printers = printers; + total_printers = total; } BasePrinter* get_current_printer() @@ -66,7 +71,7 @@ BasePrinter* get_current_printer() BasePrinter* get_printer(int idx) { - return registered_printers + idx; + return registered_printers[idx]; } int get_current_printer_index() @@ -86,7 +91,7 @@ unsigned int get_printer_count() void announce_printer_data_minimal(PrinterDataMinimal* printer_data) { - memcpy(printer_data_copy, printer_data, sizeof(PrinterDataMinimal) * total_printers); + memcpy(minimal_data_copy, printer_data, sizeof(PrinterDataMinimal) * total_printers); lv_msg_send(DATA_PRINTER_MINIMAL, get_current_printer()); } @@ -95,20 +100,10 @@ PrinterDataMinimal* get_printer_data_minimal(int idx) return &(minimal_data_copy[idx]); } -void BasePrinter::save_printer_config() -{ - // TODO -} - - -void add_printer() -{ - -} - void set_current_printer(int idx) { - //set_printer_config_index(index); + current_printer_index = idx; + global_config_set_printer(idx); set_color_scheme(); set_invert_display(); } \ 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 7c34875..3381165 100644 --- a/CYD-Klipper/src/core/printer_integration.hpp +++ b/CYD-Klipper/src/core/printer_integration.hpp @@ -163,7 +163,6 @@ class BasePrinter BasePrinter(unsigned char index); PrinterData* AnnouncePrinterData(); - void save_printer_config(); }; #define DATA_PRINTER_STATE 1 @@ -174,11 +173,10 @@ class BasePrinter BasePrinter* get_current_printer(); BasePrinter* get_printer(int idx); -void initialize_printers(); +void initialize_printers(BasePrinter** printers, unsigned char total); PrinterData* get_current_printer_data(); unsigned int get_printer_count(); void announce_printer_data_minimal(PrinterDataMinimal* printer_data); PrinterDataMinimal* get_printer_data_minimal(int idx); int get_current_printer_index(); -void add_printer(); void set_current_printer(int idx); \ 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 b048437..fefca49 100644 --- a/CYD-Klipper/src/ui/panels/printer_panel.cpp +++ b/CYD-Klipper/src/ui/panels/printer_panel.cpp @@ -165,9 +165,7 @@ static void btn_printer_secondary(lv_event_t * e) return; } - printer->printer_config->ip_configured = false; - write_global_config(); - + global_config_delete_printer(config_index); nav_buttons_setup(PANEL_PRINTER); } @@ -196,7 +194,7 @@ static void btn_printer_activate(lv_event_t * e) static void btn_printer_add(lv_event_t * e) { - add_printer(); + global_config_add_new_printer(); } void create_printer_ui(PrinterConfiguration * config, lv_obj_t * root)