Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Oct 22, 2024
1 parent 22fc25c commit eed4b3e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 84 deletions.
37 changes: 26 additions & 11 deletions CYD-Klipper/src/core/klipper/klipper_printer_integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,6 @@ bool KlipperPrinter::fetch()

if (printer_data.state_message == NULL || strcmp(printer_data.state_message, message))
{
if (printer_data.state_message != NULL)
{
free(printer_data.state_message);
}

printer_data.state_message = (char *)malloc(strlen(message) + 1);
strcpy(printer_data.state_message, message);
}
Expand Down Expand Up @@ -286,11 +281,6 @@ bool KlipperPrinter::fetch()

if (filename != NULL && (printer_data.print_filename == NULL || strcmp(printer_data.print_filename, filename)))
{
if (printer_data.print_filename != NULL)
{
free(printer_data.print_filename);
}

printer_data.print_filename = (char *)malloc(strlen(filename) + 1);
strcpy(printer_data.print_filename, filename);
}
Expand Down Expand Up @@ -325,7 +315,12 @@ bool KlipperPrinter::fetch()
{
printer_data.print_progress = status["display_status"]["progress"];
const char* message = status["display_status"]["message"];
store_available_popup_message(message);

if (message != NULL && (printer_data.popup_message == NULL || strcmp(printer_data.popup_message, message)))
{
printer_data.popup_message = (char*)malloc(strlen(message) + 1);
strcpy(printer_data.popup_message, message);
}
}

if (printer_data.state == PrinterStatePrinting && printer_data.print_progress > 0)
Expand Down Expand Up @@ -459,4 +454,24 @@ PrinterDataMinimal KlipperPrinter::fetch_min()
data.state = PrinterStateOffline;
data.power_devices = get_power_devices_count();
}
}

void KlipperPrinter::disconnect()
{
// Nothing to disconnect, everything is http request based

if (printer_data.state_message != NULL)
{
free(printer_data.state_message);
}

if (printer_data.print_filename != NULL)
{
free(printer_data.print_filename);
}

if (printer_data.popup_message != NULL)
{
free(printer_data.popup_message);
}
}
108 changes: 42 additions & 66 deletions CYD-Klipper/src/core/printer_integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,63 @@

unsigned char current_printer_index = 0;
BasePrinter* registered_printers;

PrinterData* printer_data_copy;
char* state_message_copy;
char* print_filename_copy;
char* popup_message_copy = NULL;

bool available_data_message;
bool available_state_message;
bool available_popup_message;

BasePrinter::BasePrinter(unsigned char index)
{
{
config_index = index;

printer_data.state_message = (char*)malloc(1);
printer_data.print_filename = (char*)malloc(1);
printer_data.popup_message = (char*)malloc(1);
*printer_data.state_message = '\0';
*printer_data.print_filename = '\0';
*printer_data.popup_message = '\0';

// TODO: Fetch printer config and global config
}

PrinterData* BasePrinter::CopyPrinterData()
#define DATA_PRINTER_STATE 1
#define DATA_PRINTER_DATA 2
#define DATA_PRINTER_TEMP_PRESET 3
#define DATA_PRINTER_MINIMAL 4
#define DATA_PRINTER_POPUP 5

PrinterData* BasePrinter::AnnouncePrinterData()
{
available_data_message = true;
available_state_message = printer_data.state != printer_data_copy->state;
char* old_state_message = printer_data_copy->state_message;
char* old_print_filename = printer_data_copy->print_filename;
char* old_popup_message = printer_data_copy->print_filename;

memcpy(printer_data_copy, &printer_data, sizeof(PrinterData));
printer_data_copy->state_message = state_message_copy;
printer_data_copy->print_filename = print_filename_copy;
strcpy(state_message_copy, printer_data.state_message);
strcpy(print_filename_copy, printer_data.print_filename);

if (old_state_message != printer_data_copy->state_message)
{
free(old_state_message);
lv_msg_send(DATA_PRINTER_STATE, get_current_printer());
}
else if (printer_data.state != printer_data_copy->state)
{
lv_msg_send(DATA_PRINTER_STATE, get_current_printer());
}

if (old_print_filename != printer_data_copy->print_filename)
{
free(old_print_filename);
}

if (old_popup_message != printer_data_copy->popup_message)
{
free(old_popup_message);
lv_msg_send(DATA_PRINTER_POPUP, get_current_printer());
}

lv_msg_send(DATA_PRINTER_DATA, get_current_printer());
}

void initialize_printers()
{
printer_data_copy = (PrinterData*)malloc(sizeof(PrinterData));
state_message_copy = (char*)malloc(256);
print_filename_copy = (char*)malloc(256);
}

BasePrinter* get_current_printer()
Expand All @@ -45,52 +69,4 @@ BasePrinter* get_current_printer()
BasePrinter* get_printer(int idx)
{
return registered_printers + idx;
}

void store_available_popup_message(const char *message)
{
if (message != NULL && (!get_current_printer()->global_config->disable_m117_messaging) && (popup_message_copy == NULL || strcmp(popup_message_copy, message)))
{
if (popup_message_copy != NULL)
{
free(popup_message_copy);
}

popup_message_copy = (char*)malloc(strlen(message) + 1);
strcpy(popup_message_copy, message);
available_popup_message = true;
}
}

#define DATA_PRINTER_STATE 1
#define DATA_PRINTER_DATA 2
#define DATA_PRINTER_TEMP_PRESET 3
#define DATA_PRINTER_MINIMAL 4
#define DATA_PRINTER_POPUP 5

void send_available_popup_message()
{
if (available_popup_message)
{
available_data_message = false;
lv_msg_send(DATA_PRINTER_POPUP, popup_message_copy);
}
}

void send_available_data_message()
{
if (available_data_message)
{
available_data_message = false;
lv_msg_send(DATA_PRINTER_DATA, get_current_printer());
}
}

void send_available_state_message()
{
if (available_state_message)
{
available_state_message = false;
lv_msg_send(DATA_PRINTER_STATE, get_current_printer());
}
}
10 changes: 3 additions & 7 deletions CYD-Klipper/src/core/printer_integration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ typedef struct _PrinterData {
};
PrinterState state;
char* state_message;
char* popup_message;
float temperatures[10];
float target_temperatures[10];
PrinterTemperatureDevice AvailableDevices;
Expand Down Expand Up @@ -159,14 +160,9 @@ class BasePrinter
virtual bool set_target_temperature(PrinterTemperatureDevice device, float temperature) = 0;

BasePrinter(unsigned char index);
PrinterData* CopyPrinterData();
PrinterData* AnnouncePrinterData();
};

BasePrinter* get_current_printer();
BasePrinter* get_printer(int idx);
void initialize_printers();

void store_available_popup_message(const char *message);
void send_available_popup_message();
void send_available_data_message();
void send_available_state_message();
void initialize_printers();

0 comments on commit eed4b3e

Please sign in to comment.