From 63717822637064b347d046f8f39be824eff51785 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 12 May 2020 05:50:28 -0500 Subject: [PATCH] Add HAS_MULTI_SERIAL conditional --- Marlin/src/MarlinCore.cpp | 2 +- Marlin/src/core/serial.cpp | 2 +- Marlin/src/core/serial.h | 2 +- Marlin/src/feature/binary_protocol.h | 4 ++-- Marlin/src/gcode/config/M575.cpp | 6 +++--- Marlin/src/gcode/host/M118.cpp | 10 ++++------ Marlin/src/gcode/parser.cpp | 2 +- Marlin/src/gcode/queue.cpp | 30 ++++++++++------------------ Marlin/src/gcode/queue.h | 12 ++++------- Marlin/src/gcode/sd/M28_M29.cpp | 6 ++---- Marlin/src/inc/Conditionals_post.h | 2 ++ Marlin/src/sd/cardreader.cpp | 4 ++-- Marlin/src/sd/cardreader.h | 8 +++----- 13 files changed, 37 insertions(+), 53 deletions(-) diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp index 3b07676840..3b0ba07c16 100644 --- a/Marlin/src/MarlinCore.cpp +++ b/Marlin/src/MarlinCore.cpp @@ -862,7 +862,7 @@ void setup() { MYSERIAL0.begin(BAUDRATE); uint32_t serial_connect_timeout = millis() + 1000UL; while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ } - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL MYSERIAL1.begin(BAUDRATE); serial_connect_timeout = millis() + 1000UL; while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ } diff --git a/Marlin/src/core/serial.cpp b/Marlin/src/core/serial.cpp index 1d3907bc56..313d6ce20e 100644 --- a/Marlin/src/core/serial.cpp +++ b/Marlin/src/core/serial.cpp @@ -28,7 +28,7 @@ uint8_t marlin_debug_flags = MARLIN_DEBUG_NONE; static PGMSTR(errormagic, "Error:"); static PGMSTR(echomagic, "echo:"); -#if NUM_SERIAL > 1 +#if HAS_MULTI_SERIAL int8_t serial_port_index = 0; #endif diff --git a/Marlin/src/core/serial.h b/Marlin/src/core/serial.h index 5f800c32cf..1a319e6322 100644 --- a/Marlin/src/core/serial.h +++ b/Marlin/src/core/serial.h @@ -47,7 +47,7 @@ extern uint8_t marlin_debug_flags; #define DEBUGGING(F) (marlin_debug_flags & (MARLIN_DEBUG_## F)) #define SERIAL_BOTH 0x7F -#if NUM_SERIAL > 1 +#if HAS_MULTI_SERIAL extern int8_t serial_port_index; #define _PORT_REDIRECT(n,p) REMEMBER(n,serial_port_index,p) #define _PORT_RESTORE(n) RESTORE(n) diff --git a/Marlin/src/feature/binary_protocol.h b/Marlin/src/feature/binary_protocol.h index c7850c4315..e9a723c1eb 100644 --- a/Marlin/src/feature/binary_protocol.h +++ b/Marlin/src/feature/binary_protocol.h @@ -32,7 +32,7 @@ inline bool bs_serial_data_available(const uint8_t index) { switch (index) { case 0: return MYSERIAL0.available(); - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL case 1: return MYSERIAL1.available(); #endif } @@ -42,7 +42,7 @@ inline bool bs_serial_data_available(const uint8_t index) { inline int bs_read_serial(const uint8_t index) { switch (index) { case 0: return MYSERIAL0.read(); - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL case 1: return MYSERIAL1.read(); #endif } diff --git a/Marlin/src/gcode/config/M575.cpp b/Marlin/src/gcode/config/M575.cpp index 947af14e30..a81d1e57c9 100644 --- a/Marlin/src/gcode/config/M575.cpp +++ b/Marlin/src/gcode/config/M575.cpp @@ -42,7 +42,7 @@ void GcodeSuite::M575() { if (set0) { SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR(" Serial " - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL , '0', #else "0" @@ -50,7 +50,7 @@ void GcodeSuite::M575() { " baud rate set to ", baud ); } - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL const bool set1 = (port == -99 || port == 1); if (set1) { SERIAL_ECHO_START(); @@ -62,7 +62,7 @@ void GcodeSuite::M575() { if (set0) { MYSERIAL0.end(); MYSERIAL0.begin(baud); } - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL if (set1) { MYSERIAL1.end(); MYSERIAL1.begin(baud); } #endif diff --git a/Marlin/src/gcode/host/M118.cpp b/Marlin/src/gcode/host/M118.cpp index ba52a4f816..81bb830d83 100644 --- a/Marlin/src/gcode/host/M118.cpp +++ b/Marlin/src/gcode/host/M118.cpp @@ -34,7 +34,7 @@ */ void GcodeSuite::M118() { bool hasE = false, hasA = false; - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL int8_t port = -1; // Assume no redirect #endif char *p = parser.string_arg; @@ -44,7 +44,7 @@ void GcodeSuite::M118() { switch (p[0]) { case 'A': hasA = true; break; case 'E': hasE = true; break; - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL case 'P': port = p[1] - '0'; break; #endif } @@ -52,7 +52,7 @@ void GcodeSuite::M118() { while (*p == ' ') ++p; } - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL const int8_t old_serial = serial_port_index; if (WITHIN(port, 0, NUM_SERIAL)) serial_port_index = ( @@ -69,7 +69,5 @@ void GcodeSuite::M118() { if (hasA) SERIAL_ECHOPGM("// "); SERIAL_ECHOLN(p); - #if NUM_SERIAL > 1 - serial_port_index = old_serial; - #endif + TERN_(HAS_MULTI_SERIAL, serial_port_index = old_serial); } diff --git a/Marlin/src/gcode/parser.cpp b/Marlin/src/gcode/parser.cpp index 9a2272e747..a025cebc88 100644 --- a/Marlin/src/gcode/parser.cpp +++ b/Marlin/src/gcode/parser.cpp @@ -28,7 +28,7 @@ #include "../MarlinCore.h" -#if NUM_SERIAL > 1 +#if HAS_MULTI_SERIAL #include "queue.h" #endif diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp index a4a4993556..24d1d2cf67 100644 --- a/Marlin/src/gcode/queue.cpp +++ b/Marlin/src/gcode/queue.cpp @@ -72,7 +72,7 @@ char GCodeQueue::command_buffer[BUFSIZE][MAX_CMD_SIZE]; /* * The port that the command was received on */ -#if NUM_SERIAL > 1 +#if HAS_MULTI_SERIAL int16_t GCodeQueue::port[BUFSIZE]; #endif @@ -119,14 +119,12 @@ void GCodeQueue::clear() { * Once a new command is in the ring buffer, call this to commit it */ void GCodeQueue::_commit_command(bool say_ok - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL , int16_t p/*=-1*/ #endif ) { send_ok[index_w] = say_ok; - #if NUM_SERIAL > 1 - port[index_w] = p; - #endif + TERN_(HAS_MULTI_SERIAL, port[index_w] = p); TERN_(POWER_LOSS_RECOVERY, recovery.commit_sdpos(index_w)); if (++index_w >= BUFSIZE) index_w = 0; length++; @@ -138,14 +136,14 @@ void GCodeQueue::_commit_command(bool say_ok * Return false for a full buffer, or if the 'command' is a comment. */ bool GCodeQueue::_enqueue(const char* cmd, bool say_ok/*=false*/ - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL , int16_t pn/*=-1*/ #endif ) { if (*cmd == ';' || length >= BUFSIZE) return false; strcpy(command_buffer[index_w], cmd); _commit_command(say_ok - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL , pn #endif ); @@ -276,7 +274,7 @@ void GCodeQueue::enqueue_now_P(PGM_P const pgcode) { * B Block queue space remaining */ void GCodeQueue::ok_to_send() { - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL const int16_t pn = command_port(); if (pn < 0) return; PORT_REDIRECT(pn); // Reply to the serial port that sent the command @@ -303,30 +301,24 @@ void GCodeQueue::ok_to_send() { */ void GCodeQueue::flush_and_request_resend() { const int16_t pn = command_port(); - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL if (pn < 0) return; PORT_REDIRECT(pn); // Reply to the serial port that sent the command #endif SERIAL_FLUSH(); SERIAL_ECHOPGM(STR_RESEND); - - SERIAL_ECHOLN(last_N[pn] + 1); + SERIAL_ECHOLN(last_N[pn] + 1); ok_to_send(); } inline bool serial_data_available() { - return false - || MYSERIAL0.available() - #if NUM_SERIAL > 1 - || MYSERIAL1.available() - #endif - ; + return MYSERIAL0.available() || TERN0(HAS_MULTI_SERIAL, MYSERIAL1.available()); } inline int read_serial(const uint8_t index) { switch (index) { case 0: return MYSERIAL0.read(); - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL case 1: return MYSERIAL1.read(); #endif default: return -1; @@ -538,7 +530,7 @@ void GCodeQueue::get_serial_commands() { // Add the command to the queue _enqueue(serial_line_buffer[i], true - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL , i #endif ); diff --git a/Marlin/src/gcode/queue.h b/Marlin/src/gcode/queue.h index 6c14d7d3db..25731782d3 100644 --- a/Marlin/src/gcode/queue.h +++ b/Marlin/src/gcode/queue.h @@ -55,16 +55,12 @@ public: /** * The port that the command was received on */ - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL static int16_t port[BUFSIZE]; #endif static int16_t command_port() { - return (0 - #if NUM_SERIAL > 1 - + port[index_r] - #endif - ); + return TERN0(HAS_MULTI_SERIAL, port[index_r]); } GCodeQueue(); @@ -162,13 +158,13 @@ private: #endif static void _commit_command(bool say_ok - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL , int16_t p=-1 #endif ); static bool _enqueue(const char* cmd, bool say_ok=false - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL , int16_t p=-1 #endif ); diff --git a/Marlin/src/gcode/sd/M28_M29.cpp b/Marlin/src/gcode/sd/M28_M29.cpp index 9dc192c09b..45e0782fa7 100644 --- a/Marlin/src/gcode/sd/M28_M29.cpp +++ b/Marlin/src/gcode/sd/M28_M29.cpp @@ -27,7 +27,7 @@ #include "../gcode.h" #include "../../sd/cardreader.h" -#if NUM_SERIAL > 1 +#if HAS_MULTI_SERIAL #include "../queue.h" #endif @@ -49,9 +49,7 @@ void GcodeSuite::M28() { // Binary transfer mode if ((card.flag.binary_mode = binary_mode)) { SERIAL_ECHO_MSG("Switching to Binary Protocol"); - #if NUM_SERIAL > 1 - card.transfer_port_index = queue.port[queue.index_r]; - #endif + TERN_(HAS_MULTI_SERIAL, card.transfer_port_index = queue.port[queue.index_r]); } else card.openFileWrite(p); diff --git a/Marlin/src/inc/Conditionals_post.h b/Marlin/src/inc/Conditionals_post.h index edc981520d..6fb546822b 100644 --- a/Marlin/src/inc/Conditionals_post.h +++ b/Marlin/src/inc/Conditionals_post.h @@ -2519,4 +2519,6 @@ #if !NUM_SERIAL #undef BAUD_RATE_GCODE +#elif NUM_SERIAL > 1 + #define HAS_MULTI_SERIAL 1 #endif diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index 9ce2c5304c..9c662fd755 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -51,7 +51,7 @@ card_flags_t CardReader::flag; char CardReader::filename[FILENAME_LENGTH], CardReader::longFilename[LONG_FILENAME_LENGTH]; int8_t CardReader::autostart_index; -#if ENABLED(BINARY_FILE_TRANSFER) && NUM_SERIAL > 1 +#if BOTH(HAS_MULTI_SERIAL, BINARY_FILE_TRANSFER) int8_t CardReader::transfer_port_index; #endif @@ -1095,7 +1095,7 @@ void CardReader::fileHasFinished() { #if ENABLED(AUTO_REPORT_SD_STATUS) uint8_t CardReader::auto_report_sd_interval = 0; millis_t CardReader::next_sd_report_ms; - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL int8_t CardReader::auto_report_port; #endif diff --git a/Marlin/src/sd/cardreader.h b/Marlin/src/sd/cardreader.h index f783f96ca7..4f488e3148 100644 --- a/Marlin/src/sd/cardreader.h +++ b/Marlin/src/sd/cardreader.h @@ -61,7 +61,7 @@ public: // Fast! binary file transfer #if ENABLED(BINARY_FILE_TRANSFER) - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL static int8_t transfer_port_index; #else static constexpr int8_t transfer_port_index = 0; @@ -164,9 +164,7 @@ public: #if ENABLED(AUTO_REPORT_SD_STATUS) static void auto_report_sd_status(); static inline void set_auto_report_interval(uint8_t v) { - #if NUM_SERIAL > 1 - auto_report_port = serial_port_index; - #endif + TERN_(HAS_MULTI_SERIAL, auto_report_port = serial_port_index); NOMORE(v, 60); auto_report_sd_interval = v; next_sd_report_ms = millis() + 1000UL * v; @@ -258,7 +256,7 @@ private: #if ENABLED(AUTO_REPORT_SD_STATUS) static uint8_t auto_report_sd_interval; static millis_t next_sd_report_ms; - #if NUM_SERIAL > 1 + #if HAS_MULTI_SERIAL static int8_t auto_report_port; #endif #endif