From a8a64f19f4350dc1874b3bc639e9ffde6e15e54e Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 16 Feb 2018 17:35:26 -0600 Subject: [PATCH 1/4] serial_echopair_P => serial_echopair_PGM --- Marlin/serial.cpp | 14 +++++++------- Marlin/serial.h | 26 +++++++++++++------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Marlin/serial.cpp b/Marlin/serial.cpp index 18bb3bcdd0..2859b058aa 100644 --- a/Marlin/serial.cpp +++ b/Marlin/serial.cpp @@ -25,12 +25,12 @@ const char errormagic[] PROGMEM = "Error:"; const char echomagic[] PROGMEM = "echo:"; -void serial_echopair_P(const char* s_P, const char *v) { serialprintPGM(s_P); SERIAL_ECHO(v); } -void serial_echopair_P(const char* s_P, char v) { serialprintPGM(s_P); SERIAL_CHAR(v); } -void serial_echopair_P(const char* s_P, int v) { serialprintPGM(s_P); SERIAL_ECHO(v); } -void serial_echopair_P(const char* s_P, long v) { serialprintPGM(s_P); SERIAL_ECHO(v); } -void serial_echopair_P(const char* s_P, float v) { serialprintPGM(s_P); SERIAL_ECHO(v); } -void serial_echopair_P(const char* s_P, double v) { serialprintPGM(s_P); SERIAL_ECHO(v); } -void serial_echopair_P(const char* s_P, unsigned long v) { serialprintPGM(s_P); SERIAL_ECHO(v); } +void serial_echopair_PGM(const char* s_P, const char *v) { serialprintPGM(s_P); SERIAL_ECHO(v); } +void serial_echopair_PGM(const char* s_P, char v) { serialprintPGM(s_P); SERIAL_CHAR(v); } +void serial_echopair_PGM(const char* s_P, int v) { serialprintPGM(s_P); SERIAL_ECHO(v); } +void serial_echopair_PGM(const char* s_P, long v) { serialprintPGM(s_P); SERIAL_ECHO(v); } +void serial_echopair_PGM(const char* s_P, float v) { serialprintPGM(s_P); SERIAL_ECHO(v); } +void serial_echopair_PGM(const char* s_P, double v) { serialprintPGM(s_P); SERIAL_ECHO(v); } +void serial_echopair_PGM(const char* s_P, unsigned long v) { serialprintPGM(s_P); SERIAL_ECHO(v); } void serial_spaces(uint8_t count) { count *= (PROPORTIONAL_FONT_RATIO); while (count--) MYSERIAL0.write(' '); } diff --git a/Marlin/serial.h b/Marlin/serial.h index d2ce695839..5e9d9fbcec 100644 --- a/Marlin/serial.h +++ b/Marlin/serial.h @@ -54,7 +54,7 @@ extern const char errormagic[] PROGMEM; #define SERIAL_PROTOCOLPGM(x) serialprintPGM(PSTR(x)) #define SERIAL_PROTOCOLLN(x) do{ MYSERIAL0.print(x); SERIAL_EOL(); }while(0) #define SERIAL_PROTOCOLLNPGM(x) serialprintPGM(PSTR(x "\n")) -#define SERIAL_PROTOCOLPAIR(name, value) serial_echopair_P(PSTR(name),(value)) +#define SERIAL_PROTOCOLPAIR(name, value) serial_echopair_PGM(PSTR(name),(value)) #define SERIAL_PROTOCOLLNPAIR(name, value) do{ SERIAL_PROTOCOLPAIR(name, value); SERIAL_EOL(); }while(0) #define SERIAL_ECHO_START() serialprintPGM(echomagic) @@ -78,18 +78,18 @@ extern const char errormagic[] PROGMEM; #define SERIAL_ECHOPAIR_F(pre,value) SERIAL_ECHOPAIR(pre, FIXFLOAT(value)) #define SERIAL_ECHOLNPAIR_F(pre, value) SERIAL_ECHOLNPAIR(pre, FIXFLOAT(value)) -void serial_echopair_P(const char* s_P, const char *v); -void serial_echopair_P(const char* s_P, char v); -void serial_echopair_P(const char* s_P, int v); -void serial_echopair_P(const char* s_P, long v); -void serial_echopair_P(const char* s_P, float v); -void serial_echopair_P(const char* s_P, double v); -void serial_echopair_P(const char* s_P, unsigned int v); -void serial_echopair_P(const char* s_P, unsigned long v); -FORCE_INLINE void serial_echopair_P(const char* s_P, uint8_t v) { serial_echopair_P(s_P, (int)v); } -FORCE_INLINE void serial_echopair_P(const char* s_P, uint16_t v) { serial_echopair_P(s_P, (int)v); } -FORCE_INLINE void serial_echopair_P(const char* s_P, bool v) { serial_echopair_P(s_P, (int)v); } -FORCE_INLINE void serial_echopair_P(const char* s_P, void *v) { serial_echopair_P(s_P, (unsigned long)v); } +void serial_echopair_PGM(const char* s_P, const char *v); +void serial_echopair_PGM(const char* s_P, char v); +void serial_echopair_PGM(const char* s_P, int v); +void serial_echopair_PGM(const char* s_P, long v); +void serial_echopair_PGM(const char* s_P, float v); +void serial_echopair_PGM(const char* s_P, double v); +void serial_echopair_PGM(const char* s_P, unsigned int v); +void serial_echopair_PGM(const char* s_P, unsigned long v); +FORCE_INLINE void serial_echopair_PGM(const char* s_P, uint8_t v) { serial_echopair_PGM(s_P, (int)v); } +FORCE_INLINE void serial_echopair_PGM(const char* s_P, uint16_t v) { serial_echopair_PGM(s_P, (int)v); } +FORCE_INLINE void serial_echopair_PGM(const char* s_P, bool v) { serial_echopair_PGM(s_P, (int)v); } +FORCE_INLINE void serial_echopair_PGM(const char* s_P, void *v) { serial_echopair_PGM(s_P, (unsigned long)v); } void serial_spaces(uint8_t count); #define SERIAL_ECHO_SP(C) serial_spaces(C) From 023d21a1cf7c426666f15bae8367b3587fe0842e Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 16 Feb 2018 17:50:58 -0600 Subject: [PATCH 2/4] Add SERIAL_FLUSH and SERIAL_FLUSHTX macros --- Marlin/G26_Mesh_Validation_Tool.cpp | 10 +++++----- Marlin/Marlin_main.cpp | 2 +- Marlin/serial.h | 5 +++++ Marlin/ubl.cpp | 2 +- Marlin/ubl_G29.cpp | 6 +++--- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Marlin/G26_Mesh_Validation_Tool.cpp b/Marlin/G26_Mesh_Validation_Tool.cpp index 09dc6e3249..9bddfc1f65 100644 --- a/Marlin/G26_Mesh_Validation_Tool.cpp +++ b/Marlin/G26_Mesh_Validation_Tool.cpp @@ -274,7 +274,7 @@ // action to give the user a more responsive 'Stop'. set_destination_from_current(); idle(); - MYSERIAL0.flush(); // Prevent host M105 buffer overrun. + SERIAL_FLUSH(); // Prevent host M105 buffer overrun. } wait_for_release(); @@ -501,7 +501,7 @@ SERIAL_EOL(); } idle(); - MYSERIAL0.flush(); // Prevent host M105 buffer overrun. + SERIAL_FLUSH(); // Prevent host M105 buffer overrun. } #if ENABLED(ULTRA_LCD) } @@ -525,7 +525,7 @@ } idle(); - MYSERIAL0.flush(); // Prevent host M105 buffer overrun. + SERIAL_FLUSH(); // Prevent host M105 buffer overrun. } #if ENABLED(ULTRA_LCD) lcd_reset_status(); @@ -811,12 +811,12 @@ #endif print_line_from_here_to_there(rx, ry, g26_layer_height, xe, ye, g26_layer_height); - MYSERIAL0.flush(); // Prevent host M105 buffer overrun. + SERIAL_FLUSH(); // Prevent host M105 buffer overrun. } if (look_for_lines_to_connect()) goto LEAVE; } - MYSERIAL0.flush(); // Prevent host M105 buffer overrun. + SERIAL_FLUSH(); // Prevent host M105 buffer overrun. } while (--g26_repeats && location.x_index >= 0 && location.y_index >= 0); LEAVE: diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 8b40524c97..34a82140a5 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -11981,7 +11981,7 @@ void process_next_command() { */ void FlushSerialRequestResend() { //char command_queue[cmd_queue_index_r][100]="Resend:"; - MYSERIAL0.flush(); + SERIAL_FLUSH(); SERIAL_PROTOCOLPGM(MSG_RESEND); SERIAL_PROTOCOLLN(gcode_LastN + 1); ok_to_send(); diff --git a/Marlin/serial.h b/Marlin/serial.h index 5e9d9fbcec..acad502668 100644 --- a/Marlin/serial.h +++ b/Marlin/serial.h @@ -48,6 +48,11 @@ extern const char errormagic[] PROGMEM; #define SERIAL_PRINTLN(x,b) MYSERIAL0.println(x,b) #define SERIAL_PRINTF(args...) MYSERIAL0.printf(args) +#define SERIAL_FLUSH() MYSERIAL0.flush() +#if TX_BUFFER_SIZE > 0 + #define SERIAL_FLUSHTX() MYSERIAL0.flushTX() +#endif + #define SERIAL_PROTOCOLCHAR(x) SERIAL_CHAR(x) #define SERIAL_PROTOCOL(x) MYSERIAL0.print(x) #define SERIAL_PROTOCOL_F(x,y) MYSERIAL0.print(x,y) diff --git a/Marlin/ubl.cpp b/Marlin/ubl.cpp index f6a9a6873d..92eacacdfb 100644 --- a/Marlin/ubl.cpp +++ b/Marlin/ubl.cpp @@ -199,7 +199,7 @@ if (map_type == 1 && i < GRID_MAX_POINTS_X - 1) SERIAL_CHAR(','); #if TX_BUFFER_SIZE > 0 - MYSERIAL0.flushTX(); + SERIAL_FLUSHTX(); #endif safe_delay(15); if (map_type == 0) { diff --git a/Marlin/ubl_G29.cpp b/Marlin/ubl_G29.cpp index 8b8ebc3654..bd232cb27c 100644 --- a/Marlin/ubl_G29.cpp +++ b/Marlin/ubl_G29.cpp @@ -763,7 +763,7 @@ const float measured_z = probe_pt(rawx, rawy, stow_probe, g29_verbose_level); // TODO: Needs error handling z_values[location.x_index][location.y_index] = measured_z; } - MYSERIAL0.flush(); // Prevent host M105 buffer overrun. + SERIAL_FLUSH(); // Prevent host M105 buffer overrun. } while (location.x_index >= 0 && --max_iterations); @@ -902,7 +902,7 @@ SERIAL_PROTOCOL_F(z_values[location.x_index][location.y_index], 6); SERIAL_EOL(); } - MYSERIAL0.flush(); // Prevent host M105 buffer overrun. + SERIAL_FLUSH(); // Prevent host M105 buffer overrun. } while (location.x_index >= 0 && location.y_index >= 0); if (do_ubl_mesh_map) display_map(g29_map_type); // show user where we're probing @@ -1414,7 +1414,7 @@ do_blocking_move_to_z(h_offset + new_z); // Move the nozzle as the point is edited #endif idle(); - MYSERIAL0.flush(); // Prevent host M105 buffer overrun. + SERIAL_FLUSH(); // Prevent host M105 buffer overrun. } while (!is_lcd_clicked()); if (!lcd_map_control) lcd_return_to_status(); From 12628d43ce94592885f746910a47d37a149e25e9 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 16 Feb 2018 17:53:47 -0600 Subject: [PATCH 3/4] Use serial macros where possible --- Marlin/SdBaseFile.cpp | 36 ++++++++++++++++++------------------ Marlin/SdFatUtil.cpp | 4 ++-- Marlin/pinsDebug.h | 4 ++-- Marlin/serial.cpp | 2 +- Marlin/stepper.h | 4 ++-- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Marlin/SdBaseFile.cpp b/Marlin/SdBaseFile.cpp index ac8e445c8c..3460b24d3f 100644 --- a/Marlin/SdBaseFile.cpp +++ b/Marlin/SdBaseFile.cpp @@ -339,38 +339,38 @@ int8_t SdBaseFile::lsPrintNext(uint8_t flags, uint8_t indent) { && DIR_IS_FILE_OR_SUBDIR(&dir)) break; } // indent for dir level - for (uint8_t i = 0; i < indent; i++) MYSERIAL0.write(' '); + for (uint8_t i = 0; i < indent; i++) SERIAL_CHAR(' '); // print name for (uint8_t i = 0; i < 11; i++) { if (dir.name[i] == ' ')continue; if (i == 8) { - MYSERIAL0.write('.'); + SERIAL_CHAR('.'); w++; } - MYSERIAL0.write(dir.name[i]); + SERIAL_CHAR(dir.name[i]); w++; } if (DIR_IS_SUBDIR(&dir)) { - MYSERIAL0.write('/'); + SERIAL_CHAR('/'); w++; } if (flags & (LS_DATE | LS_SIZE)) { - while (w++ < 14) MYSERIAL0.write(' '); + while (w++ < 14) SERIAL_CHAR(' '); } // print modify date/time if requested if (flags & LS_DATE) { - MYSERIAL0.write(' '); + SERIAL_CHAR(' '); printFatDate(dir.lastWriteDate); - MYSERIAL0.write(' '); + SERIAL_CHAR(' '); printFatTime(dir.lastWriteTime); } // print size if requested if (!DIR_IS_SUBDIR(&dir) && (flags & LS_SIZE)) { - MYSERIAL0.write(' '); - MYSERIAL0.print(dir.fileSize); + SERIAL_CHAR(' '); + SERIAL_PROTOCOL(dir.fileSize); } - MYSERIAL0.println(); + SERIAL_EOL(); return DIR_IS_FILE(&dir) ? 1 : 2; } @@ -905,8 +905,8 @@ int SdBaseFile::peek() { // print uint8_t with width 2 static void print2u(uint8_t v) { - if (v < 10) MYSERIAL0.write('0'); - MYSERIAL0.print(v, DEC); + if (v < 10) SERIAL_CHAR('0'); + SERIAL_PRINT(v, DEC); } /** @@ -927,10 +927,10 @@ static void print2u(uint8_t v) { * \param[in] fatDate The date field from a directory entry. */ void SdBaseFile::printFatDate(uint16_t fatDate) { - MYSERIAL0.print(FAT_YEAR(fatDate)); - MYSERIAL0.write('-'); + SERIAL_PROTOCOL(FAT_YEAR(fatDate)); + SERIAL_CHAR('-'); print2u(FAT_MONTH(fatDate)); - MYSERIAL0.write('-'); + SERIAL_CHAR('-'); print2u(FAT_DAY(fatDate)); } @@ -945,9 +945,9 @@ void SdBaseFile::printFatDate(uint16_t fatDate) { */ void SdBaseFile::printFatTime(uint16_t fatTime) { print2u(FAT_HOUR(fatTime)); - MYSERIAL0.write(':'); + SERIAL_CHAR(':'); print2u(FAT_MINUTE(fatTime)); - MYSERIAL0.write(':'); + SERIAL_CHAR(':'); print2u(FAT_SECOND(fatTime)); } @@ -959,7 +959,7 @@ void SdBaseFile::printFatTime(uint16_t fatTime) { bool SdBaseFile::printName() { char name[FILENAME_LENGTH]; if (!getFilename(name)) return false; - MYSERIAL0.print(name); + SERIAL_PROTOCOL(name); return true; } diff --git a/Marlin/SdFatUtil.cpp b/Marlin/SdFatUtil.cpp index 2e9ce939b0..2bd471bc67 100644 --- a/Marlin/SdFatUtil.cpp +++ b/Marlin/SdFatUtil.cpp @@ -63,7 +63,7 @@ int SdFatUtil::FreeRam() { * \param[in] str Pointer to string stored in flash memory. */ void SdFatUtil::print_P(PGM_P str) { - for (uint8_t c; (c = pgm_read_byte(str)); str++) MYSERIAL0.write(c); + for (uint8_t c; (c = pgm_read_byte(str)); str++) SERIAL_CHAR(c); } /** @@ -72,7 +72,7 @@ void SdFatUtil::print_P(PGM_P str) { * \param[in] pr Print object for output. * \param[in] str Pointer to string stored in flash memory. */ -void SdFatUtil::println_P(PGM_P str) { print_P(str); MYSERIAL0.println(); } +void SdFatUtil::println_P(PGM_P str) { print_P(str); SERIAL_EOL(); } /** * %Print a string in flash memory to Serial. diff --git a/Marlin/pinsDebug.h b/Marlin/pinsDebug.h index 8a89818f56..9056f3a3a9 100644 --- a/Marlin/pinsDebug.h +++ b/Marlin/pinsDebug.h @@ -474,9 +474,9 @@ inline void report_pin_state_extended(int8_t pin, bool ignore, bool extended = f for (uint8_t y = 0; y < 28; y++) { // always print pin name temp_char = pgm_read_byte(name_mem_pointer + y); if (temp_char != 0) - MYSERIAL0.write(temp_char); + SERIAL_CHAR(temp_char); else { - for (uint8_t i = 0; i < 28 - y; i++) MYSERIAL0.write(' '); + for (uint8_t i = 0; i < 28 - y; i++) SERIAL_CHAR(' '); break; } } diff --git a/Marlin/serial.cpp b/Marlin/serial.cpp index 2859b058aa..8b6a1c2b49 100644 --- a/Marlin/serial.cpp +++ b/Marlin/serial.cpp @@ -33,4 +33,4 @@ void serial_echopair_PGM(const char* s_P, float v) { serialprintPGM(s_P) void serial_echopair_PGM(const char* s_P, double v) { serialprintPGM(s_P); SERIAL_ECHO(v); } void serial_echopair_PGM(const char* s_P, unsigned long v) { serialprintPGM(s_P); SERIAL_ECHO(v); } -void serial_spaces(uint8_t count) { count *= (PROPORTIONAL_FONT_RATIO); while (count--) MYSERIAL0.write(' '); } +void serial_spaces(uint8_t count) { count *= (PROPORTIONAL_FONT_RATIO); while (count--) SERIAL_CHAR(' '); } diff --git a/Marlin/stepper.h b/Marlin/stepper.h index 3dd0283c3f..af541e89d9 100644 --- a/Marlin/stepper.h +++ b/Marlin/stepper.h @@ -336,8 +336,8 @@ class Stepper { } if (timer < 100) { // (20kHz - this should never happen) timer = 100; - MYSERIAL0.print(MSG_STEPPER_TOO_HIGH); - MYSERIAL0.println(step_rate); + SERIAL_PROTOCOL(MSG_STEPPER_TOO_HIGH); + SERIAL_PROTOCOLLN(step_rate); } return timer; } From ba4a17f251431aec790d03418d66a00598c22cdc Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 16 Feb 2018 17:54:07 -0600 Subject: [PATCH 4/4] Tweak serial.h --- Marlin/serial.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Marlin/serial.h b/Marlin/serial.h index acad502668..9946571213 100644 --- a/Marlin/serial.h +++ b/Marlin/serial.h @@ -83,6 +83,13 @@ extern const char errormagic[] PROGMEM; #define SERIAL_ECHOPAIR_F(pre,value) SERIAL_ECHOPAIR(pre, FIXFLOAT(value)) #define SERIAL_ECHOLNPAIR_F(pre, value) SERIAL_ECHOLNPAIR(pre, FIXFLOAT(value)) +// +// Functions for serial printing from PROGMEM. (Saves loads of SRAM.) +// +FORCE_INLINE void serialprintPGM(const char* str) { + while (char ch = pgm_read_byte(str++)) SERIAL_CHAR(ch); +} + void serial_echopair_PGM(const char* s_P, const char *v); void serial_echopair_PGM(const char* s_P, char v); void serial_echopair_PGM(const char* s_P, int v); @@ -101,11 +108,4 @@ void serial_spaces(uint8_t count); #define SERIAL_ERROR_SP(C) serial_spaces(C) #define SERIAL_PROTOCOL_SP(C) serial_spaces(C) -// -// Functions for serial printing from PROGMEM. (Saves loads of SRAM.) -// -FORCE_INLINE void serialprintPGM(const char* str) { - while (char ch = pgm_read_byte(str++)) MYSERIAL0.write(ch); -} - #endif // __SERIAL_H__