My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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