cmdqueue.cpp 27 KB

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