cmdqueue.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. #include "cmdqueue.h"
  2. #include "cardreader.h"
  3. #include "ultralcd.h"
  4. // Reserve BUFSIZE lines of length MAX_CMD_SIZE plus CMDBUFFER_RESERVE_FRONT.
  5. char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT];
  6. // Head of the circular buffer, where to read.
  7. size_t bufindr = 0;
  8. // Tail of the buffer, where to write.
  9. static size_t bufindw = 0;
  10. // Number of lines in cmdbuffer.
  11. int buflen = 0;
  12. // Flag for processing the current command inside the main Arduino loop().
  13. // If a new command was pushed to the front of a command buffer while
  14. // processing another command, this replaces the command on the top.
  15. // Therefore don't remove the command from the queue in the loop() function.
  16. bool cmdbuffer_front_already_processed = false;
  17. // Used for temporarely preventing accidental adding of Serial commands to the queue.
  18. // For now only check_file and the fancheck pause use this.
  19. bool cmdqueue_serial_disabled = false;
  20. int serial_count = 0; //index of character read from serial line
  21. bool comment_mode = false;
  22. char *strchr_pointer; // just a pointer to find chars in the command string like X, Y, Z, E, etc
  23. ShortTimer serialTimeoutTimer;
  24. long gcode_N = 0;
  25. long gcode_LastN = 0;
  26. long Stopped_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 cmdqueue_could_enqueue_back(size_t len_asked, bool atomic_update = false)
  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 (serial_count > 0) {
  155. // If there is some data stored starting at bufindw, len_asked is certainly smaller than
  156. // the allocated data buffer. Try to reserve a new buffer and to move the already received
  157. // serial data.
  158. // How much memory to reserve for the commands pushed to the front?
  159. // End of the queue, when pushing to the end.
  160. size_t endw = bufindw + len_asked + (1 + CMDHDRSIZE);
  161. if (bufindw < bufindr)
  162. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  163. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  164. // Otherwise the free space is split between the start and end.
  165. if (// Could one fit to the end, including the reserve?
  166. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  167. // Could one fit to the end, and the reserve to the start?
  168. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  169. return true;
  170. // Could one fit both to the start?
  171. if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  172. // Mark the rest of the buffer as used.
  173. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  174. // and point to the start.
  175. // Be careful! The bufindw needs to be changed atomically for the power panic & filament panic to work.
  176. if (atomic_update)
  177. cli();
  178. bufindw = 0;
  179. if (atomic_update)
  180. sei();
  181. return true;
  182. }
  183. } else {
  184. // How much memory to reserve for the commands pushed to the front?
  185. // End of the queue, when pushing to the end.
  186. size_t endw = bufindw + len_asked + (1 + CMDHDRSIZE);
  187. if (bufindw < bufindr)
  188. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  189. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  190. // Otherwise the free space is split between the start and end.
  191. if (// Could one fit to the end, including the reserve?
  192. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  193. // Could one fit to the end, and the reserve to the start?
  194. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  195. return true;
  196. // Could one fit both to the start?
  197. if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  198. // Mark the rest of the buffer as used.
  199. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  200. // and point to the start.
  201. // Be careful! The bufindw needs to be changed atomically for the power panic & filament panic to work.
  202. if (atomic_update)
  203. cli();
  204. bufindw = 0;
  205. if (atomic_update)
  206. sei();
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. #ifdef CMDBUFFER_DEBUG
  213. void cmdqueue_dump_to_serial_single_line(int nr, const char *p)
  214. {
  215. SERIAL_ECHOPGM("Entry nr: ");
  216. SERIAL_ECHO(nr);
  217. SERIAL_ECHOPGM(", type: ");
  218. int type = *p;
  219. SERIAL_ECHO(type);
  220. SERIAL_ECHOPGM(", size: ");
  221. unsigned int size = *(unsigned int*)(p + 1);
  222. SERIAL_ECHO(size);
  223. SERIAL_ECHOPGM(", cmd: ");
  224. SERIAL_ECHO(p + CMDHDRSIZE);
  225. SERIAL_ECHOLNPGM("");
  226. }
  227. void cmdqueue_dump_to_serial()
  228. {
  229. if (buflen == 0) {
  230. SERIAL_ECHOLNPGM("The command buffer is empty.");
  231. } else {
  232. SERIAL_ECHOPGM("Content of the buffer: entries ");
  233. SERIAL_ECHO(buflen);
  234. SERIAL_ECHOPGM(", indr ");
  235. SERIAL_ECHO(bufindr);
  236. SERIAL_ECHOPGM(", indw ");
  237. SERIAL_ECHO(bufindw);
  238. SERIAL_ECHOLNPGM("");
  239. int nr = 0;
  240. if (bufindr < bufindw) {
  241. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + bufindw; ++ nr) {
  242. cmdqueue_dump_to_serial_single_line(nr, p);
  243. // Skip the command.
  244. for (p += CMDHDRSIZE; *p != 0; ++ p);
  245. // Skip the gaps.
  246. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  247. }
  248. } else {
  249. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + sizeof(cmdbuffer); ++ nr) {
  250. cmdqueue_dump_to_serial_single_line(nr, p);
  251. // Skip the command.
  252. for (p += CMDHDRSIZE; *p != 0; ++ p);
  253. // Skip the gaps.
  254. for (++p; p < cmdbuffer + sizeof(cmdbuffer) && *p == 0; ++ p);
  255. }
  256. for (const char *p = cmdbuffer; p < cmdbuffer + bufindw; ++ nr) {
  257. cmdqueue_dump_to_serial_single_line(nr, p);
  258. // Skip the command.
  259. for (p += CMDHDRSIZE; *p != 0; ++ p);
  260. // Skip the gaps.
  261. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  262. }
  263. }
  264. SERIAL_ECHOLNPGM("End of the buffer.");
  265. }
  266. }
  267. #endif /* CMDBUFFER_DEBUG */
  268. //adds an command to the main command buffer
  269. //thats really done in a non-safe way.
  270. //needs overworking someday
  271. // Currently the maximum length of a command piped through this function is around 20 characters
  272. void enquecommand(const char *cmd, bool from_progmem)
  273. {
  274. size_t len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  275. // Does cmd fit the queue while leaving sufficient space at the front for the chained commands?
  276. // If it fits, it may move bufindw, so it points to a contiguous buffer, which fits cmd.
  277. if (cmdqueue_could_enqueue_back(len)) {
  278. // This is dangerous if a mixing of serial and this happens
  279. // This may easily be tested: If serial_count > 0, we have a problem.
  280. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_UI;
  281. if (from_progmem)
  282. strcpy_P(cmdbuffer + bufindw + CMDHDRSIZE, cmd);
  283. else
  284. strcpy(cmdbuffer + bufindw + CMDHDRSIZE, cmd);
  285. SERIAL_ECHO_START;
  286. SERIAL_ECHORPGM(MSG_Enqueing);
  287. SERIAL_ECHO(cmdbuffer + bufindw + CMDHDRSIZE);
  288. SERIAL_ECHOLNPGM("\"");
  289. bufindw += len + (CMDHDRSIZE + 1);
  290. if (bufindw == sizeof(cmdbuffer))
  291. bufindw = 0;
  292. ++ buflen;
  293. #ifdef CMDBUFFER_DEBUG
  294. cmdqueue_dump_to_serial();
  295. #endif /* CMDBUFFER_DEBUG */
  296. } else {
  297. SERIAL_ERROR_START;
  298. SERIAL_ECHORPGM(MSG_Enqueing);
  299. if (from_progmem)
  300. SERIAL_PROTOCOLRPGM(cmd);
  301. else
  302. SERIAL_ECHO(cmd);
  303. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  304. #ifdef CMDBUFFER_DEBUG
  305. cmdqueue_dump_to_serial();
  306. #endif /* CMDBUFFER_DEBUG */
  307. }
  308. }
  309. bool cmd_buffer_empty()
  310. {
  311. return (buflen == 0);
  312. }
  313. void enquecommand_front(const char *cmd, bool from_progmem)
  314. {
  315. size_t len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  316. // Does cmd fit the queue? This call shall move bufindr, so the command may be copied.
  317. if (cmdqueue_could_enqueue_front(len)) {
  318. cmdbuffer[bufindr] = CMDBUFFER_CURRENT_TYPE_UI;
  319. if (from_progmem)
  320. strcpy_P(cmdbuffer + bufindr + CMDHDRSIZE, cmd);
  321. else
  322. strcpy(cmdbuffer + bufindr + CMDHDRSIZE, cmd);
  323. ++ buflen;
  324. SERIAL_ECHO_START;
  325. SERIAL_ECHOPGM("Enqueing to the front: \"");
  326. SERIAL_ECHO(cmdbuffer + bufindr + CMDHDRSIZE);
  327. SERIAL_ECHOLNPGM("\"");
  328. #ifdef CMDBUFFER_DEBUG
  329. cmdqueue_dump_to_serial();
  330. #endif /* CMDBUFFER_DEBUG */
  331. } else {
  332. SERIAL_ERROR_START;
  333. SERIAL_ECHOPGM("Enqueing to the front: \"");
  334. if (from_progmem)
  335. SERIAL_PROTOCOLRPGM(cmd);
  336. else
  337. SERIAL_ECHO(cmd);
  338. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  339. #ifdef CMDBUFFER_DEBUG
  340. cmdqueue_dump_to_serial();
  341. #endif /* CMDBUFFER_DEBUG */
  342. }
  343. }
  344. // Mark the command at the top of the command queue as new.
  345. // Therefore it will not be removed from the queue.
  346. void repeatcommand_front()
  347. {
  348. cmdbuffer_front_already_processed = true;
  349. }
  350. void proc_commands() {
  351. if (buflen)
  352. {
  353. process_commands();
  354. if (!cmdbuffer_front_already_processed)
  355. cmdqueue_pop_front();
  356. cmdbuffer_front_already_processed = false;
  357. }
  358. }
  359. void get_command()
  360. {
  361. // Test and reserve space for the new command string.
  362. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE - 1, true))
  363. return;
  364. if (MYSERIAL.available() == RX_BUFFER_SIZE - 1) { //compare number of chars buffered in rx buffer with rx buffer size
  365. MYSERIAL.flush();
  366. SERIAL_ECHOLNPGM("Full RX Buffer"); //if buffer was full, there is danger that reading of last gcode will not be completed
  367. }
  368. // start of serial line processing loop
  369. 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
  370. char serial_char = MYSERIAL.read();
  371. serialTimeoutTimer.start();
  372. if (serial_char < 0)
  373. // Ignore extended ASCII characters. These characters have no meaning in the G-code apart from the file names
  374. // and Marlin does not support such file names anyway.
  375. // Serial characters with a highest bit set to 1 are generated when the USB cable is unplugged, leading
  376. // to a hang-up of the print process from an SD card.
  377. continue;
  378. if(serial_char == '\n' ||
  379. serial_char == '\r' ||
  380. serial_count >= (MAX_CMD_SIZE - 1) )
  381. {
  382. if(!serial_count) { //if empty line
  383. comment_mode = false; //for new command
  384. return;
  385. }
  386. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; //terminate string
  387. if(!comment_mode){
  388. gcode_N = 0;
  389. // Line numbers must be first in buffer
  390. if ((strstr(cmdbuffer+bufindw+CMDHDRSIZE, "PRUSA") == NULL) &&
  391. (cmdbuffer[bufindw+CMDHDRSIZE] == 'N')) {
  392. // Line number met. When sending a G-code over a serial line, each line may be stamped with its index,
  393. // and Marlin tests, whether the successive lines are stamped with an increasing line number ID
  394. gcode_N = (strtol(cmdbuffer+bufindw+CMDHDRSIZE+1, NULL, 10));
  395. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer+bufindw+CMDHDRSIZE, PSTR("M110")) == NULL) ) {
  396. // M110 - set current line number.
  397. // Line numbers not sent in succession.
  398. SERIAL_ERROR_START;
  399. SERIAL_ERRORRPGM(_n("Line Number is not Last Line Number+1, Last Line: "));////MSG_ERR_LINE_NO
  400. SERIAL_ERRORLN(gcode_LastN);
  401. //Serial.println(gcode_N);
  402. FlushSerialRequestResend();
  403. serial_count = 0;
  404. return;
  405. }
  406. if((strchr_pointer = strchr(cmdbuffer+bufindw+CMDHDRSIZE, '*')) != NULL)
  407. {
  408. byte checksum = 0;
  409. char *p = cmdbuffer+bufindw+CMDHDRSIZE;
  410. while (p != strchr_pointer)
  411. checksum = checksum^(*p++);
  412. if (code_value_short() != (int16_t)checksum) {
  413. SERIAL_ERROR_START;
  414. SERIAL_ERRORRPGM(_n("checksum mismatch, Last Line: "));////MSG_ERR_CHECKSUM_MISMATCH
  415. SERIAL_ERRORLN(gcode_LastN);
  416. FlushSerialRequestResend();
  417. serial_count = 0;
  418. return;
  419. }
  420. // If no errors, remove the checksum and continue parsing.
  421. *strchr_pointer = 0;
  422. }
  423. else
  424. {
  425. SERIAL_ERROR_START;
  426. SERIAL_ERRORRPGM(_n("No Checksum with line number, Last Line: "));////MSG_ERR_NO_CHECKSUM
  427. SERIAL_ERRORLN(gcode_LastN);
  428. FlushSerialRequestResend();
  429. serial_count = 0;
  430. return;
  431. }
  432. // Don't parse N again with code_seen('N')
  433. cmdbuffer[bufindw + CMDHDRSIZE] = '$';
  434. //if no errors, continue parsing
  435. gcode_LastN = gcode_N;
  436. }
  437. // if we don't receive 'N' but still see '*'
  438. if ((cmdbuffer[bufindw + CMDHDRSIZE] != 'N') && (cmdbuffer[bufindw + CMDHDRSIZE] != '$') && (strchr(cmdbuffer+bufindw+CMDHDRSIZE, '*') != NULL))
  439. {
  440. SERIAL_ERROR_START;
  441. SERIAL_ERRORRPGM(_n("No Line Number with checksum, Last Line: "));////MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM
  442. SERIAL_ERRORLN(gcode_LastN);
  443. FlushSerialRequestResend();
  444. serial_count = 0;
  445. return;
  446. }
  447. if ((strchr_pointer = strchr(cmdbuffer+bufindw+CMDHDRSIZE, 'G')) != NULL) {
  448. if (!IS_SD_PRINTING) {
  449. usb_timer.start();
  450. }
  451. if (Stopped == true) {
  452. if (code_value_uint8() <= 3) {
  453. SERIAL_ERRORLNRPGM(MSG_ERR_STOPPED);
  454. LCD_MESSAGERPGM(_T(MSG_STOPPED));
  455. }
  456. }
  457. } // end of 'G' command
  458. //If command was e-stop process now
  459. if(strcmp(cmdbuffer+bufindw+CMDHDRSIZE, "M112") == 0)
  460. kill(MSG_M112_KILL, 2);
  461. // Store the current line into buffer, move to the next line.
  462. // Store type of entry
  463. cmdbuffer[bufindw] = gcode_N ? CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR : CMDBUFFER_CURRENT_TYPE_USB;
  464. #ifdef CMDBUFFER_DEBUG
  465. SERIAL_ECHO_START;
  466. SERIAL_ECHOPGM("Storing a command line to buffer: ");
  467. SERIAL_ECHO(cmdbuffer+bufindw+CMDHDRSIZE);
  468. SERIAL_ECHOLNPGM("");
  469. #endif /* CMDBUFFER_DEBUG */
  470. bufindw += strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  471. if (bufindw == sizeof(cmdbuffer))
  472. bufindw = 0;
  473. ++ buflen;
  474. #ifdef CMDBUFFER_DEBUG
  475. SERIAL_ECHOPGM("Number of commands in the buffer: ");
  476. SERIAL_ECHO(buflen);
  477. SERIAL_ECHOLNPGM("");
  478. #endif /* CMDBUFFER_DEBUG */
  479. } // end of 'not comment mode'
  480. serial_count = 0; //clear buffer
  481. // Don't call cmdqueue_could_enqueue_back if there are no characters waiting
  482. // in the queue, as this function will reserve the memory.
  483. if (MYSERIAL.available() == 0 || ! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1, true))
  484. return;
  485. } // end of "end of line" processing
  486. else {
  487. // Not an "end of line" symbol. Store the new character into a buffer.
  488. if(serial_char == ';') comment_mode = true;
  489. if(!comment_mode) cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char;
  490. }
  491. } // end of serial line processing loop
  492. if (serial_count > 0 && serialTimeoutTimer.expired(farm_mode ? 800 : 2000)) {
  493. comment_mode = false;
  494. serial_count = 0;
  495. SERIAL_ECHOLNPGM("RX timeout");
  496. return;
  497. }
  498. #ifdef SDSUPPORT
  499. if(!card.sdprinting || serial_count!=0){
  500. // If there is a half filled buffer from serial line, wait until return before
  501. // continuing with the serial line.
  502. return;
  503. }
  504. //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible
  505. // if it occurs, stop_buffering is triggered and the buffer is ran dry.
  506. // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing
  507. static bool stop_buffering=false;
  508. if(buflen==0) stop_buffering=false;
  509. union {
  510. struct {
  511. char lo;
  512. char hi;
  513. } lohi;
  514. uint16_t value;
  515. } sd_count;
  516. sd_count.value = 0;
  517. // Reads whole lines from the SD card. Never leaves a half-filled line in the cmdbuffer.
  518. while( !card.eof() && !stop_buffering) {
  519. int16_t n=card.getFilteredGcodeChar();
  520. char serial_char = (char)n;
  521. if( serial_char == '\n'
  522. || serial_char == '\r'
  523. || ((serial_char == '#' || serial_char == ':') )
  524. || serial_count >= (MAX_CMD_SIZE - 1)
  525. || n==-1
  526. ){
  527. if(serial_char=='#')
  528. stop_buffering=true;
  529. if(!serial_count)
  530. {
  531. // This is either an empty line, or a line with just a comment.
  532. // Continue to the following line, and continue accumulating the number of bytes
  533. // read from the sdcard into sd_count,
  534. // so that the lenght of the already read empty lines and comments will be added
  535. // to the following non-empty line.
  536. return; // prevent cycling indefinitely - let manage_heaters do their job
  537. }
  538. // The new command buffer could be updated non-atomically, because it is not yet considered
  539. // to be inside the active queue.
  540. sd_count.value = card.get_sdpos() - sdpos_atomic;
  541. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_SDCARD;
  542. cmdbuffer[bufindw+1] = sd_count.lohi.lo;
  543. cmdbuffer[bufindw+2] = sd_count.lohi.hi;
  544. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; //terminate string
  545. // Calculate the length before disabling the interrupts.
  546. uint8_t len = strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  547. // SERIAL_ECHOPGM("SD cmd(");
  548. // MYSERIAL.print(sd_count.value, DEC);
  549. // SERIAL_ECHOPGM(") ");
  550. // SERIAL_ECHOLN(cmdbuffer+bufindw+CMDHDRSIZE);
  551. // SERIAL_ECHOPGM("cmdbuffer:");
  552. // MYSERIAL.print(cmdbuffer);
  553. // SERIAL_ECHOPGM("buflen:");
  554. // MYSERIAL.print(buflen+1);
  555. sd_count.value = 0;
  556. cli();
  557. // This block locks the interrupts globally for 3.56 us,
  558. // which corresponds to a maximum repeat frequency of 280.70 kHz.
  559. // This blocking is safe in the context of a 10kHz stepper driver interrupt
  560. // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz.
  561. ++ buflen;
  562. bufindw += len;
  563. sdpos_atomic = card.get_sdpos();
  564. if (bufindw == sizeof(cmdbuffer))
  565. bufindw = 0;
  566. sei();
  567. comment_mode = false; //for new command
  568. serial_count = 0; //clear buffer
  569. if(card.eof()) break;
  570. // The following line will reserve buffer space if available.
  571. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1, true))
  572. return;
  573. }
  574. else
  575. {
  576. // there are no comments coming from the filtered file
  577. cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char;
  578. }
  579. }
  580. if(card.eof())
  581. {
  582. // file was fully buffered, but commands might still need to be planned!
  583. // do *not* clear sdprinting until all SD commands are consumed to ensure
  584. // SD state can be resumed from a saved printing state. sdprinting is only
  585. // cleared by printingHasFinished after peforming all remaining moves.
  586. if(!cmdqueue_calc_sd_length())
  587. {
  588. SERIAL_PROTOCOLLNRPGM(_n("Done printing file"));////MSG_FILE_PRINTED
  589. stoptime=_millis();
  590. char time[30];
  591. unsigned long t=(stoptime-starttime-pause_time)/1000;
  592. pause_time = 0;
  593. int hours, minutes;
  594. minutes=(t/60)%60;
  595. hours=t/60/60;
  596. save_statistics(total_filament_used, t);
  597. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  598. SERIAL_ECHO_START;
  599. SERIAL_ECHOLN(time);
  600. lcd_setstatus(time);
  601. card.printingHasFinished();
  602. card.checkautostart(true);
  603. if (farm_mode)
  604. {
  605. prusa_statistics(6);
  606. lcd_commands_type = LcdCommands::FarmModeConfirm;
  607. }
  608. }
  609. }
  610. #endif //SDSUPPORT
  611. }
  612. uint16_t cmdqueue_calc_sd_length()
  613. {
  614. if (buflen == 0)
  615. return 0;
  616. union {
  617. struct {
  618. char lo;
  619. char hi;
  620. } lohi;
  621. uint16_t value;
  622. } sdlen_single;
  623. uint16_t sdlen = 0;
  624. for (size_t _buflen = buflen, _bufindr = bufindr;;) {
  625. if (cmdbuffer[_bufindr] == CMDBUFFER_CURRENT_TYPE_SDCARD) {
  626. sdlen_single.lohi.lo = cmdbuffer[_bufindr + 1];
  627. sdlen_single.lohi.hi = cmdbuffer[_bufindr + 2];
  628. sdlen += sdlen_single.value;
  629. }
  630. if (-- _buflen == 0)
  631. break;
  632. // First skip the current command ID and iterate up to the end of the string.
  633. for (_bufindr += CMDHDRSIZE; cmdbuffer[_bufindr] != 0; ++ _bufindr) ;
  634. // Second, skip the end of string null character and iterate until a nonzero command ID is found.
  635. for (++ _bufindr; _bufindr < sizeof(cmdbuffer) && cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  636. // If the end of the buffer was empty,
  637. if (_bufindr == sizeof(cmdbuffer)) {
  638. // skip to the start and find the nonzero command.
  639. for (_bufindr = 0; cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  640. }
  641. }
  642. return sdlen;
  643. }