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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * GCode line number handling. Hosts may include line numbers when sending
  32. * commands to Marlin, and lines will be checked for sequentiality.
  33. * M110 N<int> sets the current line number.
  34. */
  35. static long last_N[NUM_SERIAL];
  36. /**
  37. * GCode Command Queue
  38. * A simple ring buffer of BUFSIZE command strings.
  39. *
  40. * Commands are copied into this buffer by the command injectors
  41. * (immediate, serial, sd card) and they are processed sequentially by
  42. * the main loop. The gcode.process_next_command method parses the next
  43. * command and hands off execution to individual handler functions.
  44. */
  45. static uint8_t length, // Count of commands in the queue
  46. index_r; // Ring buffer read position
  47. static char command_buffer[BUFSIZE][MAX_CMD_SIZE];
  48. /**
  49. * The port that the command was received on
  50. */
  51. #if HAS_MULTI_SERIAL
  52. static int16_t port[BUFSIZE];
  53. #endif
  54. static int16_t command_port() {
  55. return TERN0(HAS_MULTI_SERIAL, port[index_r]);
  56. }
  57. GCodeQueue();
  58. /**
  59. * Clear the Marlin command queue
  60. */
  61. static void clear();
  62. /**
  63. * Next Injected Command (PROGMEM) pointer. (nullptr == empty)
  64. * Internal commands are enqueued ahead of serial / SD commands.
  65. */
  66. static PGM_P injected_commands_P;
  67. /**
  68. * Injected Commands (SRAM)
  69. */
  70. static char injected_commands[64];
  71. /**
  72. * Enqueue command(s) to run from PROGMEM. Drained by process_injected_command_P().
  73. * Don't inject comments or use leading spaces!
  74. * Aborts the current PROGMEM queue so only use for one or two commands.
  75. */
  76. static inline void inject_P(PGM_P const pgcode) { injected_commands_P = pgcode; }
  77. /**
  78. * Enqueue command(s) to run from SRAM. Drained by process_injected_command().
  79. * Aborts the current SRAM queue so only use for one or two commands.
  80. */
  81. static inline void inject(char * const gcode) {
  82. strncpy(injected_commands, gcode, sizeof(injected_commands) - 1);
  83. }
  84. /**
  85. * Enqueue and return only when commands are actually enqueued
  86. */
  87. static void enqueue_one_now(const char* cmd);
  88. /**
  89. * Attempt to enqueue a single G-code command
  90. * and return 'true' if successful.
  91. */
  92. static bool enqueue_one_P(PGM_P const pgcode);
  93. /**
  94. * Enqueue from program memory and return only when commands are actually enqueued
  95. */
  96. static void enqueue_now_P(PGM_P const cmd);
  97. /**
  98. * Check whether there are any commands yet to be executed
  99. */
  100. static bool has_commands_queued();
  101. /**
  102. * Get the next command in the queue, optionally log it to SD, then dispatch it
  103. */
  104. static void advance();
  105. /**
  106. * Add to the circular command queue the next command from:
  107. * - The command-injection queue (injected_commands_P)
  108. * - The active serial input (usually USB)
  109. * - The SD card file being actively printed
  110. */
  111. static void get_available_commands();
  112. /**
  113. * Send an "ok" message to the host, indicating
  114. * that a command was successfully processed.
  115. *
  116. * If ADVANCED_OK is enabled also include:
  117. * N<int> Line number of the command, if any
  118. * P<int> Planner space remaining
  119. * B<int> Block queue space remaining
  120. */
  121. static void ok_to_send();
  122. /**
  123. * Clear the serial line and request a resend of
  124. * the next expected line number.
  125. */
  126. static void flush_and_request_resend();
  127. private:
  128. static uint8_t index_w; // Ring buffer write position
  129. static void get_serial_commands();
  130. #if ENABLED(SDSUPPORT)
  131. static void get_sdcard_commands();
  132. #endif
  133. static void _commit_command(bool say_ok
  134. #if HAS_MULTI_SERIAL
  135. , int16_t p=-1
  136. #endif
  137. );
  138. static bool _enqueue(const char* cmd, bool say_ok=false
  139. #if HAS_MULTI_SERIAL
  140. , int16_t p=-1
  141. #endif
  142. );
  143. // Process the next "immediate" command (PROGMEM)
  144. static bool process_injected_command_P();
  145. // Process the next "immediate" command (SRAM)
  146. static bool process_injected_command();
  147. /**
  148. * Enqueue with Serial Echo
  149. * Return true on success
  150. */
  151. static bool enqueue_one(const char* cmd);
  152. static void gcode_line_error(PGM_P const err, const int8_t pn);
  153. };
  154. extern GCodeQueue queue;