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

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