My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

queue.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. /**
  23. * queue.cpp - The G-code command queue
  24. */
  25. #include "queue.h"
  26. GCodeQueue queue;
  27. #include "gcode.h"
  28. #include "../lcd/marlinui.h"
  29. #include "../sd/cardreader.h"
  30. #include "../module/motion.h"
  31. #include "../module/planner.h"
  32. #include "../module/temperature.h"
  33. #include "../MarlinCore.h"
  34. #if ENABLED(PRINTER_EVENT_LEDS)
  35. #include "../feature/leds/printer_event_leds.h"
  36. #endif
  37. #if HAS_ETHERNET
  38. #include "../feature/ethernet.h"
  39. #endif
  40. #if ENABLED(BINARY_FILE_TRANSFER)
  41. #include "../feature/binary_stream.h"
  42. #endif
  43. #if ENABLED(MEATPACK)
  44. #include "../feature/meatpack.h"
  45. #endif
  46. #if ENABLED(POWER_LOSS_RECOVERY)
  47. #include "../feature/powerloss.h"
  48. #endif
  49. #if ENABLED(GCODE_REPEAT_MARKERS)
  50. #include "../feature/repeat.h"
  51. #endif
  52. // Frequently used G-code strings
  53. PGMSTR(G28_STR, "G28");
  54. /**
  55. * GCode line number handling. Hosts may opt to include line numbers when
  56. * sending commands to Marlin, and lines will be checked for sequentiality.
  57. * M110 N<int> sets the current line number.
  58. */
  59. long GCodeQueue::last_N[NUM_SERIAL];
  60. /**
  61. * GCode Command Queue
  62. * A simple ring buffer of BUFSIZE command strings.
  63. *
  64. * Commands are copied into this buffer by the command injectors
  65. * (immediate, serial, sd card) and they are processed sequentially by
  66. * the main loop. The gcode.process_next_command method parses the next
  67. * command and hands off execution to individual handler functions.
  68. */
  69. uint8_t GCodeQueue::length = 0, // Count of commands in the queue
  70. GCodeQueue::index_r = 0, // Ring buffer read position
  71. GCodeQueue::index_w = 0; // Ring buffer write position
  72. char GCodeQueue::command_buffer[BUFSIZE][MAX_CMD_SIZE];
  73. /*
  74. * The port that the command was received on
  75. */
  76. #if HAS_MULTI_SERIAL
  77. serial_index_t GCodeQueue::port[BUFSIZE];
  78. #endif
  79. /**
  80. * Serial command injection
  81. */
  82. // Number of characters read in the current line of serial input
  83. static int serial_count[NUM_SERIAL] = { 0 };
  84. bool send_ok[BUFSIZE];
  85. /**
  86. * Next Injected PROGMEM Command pointer. (nullptr == empty)
  87. * Internal commands are enqueued ahead of serial / SD commands.
  88. */
  89. PGM_P GCodeQueue::injected_commands_P; // = nullptr
  90. /**
  91. * Injected SRAM Commands
  92. */
  93. char GCodeQueue::injected_commands[64]; // = { 0 }
  94. GCodeQueue::GCodeQueue() {
  95. // Send "ok" after commands by default
  96. LOOP_L_N(i, COUNT(send_ok)) send_ok[i] = true;
  97. }
  98. /**
  99. * Check whether there are any commands yet to be executed
  100. */
  101. bool GCodeQueue::has_commands_queued() {
  102. return queue.length || injected_commands_P || injected_commands[0];
  103. }
  104. /**
  105. * Clear the Marlin command queue
  106. */
  107. void GCodeQueue::clear() {
  108. index_r = index_w = length = 0;
  109. }
  110. /**
  111. * Once a new command is in the ring buffer, call this to commit it
  112. */
  113. void GCodeQueue::_commit_command(bool say_ok
  114. #if HAS_MULTI_SERIAL
  115. , serial_index_t serial_ind/*=-1*/
  116. #endif
  117. ) {
  118. send_ok[index_w] = say_ok;
  119. TERN_(HAS_MULTI_SERIAL, port[index_w] = serial_ind);
  120. TERN_(POWER_LOSS_RECOVERY, recovery.commit_sdpos(index_w));
  121. if (++index_w >= BUFSIZE) index_w = 0;
  122. length++;
  123. }
  124. /**
  125. * Copy a command from RAM into the main command buffer.
  126. * Return true if the command was successfully added.
  127. * Return false for a full buffer, or if the 'command' is a comment.
  128. */
  129. bool GCodeQueue::_enqueue(const char* cmd, bool say_ok/*=false*/
  130. #if HAS_MULTI_SERIAL
  131. , serial_index_t serial_ind/*=-1*/
  132. #endif
  133. ) {
  134. if (*cmd == ';' || length >= BUFSIZE) return false;
  135. strcpy(command_buffer[index_w], cmd);
  136. _commit_command(say_ok
  137. #if HAS_MULTI_SERIAL
  138. , serial_ind
  139. #endif
  140. );
  141. return true;
  142. }
  143. /**
  144. * Enqueue with Serial Echo
  145. * Return true if the command was consumed
  146. */
  147. bool GCodeQueue::enqueue_one(const char* cmd) {
  148. //SERIAL_ECHOPGM("enqueue_one(\"");
  149. //SERIAL_ECHO(cmd);
  150. //SERIAL_ECHOPGM("\") \n");
  151. if (*cmd == 0 || ISEOL(*cmd)) return true;
  152. if (_enqueue(cmd)) {
  153. SERIAL_ECHO_MSG(STR_ENQUEUEING, cmd, "\"");
  154. return true;
  155. }
  156. return false;
  157. }
  158. /**
  159. * Process the next "immediate" command from PROGMEM.
  160. * Return 'true' if any commands were processed.
  161. */
  162. bool GCodeQueue::process_injected_command_P() {
  163. if (!injected_commands_P) return false;
  164. char c;
  165. size_t i = 0;
  166. while ((c = pgm_read_byte(&injected_commands_P[i])) && c != '\n') i++;
  167. // Extract current command and move pointer to next command
  168. char cmd[i + 1];
  169. memcpy_P(cmd, injected_commands_P, i);
  170. cmd[i] = '\0';
  171. injected_commands_P = c ? injected_commands_P + i + 1 : nullptr;
  172. // Execute command if non-blank
  173. if (i) {
  174. parser.parse(cmd);
  175. gcode.process_parsed_command();
  176. }
  177. return true;
  178. }
  179. /**
  180. * Process the next "immediate" command from SRAM.
  181. * Return 'true' if any commands were processed.
  182. */
  183. bool GCodeQueue::process_injected_command() {
  184. if (injected_commands[0] == '\0') return false;
  185. char c;
  186. size_t i = 0;
  187. while ((c = injected_commands[i]) && c != '\n') i++;
  188. // Execute a non-blank command
  189. if (i) {
  190. injected_commands[i] = '\0';
  191. parser.parse(injected_commands);
  192. gcode.process_parsed_command();
  193. }
  194. // Copy the next command into place
  195. for (
  196. uint8_t d = 0, s = i + !!c; // dst, src
  197. (injected_commands[d] = injected_commands[s]); // copy, exit if 0
  198. d++, s++ // next dst, src
  199. );
  200. return true;
  201. }
  202. /**
  203. * Enqueue and return only when commands are actually enqueued.
  204. * Never call this from a G-code handler!
  205. */
  206. void GCodeQueue::enqueue_one_now(const char* cmd) { while (!enqueue_one(cmd)) idle(); }
  207. /**
  208. * Attempt to enqueue a single G-code command
  209. * and return 'true' if successful.
  210. */
  211. bool GCodeQueue::enqueue_one_P(PGM_P const pgcode) {
  212. size_t i = 0;
  213. PGM_P p = pgcode;
  214. char c;
  215. while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
  216. char cmd[i + 1];
  217. memcpy_P(cmd, p, i);
  218. cmd[i] = '\0';
  219. return _enqueue(cmd);
  220. }
  221. /**
  222. * Enqueue from program memory and return only when commands are actually enqueued
  223. * Never call this from a G-code handler!
  224. */
  225. void GCodeQueue::enqueue_now_P(PGM_P const pgcode) {
  226. size_t i = 0;
  227. PGM_P p = pgcode;
  228. for (;;) {
  229. char c;
  230. while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
  231. char cmd[i + 1];
  232. memcpy_P(cmd, p, i);
  233. cmd[i] = '\0';
  234. enqueue_one_now(cmd);
  235. if (!c) break;
  236. p += i + 1;
  237. }
  238. }
  239. /**
  240. * Send an "ok" message to the host, indicating
  241. * that a command was successfully processed.
  242. *
  243. * If ADVANCED_OK is enabled also include:
  244. * N<int> Line number of the command, if any
  245. * P<int> Planner space remaining
  246. * B<int> Block queue space remaining
  247. */
  248. void GCodeQueue::ok_to_send() {
  249. #if HAS_MULTI_SERIAL
  250. const serial_index_t serial_ind = command_port();
  251. if (serial_ind < 0) return;
  252. PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
  253. #endif
  254. if (!send_ok[index_r]) return;
  255. SERIAL_ECHOPGM(STR_OK);
  256. #if ENABLED(ADVANCED_OK)
  257. char* p = command_buffer[index_r];
  258. if (*p == 'N') {
  259. SERIAL_CHAR(' ', *p++);
  260. while (NUMERIC_SIGNED(*p))
  261. SERIAL_CHAR(*p++);
  262. }
  263. SERIAL_ECHOPAIR_P(SP_P_STR, planner.moves_free(),
  264. SP_B_STR, BUFSIZE - length);
  265. #endif
  266. SERIAL_EOL();
  267. }
  268. /**
  269. * Send a "Resend: nnn" message to the host to
  270. * indicate that a command needs to be re-sent.
  271. */
  272. void GCodeQueue::flush_and_request_resend() {
  273. const serial_index_t serial_ind = command_port();
  274. #if HAS_MULTI_SERIAL
  275. if (serial_ind < 0) return; // Never mind. Command came from SD or Flash Drive
  276. PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
  277. #endif
  278. SERIAL_FLUSH();
  279. SERIAL_ECHOPGM(STR_RESEND);
  280. SERIAL_ECHOLN(last_N[serial_ind] + 1);
  281. ok_to_send();
  282. }
  283. inline bool serial_data_available() {
  284. byte data_available = 0;
  285. if (MYSERIAL0.available()) data_available++;
  286. #ifdef SERIAL_PORT_2
  287. const bool port2_open = TERN1(HAS_ETHERNET, ethernet.have_telnet_client);
  288. if (port2_open && MYSERIAL1.available()) data_available++;
  289. #endif
  290. return data_available > 0;
  291. }
  292. inline int read_serial(const uint8_t index) {
  293. switch (index) {
  294. case 0: return MYSERIAL0.read();
  295. case 1: {
  296. #if HAS_MULTI_SERIAL
  297. const bool port2_open = TERN1(HAS_ETHERNET, ethernet.have_telnet_client);
  298. if (port2_open) return MYSERIAL1.read();
  299. #endif
  300. }
  301. default: return -1;
  302. }
  303. }
  304. void GCodeQueue::gcode_line_error(PGM_P const err, const serial_index_t serial_ind) {
  305. PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
  306. SERIAL_ERROR_START();
  307. serialprintPGM(err);
  308. SERIAL_ECHOLN(last_N[serial_ind]);
  309. while (read_serial(serial_ind) != -1); // Clear out the RX buffer
  310. flush_and_request_resend();
  311. serial_count[serial_ind] = 0;
  312. }
  313. FORCE_INLINE bool is_M29(const char * const cmd) { // matches "M29" & "M29 ", but not "M290", etc
  314. const char * const m29 = strstr_P(cmd, PSTR("M29"));
  315. return m29 && !NUMERIC(m29[3]);
  316. }
  317. #define PS_NORMAL 0
  318. #define PS_EOL 1
  319. #define PS_QUOTED 2
  320. #define PS_PAREN 3
  321. #define PS_ESC 4
  322. inline void process_stream_char(const char c, uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
  323. if (sis == PS_EOL) return; // EOL comment or overflow
  324. #if ENABLED(PAREN_COMMENTS)
  325. else if (sis == PS_PAREN) { // Inline comment
  326. if (c == ')') sis = PS_NORMAL;
  327. return;
  328. }
  329. #endif
  330. else if (sis >= PS_ESC) // End escaped char
  331. sis -= PS_ESC;
  332. else if (c == '\\') { // Start escaped char
  333. sis += PS_ESC;
  334. if (sis == PS_ESC) return; // Keep if quoting
  335. }
  336. #if ENABLED(GCODE_QUOTED_STRINGS)
  337. else if (sis == PS_QUOTED) {
  338. if (c == '"') sis = PS_NORMAL; // End quoted string
  339. }
  340. else if (c == '"') // Start quoted string
  341. sis = PS_QUOTED;
  342. #endif
  343. else if (c == ';') { // Start end-of-line comment
  344. sis = PS_EOL;
  345. return;
  346. }
  347. #if ENABLED(PAREN_COMMENTS)
  348. else if (c == '(') { // Start inline comment
  349. sis = PS_PAREN;
  350. return;
  351. }
  352. #endif
  353. // Backspace erases previous characters
  354. if (c == 0x08) {
  355. if (ind) buff[--ind] = '\0';
  356. }
  357. else {
  358. buff[ind++] = c;
  359. if (ind >= MAX_CMD_SIZE - 1)
  360. sis = PS_EOL; // Skip the rest on overflow
  361. }
  362. }
  363. /**
  364. * Handle a line being completed. For an empty line
  365. * keep sensor readings going and watchdog alive.
  366. */
  367. inline bool process_line_done(uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
  368. sis = PS_NORMAL; // "Normal" Serial Input State
  369. buff[ind] = '\0'; // Of course, I'm a Terminator.
  370. const bool is_empty = (ind == 0); // An empty line?
  371. if (is_empty)
  372. thermalManager.manage_heater(); // Keep sensors satisfied
  373. else
  374. ind = 0; // Start a new line
  375. return is_empty; // Inform the caller
  376. }
  377. /**
  378. * Get all commands waiting on the serial port and queue them.
  379. * Exit when the buffer is full or when no more characters are
  380. * left on the serial port.
  381. */
  382. void GCodeQueue::get_serial_commands() {
  383. static char serial_line_buffer[NUM_SERIAL][MAX_CMD_SIZE];
  384. static uint8_t serial_input_state[NUM_SERIAL] = { PS_NORMAL };
  385. #if ENABLED(BINARY_FILE_TRANSFER)
  386. if (card.flag.binary_mode) {
  387. /**
  388. * For binary stream file transfer, use serial_line_buffer as the working
  389. * receive buffer (which limits the packet size to MAX_CMD_SIZE).
  390. * The receive buffer also limits the packet size for reliable transmission.
  391. */
  392. binaryStream[card.transfer_port_index].receive(serial_line_buffer[card.transfer_port_index]);
  393. return;
  394. }
  395. #endif
  396. // If the command buffer is empty for too long,
  397. // send "wait" to indicate Marlin is still waiting.
  398. #if NO_TIMEOUTS > 0
  399. static millis_t last_command_time = 0;
  400. const millis_t ms = millis();
  401. if (length == 0 && !serial_data_available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
  402. SERIAL_ECHOLNPGM(STR_WAIT);
  403. last_command_time = ms;
  404. }
  405. #endif
  406. /**
  407. * Loop while serial characters are incoming and the queue is not full
  408. */
  409. while (length < BUFSIZE && serial_data_available()) {
  410. LOOP_L_N(p, NUM_SERIAL) {
  411. const int c = read_serial(p);
  412. if (c < 0) continue;
  413. #if ENABLED(MEATPACK)
  414. meatpack.handle_rx_char(uint8_t(c), p);
  415. char c_res[2] = { 0, 0 };
  416. const uint8_t char_count = meatpack.get_result_char(c_res);
  417. #else
  418. constexpr uint8_t char_count = 1;
  419. #endif
  420. LOOP_L_N(char_index, char_count) {
  421. const char serial_char = TERN(MEATPACK, c_res[char_index], c);
  422. if (ISEOL(serial_char)) {
  423. // Reset our state, continue if the line was empty
  424. if (process_line_done(serial_input_state[p], serial_line_buffer[p], serial_count[p]))
  425. continue;
  426. char* command = serial_line_buffer[p];
  427. while (*command == ' ') command++; // Skip leading spaces
  428. char *npos = (*command == 'N') ? command : nullptr; // Require the N parameter to start the line
  429. if (npos) {
  430. const bool M110 = !!strstr_P(command, PSTR("M110"));
  431. if (M110) {
  432. char* n2pos = strchr(command + 4, 'N');
  433. if (n2pos) npos = n2pos;
  434. }
  435. const long gcode_N = strtol(npos + 1, nullptr, 10);
  436. if (gcode_N != last_N[p] + 1 && !M110)
  437. return gcode_line_error(PSTR(STR_ERR_LINE_NO), p);
  438. char *apos = strrchr(command, '*');
  439. if (apos) {
  440. uint8_t checksum = 0, count = uint8_t(apos - command);
  441. while (count) checksum ^= command[--count];
  442. if (strtol(apos + 1, nullptr, 10) != checksum)
  443. return gcode_line_error(PSTR(STR_ERR_CHECKSUM_MISMATCH), p);
  444. }
  445. else
  446. return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), p);
  447. last_N[p] = gcode_N;
  448. }
  449. #if ENABLED(SDSUPPORT)
  450. // Pronterface "M29" and "M29 " has no line number
  451. else if (card.flag.saving && !is_M29(command))
  452. return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), p);
  453. #endif
  454. //
  455. // Movement commands give an alert when the machine is stopped
  456. //
  457. if (IsStopped()) {
  458. char* gpos = strchr(command, 'G');
  459. if (gpos) {
  460. switch (strtol(gpos + 1, nullptr, 10)) {
  461. case 0: case 1:
  462. #if ENABLED(ARC_SUPPORT)
  463. case 2: case 3:
  464. #endif
  465. #if ENABLED(BEZIER_CURVE_SUPPORT)
  466. case 5:
  467. #endif
  468. PORT_REDIRECT(SERIAL_PORTMASK(p)); // Reply to the serial port that sent the command
  469. SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
  470. LCD_MESSAGEPGM(MSG_STOPPED);
  471. break;
  472. }
  473. }
  474. }
  475. #if DISABLED(EMERGENCY_PARSER)
  476. // Process critical commands early
  477. if (command[0] == 'M') switch (command[3]) {
  478. case '8': if (command[2] == '0' && command[1] == '1') { wait_for_heatup = false; TERN_(HAS_LCD_MENU, wait_for_user = false); } break;
  479. case '2': if (command[2] == '1' && command[1] == '1') kill(M112_KILL_STR, nullptr, true); break;
  480. case '0': if (command[1] == '4' && command[2] == '1') quickstop_stepper(); break;
  481. }
  482. #endif
  483. #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
  484. last_command_time = ms;
  485. #endif
  486. // Add the command to the queue
  487. _enqueue(serial_line_buffer[p], true
  488. #if HAS_MULTI_SERIAL
  489. , p
  490. #endif
  491. );
  492. }
  493. else
  494. process_stream_char(serial_char, serial_input_state[p], serial_line_buffer[p], serial_count[p]);
  495. } // char_count loop
  496. } // NUM_SERIAL loop
  497. } // queue has space, serial has data
  498. }
  499. #if ENABLED(SDSUPPORT)
  500. /**
  501. * Get lines from the SD Card until the command buffer is full
  502. * or until the end of the file is reached. Because this method
  503. * always receives complete command-lines, they can go directly
  504. * into the main command queue.
  505. */
  506. inline void GCodeQueue::get_sdcard_commands() {
  507. static uint8_t sd_input_state = PS_NORMAL;
  508. if (!IS_SD_PRINTING()) return;
  509. int sd_count = 0;
  510. while (length < BUFSIZE && !card.eof()) {
  511. const int16_t n = card.get();
  512. const bool card_eof = card.eof();
  513. if (n < 0 && !card_eof) { SERIAL_ERROR_MSG(STR_SD_ERR_READ); continue; }
  514. const char sd_char = (char)n;
  515. const bool is_eol = ISEOL(sd_char);
  516. if (is_eol || card_eof) {
  517. // Reset stream state, terminate the buffer, and commit a non-empty command
  518. if (!is_eol && sd_count) ++sd_count; // End of file with no newline
  519. if (!process_line_done(sd_input_state, command_buffer[index_w], sd_count)) {
  520. // M808 S saves the sdpos of the next line. M808 loops to a new sdpos.
  521. TERN_(GCODE_REPEAT_MARKERS, repeat.early_parse_M808(command_buffer[index_w]));
  522. // Put the new command into the buffer (no "ok" sent)
  523. _commit_command(false);
  524. // Prime Power-Loss Recovery for the NEXT _commit_command
  525. TERN_(POWER_LOSS_RECOVERY, recovery.cmd_sdpos = card.getIndex());
  526. }
  527. if (card.eof()) card.fileHasFinished(); // Handle end of file reached
  528. }
  529. else
  530. process_stream_char(sd_char, sd_input_state, command_buffer[index_w], sd_count);
  531. }
  532. }
  533. #endif // SDSUPPORT
  534. /**
  535. * Add to the circular command queue the next command from:
  536. * - The command-injection queues (injected_commands_P, injected_commands)
  537. * - The active serial input (usually USB)
  538. * - The SD card file being actively printed
  539. */
  540. void GCodeQueue::get_available_commands() {
  541. get_serial_commands();
  542. TERN_(SDSUPPORT, get_sdcard_commands());
  543. }
  544. /**
  545. * Get the next command in the queue, optionally log it to SD, then dispatch it
  546. */
  547. void GCodeQueue::advance() {
  548. // Process immediate commands
  549. if (process_injected_command_P() || process_injected_command()) return;
  550. // Return if the G-code buffer is empty
  551. if (!length) return;
  552. #if ENABLED(SDSUPPORT)
  553. if (card.flag.saving) {
  554. char* command = command_buffer[index_r];
  555. if (is_M29(command)) {
  556. // M29 closes the file
  557. card.closefile();
  558. SERIAL_ECHOLNPGM(STR_FILE_SAVED);
  559. #if !defined(__AVR__) || !defined(USBCON)
  560. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  561. SERIAL_ECHOLNPAIR("Dropped bytes: ", MYSERIAL0.dropped());
  562. #endif
  563. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  564. SERIAL_ECHOLNPAIR("Max RX Queue Size: ", MYSERIAL0.rxMaxEnqueued());
  565. #endif
  566. #endif
  567. ok_to_send();
  568. }
  569. else {
  570. // Write the string from the read buffer to SD
  571. card.write_command(command);
  572. if (card.flag.logging)
  573. gcode.process_next_command(); // The card is saving because it's logging
  574. else
  575. ok_to_send();
  576. }
  577. }
  578. else
  579. gcode.process_next_command();
  580. #else
  581. gcode.process_next_command();
  582. #endif // SDSUPPORT
  583. // The queue may be reset by a command handler or by code invoked by idle() within a handler
  584. --length;
  585. if (++index_r >= BUFSIZE) index_r = 0;
  586. }