cmdqueue.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. #include <util/atomic.h>
  2. #include "cmdqueue.h"
  3. #include "cardreader.h"
  4. #include "ultralcd.h"
  5. #include "Prusa_farm.h"
  6. // Reserve BUFSIZE lines of length MAX_CMD_SIZE plus CMDBUFFER_RESERVE_FRONT.
  7. char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT];
  8. // Head of the circular buffer, where to read.
  9. size_t bufindr = 0;
  10. // Tail of the buffer, where to write.
  11. static size_t bufindw = 0;
  12. // Number of lines in cmdbuffer.
  13. int buflen = 0;
  14. // Flag for processing the current command inside the main Arduino loop().
  15. // If a new command was pushed to the front of a command buffer while
  16. // processing another command, this replaces the command on the top.
  17. // Therefore don't remove the command from the queue in the loop() function.
  18. bool cmdbuffer_front_already_processed = false;
  19. // Used for temporarely preventing accidental adding of Serial commands to the queue.
  20. // For now only check_file and the fancheck pause use this.
  21. bool cmdqueue_serial_disabled = false;
  22. int serial_count = 0; //index of character read from serial line
  23. bool comment_mode = false;
  24. char *strchr_pointer; // just a pointer to find chars in the command string like X, Y, Z, E, etc
  25. ShortTimer serialTimeoutTimer;
  26. long gcode_LastN = 0;
  27. uint32_t sdpos_atomic = 0;
  28. // Pop the currently processed command from the queue.
  29. // It is expected, that there is at least one command in the queue.
  30. bool cmdqueue_pop_front()
  31. {
  32. if (buflen > 0) {
  33. #ifdef CMDBUFFER_DEBUG
  34. SERIAL_ECHOPGM("Dequeing ");
  35. SERIAL_ECHO(cmdbuffer+bufindr+CMDHDRSIZE);
  36. SERIAL_ECHOLNPGM("");
  37. SERIAL_ECHOPGM("Old indices: buflen ");
  38. SERIAL_ECHO(buflen);
  39. SERIAL_ECHOPGM(", bufindr ");
  40. SERIAL_ECHO(bufindr);
  41. SERIAL_ECHOPGM(", bufindw ");
  42. SERIAL_ECHO(bufindw);
  43. SERIAL_ECHOPGM(", serial_count ");
  44. SERIAL_ECHO(serial_count);
  45. SERIAL_ECHOPGM(", bufsize ");
  46. SERIAL_ECHO(sizeof(cmdbuffer));
  47. SERIAL_ECHOLNPGM("");
  48. #endif /* CMDBUFFER_DEBUG */
  49. if (-- buflen == 0) {
  50. // Empty buffer.
  51. if (serial_count == 0)
  52. // No serial communication is pending. Reset both pointers to zero.
  53. bufindw = 0;
  54. bufindr = bufindw;
  55. } else {
  56. // There is at least one ready line in the buffer.
  57. // First skip the current command ID and iterate up to the end of the string.
  58. for (bufindr += CMDHDRSIZE; cmdbuffer[bufindr] != 0; ++ bufindr) ;
  59. // Second, skip the end of string null character and iterate until a nonzero command ID is found.
  60. for (++ bufindr; bufindr < sizeof(cmdbuffer) && cmdbuffer[bufindr] == 0; ++ bufindr) ;
  61. // If the end of the buffer was empty,
  62. if (bufindr == sizeof(cmdbuffer)) {
  63. // skip to the start and find the nonzero command.
  64. for (bufindr = 0; cmdbuffer[bufindr] == 0; ++ bufindr) ;
  65. }
  66. #ifdef CMDBUFFER_DEBUG
  67. SERIAL_ECHOPGM("New indices: buflen ");
  68. SERIAL_ECHO(buflen);
  69. SERIAL_ECHOPGM(", bufindr ");
  70. SERIAL_ECHO(bufindr);
  71. SERIAL_ECHOPGM(", bufindw ");
  72. SERIAL_ECHO(bufindw);
  73. SERIAL_ECHOPGM(", serial_count ");
  74. SERIAL_ECHO(serial_count);
  75. SERIAL_ECHOPGM(" new command on the top: ");
  76. SERIAL_ECHO(cmdbuffer+bufindr+CMDHDRSIZE);
  77. SERIAL_ECHOLNPGM("");
  78. #endif /* CMDBUFFER_DEBUG */
  79. }
  80. return true;
  81. }
  82. return false;
  83. }
  84. void cmdqueue_reset()
  85. {
  86. while (buflen)
  87. {
  88. // printf_P(PSTR("dumping: \"%s\" of type %u\n"), cmdbuffer+bufindr+CMDHDRSIZE, CMDBUFFER_CURRENT_TYPE);
  89. ClearToSend();
  90. cmdqueue_pop_front();
  91. }
  92. bufindr = 0;
  93. bufindw = 0;
  94. //commands are removed from command queue after process_command() function is finished
  95. //reseting command queue and enqueing new commands during some (usually long running) command processing would cause that new commands are immediately removed from queue (or damaged)
  96. //this will ensure that all new commands which are enqueued after cmdqueue reset, will be always executed
  97. cmdbuffer_front_already_processed = true;
  98. }
  99. // How long a string could be pushed to the front of the command queue?
  100. // If yes, adjust bufindr to the new position, where the new command could be enqued.
  101. // len_asked does not contain the zero terminator size.
  102. static bool cmdqueue_could_enqueue_front(size_t len_asked)
  103. {
  104. // MAX_CMD_SIZE has to accommodate the zero terminator.
  105. if (len_asked >= MAX_CMD_SIZE)
  106. return false;
  107. // Remove the currently processed command from the queue.
  108. if (! cmdbuffer_front_already_processed) {
  109. cmdqueue_pop_front();
  110. cmdbuffer_front_already_processed = true;
  111. }
  112. if (bufindr == bufindw && buflen > 0)
  113. // Full buffer.
  114. return false;
  115. // Adjust the end of the write buffer based on whether a partial line is in the receive buffer.
  116. int endw = (serial_count > 0) ? (bufindw + MAX_CMD_SIZE + 1) : bufindw;
  117. if (bufindw < bufindr) {
  118. int bufindr_new = bufindr - len_asked - (1 + CMDHDRSIZE);
  119. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  120. if (endw <= bufindr_new) {
  121. bufindr = bufindr_new;
  122. return true;
  123. }
  124. } else {
  125. // Otherwise the free space is split between the start and end.
  126. if (len_asked + (1 + CMDHDRSIZE) <= bufindr) {
  127. // Could fit at the start.
  128. bufindr -= len_asked + (1 + CMDHDRSIZE);
  129. return true;
  130. }
  131. int bufindr_new = sizeof(cmdbuffer) - len_asked - (1 + CMDHDRSIZE);
  132. if (endw <= bufindr_new) {
  133. memset(cmdbuffer, 0, bufindr);
  134. bufindr = bufindr_new;
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. // Could one enqueue a command of length len_asked into the buffer,
  141. // while leaving CMDBUFFER_RESERVE_FRONT at the start?
  142. // If yes, adjust bufindw to the new position, where the new command could be enqued.
  143. // len_asked does not contain the zero terminator size.
  144. // This function may update bufindw, therefore for the power panic to work, this function must be called
  145. // with the interrupts disabled!
  146. static bool __attribute__((noinline)) cmdqueue_could_enqueue_back(size_t len_asked)
  147. {
  148. // MAX_CMD_SIZE has to accommodate the zero terminator.
  149. if (len_asked >= MAX_CMD_SIZE)
  150. return false;
  151. if (bufindr == bufindw && buflen > 0)
  152. // Full buffer.
  153. return false;
  154. // If there is some data stored starting at bufindw, len_asked is certainly smaller than
  155. // the allocated data buffer. Try to reserve a new buffer and to move the already received
  156. // serial data.
  157. // How much memory to reserve for the commands pushed to the front?
  158. // End of the queue, when pushing to the end.
  159. size_t endw = bufindw + len_asked + (1 + CMDHDRSIZE);
  160. if (bufindw < bufindr)
  161. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  162. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  163. // Otherwise the free space is split between the start and end.
  164. if (// Could one fit to the end, including the reserve?
  165. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  166. // Could one fit to the end, and the reserve to the start?
  167. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  168. return true;
  169. // Could one fit both to the start?
  170. if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  171. // Mark the rest of the buffer as used.
  172. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  173. // and point to the start.
  174. // Be careful! The bufindw needs to be changed atomically for the power panic & filament panic to work.
  175. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { bufindw = 0; }
  176. return true;
  177. }
  178. return false;
  179. }
  180. #ifdef CMDBUFFER_DEBUG
  181. void cmdqueue_dump_to_serial_single_line(int nr, const char *p)
  182. {
  183. SERIAL_ECHOPGM("Entry nr: ");
  184. SERIAL_ECHO(nr);
  185. SERIAL_ECHOPGM(", type: ");
  186. int type = *p;
  187. SERIAL_ECHO(type);
  188. SERIAL_ECHOPGM(", size: ");
  189. unsigned int size = *(unsigned int*)(p + 1);
  190. SERIAL_ECHO(size);
  191. SERIAL_ECHOPGM(", cmd: ");
  192. SERIAL_ECHO(p + CMDHDRSIZE);
  193. SERIAL_ECHOLNPGM("");
  194. }
  195. void cmdqueue_dump_to_serial()
  196. {
  197. if (buflen == 0) {
  198. SERIAL_ECHOLNPGM("The command buffer is empty.");
  199. } else {
  200. SERIAL_ECHOPGM("Content of the buffer: entries ");
  201. SERIAL_ECHO(buflen);
  202. SERIAL_ECHOPGM(", indr ");
  203. SERIAL_ECHO(bufindr);
  204. SERIAL_ECHOPGM(", indw ");
  205. SERIAL_ECHO(bufindw);
  206. SERIAL_ECHOLNPGM("");
  207. int nr = 0;
  208. if (bufindr < bufindw) {
  209. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + bufindw; ++ nr) {
  210. cmdqueue_dump_to_serial_single_line(nr, p);
  211. // Skip the command.
  212. for (p += CMDHDRSIZE; *p != 0; ++ p);
  213. // Skip the gaps.
  214. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  215. }
  216. } else {
  217. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + sizeof(cmdbuffer); ++ nr) {
  218. cmdqueue_dump_to_serial_single_line(nr, p);
  219. // Skip the command.
  220. for (p += CMDHDRSIZE; *p != 0; ++ p);
  221. // Skip the gaps.
  222. for (++p; p < cmdbuffer + sizeof(cmdbuffer) && *p == 0; ++ p);
  223. }
  224. for (const char *p = cmdbuffer; p < cmdbuffer + bufindw; ++ nr) {
  225. cmdqueue_dump_to_serial_single_line(nr, p);
  226. // Skip the command.
  227. for (p += CMDHDRSIZE; *p != 0; ++ p);
  228. // Skip the gaps.
  229. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  230. }
  231. }
  232. SERIAL_ECHOLNPGM("End of the buffer.");
  233. }
  234. }
  235. #endif /* CMDBUFFER_DEBUG */
  236. //adds an command to the main command buffer
  237. //thats really done in a non-safe way.
  238. //needs overworking someday
  239. // Currently the maximum length of a command piped through this function is around 20 characters
  240. void enquecommand(const char *cmd, bool from_progmem)
  241. {
  242. size_t len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  243. // Does cmd fit the queue while leaving sufficient space at the front for the chained commands?
  244. // If it fits, it may move bufindw, so it points to a contiguous buffer, which fits cmd.
  245. if (cmdqueue_could_enqueue_back(len)) {
  246. // This is dangerous if a mixing of serial and this happens
  247. // This may easily be tested: If serial_count > 0, we have a problem.
  248. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_UI;
  249. if (from_progmem)
  250. strcpy_P(cmdbuffer + bufindw + CMDHDRSIZE, cmd);
  251. else
  252. strcpy(cmdbuffer + bufindw + CMDHDRSIZE, cmd);
  253. SERIAL_ECHO_START;
  254. SERIAL_ECHORPGM(MSG_Enqueing);
  255. SERIAL_ECHO(cmdbuffer + bufindw + CMDHDRSIZE);
  256. SERIAL_ECHOLNPGM("\"");
  257. bufindw += len + (CMDHDRSIZE + 1);
  258. if (bufindw == sizeof(cmdbuffer))
  259. bufindw = 0;
  260. ++ buflen;
  261. #ifdef CMDBUFFER_DEBUG
  262. cmdqueue_dump_to_serial();
  263. #endif /* CMDBUFFER_DEBUG */
  264. } else {
  265. SERIAL_ERROR_START;
  266. SERIAL_ECHORPGM(MSG_Enqueing);
  267. if (from_progmem)
  268. SERIAL_PROTOCOLRPGM(cmd);
  269. else
  270. SERIAL_ECHO(cmd);
  271. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  272. #ifdef CMDBUFFER_DEBUG
  273. cmdqueue_dump_to_serial();
  274. #endif /* CMDBUFFER_DEBUG */
  275. }
  276. }
  277. bool cmd_buffer_empty()
  278. {
  279. return (buflen == 0);
  280. }
  281. void enquecommand_front(const char *cmd, bool from_progmem)
  282. {
  283. size_t len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  284. // Does cmd fit the queue? This call shall move bufindr, so the command may be copied.
  285. if (cmdqueue_could_enqueue_front(len)) {
  286. cmdbuffer[bufindr] = CMDBUFFER_CURRENT_TYPE_UI;
  287. if (from_progmem)
  288. strcpy_P(cmdbuffer + bufindr + CMDHDRSIZE, cmd);
  289. else
  290. strcpy(cmdbuffer + bufindr + CMDHDRSIZE, cmd);
  291. ++ buflen;
  292. SERIAL_ECHO_START;
  293. SERIAL_ECHOPGM("Enqueing to the front: \"");
  294. SERIAL_ECHO(cmdbuffer + bufindr + CMDHDRSIZE);
  295. SERIAL_ECHOLNPGM("\"");
  296. #ifdef CMDBUFFER_DEBUG
  297. cmdqueue_dump_to_serial();
  298. #endif /* CMDBUFFER_DEBUG */
  299. } else {
  300. SERIAL_ERROR_START;
  301. SERIAL_ECHOPGM("Enqueing to the front: \"");
  302. if (from_progmem)
  303. SERIAL_PROTOCOLRPGM(cmd);
  304. else
  305. SERIAL_ECHO(cmd);
  306. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  307. #ifdef CMDBUFFER_DEBUG
  308. cmdqueue_dump_to_serial();
  309. #endif /* CMDBUFFER_DEBUG */
  310. }
  311. }
  312. // Mark the command at the top of the command queue as new.
  313. // Therefore it will not be removed from the queue.
  314. void repeatcommand_front()
  315. {
  316. cmdbuffer_front_already_processed = true;
  317. }
  318. void get_command()
  319. {
  320. // Test and reserve space for the new command string.
  321. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE - 1))
  322. return;
  323. if (MYSERIAL.available() == RX_BUFFER_SIZE - 1) { //compare number of chars buffered in rx buffer with rx buffer size
  324. MYSERIAL.flush();
  325. SERIAL_ECHOLNPGM("Full RX Buffer"); //if buffer was full, there is danger that reading of last gcode will not be completed
  326. }
  327. // start of serial line processing loop
  328. while (((MYSERIAL.available() > 0 && !saved_printing) || (MYSERIAL.available() > 0 && isPrintPaused)) && !cmdqueue_serial_disabled) { //is print is saved (crash detection or filament detection), dont process data from serial line
  329. char serial_char = MYSERIAL.read();
  330. serialTimeoutTimer.start();
  331. if (serial_char < 0)
  332. // Ignore extended ASCII characters. These characters have no meaning in the G-code apart from the file names
  333. // and Marlin does not support such file names anyway.
  334. // Serial characters with a highest bit set to 1 are generated when the USB cable is unplugged, leading
  335. // to a hang-up of the print process from an SD card.
  336. continue;
  337. if(serial_char == '\n' ||
  338. serial_char == '\r' ||
  339. serial_count >= (MAX_CMD_SIZE - 1) )
  340. {
  341. if(!serial_count) { //if empty line
  342. comment_mode = false; //for new command
  343. return;
  344. }
  345. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; // terminate string
  346. char* cmd_head = cmdbuffer+bufindw+CMDHDRSIZE; // current command pointer
  347. char* cmd_start = cmd_head; // pointer past the line number (if any)
  348. if(!comment_mode){
  349. long gcode_N = -1; // seen line number
  350. // Line numbers must be first in buffer
  351. if (*cmd_head == 'N') {
  352. // Line number met: decode the number, then move cmd_start past all spaces.
  353. gcode_N = (strtol(cmd_head+1, &cmd_start, 10));
  354. while (*cmd_start == ' ') ++cmd_start;
  355. // Test whether the successive lines are stamped with an increasing line number ID.
  356. if(gcode_N != gcode_LastN+1 && strncmp_P(cmd_start, PSTR("M110"), 4)) {
  357. // Line numbers not sent in succession and M110 not seen.
  358. SERIAL_ERROR_START;
  359. SERIAL_ERRORRPGM(_n("Line Number is not Last Line Number+1, Last Line: "));////MSG_ERR_LINE_NO
  360. SERIAL_ERRORLN(gcode_LastN);
  361. //Serial.println(gcode_N);
  362. FlushSerialRequestResend();
  363. serial_count = 0;
  364. return;
  365. }
  366. if((strchr_pointer = strchr(cmd_start, '*')) != NULL)
  367. {
  368. byte checksum = 0;
  369. char *p = cmd_head;
  370. while (p != strchr_pointer)
  371. checksum = checksum^(*p++);
  372. if (code_value_short() != (int16_t)checksum) {
  373. SERIAL_ERROR_START;
  374. SERIAL_ERRORRPGM(_n("checksum mismatch, Last Line: "));////MSG_ERR_CHECKSUM_MISMATCH
  375. SERIAL_ERRORLN(gcode_LastN);
  376. FlushSerialRequestResend();
  377. serial_count = 0;
  378. return;
  379. }
  380. // If no errors, remove the checksum and continue parsing.
  381. *strchr_pointer = 0;
  382. }
  383. else
  384. {
  385. SERIAL_ERROR_START;
  386. SERIAL_ERRORRPGM(_n("No Checksum with line number, Last Line: "));////MSG_ERR_NO_CHECKSUM
  387. SERIAL_ERRORLN(gcode_LastN);
  388. FlushSerialRequestResend();
  389. serial_count = 0;
  390. return;
  391. }
  392. }
  393. else
  394. {
  395. // move cmd_start past all spaces
  396. while (*cmd_start == ' ') ++cmd_start;
  397. // if we didn't receive 'N' but still see '*'
  398. if (strchr(cmd_start, '*') != NULL)
  399. {
  400. SERIAL_ERROR_START;
  401. SERIAL_ERRORRPGM(_n("No Line Number with checksum, Last Line: "));////MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM
  402. SERIAL_ERRORLN(gcode_LastN);
  403. FlushSerialRequestResend();
  404. serial_count = 0;
  405. return;
  406. }
  407. }
  408. // Handle KILL early, even when Stopped
  409. if(strcmp_P(cmd_start, PSTR("M112")) == 0)
  410. kill(MSG_M112_KILL, 2);
  411. // Bypass Stopped for some commands
  412. bool allow_when_stopped = false;
  413. if(strncmp_P(cmd_start, PSTR("M310"), 4) == 0)
  414. allow_when_stopped = true;
  415. // Handle the USB timer
  416. if ((*cmd_start == 'G') && !(IS_SD_PRINTING))
  417. usb_timer.start();
  418. if (allow_when_stopped == false && Stopped == true) {
  419. // Stopped can be set either during error states (thermal error: cannot continue), or
  420. // when a printer-initiated action is processed. In such case the printer will send to
  421. // the host an action, but cannot know if the action has been processed while new
  422. // commands are being sent. In this situation we just drop the command while issuing
  423. // periodic "busy" messages in the main loop. Since we're not incrementing the received
  424. // line number, a request for resend will happen (if necessary), ensuring we don't skip
  425. // commands whenever Stopped is cleared and processing resumes.
  426. serial_count = 0;
  427. return;
  428. }
  429. // Command is complete: store the current line into buffer, move to the next line.
  430. // Store type of entry
  431. cmdbuffer[bufindw] = gcode_N >= 0 ? CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR : CMDBUFFER_CURRENT_TYPE_USB;
  432. #ifdef CMDBUFFER_DEBUG
  433. SERIAL_ECHO_START;
  434. SERIAL_ECHOPGM("Storing a command line to buffer: ");
  435. SERIAL_ECHO(cmd_start);
  436. SERIAL_ECHOLNPGM("");
  437. #endif /* CMDBUFFER_DEBUG */
  438. // Store the command itself (without line number or checksum)
  439. size_t cmd_len;
  440. if (cmd_head == cmd_start)
  441. cmd_len = strlen(cmd_start) + 1;
  442. else {
  443. // strip the line number
  444. cmd_len = 0;
  445. do { cmd_head[cmd_len] = cmd_start[cmd_len]; }
  446. while (cmd_head[cmd_len++]);
  447. }
  448. bufindw += cmd_len + CMDHDRSIZE;
  449. if (bufindw == sizeof(cmdbuffer))
  450. bufindw = 0;
  451. ++ buflen;
  452. // Update the processed gcode line
  453. if (gcode_N >= 0)
  454. gcode_LastN = gcode_N;
  455. #ifdef CMDBUFFER_DEBUG
  456. SERIAL_ECHOPGM("Number of commands in the buffer: ");
  457. SERIAL_ECHO(buflen);
  458. SERIAL_ECHOLNPGM("");
  459. #endif /* CMDBUFFER_DEBUG */
  460. } // end of 'not comment mode'
  461. serial_count = 0; //clear buffer
  462. // Don't call cmdqueue_could_enqueue_back if there are no characters waiting
  463. // in the queue, as this function will reserve the memory.
  464. if (MYSERIAL.available() == 0 || ! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1))
  465. return;
  466. } // end of "end of line" processing
  467. else {
  468. // Not an "end of line" symbol. Store the new character into a buffer.
  469. if(serial_char == ';') comment_mode = true;
  470. if(!comment_mode) cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char;
  471. }
  472. } // end of serial line processing loop
  473. if (serial_count > 0 && serialTimeoutTimer.expired(farm_mode ? 800 : 2000)) {
  474. comment_mode = false;
  475. serial_count = 0;
  476. SERIAL_ECHOLNPGM("RX timeout");
  477. return;
  478. }
  479. #ifdef SDSUPPORT
  480. if(!card.sdprinting || !card.isFileOpen() || serial_count!=0){
  481. // If there is a half filled buffer from serial line, wait until return before
  482. // continuing with the serial line.
  483. return;
  484. }
  485. //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible
  486. // if it occurs, stop_buffering is triggered and the buffer is ran dry.
  487. // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing
  488. static bool stop_buffering=false;
  489. if(buflen==0) stop_buffering=false;
  490. union {
  491. struct {
  492. char lo;
  493. char hi;
  494. } lohi;
  495. uint16_t value;
  496. } sd_count;
  497. sd_count.value = 0;
  498. // Reads whole lines from the SD card. Never leaves a half-filled line in the cmdbuffer.
  499. while( !card.eof() && !stop_buffering) {
  500. int16_t n=card.getFilteredGcodeChar();
  501. char serial_char = (char)n;
  502. if( serial_char == '\n'
  503. || serial_char == '\r'
  504. || ((serial_char == '#' || serial_char == ':') )
  505. || serial_count >= (MAX_CMD_SIZE - 1)
  506. || n==-1
  507. ){
  508. if(serial_char=='#')
  509. stop_buffering=true;
  510. if(!serial_count)
  511. {
  512. // This is either an empty line, or a line with just a comment.
  513. // Continue to the following line, and continue accumulating the number of bytes
  514. // read from the sdcard into sd_count,
  515. // so that the length of the already read empty lines and comments will be added
  516. // to the following non-empty line.
  517. return; // prevent cycling indefinitely - let manage_heaters do their job
  518. }
  519. // The new command buffer could be updated non-atomically, because it is not yet considered
  520. // to be inside the active queue.
  521. sd_count.value = card.get_sdpos() - sdpos_atomic;
  522. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_SDCARD;
  523. cmdbuffer[bufindw+1] = sd_count.lohi.lo;
  524. cmdbuffer[bufindw+2] = sd_count.lohi.hi;
  525. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; //terminate string
  526. // Calculate the length before disabling the interrupts.
  527. uint8_t len = strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  528. // SERIAL_ECHOPGM("SD cmd(");
  529. // MYSERIAL.print(sd_count.value, DEC);
  530. // SERIAL_ECHOPGM(") ");
  531. // SERIAL_ECHOLN(cmdbuffer+bufindw+CMDHDRSIZE);
  532. // SERIAL_ECHOPGM("cmdbuffer:");
  533. // MYSERIAL.print(cmdbuffer);
  534. // SERIAL_ECHOPGM("buflen:");
  535. // MYSERIAL.print(buflen+1);
  536. sd_count.value = 0;
  537. cli();
  538. // This block locks the interrupts globally for 3.56 us,
  539. // which corresponds to a maximum repeat frequency of 280.70 kHz.
  540. // This blocking is safe in the context of a 10kHz stepper driver interrupt
  541. // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz.
  542. ++ buflen;
  543. bufindw += len;
  544. sdpos_atomic = card.get_sdpos();
  545. if (bufindw == sizeof(cmdbuffer))
  546. bufindw = 0;
  547. sei();
  548. comment_mode = false; //for new command
  549. serial_count = 0; //clear buffer
  550. if(card.eof()) break;
  551. // The following line will reserve buffer space if available.
  552. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1))
  553. return;
  554. }
  555. else
  556. {
  557. // there are no comments coming from the filtered file
  558. cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char;
  559. }
  560. }
  561. if(card.eof())
  562. {
  563. // file was fully buffered, but commands might still need to be planned!
  564. // do *not* clear sdprinting until all SD commands are consumed to ensure
  565. // SD state can be resumed from a saved printing state. sdprinting is only
  566. // cleared by printingHasFinished after peforming all remaining moves.
  567. if(!cmdqueue_calc_sd_length())
  568. {
  569. // queue is complete, but before we process EOF commands prevent
  570. // re-entry by disabling SD processing from any st_synchronize call
  571. card.closefile();
  572. SERIAL_PROTOCOLLNRPGM(_n("Done printing file"));////MSG_FILE_PRINTED
  573. stoptime=_millis();
  574. char time[30];
  575. unsigned long t=(stoptime-starttime-pause_time)/1000;
  576. pause_time = 0;
  577. int hours, minutes;
  578. minutes=(t/60)%60;
  579. hours=t/60/60;
  580. save_statistics(total_filament_used, t);
  581. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  582. SERIAL_ECHO_START;
  583. SERIAL_ECHOLN(time);
  584. lcd_setstatus(time);
  585. card.printingHasFinished();
  586. card.checkautostart(true);
  587. if (farm_mode)
  588. prusa_statistics(6);
  589. }
  590. }
  591. #endif //SDSUPPORT
  592. }
  593. uint16_t cmdqueue_calc_sd_length()
  594. {
  595. if (buflen == 0)
  596. return 0;
  597. union {
  598. struct {
  599. char lo;
  600. char hi;
  601. } lohi;
  602. uint16_t value;
  603. } sdlen_single;
  604. uint16_t sdlen = 0;
  605. for (size_t _buflen = buflen, _bufindr = bufindr;;) {
  606. if (cmdbuffer[_bufindr] == CMDBUFFER_CURRENT_TYPE_SDCARD) {
  607. sdlen_single.lohi.lo = cmdbuffer[_bufindr + 1];
  608. sdlen_single.lohi.hi = cmdbuffer[_bufindr + 2];
  609. sdlen += sdlen_single.value;
  610. }
  611. if (-- _buflen == 0)
  612. break;
  613. // First skip the current command ID and iterate up to the end of the string.
  614. for (_bufindr += CMDHDRSIZE; cmdbuffer[_bufindr] != 0; ++ _bufindr) ;
  615. // Second, skip the end of string null character and iterate until a nonzero command ID is found.
  616. for (++ _bufindr; _bufindr < sizeof(cmdbuffer) && cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  617. // If the end of the buffer was empty,
  618. if (_bufindr == sizeof(cmdbuffer)) {
  619. // skip to the start and find the nonzero command.
  620. for (_bufindr = 0; cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  621. }
  622. }
  623. return sdlen;
  624. }