Skip to content

Commit 2f1833b

Browse files
ellenspthinkyhead
authored andcommitted
Meatpack::report_state on serial port init (MarlinFirmware#20903)
Co-authored-by: Scott Lahteine <github@thinkyhead.com>
1 parent 80fd6c2 commit 2f1833b

File tree

10 files changed

+48
-49
lines changed

10 files changed

+48
-49
lines changed

Marlin/src/core/serial.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ PGMSTR(SP_X_STR, " X"); PGMSTR(SP_Y_STR, " Y"); PGMSTR(SP_Z_STR, " Z"); PGMST
3434
PGMSTR(SP_X_LBL, " X:"); PGMSTR(SP_Y_LBL, " Y:"); PGMSTR(SP_Z_LBL, " Z:"); PGMSTR(SP_E_LBL, " E:");
3535

3636
#if HAS_MULTI_SERIAL
37-
int8_t serial_port_index = 0;
37+
serial_index_t serial_port_index = 0;
3838
#endif
3939

4040
void serialprintPGM(PGM_P str) {

Marlin/src/core/serial.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ extern uint8_t marlin_debug_flags;
6161
//
6262
// Serial redirection
6363
//
64+
typedef int8_t serial_index_t;
6465
#define SERIAL_BOTH 0x7F
66+
6567
#if HAS_MULTI_SERIAL
66-
extern int8_t serial_port_index;
68+
extern serial_index_t serial_port_index;
6769
#define _PORT_REDIRECT(n,p) REMEMBER(n,serial_port_index,p)
6870
#define _PORT_RESTORE(n) RESTORE(n)
6971

Marlin/src/feature/meatpack.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ void MeatPack::reset_state() {
7474
second_char = 0;
7575
cmd_count = full_char_count = char_out_count = 0;
7676
TERN_(MP_DEBUG, chars_decoded = 0);
77-
report_state();
7877
}
7978

8079
/**
@@ -189,7 +188,7 @@ void MeatPack::report_state() {
189188
* Interpret a single character received from serial
190189
* according to the current meatpack state.
191190
*/
192-
void MeatPack::handle_rx_char(const uint8_t c) {
191+
void MeatPack::handle_rx_char(const uint8_t c, const serial_index_t serial_ind) {
193192
if (c == kCommandByte) { // A command (0xFF) byte?
194193
if (cmd_count) { // In fact, two in a row?
195194
cmd_is_next = true; // Then a MeatPack command follows
@@ -201,6 +200,7 @@ void MeatPack::handle_rx_char(const uint8_t c) {
201200
}
202201

203202
if (cmd_is_next) { // Were two command bytes received?
203+
PORT_REDIRECT(serial_ind);
204204
handle_command((MeatPack_Command)c); // Then the byte is a MeatPack command
205205
cmd_is_next = false;
206206
return;

Marlin/src/feature/meatpack.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class MeatPack {
101101

102102
// Pass in a character rx'd by SD card or serial. Automatically parses command/ctrl sequences,
103103
// and will control state internally.
104-
static void handle_rx_char(const uint8_t c);
104+
static void handle_rx_char(const uint8_t c, const serial_index_t serial_ind);
105105

106106
/**
107107
* After passing in rx'd char using above method, call this to get characters out.

Marlin/src/feature/spindle_laser.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class SpindleLaser {
157157
#elif CUTTER_UNIT_IS(RPM)
158158
2
159159
#else
160-
#error "CUTTER_UNIT_IS(???)"
160+
#error "CUTTER_UNIT_IS(unknown)"
161161
#endif
162162
));
163163
}

Marlin/src/gcode/host/M118.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void GcodeSuite::M118() {
5353
}
5454

5555
#if HAS_MULTI_SERIAL
56-
const int8_t old_serial = serial_port_index;
56+
const serial_index_t old_serial = serial_port_index;
5757
if (WITHIN(port, 0, NUM_SERIAL))
5858
serial_port_index = (
5959
port == 0 ? SERIAL_BOTH

Marlin/src/gcode/queue.cpp

+32-32
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ char GCodeQueue::command_buffer[BUFSIZE][MAX_CMD_SIZE];
8989
* The port that the command was received on
9090
*/
9191
#if HAS_MULTI_SERIAL
92-
int16_t GCodeQueue::port[BUFSIZE];
92+
serial_index_t GCodeQueue::port[BUFSIZE];
9393
#endif
9494

9595
/**
@@ -136,11 +136,11 @@ void GCodeQueue::clear() {
136136
*/
137137
void GCodeQueue::_commit_command(bool say_ok
138138
#if HAS_MULTI_SERIAL
139-
, int16_t p/*=-1*/
139+
, serial_index_t serial_ind/*=-1*/
140140
#endif
141141
) {
142142
send_ok[index_w] = say_ok;
143-
TERN_(HAS_MULTI_SERIAL, port[index_w] = p);
143+
TERN_(HAS_MULTI_SERIAL, port[index_w] = serial_ind);
144144
TERN_(POWER_LOSS_RECOVERY, recovery.commit_sdpos(index_w));
145145
if (++index_w >= BUFSIZE) index_w = 0;
146146
length++;
@@ -153,14 +153,14 @@ void GCodeQueue::_commit_command(bool say_ok
153153
*/
154154
bool GCodeQueue::_enqueue(const char* cmd, bool say_ok/*=false*/
155155
#if HAS_MULTI_SERIAL
156-
, int16_t pn/*=-1*/
156+
, serial_index_t serial_ind/*=-1*/
157157
#endif
158158
) {
159159
if (*cmd == ';' || length >= BUFSIZE) return false;
160160
strcpy(command_buffer[index_w], cmd);
161161
_commit_command(say_ok
162162
#if HAS_MULTI_SERIAL
163-
, pn
163+
, serial_ind
164164
#endif
165165
);
166166
return true;
@@ -289,9 +289,9 @@ void GCodeQueue::enqueue_now_P(PGM_P const pgcode) {
289289
*/
290290
void GCodeQueue::ok_to_send() {
291291
#if HAS_MULTI_SERIAL
292-
const int16_t pn = command_port();
293-
if (pn < 0) return;
294-
PORT_REDIRECT(pn); // Reply to the serial port that sent the command
292+
const serial_index_t serial_ind = command_port();
293+
if (serial_ind < 0) return; // Never mind. Command came from SD or Flash Drive
294+
PORT_REDIRECT(serial_ind); // Reply to the serial port that sent the command
295295
#endif
296296
if (!send_ok[index_r]) return;
297297
SERIAL_ECHOPGM(STR_OK);
@@ -314,14 +314,14 @@ void GCodeQueue::ok_to_send() {
314314
* indicate that a command needs to be re-sent.
315315
*/
316316
void GCodeQueue::flush_and_request_resend() {
317-
const int16_t pn = command_port();
317+
const serial_index_t serial_ind = command_port();
318318
#if HAS_MULTI_SERIAL
319-
if (pn < 0) return;
320-
PORT_REDIRECT(pn); // Reply to the serial port that sent the command
319+
if (serial_ind < 0) return; // Never mind. Command came from SD or Flash Drive
320+
PORT_REDIRECT(serial_ind); // Reply to the serial port that sent the command
321321
#endif
322322
SERIAL_FLUSH();
323323
SERIAL_ECHOPGM(STR_RESEND);
324-
SERIAL_ECHOLN(last_N[pn] + 1);
324+
SERIAL_ECHOLN(last_N[serial_ind] + 1);
325325
ok_to_send();
326326
}
327327

@@ -348,14 +348,14 @@ inline int read_serial(const uint8_t index) {
348348
}
349349
}
350350

351-
void GCodeQueue::gcode_line_error(PGM_P const err, const int8_t pn) {
352-
PORT_REDIRECT(pn); // Reply to the serial port that sent the command
351+
void GCodeQueue::gcode_line_error(PGM_P const err, const serial_index_t serial_ind) {
352+
PORT_REDIRECT(serial_ind); // Reply to the serial port that sent the command
353353
SERIAL_ERROR_START();
354354
serialprintPGM(err);
355-
SERIAL_ECHOLN(last_N[pn]);
356-
while (read_serial(pn) != -1); // Clear out the RX buffer
355+
SERIAL_ECHOLN(last_N[serial_ind]);
356+
while (read_serial(serial_ind) != -1); // Clear out the RX buffer
357357
flush_and_request_resend();
358-
serial_count[pn] = 0;
358+
serial_count[serial_ind] = 0;
359359
}
360360

361361
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() {
473473
* Loop while serial characters are incoming and the queue is not full
474474
*/
475475
while (length < BUFSIZE && serial_data_available()) {
476-
LOOP_L_N(i, NUM_SERIAL) {
476+
LOOP_L_N(p, NUM_SERIAL) {
477477

478-
const int c = read_serial(i);
478+
const int c = read_serial(p);
479479
if (c < 0) continue;
480480

481481
#if ENABLED(MEATPACK)
482-
meatpack.handle_rx_char(uint8_t(c));
482+
meatpack.handle_rx_char(uint8_t(c), p);
483483
char c_res[2] = { 0, 0 };
484484
const uint8_t char_count = meatpack.get_result_char(c_res);
485485
#else
@@ -492,10 +492,10 @@ void GCodeQueue::get_serial_commands() {
492492
if (ISEOL(serial_char)) {
493493

494494
// Reset our state, continue if the line was empty
495-
if (process_line_done(serial_input_state[i], serial_line_buffer[i], serial_count[i]))
495+
if (process_line_done(serial_input_state[p], serial_line_buffer[p], serial_count[p]))
496496
continue;
497497

498-
char* command = serial_line_buffer[i];
498+
char* command = serial_line_buffer[p];
499499

500500
while (*command == ' ') command++; // Skip leading spaces
501501
char *npos = (*command == 'N') ? command : nullptr; // Require the N parameter to start the line
@@ -511,25 +511,25 @@ void GCodeQueue::get_serial_commands() {
511511

512512
const long gcode_N = strtol(npos + 1, nullptr, 10);
513513

514-
if (gcode_N != last_N[i] + 1 && !M110)
515-
return gcode_line_error(PSTR(STR_ERR_LINE_NO), i);
514+
if (gcode_N != last_N[p] + 1 && !M110)
515+
return gcode_line_error(PSTR(STR_ERR_LINE_NO), p);
516516

517517
char *apos = strrchr(command, '*');
518518
if (apos) {
519519
uint8_t checksum = 0, count = uint8_t(apos - command);
520520
while (count) checksum ^= command[--count];
521521
if (strtol(apos + 1, nullptr, 10) != checksum)
522-
return gcode_line_error(PSTR(STR_ERR_CHECKSUM_MISMATCH), i);
522+
return gcode_line_error(PSTR(STR_ERR_CHECKSUM_MISMATCH), p);
523523
}
524524
else
525-
return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i);
525+
return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), p);
526526

527-
last_N[i] = gcode_N;
527+
last_N[p] = gcode_N;
528528
}
529529
#if ENABLED(SDSUPPORT)
530530
// Pronterface "M29" and "M29 " has no line number
531531
else if (card.flag.saving && !is_M29(command))
532-
return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i);
532+
return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), p);
533533
#endif
534534

535535
//
@@ -547,7 +547,7 @@ void GCodeQueue::get_serial_commands() {
547547
#if ENABLED(BEZIER_CURVE_SUPPORT)
548548
case 5:
549549
#endif
550-
PORT_REDIRECT(i); // Reply to the serial port that sent the command
550+
PORT_REDIRECT(p); // Reply to the serial port that sent the command
551551
SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
552552
LCD_MESSAGEPGM(MSG_STOPPED);
553553
break;
@@ -569,14 +569,14 @@ void GCodeQueue::get_serial_commands() {
569569
#endif
570570

571571
// Add the command to the queue
572-
_enqueue(serial_line_buffer[i], true
572+
_enqueue(serial_line_buffer[p], true
573573
#if HAS_MULTI_SERIAL
574-
, i
574+
, p
575575
#endif
576576
);
577577
}
578578
else
579-
process_stream_char(serial_char, serial_input_state[i], serial_line_buffer[i], serial_count[i]);
579+
process_stream_char(serial_char, serial_input_state[p], serial_line_buffer[p], serial_count[p]);
580580

581581
} // char_count loop
582582

Marlin/src/gcode/queue.h

+5-8
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,9 @@ class GCodeQueue {
5656
* The port that the command was received on
5757
*/
5858
#if HAS_MULTI_SERIAL
59-
static int16_t port[BUFSIZE];
59+
static serial_index_t port[BUFSIZE];
6060
#endif
61-
62-
static int16_t command_port() {
63-
return TERN0(HAS_MULTI_SERIAL, port[index_r]);
64-
}
61+
static inline serial_index_t command_port() { return TERN0(HAS_MULTI_SERIAL, port[index_r]); }
6562

6663
GCodeQueue();
6764

@@ -159,13 +156,13 @@ class GCodeQueue {
159156

160157
static void _commit_command(bool say_ok
161158
#if HAS_MULTI_SERIAL
162-
, int16_t p=-1
159+
, serial_index_t serial_ind=-1
163160
#endif
164161
);
165162

166163
static bool _enqueue(const char* cmd, bool say_ok=false
167164
#if HAS_MULTI_SERIAL
168-
, int16_t p=-1
165+
, serial_index_t serial_ind=-1
169166
#endif
170167
);
171168

@@ -181,7 +178,7 @@ class GCodeQueue {
181178
*/
182179
static bool enqueue_one(const char* cmd);
183180

184-
static void gcode_line_error(PGM_P const err, const int8_t pn);
181+
static void gcode_line_error(PGM_P const err, const serial_index_t serial_ind);
185182

186183
};
187184

Marlin/src/sd/cardreader.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ void CardReader::fileHasFinished() {
12291229
uint8_t CardReader::auto_report_sd_interval = 0;
12301230
millis_t CardReader::next_sd_report_ms;
12311231
#if HAS_MULTI_SERIAL
1232-
int8_t CardReader::auto_report_port;
1232+
serial_index_t CardReader::auto_report_port;
12331233
#endif
12341234

12351235
void CardReader::auto_report_sd_status() {

Marlin/src/sd/cardreader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class CardReader {
267267
static uint8_t auto_report_sd_interval;
268268
static millis_t next_sd_report_ms;
269269
#if HAS_MULTI_SERIAL
270-
static int8_t auto_report_port;
270+
static serial_index_t auto_report_port;
271271
#endif
272272
#endif
273273

0 commit comments

Comments
 (0)