My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

queue.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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_ECHO(' ');
  260. SERIAL_ECHO(*p++);
  261. while (NUMERIC_SIGNED(*p))
  262. SERIAL_ECHO(*p++);
  263. }
  264. SERIAL_ECHOPAIR_P(SP_P_STR, int(planner.moves_free()),
  265. SP_B_STR, int(BUFSIZE - length));
  266. #endif
  267. SERIAL_EOL();
  268. }
  269. /**
  270. * Send a "Resend: nnn" message to the host to
  271. * indicate that a command needs to be re-sent.
  272. */
  273. void GCodeQueue::flush_and_request_resend() {
  274. const serial_index_t serial_ind = command_port();
  275. #if HAS_MULTI_SERIAL
  276. if (serial_ind < 0) return; // Never mind. Command came from SD or Flash Drive
  277. PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
  278. #endif
  279. SERIAL_FLUSH();
  280. SERIAL_ECHOPGM(STR_RESEND);
  281. SERIAL_ECHOLN(last_N[serial_ind] + 1);
  282. ok_to_send();
  283. }
  284. inline bool serial_data_available() {
  285. byte data_available = 0;
  286. if (MYSERIAL0.available()) data_available++;
  287. #ifdef SERIAL_PORT_2
  288. const bool port2_open = TERN1(HAS_ETHERNET, ethernet.have_telnet_client);
  289. if (port2_open && MYSERIAL1.available()) data_available++;
  290. #endif
  291. return data_available > 0;
  292. }
  293. inline int read_serial(const uint8_t index) {
  294. switch (index) {
  295. case 0: return MYSERIAL0.read();
  296. case 1: {
  297. #if HAS_MULTI_SERIAL
  298. const bool port2_open = TERN1(HAS_ETHERNET, ethernet.have_telnet_client);
  299. if (port2_open) return MYSERIAL1.read();
  300. #endif
  301. }
  302. default: return -1;
  303. }
  304. }
  305. void GCodeQueue::gcode_line_error(PGM_P const err, const serial_index_t serial_ind) {
  306. PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
  307. SERIAL_ERROR_START();
  308. serialprintPGM(err);
  309. SERIAL_ECHOLN(last_N[serial_ind]);
  310. while (read_serial(serial_ind) != -1); // Clear out the RX buffer
  311. flush_and_request_resend();
  312. serial_count[serial_ind] = 0;
  313. }
  314. FORCE_INLINE bool is_M29(const char * const cmd) { // matches "M29" & "M29 ", but not "M290", etc
  315. const char * const m29 = strstr_P(cmd, PSTR("M29"));
  316. return m29 && !NUMERIC(m29[3]);
  317. }
  318. #define PS_NORMAL 0
  319. #define PS_EOL 1
  320. #define PS_QUOTED 2
  321. #define PS_PAREN 3
  322. #define PS_ESC 4
  323. inline void process_stream_char(const char c, uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
  324. if (sis == PS_EOL) return; // EOL comment or overflow
  325. #if ENABLED(PAREN_COMMENTS)
  326. else if (sis == PS_PAREN) { // Inline comment
  327. if (c == ')') sis = PS_NORMAL;
  328. return;
  329. }
  330. #endif
  331. else if (sis >= PS_ESC) // End escaped char
  332. sis -= PS_ESC;
  333. else if (c == '\\') { // Start escaped char
  334. sis += PS_ESC;
  335. if (sis == PS_ESC) return; // Keep if quoting
  336. }
  337. #if ENABLED(GCODE_QUOTED_STRINGS)
  338. else if (sis == PS_QUOTED) {
  339. if (c == '"') sis = PS_NORMAL; // End quoted string
  340. }
  341. else if (c == '"') // Start quoted string
  342. sis = PS_QUOTED;
  343. #endif
  344. else if (c == ';') { // Start end-of-line comment
  345. sis = PS_EOL;
  346. return;
  347. }
  348. #if ENABLED(PAREN_COMMENTS)
  349. else if (c == '(') { // Start inline comment
  350. sis = PS_PAREN;
  351. return;
  352. }
  353. #endif
  354. // Backspace erases previous characters
  355. if (c == 0x08) {
  356. if (ind) buff[--ind] = '\0';
  357. }
  358. else {
  359. buff[ind++] = c;
  360. if (ind >= MAX_CMD_SIZE - 1)
  361. sis = PS_EOL; // Skip the rest on overflow
  362. }
  363. }
  364. /**
  365. * Handle a line being completed. For an empty line
  366. * keep sensor readings going and watchdog alive.
  367. */
  368. inline bool process_line_done(uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
  369. sis = PS_NORMAL; // "Normal" Serial Input State
  370. buff[ind] = '\0'; // Of course, I'm a Terminator.
  371. const bool is_empty = (ind == 0); // An empty line?
  372. if (is_empty)
  373. thermalManager.manage_heater(); // Keep sensors satisfied
  374. else
  375. ind = 0; // Start a new line
  376. return is_empty; // Inform the caller
  377. }
  378. /**
  379. * Get all commands waiting on the serial port and queue them.
  380. * Exit when the buffer is full or when no more characters are
  381. * left on the serial port.
  382. */
  383. void GCodeQueue::get_serial_commands() {
  384. static char serial_line_buffer[NUM_SERIAL][MAX_CMD_SIZE];
  385. static uint8_t serial_input_state[NUM_SERIAL] = { PS_NORMAL };
  386. #if ENABLED(BINARY_FILE_TRANSFER)
  387. if (card.flag.binary_mode) {
  388. /**
  389. * For binary stream file transfer, use serial_line_buffer as the working
  390. * receive buffer (which limits the packet size to MAX_CMD_SIZE).
  391. * The receive buffer also limits the packet size for reliable transmission.
  392. */
  393. binaryStream[card.transfer_port_index].receive(serial_line_buffer[card.transfer_port_index]);
  394. return;
  395. }
  396. #endif
  397. // If the command buffer is empty for too long,
  398. // send "wait" to indicate Marlin is still waiting.
  399. #if NO_TIMEOUTS > 0
  400. static millis_t last_command_time = 0;
  401. const millis_t ms = millis();
  402. if (length == 0 && !serial_data_available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
  403. SERIAL_ECHOLNPGM(STR_WAIT);
  404. last_command_time = ms;
  405. }
  406. #endif
  407. /**
  408. * Loop while serial characters are incoming and the queue is not full
  409. */
  410. while (length < BUFSIZE && serial_data_available()) {
  411. LOOP_L_N(p, NUM_SERIAL) {
  412. const int c = read_serial(p);
  413. if (c < 0) continue;
  414. #if ENABLED(MEATPACK)
  415. meatpack.handle_rx_char(uint8_t(c), p);
  416. char c_res[2] = { 0, 0 };
  417. const uint8_t char_count = meatpack.get_result_char(c_res);
  418. #else
  419. constexpr uint8_t char_count = 1;
  420. #endif
  421. LOOP_L_N(char_index, char_count) {
  422. const char serial_char = TERN(MEATPACK, c_res[char_index], c);
  423. if (ISEOL(serial_char)) {
  424. // Reset our state, continue if the line was empty
  425. if (process_line_done(serial_input_state[p], serial_line_buffer[p], serial_count[p]))
  426. continue;
  427. char* command = serial_line_buffer[p];
  428. while (*command == ' ') command++; // Skip leading spaces
  429. char *npos = (*command == 'N') ? command : nullptr; // Require the N parameter to start the line
  430. if (npos) {
  431. const bool M110 = !!strstr_P(command, PSTR("M110"));
  432. if (M110) {
  433. char* n2pos = strchr(command + 4, 'N');
  434. if (n2pos) npos = n2pos;
  435. }
  436. const long gcode_N = strtol(npos + 1, nullptr, 10);
  437. if (gcode_N != last_N[p] + 1 && !M110)
  438. return gcode_line_error(PSTR(STR_ERR_LINE_NO), p);
  439. char *apos = strrchr(command, '*');
  440. if (apos) {
  441. uint8_t checksum = 0, count = uint8_t(apos - command);
  442. while (count) checksum ^= command[--count];
  443. if (strtol(apos + 1, nullptr, 10) != checksum)
  444. return gcode_line_error(PSTR(STR_ERR_CHECKSUM_MISMATCH), p);
  445. }
  446. else
  447. return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), p);
  448. last_N[p] = gcode_N;
  449. }
  450. #if ENABLED(SDSUPPORT)
  451. // Pronterface "M29" and "M29 " has no line number
  452. else if (card.flag.saving && !is_M29(command))
  453. return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), p);
  454. #endif
  455. //
  456. // Movement commands give an alert when the machine is stopped
  457. //
  458. if (IsStopped()) {
  459. char* gpos = strchr(command, 'G');
  460. if (gpos) {
  461. switch (strtol(gpos + 1, nullptr, 10)) {
  462. case 0: case 1:
  463. #if ENABLED(ARC_SUPPORT)
  464. case 2: case 3:
  465. #endif
  466. #if ENABLED(BEZIER_CURVE_SUPPORT)
  467. case 5:
  468. #endif
  469. PORT_REDIRECT(SERIAL_PORTMASK(p)); // Reply to the serial port that sent the command
  470. SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
  471. LCD_MESSAGEPGM(MSG_STOPPED);
  472. break;
  473. }
  474. }
  475. }
  476. #if DISABLED(EMERGENCY_PARSER)
  477. // Process critical commands early
  478. if (command[0] == 'M') switch (command[3]) {
  479. case '8': if (command[2] == '0' && command[1] == '1') { wait_for_heatup = false; TERN_(HAS_LCD_MENU, wait_for_user = false); } break;
  480. case '2': if (command[2] == '1' && command[1] == '1') kill(M112_KILL_STR, nullptr, true); break;
  481. case '0': if (command[1] == '4' && command[2] == '1') quickstop_stepper(); break;
  482. }
  483. #endif
  484. #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
  485. last_command_time = ms;
  486. #endif
  487. // Add the command to the queue
  488. _enqueue(serial_line_buffer[p], true
  489. #if HAS_MULTI_SERIAL
  490. , p
  491. #endif
  492. );
  493. }
  494. else
  495. process_stream_char(serial_char, serial_input_state[p], serial_line_buffer[p], serial_count[p]);
  496. } // char_count loop
  497. } // NUM_SERIAL loop
  498. } // queue has space, serial has data
  499. }
  500. #if ENABLED(SDSUPPORT)
  501. /**
  502. * Get lines from the SD Card until the command buffer is full
  503. * or until the end of the file is reached. Because this method
  504. * always receives complete command-lines, they can go directly
  505. * into the main command queue.
  506. */
  507. inline void GCodeQueue::get_sdcard_commands() {
  508. static uint8_t sd_input_state = PS_NORMAL;
  509. if (!IS_SD_PRINTING()) return;
  510. int sd_count = 0;
  511. while (length < BUFSIZE && !card.eof()) {
  512. const int16_t n = card.get();
  513. const bool card_eof = card.eof();
  514. if (n < 0 && !card_eof) { SERIAL_ERROR_MSG(STR_SD_ERR_READ); continue; }
  515. const char sd_char = (char)n;
  516. const bool is_eol = ISEOL(sd_char);
  517. if (is_eol || card_eof) {
  518. // Reset stream state, terminate the buffer, and commit a non-empty command
  519. if (!is_eol && sd_count) ++sd_count; // End of file with no newline
  520. if (!process_line_done(sd_input_state, command_buffer[index_w], sd_count)) {
  521. // M808 S saves the sdpos of the next line. M808 loops to a new sdpos.
  522. TERN_(GCODE_REPEAT_MARKERS, repeat.early_parse_M808(command_buffer[index_w]));
  523. // Put the new command into the buffer (no "ok" sent)
  524. _commit_command(false);
  525. // Prime Power-Loss Recovery for the NEXT _commit_command
  526. TERN_(POWER_LOSS_RECOVERY, recovery.cmd_sdpos = card.getIndex());
  527. }
  528. if (card.eof()) card.fileHasFinished(); // Handle end of file reached
  529. }
  530. else
  531. process_stream_char(sd_char, sd_input_state, command_buffer[index_w], sd_count);
  532. }
  533. }
  534. #endif // SDSUPPORT
  535. /**
  536. * Add to the circular command queue the next command from:
  537. * - The command-injection queues (injected_commands_P, injected_commands)
  538. * - The active serial input (usually USB)
  539. * - The SD card file being actively printed
  540. */
  541. void GCodeQueue::get_available_commands() {
  542. get_serial_commands();
  543. TERN_(SDSUPPORT, get_sdcard_commands());
  544. }
  545. /**
  546. * Get the next command in the queue, optionally log it to SD, then dispatch it
  547. */
  548. void GCodeQueue::advance() {
  549. // Process immediate commands
  550. if (process_injected_command_P() || process_injected_command()) return;
  551. // Return if the G-code buffer is empty
  552. if (!length) return;
  553. #if ENABLED(SDSUPPORT)
  554. if (card.flag.saving) {
  555. char* command = command_buffer[index_r];
  556. if (is_M29(command)) {
  557. // M29 closes the file
  558. card.closefile();
  559. SERIAL_ECHOLNPGM(STR_FILE_SAVED);
  560. #if !defined(__AVR__) || !defined(USBCON)
  561. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  562. SERIAL_ECHOLNPAIR("Dropped bytes: ", MYSERIAL0.dropped());
  563. #endif
  564. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  565. SERIAL_ECHOLNPAIR("Max RX Queue Size: ", MYSERIAL0.rxMaxEnqueued());
  566. #endif
  567. #endif
  568. ok_to_send();
  569. }
  570. else {
  571. // Write the string from the read buffer to SD
  572. card.write_command(command);
  573. if (card.flag.logging)
  574. gcode.process_next_command(); // The card is saving because it's logging
  575. else
  576. ok_to_send();
  577. }
  578. }
  579. else
  580. gcode.process_next_command();
  581. #else
  582. gcode.process_next_command();
  583. #endif // SDSUPPORT
  584. // The queue may be reset by a command handler or by code invoked by idle() within a handler
  585. --length;
  586. if (++index_r >= BUFSIZE) index_r = 0;
  587. }