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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. static void enqueue_one_now(FSTR_P const fcmd);
  121. /**
  122. * Attempt to enqueue a single G-code command
  123. * and return 'true' if successful.
  124. */
  125. static bool enqueue_one(FSTR_P const fcmd);
  126. /**
  127. * Enqueue with Serial Echo
  128. * Return true on success
  129. */
  130. static bool enqueue_one(const char *cmd);
  131. /**
  132. * Enqueue from program memory and return only when commands are actually enqueued
  133. */
  134. static void enqueue_now_P(PGM_P const pcmd);
  135. static void enqueue_now(FSTR_P const fcmd) { enqueue_now_P(FTOP(fcmd)); }
  136. /**
  137. * Check whether there are any commands yet to be executed
  138. */
  139. static bool has_commands_queued() { return ring_buffer.length || injected_commands_P || injected_commands[0]; }
  140. /**
  141. * Get the next command in the queue, optionally log it to SD, then dispatch it
  142. */
  143. static void advance();
  144. /**
  145. * Run the entire queue in-place
  146. */
  147. static void exhaust();
  148. /**
  149. * Add to the circular command queue the next command from:
  150. * - The command-injection queue (injected_commands_P)
  151. * - The active serial input (usually USB)
  152. * - The SD card file being actively printed
  153. */
  154. static void get_available_commands();
  155. /**
  156. * Send an "ok" message to the host, indicating
  157. * that a command was successfully processed.
  158. *
  159. * If ADVANCED_OK is enabled also include:
  160. * N<int> Line number of the command, if any
  161. * P<int> Planner space remaining
  162. * B<int> Block queue space remaining
  163. */
  164. static void ok_to_send() { ring_buffer.ok_to_send(); }
  165. /**
  166. * Clear the serial line and request a resend of
  167. * the next expected line number.
  168. */
  169. static void flush_and_request_resend(const serial_index_t serial_ind);
  170. /**
  171. * (Re)Set the current line number for the last received command
  172. */
  173. static void set_current_line_number(long n) { serial_state[ring_buffer.command_port().index].last_N = n; }
  174. #if ENABLED(BUFFER_MONITORING)
  175. private:
  176. /**
  177. * Track buffer underruns
  178. */
  179. static uint32_t command_buffer_underruns, planner_buffer_underruns;
  180. static bool command_buffer_empty, planner_buffer_empty;
  181. static millis_t max_command_buffer_empty_duration, max_planner_buffer_empty_duration,
  182. command_buffer_empty_at, planner_buffer_empty_at;
  183. /**
  184. * Report buffer statistics to the host to be able to detect buffer underruns
  185. *
  186. * Returns "D576 " followed by:
  187. * P<uint> Planner space remaining
  188. * B<uint> Command buffer space remaining
  189. * PU<uint> Number of planner buffer underruns since last report
  190. * PD<uint> Max time in ms the planner buffer was empty since last report
  191. * BU<uint> Number of command buffer underruns since last report
  192. * BD<uint> Max time in ms the command buffer was empty since last report
  193. */
  194. static void report_buffer_statistics();
  195. static uint8_t auto_buffer_report_interval;
  196. static millis_t next_buffer_report_ms;
  197. public:
  198. static void auto_report_buffer_statistics();
  199. static void set_auto_report_interval(uint8_t v) {
  200. NOMORE(v, 60);
  201. auto_buffer_report_interval = v;
  202. next_buffer_report_ms = millis() + 1000UL * v;
  203. }
  204. #endif // BUFFER_MONITORING
  205. private:
  206. static void get_serial_commands();
  207. #if ENABLED(SDSUPPORT)
  208. static void get_sdcard_commands();
  209. #endif
  210. // Process the next "immediate" command (PROGMEM)
  211. static bool process_injected_command_P();
  212. // Process the next "immediate" command (SRAM)
  213. static bool process_injected_command();
  214. static void gcode_line_error(FSTR_P const ferr, const serial_index_t serial_ind);
  215. friend class GcodeSuite;
  216. };
  217. extern GCodeQueue queue;
  218. extern const char G28_STR[];