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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-04 09:25:55 +01:00
|
|
|
#pragma once
|
2017-09-08 05:33:16 +02: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 07:00:19 +02:00
|
|
|
class GCodeQueue {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-02-14 01:59:32 +01:00
|
|
|
static long last_N;
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-06-19 07:00:19 +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.
|
|
|
|
*/
|
|
|
|
static uint8_t length, // Count of commands in the queue
|
|
|
|
index_r; // Ring buffer read position
|
2017-11-05 15:49:38 +01:00
|
|
|
|
2019-09-02 03:21:11 +02:00
|
|
|
static char command_buffer[BUFSIZE][MAX_CMD_SIZE];
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
/*
|
|
|
|
* The port that the command was received on
|
|
|
|
*/
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
static int16_t port[BUFSIZE];
|
|
|
|
#endif
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
GCodeQueue();
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
/**
|
|
|
|
* Clear the Marlin command queue
|
|
|
|
*/
|
|
|
|
static void clear();
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-06-08 12:23:53 +02:00
|
|
|
/**
|
2020-04-10 03:05:58 +02:00
|
|
|
* Next Injected Command (PROGMEM) pointer. (nullptr == empty)
|
|
|
|
* Internal commands are enqueued ahead of serial / SD commands.
|
2019-06-08 12:23:53 +02:00
|
|
|
*/
|
2020-04-10 03:05:58 +02: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; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static inline void inject(char * const gcode) {
|
|
|
|
strncpy(injected_commands, gcode, sizeof(injected_commands) - 1);
|
|
|
|
}
|
2019-06-08 12:23:53 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
/**
|
|
|
|
* Enqueue and return only when commands are actually enqueued
|
|
|
|
*/
|
|
|
|
static void enqueue_one_now(const char* cmd);
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2020-03-11 00:04:54 +01:00
|
|
|
/**
|
|
|
|
* Attempt to enqueue a single G-code command
|
|
|
|
* and return 'true' if successful.
|
|
|
|
*/
|
|
|
|
static bool enqueue_one_P(PGM_P const pgcode);
|
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
/**
|
|
|
|
* Enqueue from program memory and return only when commands are actually enqueued
|
|
|
|
*/
|
|
|
|
static void enqueue_now_P(PGM_P const cmd);
|
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
|
|
|
|
*/
|
|
|
|
static bool has_commands_queued();
|
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
/**
|
|
|
|
* Get the next command in the queue, optionally log it to SD, then dispatch it
|
|
|
|
*/
|
|
|
|
static void advance();
|
2018-01-03 04:00:06 +01:00
|
|
|
|
|
|
|
/**
|
2019-06-19 07:00:19 +02: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 04:00:06 +01:00
|
|
|
*/
|
2019-06-19 07:00:19 +02: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
|
|
|
|
*/
|
|
|
|
static void ok_to_send();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the serial line and request a resend of
|
|
|
|
* the next expected line number.
|
|
|
|
*/
|
|
|
|
static void flush_and_request_resend();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
static uint8_t index_w; // Ring buffer write position
|
|
|
|
|
|
|
|
static void get_serial_commands();
|
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
static void get_sdcard_commands();
|
2018-01-03 04:00:06 +01:00
|
|
|
#endif
|
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
static void _commit_command(bool say_ok
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
, int16_t p=-1
|
|
|
|
#endif
|
|
|
|
);
|
2017-09-08 05:33:16 +02:00
|
|
|
|
2019-06-19 07:00:19 +02:00
|
|
|
static bool _enqueue(const char* cmd, bool say_ok=false
|
|
|
|
#if NUM_SERIAL > 1
|
|
|
|
, int16_t p=-1
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
2020-04-10 03:05:58 +02:00
|
|
|
// Process the next "immediate" command (PROGMEM)
|
|
|
|
static bool process_injected_command_P();
|
|
|
|
|
|
|
|
// Process the next "immediate" command (SRAM)
|
2019-06-19 07:00:19 +02:00
|
|
|
static bool process_injected_command();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue with Serial Echo
|
|
|
|
* Return true on success
|
|
|
|
*/
|
|
|
|
static bool enqueue_one(const char* cmd);
|
|
|
|
|
2020-01-27 05:46:26 +01:00
|
|
|
static void gcode_line_error(PGM_P const err, const int8_t pn);
|
2019-06-19 07:00:19 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
extern GCodeQueue queue;
|