2017-09-08 05:33:16 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2019-02-12 22:06:53 +01:00
|
|
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-08 05:33:16 +02:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* queue.cpp - The G-code command queue
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "queue.h"
|
|
|
|
#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"
|
2017-09-08 05:33:16 +02:00
|
|
|
#include "../Marlin.h"
|
|
|
|
|
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
|
|
|
|
|
2018-04-22 02:41:26 +02:00
|
|
|
#if ENABLED(POWER_LOSS_RECOVERY)
|
|
|
|
#include "../feature/power_loss_recovery.h"
|
|
|
|
#endif
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
uint8_t commands_in_queue = 0, // Count of commands in the queue
|
|
|
|
cmd_queue_index_r = 0, // Ring buffer read position
|
|
|
|
cmd_queue_index_w = 0; // Ring buffer write position
|
|
|
|
|
|
|
|
char command_queue[BUFSIZE][MAX_CMD_SIZE];
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
/*
|
|
|
|
* The port that the command was received on
|
|
|
|
*/
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
int16_t command_queue_port[BUFSIZE];
|
|
|
|
#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];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Next Injected Command pointer. NULL if no commands are being injected.
|
|
|
|
* Used by Marlin internally to ensure that commands initiated from within
|
|
|
|
* are enqueued ahead of any pending serial or sd card commands.
|
|
|
|
*/
|
2018-10-01 06:44:33 +02:00
|
|
|
static PGM_P injected_commands_P = NULL;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
|
|
|
void queue_setup() {
|
|
|
|
// Send "ok" after commands by default
|
|
|
|
for (uint8_t i = 0; i < COUNT(send_ok); i++) send_ok[i] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the Marlin command queue
|
|
|
|
*/
|
|
|
|
void clear_command_queue() {
|
2018-01-19 02:57:18 +01:00
|
|
|
cmd_queue_index_r = cmd_queue_index_w = commands_in_queue = 0;
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Once a new command is in the ring buffer, call this to commit it
|
|
|
|
*/
|
2017-11-05 15:49:38 +01:00
|
|
|
inline void _commit_command(bool say_ok
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
, int16_t port = -1
|
|
|
|
#endif
|
|
|
|
) {
|
2017-09-08 05:33:16 +02:00
|
|
|
send_ok[cmd_queue_index_w] = say_ok;
|
2017-11-05 15:49:38 +01:00
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
command_queue_port[cmd_queue_index_w] = port;
|
|
|
|
#endif
|
2017-09-08 05:33:16 +02:00
|
|
|
if (++cmd_queue_index_w >= BUFSIZE) cmd_queue_index_w = 0;
|
|
|
|
commands_in_queue++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-04-22 02:41:26 +02:00
|
|
|
inline bool _enqueuecommand(const char* cmd, bool say_ok=false
|
2017-11-05 15:49:38 +01:00
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
, int16_t port = -1
|
|
|
|
#endif
|
|
|
|
) {
|
2017-09-08 05:33:16 +02:00
|
|
|
if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false;
|
|
|
|
strcpy(command_queue[cmd_queue_index_w], cmd);
|
2017-11-05 15:49:38 +01:00
|
|
|
_commit_command(say_ok
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
, port
|
|
|
|
#endif
|
|
|
|
);
|
2017-09-08 05:33:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue with Serial Echo
|
|
|
|
*/
|
2018-04-22 02:41:26 +02:00
|
|
|
bool enqueue_and_echo_command(const char* cmd) {
|
2018-09-02 17:18:59 +02:00
|
|
|
|
2018-09-17 08:06:22 +02:00
|
|
|
//SERIAL_ECHOPGM("enqueue_and_echo_command(\"");
|
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
|
|
|
|
2018-09-05 23:57:08 +02:00
|
|
|
if (*cmd == 0 || *cmd == '\n' || *cmd == '\r') {
|
2019-02-03 06:32:48 +01:00
|
|
|
//SERIAL_ECHOLNPGM("Null command found... Did not queue!");
|
2018-09-02 17:18:59 +02:00
|
|
|
return true;
|
2018-09-04 06:15:31 +02:00
|
|
|
}
|
2018-09-02 17:18:59 +02:00
|
|
|
|
2018-04-22 02:41:26 +02:00
|
|
|
if (_enqueuecommand(cmd)) {
|
2017-09-08 05:33:16 +02:00
|
|
|
SERIAL_ECHO_START();
|
2019-03-05 13:46:19 +01:00
|
|
|
SERIAL_ECHOLNPAIR(MSG_ENQUEUEING, cmd, "\"");
|
2017-09-08 05:33:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inject the next "immediate" command, when possible, onto the front of the queue.
|
|
|
|
* Return true if any immediate commands remain to inject.
|
|
|
|
*/
|
|
|
|
static bool drain_injected_commands_P() {
|
|
|
|
if (injected_commands_P != NULL) {
|
|
|
|
size_t i = 0;
|
2018-09-02 17:18:59 +02:00
|
|
|
char c, cmd[60];
|
2017-09-08 05:33:16 +02:00
|
|
|
strncpy_P(cmd, injected_commands_P, sizeof(cmd) - 1);
|
|
|
|
cmd[sizeof(cmd) - 1] = '\0';
|
|
|
|
while ((c = cmd[i]) && c != '\n') i++; // find the end of this gcode command
|
|
|
|
cmd[i] = '\0';
|
|
|
|
if (enqueue_and_echo_command(cmd)) // success?
|
|
|
|
injected_commands_P = c ? injected_commands_P + i + 1 : NULL; // next command or done
|
|
|
|
}
|
|
|
|
return (injected_commands_P != NULL); // return whether any more remain
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record one or many commands to run from program memory.
|
|
|
|
* Aborts the current queue, if any.
|
|
|
|
* Note: drain_injected_commands_P() must be called repeatedly to drain the commands afterwards
|
|
|
|
*/
|
2018-10-01 06:44:33 +02:00
|
|
|
void enqueue_and_echo_commands_P(PGM_P const pgcode) {
|
2017-09-08 05:33:16 +02:00
|
|
|
injected_commands_P = pgcode;
|
2018-01-03 04:00:06 +01:00
|
|
|
(void)drain_injected_commands_P(); // first command executed asap (when possible)
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-03 04:00:06 +01:00
|
|
|
#if HAS_QUEUE_NOW
|
|
|
|
/**
|
2018-11-09 12:25:08 +01:00
|
|
|
* Enqueue and return only when commands are actually enqueued.
|
|
|
|
* Never call this from a G-code handler!
|
2018-01-03 04:00:06 +01:00
|
|
|
*/
|
2018-04-20 01:11:03 +02:00
|
|
|
void enqueue_and_echo_command_now(const char* cmd) {
|
|
|
|
while (!enqueue_and_echo_command(cmd)) idle();
|
2018-01-03 04:00:06 +01:00
|
|
|
}
|
|
|
|
#if HAS_LCD_QUEUE_NOW
|
|
|
|
/**
|
|
|
|
* Enqueue from program memory and return only when commands are actually enqueued
|
2018-11-09 12:25:08 +01:00
|
|
|
* Never call this from a G-code handler!
|
2018-01-03 04:00:06 +01:00
|
|
|
*/
|
2018-10-01 06:44:33 +02:00
|
|
|
void enqueue_and_echo_commands_now_P(PGM_P const pgcode) {
|
2018-01-03 04:00:06 +01:00
|
|
|
enqueue_and_echo_commands_P(pgcode);
|
|
|
|
while (drain_injected_commands_P()) idle();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
void ok_to_send() {
|
2017-11-05 15:49:38 +01:00
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
const int16_t port = command_queue_port[cmd_queue_index_r];
|
|
|
|
if (port < 0) return;
|
2019-02-24 05:53:01 +01:00
|
|
|
PORT_REDIRECT(port);
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif
|
2017-09-08 05:33:16 +02:00
|
|
|
if (!send_ok[cmd_queue_index_r]) return;
|
2019-02-24 05:53:01 +01:00
|
|
|
SERIAL_ECHOPGM(MSG_OK);
|
2017-09-08 05:33:16 +02:00
|
|
|
#if ENABLED(ADVANCED_OK)
|
|
|
|
char* p = command_queue[cmd_queue_index_r];
|
|
|
|
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
|
|
|
}
|
2019-02-24 05:53:01 +01:00
|
|
|
SERIAL_ECHOPGM(" P"); SERIAL_ECHO(int(BLOCK_BUFFER_SIZE - planner.movesplanned() - 1));
|
|
|
|
SERIAL_ECHOPGM(" B"); SERIAL_ECHO(BUFSIZE - commands_in_queue);
|
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.
|
|
|
|
*/
|
|
|
|
void flush_and_request_resend() {
|
2017-11-05 15:49:38 +01:00
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
const int16_t port = command_queue_port[cmd_queue_index_r];
|
|
|
|
if (port < 0) return;
|
2019-02-24 05:53:01 +01:00
|
|
|
PORT_REDIRECT(port);
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif
|
2019-02-24 05:53:01 +01:00
|
|
|
SERIAL_FLUSH();
|
|
|
|
SERIAL_ECHOPGM(MSG_RESEND);
|
|
|
|
SERIAL_ECHOLN(gcode_LastN + 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() {
|
2018-10-31 01:44:12 +01:00
|
|
|
return false
|
|
|
|
|| MYSERIAL0.available()
|
2017-11-05 15:49:38 +01:00
|
|
|
#if NUM_SERIAL > 1
|
2018-10-31 01:44:12 +01:00
|
|
|
|| MYSERIAL1.available()
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif
|
2018-10-31 01:44:12 +01:00
|
|
|
;
|
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();
|
2018-01-15 09:29:23 +01:00
|
|
|
#if NUM_SERIAL > 1
|
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
|
|
|
}
|
|
|
|
|
2019-02-24 05:53:01 +01:00
|
|
|
void gcode_line_error(PGM_P const err, const int8_t port) {
|
|
|
|
PORT_REDIRECT(port);
|
|
|
|
SERIAL_ERROR_START();
|
|
|
|
serialprintPGM(err);
|
|
|
|
SERIAL_ECHOLN(gcode_LastN);
|
2019-02-04 13:03:49 +01:00
|
|
|
while (read_serial(port) != -1); // clear out the RX buffer
|
|
|
|
flush_and_request_resend();
|
|
|
|
serial_count[port] = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-28 02:57:48 +01:00
|
|
|
#if ENABLED(BINARY_FILE_TRANSFER)
|
2018-10-31 01:44:12 +01:00
|
|
|
|
2019-02-04 13:03:49 +01:00
|
|
|
inline bool serial_data_available(const uint8_t index) {
|
2018-10-31 01:44:12 +01:00
|
|
|
switch (index) {
|
|
|
|
case 0: return MYSERIAL0.available();
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
case 1: return MYSERIAL1.available();
|
|
|
|
#endif
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BinaryStream {
|
|
|
|
public:
|
|
|
|
enum class StreamState : uint8_t {
|
|
|
|
STREAM_RESET,
|
|
|
|
PACKET_RESET,
|
|
|
|
STREAM_HEADER,
|
|
|
|
PACKET_HEADER,
|
|
|
|
PACKET_DATA,
|
|
|
|
PACKET_VALIDATE,
|
|
|
|
PACKET_RESEND,
|
|
|
|
PACKET_FLUSHRX,
|
|
|
|
PACKET_TIMEOUT,
|
|
|
|
STREAM_COMPLETE,
|
|
|
|
STREAM_FAILED,
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
|
|
|
struct StreamHeader {
|
|
|
|
uint16_t token;
|
|
|
|
uint32_t filesize;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
uint8_t stream_header_bytes[sizeof(StreamHeader)];
|
|
|
|
StreamHeader stream_header;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Packet {
|
|
|
|
struct Header {
|
|
|
|
uint32_t id;
|
|
|
|
uint16_t size, checksum;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
uint8_t header_bytes[sizeof(Header)];
|
|
|
|
Header header;
|
|
|
|
};
|
|
|
|
uint32_t bytes_received;
|
|
|
|
uint16_t checksum;
|
|
|
|
millis_t timeout;
|
|
|
|
} packet{};
|
|
|
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
void packet_reset() {
|
|
|
|
packet.header.id = 0;
|
|
|
|
packet.header.size = 0;
|
|
|
|
packet.header.checksum = 0;
|
|
|
|
packet.bytes_received = 0;
|
|
|
|
packet.checksum = 0x53A2;
|
|
|
|
packet.timeout = millis() + STREAM_MAX_WAIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stream_reset() {
|
|
|
|
packets_received = 0;
|
|
|
|
bytes_received = 0;
|
|
|
|
packet_retries = 0;
|
|
|
|
buffer_next_index = 0;
|
|
|
|
stream_header.token = 0;
|
|
|
|
stream_header.filesize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t checksum(uint32_t seed, uint8_t value) {
|
|
|
|
return ((seed ^ value) ^ (seed << 8)) & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the next byte from the data stream keeping track of
|
|
|
|
// whether the stream times out from data starvation
|
|
|
|
// takes the data variable by reference in order to return status
|
|
|
|
bool stream_read(uint8_t& data) {
|
|
|
|
if (ELAPSED(millis(), packet.timeout)) {
|
|
|
|
stream_state = StreamState::PACKET_TIMEOUT;
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-28 03:14:17 +01:00
|
|
|
if (!serial_data_available(card.transfer_port_index)) return false;
|
|
|
|
data = read_serial(card.transfer_port_index);
|
2018-10-31 01:44:12 +01:00
|
|
|
packet.timeout = millis() + STREAM_MAX_WAIT;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const size_t buffer_size>
|
|
|
|
void receive(char (&buffer)[buffer_size]) {
|
|
|
|
uint8_t data = 0;
|
2018-12-07 22:34:21 +01:00
|
|
|
millis_t transfer_timeout = millis() + RX_TIMESLICE;
|
2019-02-24 05:53:01 +01:00
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
2019-02-28 03:14:17 +01:00
|
|
|
PORT_REDIRECT(card.transfer_port_index);
|
2019-02-24 05:53:01 +01:00
|
|
|
#endif
|
|
|
|
|
2018-12-07 22:34:21 +01:00
|
|
|
while (PENDING(millis(), transfer_timeout)) {
|
2018-10-31 01:44:12 +01:00
|
|
|
switch (stream_state) {
|
|
|
|
case StreamState::STREAM_RESET:
|
|
|
|
stream_reset();
|
|
|
|
case StreamState::PACKET_RESET:
|
|
|
|
packet_reset();
|
|
|
|
stream_state = StreamState::PACKET_HEADER;
|
|
|
|
break;
|
2019-02-24 05:53:01 +01:00
|
|
|
case StreamState::STREAM_HEADER: // The filename could also be in this packet, rather than handling it in the gcode
|
|
|
|
for (size_t i = 0; i < sizeof(stream_header); ++i)
|
2018-10-31 01:44:12 +01:00
|
|
|
stream_header_bytes[i] = buffer[i];
|
2019-02-24 05:53:01 +01:00
|
|
|
|
2018-10-31 01:44:12 +01:00
|
|
|
if (stream_header.token == 0x1234) {
|
|
|
|
stream_state = StreamState::PACKET_RESET;
|
|
|
|
bytes_received = 0;
|
|
|
|
time_stream_start = millis();
|
2019-03-05 13:46:19 +01:00
|
|
|
// confirm active stream and the maximum block size supported
|
|
|
|
SERIAL_ECHO_START();
|
|
|
|
SERIAL_ECHOLNPAIR("Datastream initialized (", stream_header.filesize, " bytes expected)");
|
|
|
|
SERIAL_ECHOLNPAIR("so", buffer_size);
|
2018-10-31 01:44:12 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-02-28 02:57:48 +01:00
|
|
|
SERIAL_ECHO_MSG("Datastream init error (invalid token)");
|
2018-10-31 01:44:12 +01:00
|
|
|
stream_state = StreamState::STREAM_FAILED;
|
|
|
|
}
|
|
|
|
buffer_next_index = 0;
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_HEADER:
|
|
|
|
if (!stream_read(data)) break;
|
|
|
|
|
|
|
|
packet.header_bytes[packet.bytes_received++] = data;
|
|
|
|
if (packet.bytes_received == sizeof(Packet::Header)) {
|
|
|
|
if (packet.header.id == packets_received) {
|
|
|
|
buffer_next_index = 0;
|
|
|
|
packet.bytes_received = 0;
|
|
|
|
stream_state = StreamState::PACKET_DATA;
|
|
|
|
}
|
|
|
|
else {
|
2019-02-28 02:57:48 +01:00
|
|
|
SERIAL_ECHO_MSG("Datastream packet out of order");
|
2018-10-31 01:44:12 +01:00
|
|
|
stream_state = StreamState::PACKET_FLUSHRX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_DATA:
|
|
|
|
if (!stream_read(data)) break;
|
|
|
|
|
2019-02-28 02:57:48 +01:00
|
|
|
if (buffer_next_index < buffer_size)
|
2018-10-31 01:44:12 +01:00
|
|
|
buffer[buffer_next_index] = data;
|
|
|
|
else {
|
2019-02-28 02:57:48 +01:00
|
|
|
SERIAL_ECHO_MSG("Datastream packet data buffer overrun");
|
2018-10-31 01:44:12 +01:00
|
|
|
stream_state = StreamState::STREAM_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet.checksum = checksum(packet.checksum, data);
|
2019-02-28 02:57:48 +01:00
|
|
|
packet.bytes_received++;
|
|
|
|
buffer_next_index++;
|
2018-10-31 01:44:12 +01:00
|
|
|
|
2019-02-28 02:57:48 +01:00
|
|
|
if (packet.bytes_received == packet.header.size)
|
2018-10-31 01:44:12 +01:00
|
|
|
stream_state = StreamState::PACKET_VALIDATE;
|
2019-02-28 02:57:48 +01:00
|
|
|
|
2018-10-31 01:44:12 +01:00
|
|
|
break;
|
|
|
|
case StreamState::PACKET_VALIDATE:
|
|
|
|
if (packet.header.checksum == packet.checksum) {
|
|
|
|
packet_retries = 0;
|
2019-02-28 02:57:48 +01:00
|
|
|
packets_received++;
|
2018-10-31 01:44:12 +01:00
|
|
|
bytes_received += packet.header.size;
|
|
|
|
|
2019-02-28 02:57:48 +01:00
|
|
|
if (packet.header.id == 0) // id 0 is always the stream descriptor
|
2018-10-31 01:44:12 +01:00
|
|
|
stream_state = StreamState::STREAM_HEADER; // defer packet confirmation to STREAM_HEADER state
|
|
|
|
else {
|
|
|
|
if (bytes_received < stream_header.filesize) {
|
2019-02-28 03:31:42 +01:00
|
|
|
stream_state = StreamState::PACKET_RESET; // reset and receive next packet
|
|
|
|
SERIAL_ECHOLNPAIR("ok", packet.header.id); // transmit confirm packet received and valid token
|
2018-10-31 01:44:12 +01:00
|
|
|
}
|
2019-02-28 02:57:48 +01:00
|
|
|
else
|
2018-10-31 01:44:12 +01:00
|
|
|
stream_state = StreamState::STREAM_COMPLETE; // no more data required
|
2019-02-28 02:57:48 +01:00
|
|
|
|
2018-10-31 01:44:12 +01:00
|
|
|
if (card.write(buffer, buffer_next_index) < 0) {
|
|
|
|
stream_state = StreamState::STREAM_FAILED;
|
2019-02-28 02:57:48 +01:00
|
|
|
SERIAL_ECHO_MSG("SDCard IO Error");
|
2018-10-31 01:44:12 +01:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-02-28 02:57:48 +01:00
|
|
|
SERIAL_ECHO_START();
|
2019-03-05 13:46:19 +01:00
|
|
|
SERIAL_ECHOLNPAIR("Block(", packet.header.id, ") Corrupt");
|
2018-10-31 01:44:12 +01:00
|
|
|
stream_state = StreamState::PACKET_FLUSHRX;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_RESEND:
|
|
|
|
if (packet_retries < MAX_RETRIES) {
|
2019-02-28 02:57:48 +01:00
|
|
|
packet_retries++;
|
2018-10-31 01:44:12 +01:00
|
|
|
stream_state = StreamState::PACKET_RESET;
|
2019-02-28 02:57:48 +01:00
|
|
|
SERIAL_ECHO_START();
|
|
|
|
SERIAL_ECHOLNPAIR("Resend request ", int(packet_retries));
|
|
|
|
SERIAL_ECHOLNPAIR("rs", packet.header.id); // transmit resend packet token
|
2018-10-31 01:44:12 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
stream_state = StreamState::STREAM_FAILED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_FLUSHRX:
|
|
|
|
if (ELAPSED(millis(), packet.timeout)) {
|
|
|
|
stream_state = StreamState::PACKET_RESEND;
|
|
|
|
break;
|
|
|
|
}
|
2019-02-28 03:14:17 +01:00
|
|
|
if (!serial_data_available(card.transfer_port_index)) break;
|
|
|
|
read_serial(card.transfer_port_index); // throw away data
|
2018-10-31 01:44:12 +01:00
|
|
|
packet.timeout = millis() + STREAM_MAX_WAIT;
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_TIMEOUT:
|
2019-02-28 02:57:48 +01:00
|
|
|
SERIAL_ECHO_START();
|
|
|
|
SERIAL_ECHOLNPGM("Datastream timeout");
|
2018-10-31 01:44:12 +01:00
|
|
|
stream_state = StreamState::PACKET_RESEND;
|
|
|
|
break;
|
|
|
|
case StreamState::STREAM_COMPLETE:
|
|
|
|
stream_state = StreamState::STREAM_RESET;
|
2018-11-17 05:39:16 +01:00
|
|
|
card.flag.binary_mode = false;
|
2019-02-28 02:57:48 +01:00
|
|
|
SERIAL_ECHO_START();
|
2019-02-24 05:53:01 +01:00
|
|
|
SERIAL_ECHO(card.filename);
|
2019-03-05 13:46:19 +01:00
|
|
|
SERIAL_ECHOLNPAIR(" transfer completed @ ", ((bytes_received / (millis() - time_stream_start) * 1000) / 1024), "KiB/s");
|
2019-02-28 02:57:48 +01:00
|
|
|
SERIAL_ECHOLNPGM("sc"); // transmit stream complete token
|
|
|
|
card.closefile();
|
2018-10-31 01:44:12 +01:00
|
|
|
return;
|
|
|
|
case StreamState::STREAM_FAILED:
|
|
|
|
stream_state = StreamState::STREAM_RESET;
|
2018-11-17 05:39:16 +01:00
|
|
|
card.flag.binary_mode = false;
|
2018-10-31 01:44:12 +01:00
|
|
|
card.closefile();
|
|
|
|
card.removeFile(card.filename);
|
2019-02-28 02:57:48 +01:00
|
|
|
SERIAL_ECHO_START();
|
|
|
|
SERIAL_ECHOLNPGM("File transfer failed");
|
|
|
|
SERIAL_ECHOLNPGM("sf"); // transmit stream failed token
|
2018-10-31 01:44:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint16_t STREAM_MAX_WAIT = 500, RX_TIMESLICE = 20, MAX_RETRIES = 3;
|
|
|
|
uint8_t packet_retries;
|
|
|
|
uint16_t buffer_next_index;
|
|
|
|
uint32_t packets_received, bytes_received;
|
|
|
|
millis_t time_stream_start;
|
|
|
|
StreamState stream_state = StreamState::STREAM_RESET;
|
|
|
|
|
|
|
|
} binaryStream{};
|
|
|
|
|
2019-02-28 02:57:48 +01:00
|
|
|
#endif // BINARY_FILE_TRANSFER
|
2018-10-31 01:44:12 +01:00
|
|
|
|
2019-02-04 14:17:40 +01:00
|
|
|
FORCE_INLINE bool is_M29(const char * const cmd) {
|
|
|
|
return cmd[0] == 'M' && cmd[1] == '2' && cmd[2] == '9' && !WITHIN(cmd[3], '0', '9');
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
inline void get_serial_commands() {
|
2017-11-05 15:49:38 +01:00
|
|
|
static char serial_line_buffer[NUM_SERIAL][MAX_CMD_SIZE];
|
2018-10-06 01:19:45 +02:00
|
|
|
static bool serial_comment_mode[NUM_SERIAL] = { false }
|
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
, serial_comment_paren_mode[NUM_SERIAL] = { false }
|
|
|
|
#endif
|
|
|
|
;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-02-28 02:57:48 +01:00
|
|
|
#if ENABLED(BINARY_FILE_TRANSFER)
|
2018-11-17 05:39:16 +01:00
|
|
|
if (card.flag.saving && 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-02-28 03:14:17 +01:00
|
|
|
binaryStream.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();
|
2017-11-05 15:49:38 +01:00
|
|
|
if (commands_in_queue == 0 && !serial_data_available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
|
2017-09-08 05:33:16 +02:00
|
|
|
SERIAL_ECHOLNPGM(MSG_WAIT);
|
|
|
|
last_command_time = ms;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loop while serial characters are incoming and the queue is not full
|
|
|
|
*/
|
2017-11-05 15:49:38 +01:00
|
|
|
while (commands_in_queue < BUFSIZE && serial_data_available()) {
|
|
|
|
for (uint8_t i = 0; i < NUM_SERIAL; ++i) {
|
|
|
|
int c;
|
|
|
|
if ((c = read_serial(i)) < 0) continue;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
char serial_char = c;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
/**
|
|
|
|
* If the character ends the line
|
|
|
|
*/
|
|
|
|
if (serial_char == '\n' || serial_char == '\r') {
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2018-10-06 01:19:45 +02:00
|
|
|
// Start with comment mode off
|
|
|
|
serial_comment_mode[i] = false;
|
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
serial_comment_paren_mode[i] = false;
|
2018-10-05 09:35:55 +02:00
|
|
|
#endif
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2018-02-09 08:50:14 +01:00
|
|
|
// Skip empty lines and comments
|
|
|
|
if (!serial_count[i]) { thermalManager.manage_heater(); continue; }
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
serial_line_buffer[i][serial_count[i]] = 0; // Terminate string
|
|
|
|
serial_count[i] = 0; // Reset buffer
|
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
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
while (*command == ' ') command++; // Skip leading spaces
|
|
|
|
char *npos = (*command == 'N') ? command : NULL; // 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
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
bool M110 = strstr_P(command, PSTR("M110")) != NULL;
|
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
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
gcode_N = strtol(npos + 1, NULL, 10);
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2018-04-08 04:25:39 +02:00
|
|
|
if (gcode_N != gcode_LastN + 1 && !M110)
|
|
|
|
return gcode_line_error(PSTR(MSG_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];
|
2018-04-08 04:25:39 +02:00
|
|
|
if (strtol(apos + 1, NULL, 10) != checksum)
|
|
|
|
return gcode_line_error(PSTR(MSG_ERR_CHECKSUM_MISMATCH), i);
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
2018-04-08 04:25:39 +02:00
|
|
|
else
|
|
|
|
return gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM), i);
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
gcode_LastN = 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))
|
2018-04-08 04:25:39 +02:00
|
|
|
return gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM), i);
|
2018-03-28 20:45:47 +02:00
|
|
|
#endif
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
// Movement commands alert when stopped
|
|
|
|
if (IsStopped()) {
|
|
|
|
char* gpos = strchr(command, 'G');
|
|
|
|
if (gpos) {
|
2018-09-27 22:40:19 +02:00
|
|
|
switch (strtol(gpos + 1, NULL, 10)) {
|
2017-11-05 15:49:38 +01:00
|
|
|
case 0:
|
|
|
|
case 1:
|
2018-09-27 22:40:19 +02:00
|
|
|
#if ENABLED(ARC_SUPPORT)
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
#endif
|
|
|
|
#if ENABLED(BEZIER_CURVE_SUPPORT)
|
|
|
|
case 5:
|
|
|
|
#endif
|
2018-11-29 23:58:58 +01:00
|
|
|
SERIAL_ECHOLNPGM(MSG_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
|
2017-11-05 15:49:38 +01:00
|
|
|
if (strcmp(command, "M108") == 0) {
|
|
|
|
wait_for_heatup = false;
|
2018-10-30 22:34:45 +01:00
|
|
|
#if HAS_LCD_MENU
|
2017-11-05 15:49:38 +01:00
|
|
|
wait_for_user = false;
|
|
|
|
#endif
|
|
|
|
}
|
2018-10-19 04:20:56 +02:00
|
|
|
if (strcmp(command, "M112") == 0) kill();
|
2018-02-08 08:31:19 +01:00
|
|
|
if (strcmp(command, "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
|
|
|
|
_enqueuecommand(serial_line_buffer[i], true
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
, i
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (serial_count[i] >= MAX_CMD_SIZE - 1) {
|
|
|
|
// Keep fetching, but ignore normal characters beyond the max length
|
|
|
|
// The command will be injected when EOL is reached
|
|
|
|
}
|
|
|
|
else if (serial_char == '\\') { // Handle escapes
|
|
|
|
// if we have one more character, copy it over
|
2018-10-05 09:35:55 +02:00
|
|
|
if ((c = read_serial(i)) >= 0 && !serial_comment_mode[i]
|
2018-10-06 01:19:45 +02:00
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
&& !serial_comment_paren_mode[i]
|
|
|
|
#endif
|
|
|
|
)
|
2018-02-09 11:02:42 +01:00
|
|
|
serial_line_buffer[i][serial_count[i]++] = (char)c;
|
2017-11-05 15:49:38 +01:00
|
|
|
}
|
|
|
|
else { // it's not a newline, carriage return or escape char
|
2018-10-06 01:19:45 +02:00
|
|
|
if (serial_char == ';') serial_comment_mode[i] = true;
|
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
else if (serial_char == '(') serial_comment_paren_mode[i] = true;
|
|
|
|
else if (serial_char == ')') serial_comment_paren_mode[i] = false;
|
2018-10-05 09:35:55 +02:00
|
|
|
#endif
|
2018-10-06 01:19:45 +02:00
|
|
|
else if (!serial_comment_mode[i]
|
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
&& ! serial_comment_paren_mode[i]
|
|
|
|
#endif
|
2018-10-05 09:35:55 +02:00
|
|
|
) serial_line_buffer[i][serial_count[i]++] = serial_char;
|
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)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get commands from the SD Card until the command buffer is full
|
|
|
|
* or until the end of the file is reached. The special character '#'
|
|
|
|
* can also interrupt buffering.
|
|
|
|
*/
|
|
|
|
inline void get_sdcard_commands() {
|
|
|
|
static bool stop_buffering = false,
|
2018-10-06 01:19:45 +02:00
|
|
|
sd_comment_mode = false
|
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
, sd_comment_paren_mode = false
|
|
|
|
#endif
|
|
|
|
;
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* '#' stops reading from SD to the buffer prematurely, so procedural
|
|
|
|
* macro calls are possible. If it occurs, stop_buffering is triggered
|
|
|
|
* and the buffer is run dry; this character _can_ occur in serial com
|
|
|
|
* due to checksums, however, no checksums are used in SD printing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (commands_in_queue == 0) stop_buffering = false;
|
|
|
|
|
|
|
|
uint16_t sd_count = 0;
|
|
|
|
bool card_eof = card.eof();
|
|
|
|
while (commands_in_queue < BUFSIZE && !card_eof && !stop_buffering) {
|
|
|
|
const int16_t n = card.get();
|
|
|
|
char sd_char = (char)n;
|
|
|
|
card_eof = card.eof();
|
|
|
|
if (card_eof || n == -1
|
|
|
|
|| sd_char == '\n' || sd_char == '\r'
|
2018-10-05 09:35:55 +02:00
|
|
|
|| ((sd_char == '#' || sd_char == ':') && !sd_comment_mode
|
2018-10-06 01:19:45 +02:00
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
&& !sd_comment_paren_mode
|
|
|
|
#endif
|
2018-10-05 09:35:55 +02:00
|
|
|
)
|
2017-09-08 05:33:16 +02:00
|
|
|
) {
|
|
|
|
if (card_eof) {
|
2017-11-15 07:15:57 +01:00
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
card.printingHasFinished();
|
2017-11-15 07:15:57 +01:00
|
|
|
|
2018-11-07 03:52:39 +01:00
|
|
|
if (IS_SD_PRINTING())
|
2017-11-15 07:15:57 +01:00
|
|
|
sd_count = 0; // If a sub-file was printing, continue from call point
|
|
|
|
else {
|
2018-11-29 23:58:58 +01:00
|
|
|
SERIAL_ECHOLNPGM(MSG_FILE_PRINTED);
|
2017-11-15 07:15:57 +01:00
|
|
|
#if ENABLED(PRINTER_EVENT_LEDS)
|
2018-10-11 04:25:43 +02:00
|
|
|
printerEventLEDs.onPrintCompleted();
|
2017-11-15 07:15:57 +01:00
|
|
|
#if HAS_RESUME_CONTINUE
|
2018-03-29 05:59:28 +02:00
|
|
|
enqueue_and_echo_commands_P(PSTR("M0 S"
|
2018-11-03 08:07:53 +01:00
|
|
|
#if HAS_LCD_MENU
|
2018-03-29 05:59:28 +02:00
|
|
|
"1800"
|
|
|
|
#else
|
|
|
|
"60"
|
|
|
|
#endif
|
|
|
|
));
|
2017-11-15 07:15:57 +01:00
|
|
|
#endif
|
2018-03-29 05:59:28 +02:00
|
|
|
#endif // PRINTER_EVENT_LEDS
|
2017-11-15 07:15:57 +01:00
|
|
|
}
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
2018-11-29 23:58:58 +01:00
|
|
|
else if (n == -1)
|
|
|
|
SERIAL_ERROR_MSG(MSG_SD_ERR_READ);
|
|
|
|
|
2017-09-08 05:33:16 +02:00
|
|
|
if (sd_char == '#') stop_buffering = true;
|
|
|
|
|
|
|
|
sd_comment_mode = false; // for new command
|
2018-10-06 01:19:45 +02:00
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
sd_comment_paren_mode = false;
|
2018-10-05 09:35:55 +02:00
|
|
|
#endif
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2018-02-09 08:50:14 +01:00
|
|
|
// Skip empty lines and comments
|
|
|
|
if (!sd_count) { thermalManager.manage_heater(); continue; }
|
2017-09-08 05:33:16 +02:00
|
|
|
|
|
|
|
command_queue[cmd_queue_index_w][sd_count] = '\0'; // terminate string
|
|
|
|
sd_count = 0; // clear sd line buffer
|
|
|
|
|
|
|
|
_commit_command(false);
|
|
|
|
}
|
|
|
|
else if (sd_count >= MAX_CMD_SIZE - 1) {
|
|
|
|
/**
|
|
|
|
* Keep fetching, but ignore normal characters beyond the max length
|
|
|
|
* The command will be injected when EOL is reached
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
else {
|
2018-10-06 01:19:45 +02:00
|
|
|
if (sd_char == ';') sd_comment_mode = true;
|
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
else if (sd_char == '(') sd_comment_paren_mode = true;
|
|
|
|
else if (sd_char == ')') sd_comment_paren_mode = false;
|
2018-10-05 09:35:55 +02:00
|
|
|
#endif
|
|
|
|
else if (!sd_comment_mode
|
2018-10-06 01:19:45 +02:00
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
&& ! sd_comment_paren_mode
|
|
|
|
#endif
|
|
|
|
) command_queue[cmd_queue_index_w][sd_count++] = sd_char;
|
2017-09-08 05:33:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SDSUPPORT
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add to the circular command queue the next command from:
|
|
|
|
* - The command-injection queue (injected_commands_P)
|
|
|
|
* - The active serial input (usually USB)
|
|
|
|
* - The SD card file being actively printed
|
|
|
|
*/
|
|
|
|
void get_available_commands() {
|
|
|
|
|
|
|
|
// if any immediate commands remain, don't get other commands yet
|
|
|
|
if (drain_injected_commands_P()) return;
|
|
|
|
|
|
|
|
get_serial_commands();
|
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
get_sdcard_commands();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next command in the queue, optionally log it to SD, then dispatch it
|
|
|
|
*/
|
|
|
|
void advance_command_queue() {
|
|
|
|
|
|
|
|
if (!commands_in_queue) return;
|
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
|
2018-11-17 05:39:16 +01:00
|
|
|
if (card.flag.saving) {
|
2017-09-08 05:33:16 +02:00
|
|
|
char* command = command_queue[cmd_queue_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();
|
2018-11-29 23:58:58 +01:00
|
|
|
SERIAL_ECHOLNPGM(MSG_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
|
|
|
|
if (commands_in_queue) {
|
|
|
|
--commands_in_queue;
|
|
|
|
if (++cmd_queue_index_r >= BUFSIZE) cmd_queue_index_r = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|