Skip to content

Commit

Permalink
Printer init
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Oct 27, 2024
1 parent c35b146 commit be0bf0f
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 32 deletions.
66 changes: 63 additions & 3 deletions CYD-Klipper/src/conf/global_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions CYD-Klipper/src/conf/global_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "lvgl.h"

#define CONFIG_VERSION 6
#define CONFIG_VERSION 7
#define PRINTER_CONFIG_COUNT 8
#define DISPLAY_SECRETS 0

Expand Down Expand Up @@ -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;
};
};

Expand Down Expand Up @@ -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();

Expand Down
17 changes: 17 additions & 0 deletions CYD-Klipper/src/core/data_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <esp_task_wdt.h>
#include <UrlEncode.h>
#include "printer_integration.hpp"
#include "klipper/klipper_printer_integration.hpp"

SemaphoreHandle_t freezeRenderThreadSemaphore, freezeRequestThreadSemaphore;
const long data_update_interval = 780;
Expand Down Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion CYD-Klipper/src/core/klipper/klipper_printer_panels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "../../ui/ui_utils.h"
#include <stdio.h>


static void set_fan_speed_text(lv_event_t * e) {
lv_obj_t * label = lv_event_get_target(e);
char data[16];
Expand Down
27 changes: 11 additions & 16 deletions CYD-Klipper/src/core/printer_integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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());
}

Expand All @@ -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();
}
4 changes: 1 addition & 3 deletions CYD-Klipper/src/core/printer_integration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ class BasePrinter

BasePrinter(unsigned char index);
PrinterData* AnnouncePrinterData();
void save_printer_config();
};

#define DATA_PRINTER_STATE 1
Expand All @@ -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);
6 changes: 2 additions & 4 deletions CYD-Klipper/src/ui/panels/printer_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit be0bf0f

Please sign in to comment.