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

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