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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 ENABLED(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. #if HAS_MULTI_SERIAL
  74. , serial_index_t serial_ind = serial_index_t()
  75. #endif
  76. );
  77. bool enqueue(const char *cmd, bool skip_ok = true
  78. #if HAS_MULTI_SERIAL
  79. , serial_index_t serial_ind = serial_index_t()
  80. #endif
  81. );
  82. void ok_to_send();
  83. inline bool full(uint8_t cmdCount=1) const { return length > (BUFSIZE - cmdCount); }
  84. inline bool occupied() const { return length != 0; }
  85. inline bool empty() const { return !occupied(); }
  86. inline CommandLine& peek_next_command() { return commands[index_r]; }
  87. inline char* peek_next_command_string() { return peek_next_command().buffer; }
  88. };
  89. /**
  90. * The ring buffer of commands
  91. */
  92. static RingBuffer ring_buffer;
  93. /**
  94. * Clear the Marlin command queue
  95. */
  96. static void clear() { ring_buffer.clear(); }
  97. /**
  98. * Next Injected Command (PROGMEM) pointer. (nullptr == empty)
  99. * Internal commands are enqueued ahead of serial / SD commands.
  100. */
  101. static PGM_P injected_commands_P;
  102. /**
  103. * Injected Commands (SRAM)
  104. */
  105. static char injected_commands[64];
  106. /**
  107. * Enqueue command(s) to run from PROGMEM. Drained by process_injected_command_P().
  108. * Don't inject comments or use leading spaces!
  109. * Aborts the current PROGMEM queue so only use for one or two commands.
  110. */
  111. static inline void inject_P(PGM_P const pgcode) { injected_commands_P = pgcode; }
  112. /**
  113. * Enqueue command(s) to run from SRAM. Drained by process_injected_command().
  114. * Aborts the current SRAM queue so only use for one or two commands.
  115. */
  116. static inline void inject(char * const gcode) {
  117. strncpy(injected_commands, gcode, sizeof(injected_commands) - 1);
  118. }
  119. /**
  120. * Enqueue and return only when commands are actually enqueued
  121. */
  122. static void enqueue_one_now(const char *cmd);
  123. /**
  124. * Attempt to enqueue a single G-code command
  125. * and return 'true' if successful.
  126. */
  127. static bool enqueue_one_P(PGM_P const pgcode);
  128. /**
  129. * Enqueue from program memory and return only when commands are actually enqueued
  130. */
  131. static void enqueue_now_P(PGM_P const cmd);
  132. /**
  133. * Check whether there are any commands yet to be executed
  134. */
  135. static bool has_commands_queued() { return ring_buffer.length || injected_commands_P || injected_commands[0]; }
  136. /**
  137. * Get the next command in the queue, optionally log it to SD, then dispatch it
  138. */
  139. static void advance();
  140. /**
  141. * Run the entire queue in-place
  142. */
  143. static void exhaust();
  144. /**
  145. * Add to the circular command queue the next command from:
  146. * - The command-injection queue (injected_commands_P)
  147. * - The active serial input (usually USB)
  148. * - The SD card file being actively printed
  149. */
  150. static void get_available_commands();
  151. /**
  152. * Send an "ok" message to the host, indicating
  153. * that a command was successfully processed.
  154. *
  155. * If ADVANCED_OK is enabled also include:
  156. * N<int> Line number of the command, if any
  157. * P<int> Planner space remaining
  158. * B<int> Block queue space remaining
  159. */
  160. static inline void ok_to_send() { ring_buffer.ok_to_send(); }
  161. /**
  162. * Clear the serial line and request a resend of
  163. * the next expected line number.
  164. */
  165. static void flush_and_request_resend(const serial_index_t serial_ind);
  166. /**
  167. * (Re)Set the current line number for the last received command
  168. */
  169. static inline void set_current_line_number(long n) { serial_state[ring_buffer.command_port().index].last_N = n; }
  170. private:
  171. static void get_serial_commands();
  172. #if ENABLED(SDSUPPORT)
  173. static void get_sdcard_commands();
  174. #endif
  175. // Process the next "immediate" command (PROGMEM)
  176. static bool process_injected_command_P();
  177. // Process the next "immediate" command (SRAM)
  178. static bool process_injected_command();
  179. /**
  180. * Enqueue with Serial Echo
  181. * Return true on success
  182. */
  183. static bool enqueue_one(const char *cmd);
  184. static void gcode_line_error(PGM_P const err, const serial_index_t serial_ind);
  185. friend class GcodeSuite;
  186. };
  187. extern GCodeQueue queue;
  188. extern const char G28_STR[];