From c929fb52dd5ed9b265f93e3df4b69ac8ea581735 Mon Sep 17 00:00:00 2001 From: ellensp Date: Thu, 28 Jan 2021 20:47:12 +1300 Subject: [PATCH] Meatpack::report_state on serial port init (#20903) Co-authored-by: Scott Lahteine --- Marlin/src/core/serial.cpp | 2 +- Marlin/src/core/serial.h | 4 +- Marlin/src/feature/meatpack.cpp | 4 +- Marlin/src/feature/meatpack.h | 2 +- Marlin/src/feature/spindle_laser.h | 2 +- Marlin/src/gcode/host/M118.cpp | 2 +- Marlin/src/gcode/queue.cpp | 64 +++++++++++++++--------------- Marlin/src/gcode/queue.h | 13 +++--- Marlin/src/sd/cardreader.cpp | 2 +- Marlin/src/sd/cardreader.h | 2 +- 10 files changed, 48 insertions(+), 49 deletions(-) diff --git a/Marlin/src/core/serial.cpp b/Marlin/src/core/serial.cpp index 7e53f3851..5871c61f9 100644 --- a/Marlin/src/core/serial.cpp +++ b/Marlin/src/core/serial.cpp @@ -34,7 +34,7 @@ PGMSTR(SP_X_STR, " X"); PGMSTR(SP_Y_STR, " Y"); PGMSTR(SP_Z_STR, " Z"); PGMST PGMSTR(SP_X_LBL, " X:"); PGMSTR(SP_Y_LBL, " Y:"); PGMSTR(SP_Z_LBL, " Z:"); PGMSTR(SP_E_LBL, " E:"); #if HAS_MULTI_SERIAL - int8_t serial_port_index = 0; + serial_index_t serial_port_index = 0; #endif void serialprintPGM(PGM_P str) { diff --git a/Marlin/src/core/serial.h b/Marlin/src/core/serial.h index 1dd3cd9cd..ae1ef4169 100644 --- a/Marlin/src/core/serial.h +++ b/Marlin/src/core/serial.h @@ -61,9 +61,11 @@ extern uint8_t marlin_debug_flags; // // Serial redirection // +typedef int8_t serial_index_t; #define SERIAL_BOTH 0x7F + #if HAS_MULTI_SERIAL - extern int8_t serial_port_index; + extern serial_index_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/meatpack.cpp b/Marlin/src/feature/meatpack.cpp index 631fa042d..cd6d8ce6b 100644 --- a/Marlin/src/feature/meatpack.cpp +++ b/Marlin/src/feature/meatpack.cpp @@ -74,7 +74,6 @@ void MeatPack::reset_state() { second_char = 0; cmd_count = full_char_count = char_out_count = 0; TERN_(MP_DEBUG, chars_decoded = 0); - report_state(); } /** @@ -189,7 +188,7 @@ void MeatPack::report_state() { * Interpret a single character received from serial * according to the current meatpack state. */ -void MeatPack::handle_rx_char(const uint8_t c) { +void MeatPack::handle_rx_char(const uint8_t c, const serial_index_t serial_ind) { if (c == kCommandByte) { // A command (0xFF) byte? if (cmd_count) { // In fact, two in a row? cmd_is_next = true; // Then a MeatPack command follows @@ -201,6 +200,7 @@ void MeatPack::handle_rx_char(const uint8_t c) { } if (cmd_is_next) { // Were two command bytes received? + PORT_REDIRECT(serial_ind); handle_command((MeatPack_Command)c); // Then the byte is a MeatPack command cmd_is_next = false; return; diff --git a/Marlin/src/feature/meatpack.h b/Marlin/src/feature/meatpack.h index b895bcae5..2641130bd 100644 --- a/Marlin/src/feature/meatpack.h +++ b/Marlin/src/feature/meatpack.h @@ -101,7 +101,7 @@ private: // Pass in a character rx'd by SD card or serial. Automatically parses command/ctrl sequences, // and will control state internally. - static void handle_rx_char(const uint8_t c); + static void handle_rx_char(const uint8_t c, const serial_index_t serial_ind); /** * After passing in rx'd char using above method, call this to get characters out. diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 57fc136c8..d50bc7eb4 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -157,7 +157,7 @@ public: #elif CUTTER_UNIT_IS(RPM) 2 #else - #error "CUTTER_UNIT_IS(???)" + #error "CUTTER_UNIT_IS(unknown)" #endif )); } diff --git a/Marlin/src/gcode/host/M118.cpp b/Marlin/src/gcode/host/M118.cpp index 27207b717..7a77861e2 100644 --- a/Marlin/src/gcode/host/M118.cpp +++ b/Marlin/src/gcode/host/M118.cpp @@ -53,7 +53,7 @@ void GcodeSuite::M118() { } #if HAS_MULTI_SERIAL - const int8_t old_serial = serial_port_index; + const serial_index_t old_serial = serial_port_index; if (WITHIN(port, 0, NUM_SERIAL)) serial_port_index = ( port == 0 ? SERIAL_BOTH diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp index 51fec7d41..1059997ed 100644 --- a/Marlin/src/gcode/queue.cpp +++ b/Marlin/src/gcode/queue.cpp @@ -89,7 +89,7 @@ char GCodeQueue::command_buffer[BUFSIZE][MAX_CMD_SIZE]; * The port that the command was received on */ #if HAS_MULTI_SERIAL - int16_t GCodeQueue::port[BUFSIZE]; + serial_index_t GCodeQueue::port[BUFSIZE]; #endif /** @@ -136,11 +136,11 @@ void GCodeQueue::clear() { */ void GCodeQueue::_commit_command(bool say_ok #if HAS_MULTI_SERIAL - , int16_t p/*=-1*/ + , serial_index_t serial_ind/*=-1*/ #endif ) { send_ok[index_w] = say_ok; - TERN_(HAS_MULTI_SERIAL, port[index_w] = p); + TERN_(HAS_MULTI_SERIAL, port[index_w] = serial_ind); TERN_(POWER_LOSS_RECOVERY, recovery.commit_sdpos(index_w)); if (++index_w >= BUFSIZE) index_w = 0; length++; @@ -153,14 +153,14 @@ void GCodeQueue::_commit_command(bool say_ok */ bool GCodeQueue::_enqueue(const char* cmd, bool say_ok/*=false*/ #if HAS_MULTI_SERIAL - , int16_t pn/*=-1*/ + , serial_index_t serial_ind/*=-1*/ #endif ) { if (*cmd == ';' || length >= BUFSIZE) return false; strcpy(command_buffer[index_w], cmd); _commit_command(say_ok #if HAS_MULTI_SERIAL - , pn + , serial_ind #endif ); return true; @@ -289,9 +289,9 @@ void GCodeQueue::enqueue_now_P(PGM_P const pgcode) { */ void GCodeQueue::ok_to_send() { #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 + const serial_index_t serial_ind = command_port(); + if (serial_ind < 0) return; // Never mind. Command came from SD or Flash Drive + PORT_REDIRECT(serial_ind); // Reply to the serial port that sent the command #endif if (!send_ok[index_r]) return; SERIAL_ECHOPGM(STR_OK); @@ -314,14 +314,14 @@ void GCodeQueue::ok_to_send() { * indicate that a command needs to be re-sent. */ void GCodeQueue::flush_and_request_resend() { - const int16_t pn = command_port(); + const serial_index_t serial_ind = command_port(); #if HAS_MULTI_SERIAL - if (pn < 0) return; - PORT_REDIRECT(pn); // Reply to the serial port that sent the command + if (serial_ind < 0) return; // Never mind. Command came from SD or Flash Drive + PORT_REDIRECT(serial_ind); // 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[serial_ind] + 1); ok_to_send(); } @@ -348,14 +348,14 @@ inline int read_serial(const uint8_t index) { } } -void GCodeQueue::gcode_line_error(PGM_P const err, const int8_t pn) { - PORT_REDIRECT(pn); // Reply to the serial port that sent the command +void GCodeQueue::gcode_line_error(PGM_P const err, const serial_index_t serial_ind) { + PORT_REDIRECT(serial_ind); // Reply to the serial port that sent the command SERIAL_ERROR_START(); serialprintPGM(err); - SERIAL_ECHOLN(last_N[pn]); - while (read_serial(pn) != -1); // Clear out the RX buffer + SERIAL_ECHOLN(last_N[serial_ind]); + while (read_serial(serial_ind) != -1); // Clear out the RX buffer flush_and_request_resend(); - serial_count[pn] = 0; + serial_count[serial_ind] = 0; } FORCE_INLINE bool is_M29(const char * const cmd) { // matches "M29" & "M29 ", but not "M290", etc @@ -473,13 +473,13 @@ void GCodeQueue::get_serial_commands() { * Loop while serial characters are incoming and the queue is not full */ while (length < BUFSIZE && serial_data_available()) { - LOOP_L_N(i, NUM_SERIAL) { + LOOP_L_N(p, NUM_SERIAL) { - const int c = read_serial(i); + const int c = read_serial(p); if (c < 0) continue; #if ENABLED(MEATPACK) - meatpack.handle_rx_char(uint8_t(c)); + meatpack.handle_rx_char(uint8_t(c), p); char c_res[2] = { 0, 0 }; const uint8_t char_count = meatpack.get_result_char(c_res); #else @@ -492,10 +492,10 @@ void GCodeQueue::get_serial_commands() { if (ISEOL(serial_char)) { // Reset our state, continue if the line was empty - if (process_line_done(serial_input_state[i], serial_line_buffer[i], serial_count[i])) + if (process_line_done(serial_input_state[p], serial_line_buffer[p], serial_count[p])) continue; - char* command = serial_line_buffer[i]; + char* command = serial_line_buffer[p]; while (*command == ' ') command++; // Skip leading spaces char *npos = (*command == 'N') ? command : nullptr; // Require the N parameter to start the line @@ -511,25 +511,25 @@ void GCodeQueue::get_serial_commands() { const long gcode_N = strtol(npos + 1, nullptr, 10); - if (gcode_N != last_N[i] + 1 && !M110) - return gcode_line_error(PSTR(STR_ERR_LINE_NO), i); + if (gcode_N != last_N[p] + 1 && !M110) + return gcode_line_error(PSTR(STR_ERR_LINE_NO), p); char *apos = strrchr(command, '*'); if (apos) { uint8_t checksum = 0, count = uint8_t(apos - command); while (count) checksum ^= command[--count]; if (strtol(apos + 1, nullptr, 10) != checksum) - return gcode_line_error(PSTR(STR_ERR_CHECKSUM_MISMATCH), i); + return gcode_line_error(PSTR(STR_ERR_CHECKSUM_MISMATCH), p); } else - return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i); + return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), p); - last_N[i] = gcode_N; + last_N[p] = gcode_N; } #if ENABLED(SDSUPPORT) // Pronterface "M29" and "M29 " has no line number else if (card.flag.saving && !is_M29(command)) - return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i); + return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), p); #endif // @@ -547,7 +547,7 @@ void GCodeQueue::get_serial_commands() { #if ENABLED(BEZIER_CURVE_SUPPORT) case 5: #endif - PORT_REDIRECT(i); // Reply to the serial port that sent the command + PORT_REDIRECT(p); // Reply to the serial port that sent the command SERIAL_ECHOLNPGM(STR_ERR_STOPPED); LCD_MESSAGEPGM(MSG_STOPPED); break; @@ -569,14 +569,14 @@ void GCodeQueue::get_serial_commands() { #endif // Add the command to the queue - _enqueue(serial_line_buffer[i], true + _enqueue(serial_line_buffer[p], true #if HAS_MULTI_SERIAL - , i + , p #endif ); } else - process_stream_char(serial_char, serial_input_state[i], serial_line_buffer[i], serial_count[i]); + process_stream_char(serial_char, serial_input_state[p], serial_line_buffer[p], serial_count[p]); } // char_count loop diff --git a/Marlin/src/gcode/queue.h b/Marlin/src/gcode/queue.h index 57d4beecb..d677146a7 100644 --- a/Marlin/src/gcode/queue.h +++ b/Marlin/src/gcode/queue.h @@ -56,12 +56,9 @@ public: * The port that the command was received on */ #if HAS_MULTI_SERIAL - static int16_t port[BUFSIZE]; + static serial_index_t port[BUFSIZE]; #endif - - static int16_t command_port() { - return TERN0(HAS_MULTI_SERIAL, port[index_r]); - } + static inline serial_index_t command_port() { return TERN0(HAS_MULTI_SERIAL, port[index_r]); } GCodeQueue(); @@ -159,13 +156,13 @@ private: static void _commit_command(bool say_ok #if HAS_MULTI_SERIAL - , int16_t p=-1 + , serial_index_t serial_ind=-1 #endif ); static bool _enqueue(const char* cmd, bool say_ok=false #if HAS_MULTI_SERIAL - , int16_t p=-1 + , serial_index_t serial_ind=-1 #endif ); @@ -181,7 +178,7 @@ private: */ static bool enqueue_one(const char* cmd); - static void gcode_line_error(PGM_P const err, const int8_t pn); + static void gcode_line_error(PGM_P const err, const serial_index_t serial_ind); }; diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index 307f31e43..2e9101ff8 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -1229,7 +1229,7 @@ void CardReader::fileHasFinished() { uint8_t CardReader::auto_report_sd_interval = 0; millis_t CardReader::next_sd_report_ms; #if HAS_MULTI_SERIAL - int8_t CardReader::auto_report_port; + serial_index_t CardReader::auto_report_port; #endif void CardReader::auto_report_sd_status() { diff --git a/Marlin/src/sd/cardreader.h b/Marlin/src/sd/cardreader.h index 7a312b1b5..272e21380 100644 --- a/Marlin/src/sd/cardreader.h +++ b/Marlin/src/sd/cardreader.h @@ -267,7 +267,7 @@ private: static uint8_t auto_report_sd_interval; static millis_t next_sd_report_ms; #if HAS_MULTI_SERIAL - static int8_t auto_report_port; + static serial_index_t auto_report_port; #endif #endif