2017-09-08 05:33:16 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 15:00:57 +01:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-08 05:33:16 +02:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-08 05:33:16 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-07-23 05:20:14 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-09-08 05:33:16 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* queue.cpp - The G-code command queue
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "queue.h"
|
2019-06-19 07:00:19 +02:00
|
|
|
GCodeQueue queue;
|
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
#include "gcode.h"
|
|
|
|
|
|
|
|
#include "../lcd/ultralcd.h"
|
|
|
|
#include "../sd/cardreader.h"
|
|
|
|
#include "../module/planner.h"
|
2018-02-09 08:50:14 +01:00
|
|
|
#include "../module/temperature.h"
|
2020-01-03 02:01:38 +01:00
|
|
|
#include "../MarlinCore.h"
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2018-10-11 04:25:43 +02:00
|
|
|
#if ENABLED(PRINTER_EVENT_LEDS)
|
|
|
|
#include "../feature/leds/printer_event_leds.h"
|
2017-09-08 21:47:47 +02:00
|
|
|
#endif
|
|
|
|
|
2019-08-06 03:41:53 +02:00
|
|
|
#if ENABLED(BINARY_FILE_TRANSFER)
|
|
|
|
#include "../feature/binary_protocol.h"
|
|
|
|
#endif
|
|
|
|
|
2019-09-11 01:52:41 +02:00
|
|
|
#if ENABLED(POWER_LOSS_RECOVERY)
|
2020-03-13 22:29:29 +01:00
|
|
|
#include "../feature/powerloss.h"
|
2019-09-11 01:52:41 +02:00
|
|
|
#endif
|
2019-08-06 03:41:53 +02:00
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
/**
|
|
|
|
* GCode line number handling. Hosts may opt to include line numbers when
|
|
|
|
* sending commands to Marlin, and lines will be checked for sequentiality.
|
|
|
|
* M110 N<int> sets the current line number.
|
|
|
|
*/
|
2020-06-02 05:19:10 +02:00
|
|
|
long GCodeQueue::last_N[NUM_SERIAL];
|
2017-09-08 05:33:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GCode Command Queue
|
|
|
|
* A simple ring buffer of BUFSIZE command strings.
|
|
|
|
*
|
|
|
|
* Commands are copied into this buffer by the command injectors
|
|
|
|
* (immediate, serial, sd card) and they are processed sequentially by
|
|
|
|
* the main loop. The gcode.process_next_command method parses the next
|
|
|
|
* command and hands off execution to individual handler functions.
|
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
uint8_t GCodeQueue::length = 0, // Count of commands in the queue
|
|
|
|
GCodeQueue::index_r = 0, // Ring buffer read position
|
|
|
|
GCodeQueue::index_w = 0; // Ring buffer write position
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-09-02 03:21:11 +02:00
|
|
|
char GCodeQueue::command_buffer[BUFSIZE][MAX_CMD_SIZE];
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
/*
|
|
|
|
* The port that the command was received on
|
|
|
|
*/
|
2020-05-12 12:50:28 +02:00
|
|
|
#if HAS_MULTI_SERIAL
|
2019-06-19 07:00:19 +02:00
|
|
|
int16_t GCodeQueue::port[BUFSIZE];
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif
|
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
/**
|
|
|
|
* Serial command injection
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Number of characters read in the current line of serial input
|
2017-11-05 15:49:38 +01:00
|
|
|
static int serial_count[NUM_SERIAL] = { 0 };
|
2017-09-08 05:33:16 +02:00
|
|
|
|
|
|
|
bool send_ok[BUFSIZE];
|
|
|
|
|
|
|
|
/**
|
2020-04-10 03:05:58 +02:00
|
|
|
* Next Injected PROGMEM Command pointer. (nullptr == empty)
|
|
|
|
* Internal commands are enqueued ahead of serial / SD commands.
|
2017-09-08 05:33:16 +02:00
|
|
|
*/
|
2020-04-10 03:05:58 +02:00
|
|
|
PGM_P GCodeQueue::injected_commands_P; // = nullptr
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Injected SRAM Commands
|
|
|
|
*/
|
|
|
|
char GCodeQueue::injected_commands[64]; // = { 0 }
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
GCodeQueue::GCodeQueue() {
|
2017-09-08 05:33:16 +02:00
|
|
|
// Send "ok" after commands by default
|
2020-03-14 05:18:16 +01:00
|
|
|
LOOP_L_N(i, COUNT(send_ok)) send_ok[i] = true;
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 07:23:57 +02:00
|
|
|
/**
|
|
|
|
* Check whether there are any commands yet to be executed
|
|
|
|
*/
|
|
|
|
bool GCodeQueue::has_commands_queued() {
|
2020-04-10 03:05:58 +02:00
|
|
|
return queue.length || injected_commands_P || injected_commands[0];
|
2019-06-29 07:23:57 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
/**
|
|
|
|
* Clear the Marlin command queue
|
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
void GCodeQueue::clear() {
|
|
|
|
index_r = index_w = length = 0;
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Once a new command is in the ring buffer, call this to commit it
|
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
void GCodeQueue::_commit_command(bool say_ok
|
2020-05-12 12:50:28 +02:00
|
|
|
#if HAS_MULTI_SERIAL
|
2019-06-19 07:00:19 +02:00
|
|
|
, int16_t p/*=-1*/
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif
|
|
|
|
) {
|
2019-06-19 07:00:19 +02:00
|
|
|
send_ok[index_w] = say_ok;
|
2020-05-12 12:50:28 +02:00
|
|
|
TERN_(HAS_MULTI_SERIAL, port[index_w] = p);
|
2020-04-22 23:35:03 +02:00
|
|
|
TERN_(POWER_LOSS_RECOVERY, recovery.commit_sdpos(index_w));
|
2019-06-19 07:00:19 +02:00
|
|
|
if (++index_w >= BUFSIZE) index_w = 0;
|
|
|
|
length++;
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy a command from RAM into the main command buffer.
|
|
|
|
* Return true if the command was successfully added.
|
|
|
|
* Return false for a full buffer, or if the 'command' is a comment.
|
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
bool GCodeQueue::_enqueue(const char* cmd, bool say_ok/*=false*/
|
2020-05-12 12:50:28 +02:00
|
|
|
#if HAS_MULTI_SERIAL
|
2019-06-19 07:00:19 +02:00
|
|
|
, int16_t pn/*=-1*/
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif
|
|
|
|
) {
|
2019-06-19 07:00:19 +02:00
|
|
|
if (*cmd == ';' || length >= BUFSIZE) return false;
|
2019-09-02 03:21:11 +02:00
|
|
|
strcpy(command_buffer[index_w], cmd);
|
2017-11-05 15:49:38 +01:00
|
|
|
_commit_command(say_ok
|
2020-05-12 12:50:28 +02:00
|
|
|
#if HAS_MULTI_SERIAL
|
2019-06-19 07:00:19 +02:00
|
|
|
, pn
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif
|
|
|
|
);
|
2017-09-08 05:33:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-13 22:29:29 +01:00
|
|
|
#define ISEOL(C) ((C) == '\n' || (C) == '\r')
|
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
/**
|
|
|
|
* Enqueue with Serial Echo
|
2019-06-08 12:23:53 +02:00
|
|
|
* Return true if the command was consumed
|
2017-09-08 05:33:16 +02:00
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
bool GCodeQueue::enqueue_one(const char* cmd) {
|
2018-09-02 17:18:59 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
//SERIAL_ECHOPGM("enqueue_one(\"");
|
2018-09-04 06:15:31 +02:00
|
|
|
//SERIAL_ECHO(cmd);
|
2018-09-17 08:06:22 +02:00
|
|
|
//SERIAL_ECHOPGM("\") \n");
|
2018-09-04 06:15:31 +02:00
|
|
|
|
2020-03-13 22:29:29 +01:00
|
|
|
if (*cmd == 0 || ISEOL(*cmd)) return true;
|
2018-09-02 17:18:59 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
if (_enqueue(cmd)) {
|
2020-02-26 10:02:03 +01:00
|
|
|
SERIAL_ECHO_MSG(STR_ENQUEUEING, cmd, "\"");
|
2017-09-08 05:33:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
/**
|
2020-04-10 03:05:58 +02:00
|
|
|
* Process the next "immediate" command from PROGMEM.
|
|
|
|
* Return 'true' if any commands were processed.
|
2019-06-19 07:00:19 +02:00
|
|
|
*/
|
2020-04-10 03:05:58 +02:00
|
|
|
bool GCodeQueue::process_injected_command_P() {
|
2019-06-19 07:00:19 +02:00
|
|
|
if (injected_commands_P == nullptr) return false;
|
2019-06-08 12:23:53 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
char c;
|
|
|
|
size_t i = 0;
|
|
|
|
while ((c = pgm_read_byte(&injected_commands_P[i])) && c != '\n') i++;
|
2019-06-08 12:23:53 +02:00
|
|
|
|
2019-07-14 17:12:48 +02:00
|
|
|
// Extract current command and move pointer to next command
|
|
|
|
char cmd[i + 1];
|
|
|
|
memcpy_P(cmd, injected_commands_P, i);
|
|
|
|
cmd[i] = '\0';
|
|
|
|
injected_commands_P = c ? injected_commands_P + i + 1 : nullptr;
|
|
|
|
|
|
|
|
// Execute command if non-blank
|
|
|
|
if (i) {
|
2019-06-29 07:23:57 +02:00
|
|
|
parser.parse(cmd);
|
|
|
|
gcode.process_parsed_command();
|
|
|
|
}
|
2019-06-19 07:00:19 +02:00
|
|
|
return true;
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-10 03:05:58 +02:00
|
|
|
* Process the next "immediate" command from SRAM.
|
|
|
|
* Return 'true' if any commands were processed.
|
2017-09-08 05:33:16 +02:00
|
|
|
*/
|
2020-04-10 03:05:58 +02:00
|
|
|
bool GCodeQueue::process_injected_command() {
|
|
|
|
if (injected_commands[0] == '\0') return false;
|
|
|
|
|
|
|
|
char c;
|
|
|
|
size_t i = 0;
|
|
|
|
while ((c = injected_commands[i]) && c != '\n') i++;
|
|
|
|
|
|
|
|
// Execute a non-blank command
|
|
|
|
if (i) {
|
|
|
|
injected_commands[i] = '\0';
|
|
|
|
parser.parse(injected_commands);
|
|
|
|
gcode.process_parsed_command();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the next command into place
|
2020-05-12 08:45:28 +02:00
|
|
|
for (
|
|
|
|
uint8_t d = 0, s = i + !!c; // dst, src
|
|
|
|
(injected_commands[d] = injected_commands[s]); // copy, exit if 0
|
|
|
|
d++, s++ // next dst, src
|
|
|
|
);
|
2020-04-28 15:30:52 +02:00
|
|
|
|
2020-04-10 03:05:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
/**
|
|
|
|
* Enqueue and return only when commands are actually enqueued.
|
|
|
|
* Never call this from a G-code handler!
|
|
|
|
*/
|
2019-06-29 07:23:57 +02:00
|
|
|
void GCodeQueue::enqueue_one_now(const char* cmd) { while (!enqueue_one(cmd)) idle(); }
|
2019-06-19 07:00:19 +02:00
|
|
|
|
2020-03-11 00:04:54 +01:00
|
|
|
/**
|
|
|
|
* Attempt to enqueue a single G-code command
|
|
|
|
* and return 'true' if successful.
|
|
|
|
*/
|
|
|
|
bool GCodeQueue::enqueue_one_P(PGM_P const pgcode) {
|
|
|
|
size_t i = 0;
|
|
|
|
PGM_P p = pgcode;
|
|
|
|
char c;
|
|
|
|
while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
|
|
|
|
char cmd[i + 1];
|
|
|
|
memcpy_P(cmd, p, i);
|
|
|
|
cmd[i] = '\0';
|
|
|
|
return _enqueue(cmd);
|
|
|
|
}
|
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
/**
|
|
|
|
* Enqueue from program memory and return only when commands are actually enqueued
|
|
|
|
* Never call this from a G-code handler!
|
|
|
|
*/
|
|
|
|
void GCodeQueue::enqueue_now_P(PGM_P const pgcode) {
|
|
|
|
size_t i = 0;
|
|
|
|
PGM_P p = pgcode;
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
2019-06-24 05:21:33 +02:00
|
|
|
while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
|
2019-06-19 07:00:19 +02:00
|
|
|
char cmd[i + 1];
|
|
|
|
memcpy_P(cmd, p, i);
|
|
|
|
cmd[i] = '\0';
|
|
|
|
enqueue_one_now(cmd);
|
|
|
|
if (!c) break;
|
|
|
|
p += i + 1;
|
2018-01-03 04:00:06 +01:00
|
|
|
}
|
2019-06-19 07:00:19 +02:00
|
|
|
}
|
2018-01-03 04:00:06 +01:00
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
/**
|
|
|
|
* Send an "ok" message to the host, indicating
|
|
|
|
* that a command was successfully processed.
|
|
|
|
*
|
|
|
|
* If ADVANCED_OK is enabled also include:
|
|
|
|
* N<int> Line number of the command, if any
|
|
|
|
* P<int> Planner space remaining
|
|
|
|
* B<int> Block queue space remaining
|
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
void GCodeQueue::ok_to_send() {
|
2020-05-12 12:50:28 +02:00
|
|
|
#if HAS_MULTI_SERIAL
|
2020-06-02 05:19:10 +02:00
|
|
|
const int16_t pn = command_port();
|
2019-06-22 02:04:30 +02:00
|
|
|
if (pn < 0) return;
|
2020-01-27 05:46:26 +01:00
|
|
|
PORT_REDIRECT(pn); // Reply to the serial port that sent the command
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif
|
2019-06-19 07:00:19 +02:00
|
|
|
if (!send_ok[index_r]) return;
|
2020-02-26 10:02:03 +01:00
|
|
|
SERIAL_ECHOPGM(STR_OK);
|
2017-09-08 05:33:16 +02:00
|
|
|
#if ENABLED(ADVANCED_OK)
|
2019-09-02 03:21:11 +02:00
|
|
|
char* p = command_buffer[index_r];
|
2017-09-08 05:33:16 +02:00
|
|
|
if (*p == 'N') {
|
2019-02-24 05:53:01 +01:00
|
|
|
SERIAL_ECHO(' ');
|
|
|
|
SERIAL_ECHO(*p++);
|
2017-09-08 05:33:16 +02:00
|
|
|
while (NUMERIC_SIGNED(*p))
|
2019-02-24 05:53:01 +01:00
|
|
|
SERIAL_ECHO(*p++);
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
2020-06-28 06:27:28 +02:00
|
|
|
SERIAL_ECHOPAIR_P(SP_P_STR, int(planner.moves_free()),
|
|
|
|
SP_B_STR, int(BUFSIZE - length));
|
2017-09-08 05:33:16 +02:00
|
|
|
#endif
|
2019-02-24 05:53:01 +01:00
|
|
|
SERIAL_EOL();
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a "Resend: nnn" message to the host to
|
|
|
|
* indicate that a command needs to be re-sent.
|
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
void GCodeQueue::flush_and_request_resend() {
|
2020-06-02 05:19:10 +02:00
|
|
|
const int16_t pn = command_port();
|
2020-05-12 12:50:28 +02:00
|
|
|
#if HAS_MULTI_SERIAL
|
2020-01-27 05:46:26 +01:00
|
|
|
if (pn < 0) return;
|
|
|
|
PORT_REDIRECT(pn); // Reply to the serial port that sent the command
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif
|
2019-02-24 05:53:01 +01:00
|
|
|
SERIAL_FLUSH();
|
2020-02-26 10:02:03 +01:00
|
|
|
SERIAL_ECHOPGM(STR_RESEND);
|
2020-05-12 12:50:28 +02:00
|
|
|
SERIAL_ECHOLN(last_N[pn] + 1);
|
2018-03-11 13:36:00 +01:00
|
|
|
ok_to_send();
|
2017-11-05 15:49:38 +01:00
|
|
|
}
|
|
|
|
|
2019-02-04 13:03:49 +01:00
|
|
|
inline bool serial_data_available() {
|
2020-05-12 12:50:28 +02:00
|
|
|
return MYSERIAL0.available() || TERN0(HAS_MULTI_SERIAL, MYSERIAL1.available());
|
2017-11-05 15:49:38 +01:00
|
|
|
}
|
|
|
|
|
2019-02-04 13:03:49 +01:00
|
|
|
inline int read_serial(const uint8_t index) {
|
2017-11-05 15:49:38 +01:00
|
|
|
switch (index) {
|
|
|
|
case 0: return MYSERIAL0.read();
|
2020-05-12 12:50:28 +02:00
|
|
|
#if HAS_MULTI_SERIAL
|
2017-11-05 15:49:38 +01:00
|
|
|
case 1: return MYSERIAL1.read();
|
|
|
|
#endif
|
|
|
|
default: return -1;
|
|
|
|
}
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
|
2020-01-27 05:46:26 +01:00
|
|
|
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
|
2019-06-19 07:00:19 +02:00
|
|
|
SERIAL_ERROR_START();
|
|
|
|
serialprintPGM(err);
|
2020-06-02 05:19:10 +02:00
|
|
|
SERIAL_ECHOLN(last_N[pn]);
|
2020-01-27 05:46:26 +01:00
|
|
|
while (read_serial(pn) != -1); // Clear out the RX buffer
|
2019-06-19 07:00:19 +02:00
|
|
|
flush_and_request_resend();
|
2020-01-27 05:46:26 +01:00
|
|
|
serial_count[pn] = 0;
|
2019-06-19 07:00:19 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 06:46:36 +02:00
|
|
|
FORCE_INLINE bool is_M29(const char * const cmd) { // matches "M29" & "M29 ", but not "M290", etc
|
|
|
|
const char * const m29 = strstr_P(cmd, PSTR("M29"));
|
|
|
|
return m29 && !NUMERIC(m29[3]);
|
2019-02-04 14:17:40 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 21:52:15 +01:00
|
|
|
#define PS_NORMAL 0
|
|
|
|
#define PS_EOL 1
|
|
|
|
#define PS_QUOTED 2
|
|
|
|
#define PS_PAREN 3
|
|
|
|
#define PS_ESC 4
|
|
|
|
|
|
|
|
inline void process_stream_char(const char c, uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
|
|
|
|
|
|
|
|
if (sis == PS_EOL) return; // EOL comment or overflow
|
|
|
|
|
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
else if (sis == PS_PAREN) { // Inline comment
|
|
|
|
if (c == ')') sis = PS_NORMAL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
else if (sis >= PS_ESC) // End escaped char
|
|
|
|
sis -= PS_ESC;
|
|
|
|
|
|
|
|
else if (c == '\\') { // Start escaped char
|
|
|
|
sis += PS_ESC;
|
|
|
|
if (sis == PS_ESC) return; // Keep if quoting
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(GCODE_QUOTED_STRINGS)
|
|
|
|
|
|
|
|
else if (sis == PS_QUOTED) {
|
|
|
|
if (c == '"') sis = PS_NORMAL; // End quoted string
|
|
|
|
}
|
|
|
|
else if (c == '"') // Start quoted string
|
|
|
|
sis = PS_QUOTED;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
else if (c == ';') { // Start end-of-line comment
|
|
|
|
sis = PS_EOL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
else if (c == '(') { // Start inline comment
|
|
|
|
sis = PS_PAREN;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-09 00:28:25 +02:00
|
|
|
// Backspace erases previous characters
|
|
|
|
if (c == 0x08) {
|
|
|
|
if (ind) buff[--ind] = '\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buff[ind++] = c;
|
|
|
|
if (ind >= MAX_CMD_SIZE - 1)
|
|
|
|
sis = PS_EOL; // Skip the rest on overflow
|
|
|
|
}
|
2020-02-10 21:52:15 +01:00
|
|
|
}
|
|
|
|
|
2020-03-02 04:08:01 +01:00
|
|
|
/**
|
|
|
|
* Handle a line being completed. For an empty line
|
|
|
|
* keep sensor readings going and watchdog alive.
|
|
|
|
*/
|
2020-02-10 21:52:15 +01:00
|
|
|
inline bool process_line_done(uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
|
|
|
|
sis = PS_NORMAL;
|
|
|
|
buff[ind] = 0;
|
2020-02-21 03:22:27 +01:00
|
|
|
if (ind) { ind = 0; return false; }
|
|
|
|
thermalManager.manage_heater();
|
|
|
|
return true;
|
2020-02-10 21:52:15 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
/**
|
|
|
|
* Get all commands waiting on the serial port and queue them.
|
|
|
|
* Exit when the buffer is full or when no more characters are
|
|
|
|
* left on the serial port.
|
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
void GCodeQueue::get_serial_commands() {
|
2017-11-05 15:49:38 +01:00
|
|
|
static char serial_line_buffer[NUM_SERIAL][MAX_CMD_SIZE];
|
2020-02-10 21:52:15 +01:00
|
|
|
|
2020-02-14 01:30:48 +01:00
|
|
|
static uint8_t serial_input_state[NUM_SERIAL] = { PS_NORMAL };
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-02-28 02:57:48 +01:00
|
|
|
#if ENABLED(BINARY_FILE_TRANSFER)
|
2019-08-06 03:41:53 +02:00
|
|
|
if (card.flag.binary_mode) {
|
2018-10-31 01:44:12 +01:00
|
|
|
/**
|
|
|
|
* For binary stream file transfer, use serial_line_buffer as the working
|
|
|
|
* receive buffer (which limits the packet size to MAX_CMD_SIZE).
|
|
|
|
* The receive buffer also limits the packet size for reliable transmission.
|
|
|
|
*/
|
2019-08-06 03:41:53 +02:00
|
|
|
binaryStream[card.transfer_port_index].receive(serial_line_buffer[card.transfer_port_index]);
|
2018-10-31 01:44:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
// If the command buffer is empty for too long,
|
|
|
|
// send "wait" to indicate Marlin is still waiting.
|
2018-02-09 08:52:28 +01:00
|
|
|
#if NO_TIMEOUTS > 0
|
2017-09-08 05:33:16 +02:00
|
|
|
static millis_t last_command_time = 0;
|
|
|
|
const millis_t ms = millis();
|
2019-06-19 07:00:19 +02:00
|
|
|
if (length == 0 && !serial_data_available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
|
2020-02-26 10:02:03 +01:00
|
|
|
SERIAL_ECHOLNPGM(STR_WAIT);
|
2017-09-08 05:33:16 +02:00
|
|
|
last_command_time = ms;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loop while serial characters are incoming and the queue is not full
|
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
while (length < BUFSIZE && serial_data_available()) {
|
2020-03-14 05:18:16 +01:00
|
|
|
LOOP_L_N(i, NUM_SERIAL) {
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-02-10 21:52:15 +01:00
|
|
|
const int c = read_serial(i);
|
|
|
|
if (c < 0) continue;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-02-10 21:52:15 +01:00
|
|
|
const char serial_char = c;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-03-13 22:29:29 +01:00
|
|
|
if (ISEOL(serial_char)) {
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-02-21 03:22:27 +01:00
|
|
|
// Reset our state, continue if the line was empty
|
|
|
|
if (process_line_done(serial_input_state[i], serial_line_buffer[i], serial_count[i]))
|
|
|
|
continue;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
char* command = serial_line_buffer[i];
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-02-21 03:22:27 +01:00
|
|
|
while (*command == ' ') command++; // Skip leading spaces
|
2019-05-09 18:45:55 +02:00
|
|
|
char *npos = (*command == 'N') ? command : nullptr; // Require the N parameter to start the line
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
if (npos) {
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-05-09 18:45:55 +02:00
|
|
|
bool M110 = strstr_P(command, PSTR("M110")) != nullptr;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
if (M110) {
|
|
|
|
char* n2pos = strchr(command + 4, 'N');
|
|
|
|
if (n2pos) npos = n2pos;
|
|
|
|
}
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-06-02 05:19:10 +02:00
|
|
|
const long gcode_N = strtol(npos + 1, nullptr, 10);
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-06-02 05:19:10 +02:00
|
|
|
if (gcode_N != last_N[i] + 1 && !M110)
|
2020-02-26 10:02:03 +01:00
|
|
|
return gcode_line_error(PSTR(STR_ERR_LINE_NO), i);
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
char *apos = strrchr(command, '*');
|
|
|
|
if (apos) {
|
|
|
|
uint8_t checksum = 0, count = uint8_t(apos - command);
|
|
|
|
while (count) checksum ^= command[--count];
|
2019-05-09 18:45:55 +02:00
|
|
|
if (strtol(apos + 1, nullptr, 10) != checksum)
|
2020-02-26 10:02:03 +01:00
|
|
|
return gcode_line_error(PSTR(STR_ERR_CHECKSUM_MISMATCH), i);
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
2018-04-08 04:25:39 +02:00
|
|
|
else
|
2020-02-26 10:02:03 +01:00
|
|
|
return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i);
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-06-02 05:19:10 +02:00
|
|
|
last_N[i] = gcode_N;
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
2018-03-28 20:45:47 +02:00
|
|
|
#if ENABLED(SDSUPPORT)
|
2019-02-06 11:59:22 +01:00
|
|
|
// Pronterface "M29" and "M29 " has no line number
|
2019-02-04 14:17:40 +01:00
|
|
|
else if (card.flag.saving && !is_M29(command))
|
2020-02-26 10:02:03 +01:00
|
|
|
return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i);
|
2018-03-28 20:45:47 +02:00
|
|
|
#endif
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-02-10 21:52:15 +01:00
|
|
|
//
|
|
|
|
// Movement commands give an alert when the machine is stopped
|
|
|
|
//
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
if (IsStopped()) {
|
|
|
|
char* gpos = strchr(command, 'G');
|
|
|
|
if (gpos) {
|
2019-05-09 18:45:55 +02:00
|
|
|
switch (strtol(gpos + 1, nullptr, 10)) {
|
2020-02-10 21:52:15 +01:00
|
|
|
case 0: case 1:
|
2018-09-27 22:40:19 +02:00
|
|
|
#if ENABLED(ARC_SUPPORT)
|
2020-02-10 21:52:15 +01:00
|
|
|
case 2: case 3:
|
2018-09-27 22:40:19 +02:00
|
|
|
#endif
|
|
|
|
#if ENABLED(BEZIER_CURVE_SUPPORT)
|
|
|
|
case 5:
|
|
|
|
#endif
|
2020-02-20 09:22:08 +01:00
|
|
|
PORT_REDIRECT(i); // Reply to the serial port that sent the command
|
2020-02-26 10:02:03 +01:00
|
|
|
SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
|
2017-11-05 15:49:38 +01:00
|
|
|
LCD_MESSAGEPGM(MSG_STOPPED);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
#if DISABLED(EMERGENCY_PARSER)
|
2018-02-08 08:31:19 +01:00
|
|
|
// Process critical commands early
|
2020-05-23 23:12:54 +02:00
|
|
|
if (strcmp_P(command, PSTR("M108")) == 0) {
|
2017-11-05 15:49:38 +01:00
|
|
|
wait_for_heatup = false;
|
2020-04-22 23:35:03 +02:00
|
|
|
TERN_(HAS_LCD_MENU, wait_for_user = false);
|
2017-11-05 15:49:38 +01:00
|
|
|
}
|
2020-05-23 23:12:54 +02:00
|
|
|
if (strcmp_P(command, PSTR("M112")) == 0) kill(M112_KILL_STR, nullptr, true);
|
|
|
|
if (strcmp_P(command, PSTR("M410")) == 0) quickstop_stepper();
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
|
|
|
|
last_command_time = ms;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Add the command to the queue
|
2019-06-19 07:00:19 +02:00
|
|
|
_enqueue(serial_line_buffer[i], true
|
2020-05-12 12:50:28 +02:00
|
|
|
#if HAS_MULTI_SERIAL
|
2017-11-05 15:49:38 +01:00
|
|
|
, i
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
2020-02-10 21:52:15 +01:00
|
|
|
else
|
|
|
|
process_stream_char(serial_char, serial_input_state[i], serial_line_buffer[i], serial_count[i]);
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
} // for NUM_SERIAL
|
2017-09-08 05:33:16 +02:00
|
|
|
} // queue has space, serial has data
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
|
|
|
|
/**
|
2020-02-26 05:18:14 +01:00
|
|
|
* Get lines from the SD Card until the command buffer is full
|
|
|
|
* or until the end of the file is reached. Because this method
|
|
|
|
* always receives complete command-lines, they can go directly
|
|
|
|
* into the main command queue.
|
2017-09-08 05:33:16 +02:00
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
inline void GCodeQueue::get_sdcard_commands() {
|
2020-02-10 21:52:15 +01:00
|
|
|
static uint8_t sd_input_state = PS_NORMAL;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2018-10-19 20:52:44 +02:00
|
|
|
if (!IS_SD_PRINTING()) return;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-02-10 21:52:15 +01:00
|
|
|
int sd_count = 0;
|
2017-09-08 05:33:16 +02:00
|
|
|
bool card_eof = card.eof();
|
2020-02-01 13:49:56 +01:00
|
|
|
while (length < BUFSIZE && !card_eof) {
|
2017-09-08 05:33:16 +02:00
|
|
|
const int16_t n = card.get();
|
|
|
|
card_eof = card.eof();
|
2020-02-26 10:02:03 +01:00
|
|
|
if (n < 0 && !card_eof) { SERIAL_ERROR_MSG(STR_SD_ERR_READ); continue; }
|
2020-02-26 05:18:14 +01:00
|
|
|
|
2020-02-10 21:52:15 +01:00
|
|
|
const char sd_char = (char)n;
|
2020-03-13 22:29:29 +01:00
|
|
|
const bool is_eol = ISEOL(sd_char);
|
2020-02-26 05:18:14 +01:00
|
|
|
if (is_eol || card_eof) {
|
2020-02-23 03:45:59 +01:00
|
|
|
|
|
|
|
// Reset stream state, terminate the buffer, and commit a non-empty command
|
2020-02-26 05:18:14 +01:00
|
|
|
if (!is_eol && sd_count) ++sd_count; // End of file with no newline
|
2020-02-23 03:45:59 +01:00
|
|
|
if (!process_line_done(sd_input_state, command_buffer[index_w], sd_count)) {
|
2020-02-26 05:18:14 +01:00
|
|
|
_commit_command(false);
|
2020-02-23 03:45:59 +01:00
|
|
|
#if ENABLED(POWER_LOSS_RECOVERY)
|
2020-02-26 05:18:14 +01:00
|
|
|
recovery.cmd_sdpos = card.getIndex(); // Prime for the NEXT _commit_command
|
2020-02-23 03:45:59 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:18:14 +01:00
|
|
|
if (card_eof) card.fileHasFinished(); // Handle end of file reached
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
2020-02-10 21:52:15 +01:00
|
|
|
else
|
|
|
|
process_stream_char(sd_char, sd_input_state, command_buffer[index_w], sd_count);
|
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SDSUPPORT
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add to the circular command queue the next command from:
|
2020-04-10 03:05:58 +02:00
|
|
|
* - The command-injection queues (injected_commands_P, injected_commands)
|
2017-09-08 05:33:16 +02:00
|
|
|
* - The active serial input (usually USB)
|
|
|
|
* - The SD card file being actively printed
|
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
void GCodeQueue::get_available_commands() {
|
2017-09-08 05:33:16 +02:00
|
|
|
|
|
|
|
get_serial_commands();
|
|
|
|
|
2020-04-22 23:35:03 +02:00
|
|
|
TERN_(SDSUPPORT, get_sdcard_commands());
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next command in the queue, optionally log it to SD, then dispatch it
|
|
|
|
*/
|
2019-06-19 07:00:19 +02:00
|
|
|
void GCodeQueue::advance() {
|
|
|
|
|
|
|
|
// Process immediate commands
|
2020-04-10 03:05:58 +02:00
|
|
|
if (process_injected_command_P() || process_injected_command()) return;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
// Return if the G-code buffer is empty
|
|
|
|
if (!length) return;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
|
2018-11-17 05:39:16 +01:00
|
|
|
if (card.flag.saving) {
|
2019-09-02 03:21:11 +02:00
|
|
|
char* command = command_buffer[index_r];
|
2019-02-04 14:17:40 +01:00
|
|
|
if (is_M29(command)) {
|
2017-09-08 05:33:16 +02:00
|
|
|
// M29 closes the file
|
|
|
|
card.closefile();
|
2020-02-26 10:02:03 +01:00
|
|
|
SERIAL_ECHOLNPGM(STR_FILE_SAVED);
|
2017-10-02 09:47:30 +02:00
|
|
|
|
2018-03-11 05:46:32 +01:00
|
|
|
#if !defined(__AVR__) || !defined(USBCON)
|
2018-01-10 01:42:55 +01:00
|
|
|
#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
2018-10-03 07:47:27 +02:00
|
|
|
SERIAL_ECHOLNPAIR("Dropped bytes: ", MYSERIAL0.dropped());
|
2018-01-10 01:42:55 +01:00
|
|
|
#endif
|
2017-10-02 09:47:30 +02:00
|
|
|
|
2018-01-10 01:42:55 +01:00
|
|
|
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
|
2018-10-03 07:47:27 +02:00
|
|
|
SERIAL_ECHOLNPAIR("Max RX Queue Size: ", MYSERIAL0.rxMaxEnqueued());
|
2018-01-10 01:42:55 +01:00
|
|
|
#endif
|
2018-03-11 05:46:32 +01:00
|
|
|
#endif // !defined(__AVR__) || !defined(USBCON)
|
2017-10-02 09:47:30 +02:00
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
ok_to_send();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Write the string from the read buffer to SD
|
|
|
|
card.write_command(command);
|
2018-11-17 05:39:16 +01:00
|
|
|
if (card.flag.logging)
|
2017-09-08 05:33:16 +02:00
|
|
|
gcode.process_next_command(); // The card is saving because it's logging
|
|
|
|
else
|
|
|
|
ok_to_send();
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 08:47:06 +01:00
|
|
|
else
|
2017-09-08 05:33:16 +02:00
|
|
|
gcode.process_next_command();
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
gcode.process_next_command();
|
|
|
|
|
|
|
|
#endif // SDSUPPORT
|
|
|
|
|
|
|
|
// The queue may be reset by a command handler or by code invoked by idle() within a handler
|
2020-02-26 05:18:14 +01:00
|
|
|
--length;
|
|
|
|
if (++index_r >= BUFSIZE) index_r = 0;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
|
|
|
}
|