My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

queue.h 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * queue.h - The G-code command queue, which holds commands before they
  25. * go to the parser and dispatcher.
  26. */
  27. #include "../inc/MarlinConfig.h"
  28. class GCodeQueue {
  29. public:
  30. /**
  31. * The buffers per serial port.
  32. */
  33. struct SerialState {
  34. /**
  35. * GCode line number handling. Hosts may include line numbers when sending
  36. * commands to Marlin, and lines will be checked for sequentiality.
  37. * M110 N<int> sets the current line number.
  38. */
  39. long last_N;
  40. int count; //!< Number of characters read in the current line of serial input
  41. char line_buffer[MAX_CMD_SIZE]; //!< The current line accumulator
  42. uint8_t input_state; //!< The input state
  43. };
  44. static SerialState serial_state[NUM_SERIAL]; //!< Serial states for each serial port
  45. /**
  46. * GCode Command Queue
  47. * A simple (circular) ring buffer of BUFSIZE command strings.
  48. *
  49. * Commands are copied into this buffer by the command injectors
  50. * (immediate, serial, sd card) and they are processed sequentially by
  51. * the main loop. The gcode.process_next_command method parses the next
  52. * command and hands off execution to individual handler functions.
  53. */
  54. struct CommandLine {
  55. char buffer[MAX_CMD_SIZE]; //!< The command buffer
  56. bool skip_ok; //!< Skip sending ok when command is processed?
  57. #if HAS_MULTI_SERIAL
  58. serial_index_t port; //!< Serial port the command was received on
  59. #endif
  60. };
  61. /**
  62. * A handy ring buffer type
  63. */
  64. struct RingBuffer {
  65. uint8_t length, //!< Number of commands in the queue
  66. index_r, //!< Ring buffer's read position
  67. index_w; //!< Ring buffer's write position
  68. CommandLine commands[BUFSIZE]; //!< The ring buffer of commands
  69. inline serial_index_t command_port() const { return TERN0(HAS_MULTI_SERIAL, commands[index_r].port); }
  70. inline void clear() { length = index_r = index_w = 0; }
  71. void advance_pos(uint8_t &p, const int inc) { if (++p >= BUFSIZE) p = 0; length += inc; }
  72. void commit_command(bool skip_ok
  73. OPTARG(HAS_MULTI_SERIAL, serial_index_t serial_ind = serial_index_t())
  74. );
  75. bool enqueue(const char *cmd, bool skip_ok = true
  76. OPTARG(HAS_MULTI_SERIAL, serial_index_t serial_ind = serial_index_t())
  77. );
  78. void ok_to_send();
  79. inline bool full(uint8_t cmdCount=1) const { return length > (BUFSIZE - cmdCount); }
  80. inline bool occupied() const { return length != 0; }
  81. inline bool empty() const { return !occupied(); }
  82. inline CommandLine& peek_next_command() { return commands[index_r]; }
  83. inline char* peek_next_command_string() { return peek_next_command().buffer; }
  84. };
  85. /**
  86. * The ring buffer of commands
  87. */
  88. static RingBuffer ring_buffer;
  89. /**
  90. * Clear the Marlin command queue
  91. */
  92. static void clear() { ring_buffer.clear(); }
  93. /**
  94. * Next Injected Command (PROGMEM) pointer. (nullptr == empty)
  95. * Internal commands are enqueued ahead of serial / SD commands.
  96. */
  97. static PGM_P injected_commands_P;
  98. /**
  99. * Injected Commands (SRAM)
  100. */
  101. static char injected_commands[64];
  102. /**
  103. * Enqueue command(s) to run from PROGMEM. Drained by process_injected_command_P().
  104. * Don't inject comments or use leading spaces!
  105. * Aborts the current PROGMEM queue so only use for one or two commands.
  106. */
  107. static void inject_P(PGM_P const pgcode) { injected_commands_P = pgcode; }
  108. static void inject(FSTR_P const fgcode) { inject_P(FTOP(fgcode)); }
  109. /**
  110. * Enqueue command(s) to run from SRAM. Drained by process_injected_command().
  111. * Aborts the current SRAM queue so only use for one or two commands.
  112. */
  113. static void inject(const char * const gcode) {
  114. strncpy(injected_commands, gcode, sizeof(injected_commands) - 1);
  115. }
  116. /**
  117. * Enqueue and return only when commands are actually enqueued
  118. */
  119. static void enqueue_one_now(const char * const cmd);
  120. /**
  121. * Attempt to enqueue a single G-code command
  122. * and return 'true' if successful.
  123. */
  124. static bool enqueue_one(FSTR_P const fgcode);
  125. /**
  126. * Enqueue with Serial Echo
  127. * Return true on success
  128. */
  129. static bool enqueue_one(const char *cmd);
  130. /**
  131. * Enqueue from program memory and return only when commands are actually enqueued
  132. */
  133. static void enqueue_now_P(PGM_P const pcmd);
  134. static void enqueue_now(FSTR_P const fcmd) { enqueue_now_P(FTOP(fcmd)); }
  135. /**
  136. * Check whether there are any commands yet to be executed
  137. */
  138. static bool has_commands_queued() { return ring_buffer.length || injected_commands_P || injected_commands[0]; }
  139. /**
  140. * Get the next command in the queue, optionally log it to SD, then dispatch it
  141. */
  142. static void advance();
  143. /**
  144. * Run the entire queue in-place
  145. */
  146. static void exhaust();
  147. /**
  148. * Add to the circular command queue the next command from:
  149. * - The command-injection queue (injected_commands_P)
  150. * - The active serial input (usually USB)
  151. * - The SD card file being actively printed
  152. */
  153. static void get_available_commands();
  154. /**
  155. * Send an "ok" message to the host, indicating
  156. * that a command was successfully processed.
  157. *
  158. * If ADVANCED_OK is enabled also include:
  159. * N<int> Line number of the command, if any
  160. * P<int> Planner space remaining
  161. * B<int> Block queue space remaining
  162. */
  163. static void ok_to_send() { ring_buffer.ok_to_send(); }
  164. /**
  165. * Clear the serial line and request a resend of
  166. * the next expected line number.
  167. */
  168. static void flush_and_request_resend(const serial_index_t serial_ind);
  169. /**
  170. * (Re)Set the current line number for the last received command
  171. */
  172. static void set_current_line_number(long n) { serial_state[ring_buffer.command_port().index].last_N = n; }
  173. #if ENABLED(BUFFER_MONITORING)
  174. private:
  175. /**
  176. * Track buffer underruns
  177. */
  178. static uint32_t command_buffer_underruns, planner_buffer_underruns;
  179. static bool command_buffer_empty, planner_buffer_empty;
  180. static millis_t max_command_buffer_empty_duration, max_planner_buffer_empty_duration,
  181. command_buffer_empty_at, planner_buffer_empty_at;
  182. /**
  183. * Report buffer statistics to the host to be able to detect buffer underruns
  184. *
  185. * Returns "D576 " followed by:
  186. * P<uint> Planner space remaining
  187. * B<uint> Command buffer space remaining
  188. * PU<uint> Number of planner buffer underruns since last report
  189. * PD<uint> Max time in ms the planner buffer was empty since last report
  190. * BU<uint> Number of command buffer underruns since last report
  191. * BD<uint> Max time in ms the command buffer was empty since last report
  192. */
  193. static void report_buffer_statistics();
  194. static uint8_t auto_buffer_report_interval;
  195. static millis_t next_buffer_report_ms;
  196. public:
  197. static void auto_report_buffer_statistics();
  198. static void set_auto_report_interval(uint8_t v) {
  199. NOMORE(v, 60);
  200. auto_buffer_report_interval = v;
  201. next_buffer_report_ms = millis() + 1000UL * v;
  202. }
  203. #endif // BUFFER_MONITORING
  204. private:
  205. static void get_serial_commands();
  206. #if ENABLED(SDSUPPORT)
  207. static void get_sdcard_commands();
  208. #endif
  209. // Process the next "immediate" command (PROGMEM)
  210. static bool process_injected_command_P();
  211. // Process the next "immediate" command (SRAM)
  212. static bool process_injected_command();
  213. static void gcode_line_error(FSTR_P const ferr, const serial_index_t serial_ind);
  214. friend class GcodeSuite;
  215. };
  216. extern GCodeQueue queue;
  217. extern const char G28_STR[];