From 58948cffcd6d2e5ccaea6e5508f0144e34619eec Mon Sep 17 00:00:00 2001 From: DRracer Date: Tue, 11 Jun 2019 16:24:40 +0200 Subject: [PATCH] Code size reduction - saved almost 2KB --- Firmware/ultralcd.cpp | 114 ++++++++++-------------------------------- Firmware/ultralcd.h | 2 +- 2 files changed, 27 insertions(+), 89 deletions(-) mode change 100644 => 100755 Firmware/ultralcd.h diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index 4ce79dc3..8a226b4b 100755 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -74,8 +74,8 @@ extern void crashdet_disable(); bool presort_flag = false; #endif -int lcd_commands_type = LCD_COMMAND_IDLE; -int lcd_commands_step = 0; +uint8_t lcd_commands_type = LCD_COMMAND_IDLE; +uint8_t lcd_commands_step = 0; unsigned int custom_message_type = CUSTOM_MSG_TYPE_STATUS; unsigned int custom_message_state = 0; @@ -1061,6 +1061,20 @@ static void lcd_status_screen() feedmultiply = 999; } +//! extracted common code from lcd_commands into a separate function +//! along with the sprintf_P optimization this change gained more than 1.6KB +static void lcd_commands_func1(char *cmd1, uint8_t i, float width, float extr, float extr_short_segment){ + static const char fmt1[] PROGMEM = "G1 X%d Y%-.2f E%-.3f"; + static const char fmt2[] PROGMEM = "G1 Y%-.2f E%-.3f"; + sprintf_P(cmd1, fmt1, 70, (35 - i*width * 2), extr); + enquecommand(cmd1); + sprintf_P(cmd1, fmt2, (35 - (2 * i + 1)*width), extr_short_segment); + enquecommand(cmd1); + sprintf_P(cmd1, fmt1, 50, (35 - (2 * i + 1)*width), extr); + enquecommand(cmd1); + sprintf_P(cmd1, fmt2, (35 - (i + 1)*width * 2), extr_short_segment); + enquecommand(cmd1); +} void lcd_commands() { @@ -1466,27 +1480,8 @@ void lcd_commands() lcd_timeoutToStatus.start(); - for (int i = 0; i < 4; i++) { - strcpy(cmd1, "G1 X70 Y"); - strcat(cmd1, ftostr32(35 - i*width * 2)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr)); - enquecommand(cmd1); - strcpy(cmd1, "G1 Y"); - strcat(cmd1, ftostr32(35 - (2 * i + 1)*width)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr_short_segment)); - enquecommand(cmd1); - strcpy(cmd1, "G1 X50 Y"); - strcat(cmd1, ftostr32(35 - (2 * i + 1)*width)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr)); - enquecommand(cmd1); - strcpy(cmd1, "G1 Y"); - strcat(cmd1, ftostr32(35 - (i + 1)*width * 2)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr_short_segment)); - enquecommand(cmd1); + for (uint8_t i = 0; i < 4; i++) { + lcd_commands_func1(cmd1, i, width, extr, extr_short_segment ); } lcd_commands_step = 5; @@ -1495,56 +1490,18 @@ void lcd_commands() if (lcd_commands_step == 5 && !blocks_queued() && cmd_buffer_empty()) { lcd_timeoutToStatus.start(); - for (int i = 4; i < 8; i++) { - strcpy(cmd1, "G1 X70 Y"); - strcat(cmd1, ftostr32(35 - i*width * 2)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr)); - enquecommand(cmd1); - strcpy(cmd1, "G1 Y"); - strcat(cmd1, ftostr32(35 - (2 * i + 1)*width)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr_short_segment)); - enquecommand(cmd1); - strcpy(cmd1, "G1 X50 Y"); - strcat(cmd1, ftostr32(35 - (2 * i + 1)*width)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr)); - enquecommand(cmd1); - strcpy(cmd1, "G1 Y"); - strcat(cmd1, ftostr32(35 - (i + 1)*width * 2)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr_short_segment)); - enquecommand(cmd1); + for (uint8_t i = 4; i < 8; i++) { + lcd_commands_func1(cmd1, i, width, extr, extr_short_segment ); } lcd_commands_step = 4; } - + if (lcd_commands_step == 4 && !blocks_queued() && cmd_buffer_empty()) { - lcd_timeoutToStatus.start(); - for (int i = 8; i < 12; i++) { - strcpy(cmd1, "G1 X70 Y"); - strcat(cmd1, ftostr32(35 - i*width * 2)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr)); - enquecommand(cmd1); - strcpy(cmd1, "G1 Y"); - strcat(cmd1, ftostr32(35 - (2 * i + 1)*width)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr_short_segment)); - enquecommand(cmd1); - strcpy(cmd1, "G1 X50 Y"); - strcat(cmd1, ftostr32(35 - (2 * i + 1)*width)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr)); - enquecommand(cmd1); - strcpy(cmd1, "G1 Y"); - strcat(cmd1, ftostr32(35 - (i + 1)*width * 2)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr_short_segment)); - enquecommand(cmd1); + lcd_timeoutToStatus.start(); + for (uint8_t i = 8; i < 12; i++) { + lcd_commands_func1(cmd1, i, width, extr, extr_short_segment ); } lcd_commands_step = 3; @@ -1553,27 +1510,8 @@ void lcd_commands() if (lcd_commands_step == 3 && !blocks_queued() && cmd_buffer_empty()) { lcd_timeoutToStatus.start(); - for (int i = 12; i < 16; i++) { - strcpy(cmd1, "G1 X70 Y"); - strcat(cmd1, ftostr32(35 - i*width * 2)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr)); - enquecommand(cmd1); - strcpy(cmd1, "G1 Y"); - strcat(cmd1, ftostr32(35 - (2 * i + 1)*width)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr_short_segment)); - enquecommand(cmd1); - strcpy(cmd1, "G1 X50 Y"); - strcat(cmd1, ftostr32(35 - (2 * i + 1)*width)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr)); - enquecommand(cmd1); - strcpy(cmd1, "G1 Y"); - strcat(cmd1, ftostr32(35 - (i + 1)*width * 2)); - strcat(cmd1, " E"); - strcat(cmd1, ftostr43(extr_short_segment)); - enquecommand(cmd1); + for (uint8_t i = 12; i < 16; i++) { + lcd_commands_func1(cmd1, i, width, extr, extr_short_segment ); } lcd_commands_step = 2; diff --git a/Firmware/ultralcd.h b/Firmware/ultralcd.h old mode 100644 new mode 100755 index 665c1233..f8cf8365 --- a/Firmware/ultralcd.h +++ b/Firmware/ultralcd.h @@ -97,7 +97,7 @@ extern void lcd_diag_show_end_stops(); #define LCD_COMMAND_PID_EXTRUDER 7 #define LCD_COMMAND_V2_CAL 8 -extern int lcd_commands_type; +extern uint8_t lcd_commands_type; extern int8_t FSensorStateMenu; #define CUSTOM_MSG_TYPE_STATUS 0 // status message from lcd_status_message variable