2017-09-08 03:33:16 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 14:00:57 +00:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-08 03:33:16 +00:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-08 03:33:16 +00: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 03:20:14 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-09-08 03:33:16 +00:00
|
|
|
*
|
|
|
|
*/
|
2018-11-04 08:25:55 +00:00
|
|
|
#pragma once
|
2017-09-08 03:33:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* queue.h - The G-code command queue, which holds commands before they
|
|
|
|
* go to the parser and dispatcher.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
2019-06-19 05:00:19 +00:00
|
|
|
class GCodeQueue {
|
|
|
|
public:
|
|
|
|
/**
|
2021-02-26 22:54:46 +00:00
|
|
|
* The buffers per serial port.
|
2019-06-19 05:00:19 +00:00
|
|
|
*/
|
2021-02-26 22:54:46 +00:00
|
|
|
struct SerialState {
|
|
|
|
/**
|
|
|
|
* GCode line number handling. Hosts may include line numbers when sending
|
|
|
|
* commands to Marlin, and lines will be checked for sequentiality.
|
|
|
|
* M110 N<int> sets the current line number.
|
|
|
|
*/
|
|
|
|
long last_N;
|
2021-03-24 10:40:28 +00:00
|
|
|
int count; //!< Number of characters read in the current line of serial input
|
|
|
|
char line_buffer[MAX_CMD_SIZE]; //!< The current line accumulator
|
|
|
|
uint8_t input_state; //!< The input state
|
2021-02-26 22:54:46 +00:00
|
|
|
};
|
2020-06-02 03:19:10 +00:00
|
|
|
|
2021-02-26 22:54:46 +00:00
|
|
|
static SerialState serial_state[NUM_SERIAL]; //!< Serial states for each serial port
|
2017-09-08 03:33:16 +00:00
|
|
|
|
2019-06-19 05:00:19 +00:00
|
|
|
/**
|
|
|
|
* GCode Command Queue
|
2021-02-26 22:54:46 +00:00
|
|
|
* A simple (circular) ring buffer of BUFSIZE command strings.
|
2019-06-19 05:00:19 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-02-26 22:54:46 +00:00
|
|
|
struct CommandLine {
|
2021-03-24 10:40:28 +00:00
|
|
|
char buffer[MAX_CMD_SIZE]; //!< The command buffer
|
|
|
|
bool skip_ok; //!< Skip sending ok when command is processed?
|
2021-07-09 22:09:58 +00:00
|
|
|
#if HAS_MULTI_SERIAL
|
2021-03-24 10:40:28 +00:00
|
|
|
serial_index_t port; //!< Serial port the command was received on
|
|
|
|
#endif
|
2021-02-26 22:54:46 +00:00
|
|
|
};
|
2017-09-08 03:33:16 +00:00
|
|
|
|
2020-06-02 03:19:10 +00:00
|
|
|
/**
|
2021-02-26 22:54:46 +00:00
|
|
|
* A handy ring buffer type
|
2019-06-19 05:00:19 +00:00
|
|
|
*/
|
2021-02-26 22:54:46 +00:00
|
|
|
struct RingBuffer {
|
|
|
|
uint8_t length, //!< Number of commands in the queue
|
|
|
|
index_r, //!< Ring buffer's read position
|
|
|
|
index_w; //!< Ring buffer's write position
|
|
|
|
CommandLine commands[BUFSIZE]; //!< The ring buffer of commands
|
|
|
|
|
|
|
|
inline serial_index_t command_port() const { return TERN0(HAS_MULTI_SERIAL, commands[index_r].port); }
|
|
|
|
|
|
|
|
inline void clear() { length = index_r = index_w = 0; }
|
|
|
|
|
|
|
|
void advance_pos(uint8_t &p, const int inc) { if (++p >= BUFSIZE) p = 0; length += inc; }
|
|
|
|
|
|
|
|
void commit_command(bool skip_ok
|
2021-05-24 02:33:22 +00:00
|
|
|
OPTARG(HAS_MULTI_SERIAL, serial_index_t serial_ind = serial_index_t())
|
2021-02-26 22:54:46 +00:00
|
|
|
);
|
|
|
|
|
2021-03-30 01:36:37 +00:00
|
|
|
bool enqueue(const char *cmd, bool skip_ok = true
|
2021-05-24 02:33:22 +00:00
|
|
|
OPTARG(HAS_MULTI_SERIAL, serial_index_t serial_ind = serial_index_t())
|
2021-02-26 22:54:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
void ok_to_send();
|
|
|
|
|
|
|
|
inline bool full(uint8_t cmdCount=1) const { return length > (BUFSIZE - cmdCount); }
|
|
|
|
|
2021-03-06 15:04:05 +00:00
|
|
|
inline bool occupied() const { return length != 0; }
|
|
|
|
|
|
|
|
inline bool empty() const { return !occupied(); }
|
2020-06-02 03:19:10 +00:00
|
|
|
|
2021-02-26 22:54:46 +00:00
|
|
|
inline CommandLine& peek_next_command() { return commands[index_r]; }
|
|
|
|
|
|
|
|
inline char* peek_next_command_string() { return peek_next_command().buffer; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ring buffer of commands
|
|
|
|
*/
|
|
|
|
static RingBuffer ring_buffer;
|
2017-09-08 03:33:16 +00:00
|
|
|
|
2019-06-19 05:00:19 +00:00
|
|
|
/**
|
|
|
|
* Clear the Marlin command queue
|
|
|
|
*/
|
2021-02-26 22:54:46 +00:00
|
|
|
static void clear() { ring_buffer.clear(); }
|
2017-09-08 03:33:16 +00:00
|
|
|
|
2019-06-08 10:23:53 +00:00
|
|
|
/**
|
2020-04-10 01:05:58 +00:00
|
|
|
* Next Injected Command (PROGMEM) pointer. (nullptr == empty)
|
|
|
|
* Internal commands are enqueued ahead of serial / SD commands.
|
2019-06-08 10:23:53 +00:00
|
|
|
*/
|
2020-04-10 01:05:58 +00:00
|
|
|
static PGM_P injected_commands_P;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Injected Commands (SRAM)
|
|
|
|
*/
|
|
|
|
static char injected_commands[64];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue command(s) to run from PROGMEM. Drained by process_injected_command_P().
|
|
|
|
* Don't inject comments or use leading spaces!
|
|
|
|
* Aborts the current PROGMEM queue so only use for one or two commands.
|
|
|
|
*/
|
|
|
|
static inline void inject_P(PGM_P const pgcode) { injected_commands_P = pgcode; }
|
2021-09-26 02:11:31 +00:00
|
|
|
static inline void inject(FSTR_P const fgcode) { inject_P(FTOP(fgcode)); }
|
2020-04-10 01:05:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue command(s) to run from SRAM. Drained by process_injected_command().
|
|
|
|
* Aborts the current SRAM queue so only use for one or two commands.
|
|
|
|
*/
|
2021-10-18 01:24:41 +00:00
|
|
|
static inline void inject(const char * const gcode) {
|
2020-04-10 01:05:58 +00:00
|
|
|
strncpy(injected_commands, gcode, sizeof(injected_commands) - 1);
|
|
|
|
}
|
2019-06-08 10:23:53 +00:00
|
|
|
|
2019-06-19 05:00:19 +00:00
|
|
|
/**
|
|
|
|
* Enqueue and return only when commands are actually enqueued
|
|
|
|
*/
|
2021-09-26 02:11:31 +00:00
|
|
|
static void enqueue_one_now(const char * const cmd);
|
2017-09-08 03:33:16 +00:00
|
|
|
|
2020-03-10 23:04:54 +00:00
|
|
|
/**
|
|
|
|
* Attempt to enqueue a single G-code command
|
|
|
|
* and return 'true' if successful.
|
|
|
|
*/
|
2021-09-26 02:11:31 +00:00
|
|
|
static bool enqueue_one(FSTR_P const fgcode);
|
2020-03-10 23:04:54 +00:00
|
|
|
|
2021-10-09 23:05:24 +00:00
|
|
|
/**
|
|
|
|
* Enqueue with Serial Echo
|
|
|
|
* Return true on success
|
|
|
|
*/
|
|
|
|
static bool enqueue_one(const char *cmd);
|
|
|
|
|
2019-06-19 05:00:19 +00:00
|
|
|
/**
|
|
|
|
* Enqueue from program memory and return only when commands are actually enqueued
|
|
|
|
*/
|
2021-09-26 02:11:31 +00:00
|
|
|
static void enqueue_now_P(PGM_P const pcmd);
|
|
|
|
static inline void enqueue_now(FSTR_P const fcmd) { enqueue_now_P(FTOP(fcmd)); }
|
2017-09-08 03:33:16 +00:00
|
|
|
|
2019-06-29 05:23:57 +00:00
|
|
|
/**
|
|
|
|
* Check whether there are any commands yet to be executed
|
|
|
|
*/
|
2021-02-26 22:54:46 +00:00
|
|
|
static bool has_commands_queued() { return ring_buffer.length || injected_commands_P || injected_commands[0]; }
|
2019-06-29 05:23:57 +00:00
|
|
|
|
2019-06-19 05:00:19 +00:00
|
|
|
/**
|
|
|
|
* Get the next command in the queue, optionally log it to SD, then dispatch it
|
|
|
|
*/
|
|
|
|
static void advance();
|
2018-01-03 03:00:06 +00:00
|
|
|
|
2021-03-06 15:04:05 +00:00
|
|
|
/**
|
|
|
|
* Run the entire queue in-place
|
|
|
|
*/
|
|
|
|
static void exhaust();
|
|
|
|
|
2018-01-03 03:00:06 +00:00
|
|
|
/**
|
2019-06-19 05:00:19 +00:00
|
|
|
* 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
|
2018-01-03 03:00:06 +00:00
|
|
|
*/
|
2019-06-19 05:00:19 +00:00
|
|
|
static void get_available_commands();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-02-26 22:54:46 +00:00
|
|
|
static inline void ok_to_send() { ring_buffer.ok_to_send(); }
|
2019-06-19 05:00:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the serial line and request a resend of
|
|
|
|
* the next expected line number.
|
|
|
|
*/
|
2021-03-04 23:39:08 +00:00
|
|
|
static void flush_and_request_resend(const serial_index_t serial_ind);
|
2019-06-19 05:00:19 +00:00
|
|
|
|
2021-02-26 22:54:46 +00:00
|
|
|
/**
|
|
|
|
* (Re)Set the current line number for the last received command
|
|
|
|
*/
|
2021-03-09 09:20:37 +00:00
|
|
|
static inline void set_current_line_number(long n) { serial_state[ring_buffer.command_port().index].last_N = n; }
|
2019-06-19 05:00:19 +00:00
|
|
|
|
2021-07-23 03:53:00 +00:00
|
|
|
#if ENABLED(BUFFER_MONITORING)
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Track buffer underruns
|
|
|
|
*/
|
|
|
|
static uint32_t command_buffer_underruns, planner_buffer_underruns;
|
|
|
|
static bool command_buffer_empty, planner_buffer_empty;
|
|
|
|
static millis_t max_command_buffer_empty_duration, max_planner_buffer_empty_duration,
|
|
|
|
command_buffer_empty_at, planner_buffer_empty_at;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Report buffer statistics to the host to be able to detect buffer underruns
|
|
|
|
*
|
|
|
|
* Returns "D576 " followed by:
|
|
|
|
* P<uint> Planner space remaining
|
|
|
|
* B<uint> Command buffer space remaining
|
|
|
|
* PU<uint> Number of planner buffer underruns since last report
|
|
|
|
* PD<uint> Max time in ms the planner buffer was empty since last report
|
|
|
|
* BU<uint> Number of command buffer underruns since last report
|
|
|
|
* BD<uint> Max time in ms the command buffer was empty since last report
|
|
|
|
*/
|
|
|
|
static void report_buffer_statistics();
|
|
|
|
|
|
|
|
static uint8_t auto_buffer_report_interval;
|
|
|
|
static millis_t next_buffer_report_ms;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
static void auto_report_buffer_statistics();
|
|
|
|
|
|
|
|
static inline void set_auto_report_interval(uint8_t v) {
|
|
|
|
NOMORE(v, 60);
|
|
|
|
auto_buffer_report_interval = v;
|
|
|
|
next_buffer_report_ms = millis() + 1000UL * v;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // BUFFER_MONITORING
|
|
|
|
|
2021-02-26 22:54:46 +00:00
|
|
|
private:
|
2019-06-19 05:00:19 +00:00
|
|
|
|
|
|
|
static void get_serial_commands();
|
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
static void get_sdcard_commands();
|
2018-01-03 03:00:06 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-10 01:05:58 +00:00
|
|
|
// Process the next "immediate" command (PROGMEM)
|
|
|
|
static bool process_injected_command_P();
|
|
|
|
|
|
|
|
// Process the next "immediate" command (SRAM)
|
2019-06-19 05:00:19 +00:00
|
|
|
static bool process_injected_command();
|
|
|
|
|
2021-09-27 18:46:42 +00:00
|
|
|
static void gcode_line_error(FSTR_P const ferr, const serial_index_t serial_ind);
|
2019-06-19 05:00:19 +00:00
|
|
|
|
2021-02-26 22:54:46 +00:00
|
|
|
friend class GcodeSuite;
|
2019-06-19 05:00:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern GCodeQueue queue;
|
2021-01-22 21:01:19 +00:00
|
|
|
|
|
|
|
extern const char G28_STR[];
|