cmdqueue.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. #include "cmdqueue.h"
  2. #include "cardreader.h"
  3. #include "ultralcd.h"
  4. extern bool Stopped;
  5. // Reserve BUFSIZE lines of length MAX_CMD_SIZE plus CMDBUFFER_RESERVE_FRONT.
  6. char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT];
  7. // Head of the circular buffer, where to read.
  8. int bufindr = 0;
  9. // Tail of the buffer, where to write.
  10. int bufindw = 0;
  11. // Number of lines in cmdbuffer.
  12. int buflen = 0;
  13. // Flag for processing the current command inside the main Arduino loop().
  14. // If a new command was pushed to the front of a command buffer while
  15. // processing another command, this replaces the command on the top.
  16. // Therefore don't remove the command from the queue in the loop() function.
  17. bool cmdbuffer_front_already_processed = false;
  18. int serial_count = 0; //index of character read from serial line
  19. boolean comment_mode = false;
  20. char *strchr_pointer; // just a pointer to find chars in the command string like X, Y, Z, E, etc
  21. unsigned long TimeSent = millis();
  22. unsigned long TimeNow = millis();
  23. long gcode_N = 0;
  24. long gcode_LastN = 0;
  25. long Stopped_gcode_LastN = 0;
  26. // Pop the currently processed command from the queue.
  27. // It is expected, that there is at least one command in the queue.
  28. bool cmdqueue_pop_front()
  29. {
  30. if (buflen > 0) {
  31. #ifdef CMDBUFFER_DEBUG
  32. SERIAL_ECHOPGM("Dequeing ");
  33. SERIAL_ECHO(cmdbuffer+bufindr+CMDHDRSIZE);
  34. SERIAL_ECHOLNPGM("");
  35. SERIAL_ECHOPGM("Old indices: buflen ");
  36. SERIAL_ECHO(buflen);
  37. SERIAL_ECHOPGM(", bufindr ");
  38. SERIAL_ECHO(bufindr);
  39. SERIAL_ECHOPGM(", bufindw ");
  40. SERIAL_ECHO(bufindw);
  41. SERIAL_ECHOPGM(", serial_count ");
  42. SERIAL_ECHO(serial_count);
  43. SERIAL_ECHOPGM(", bufsize ");
  44. SERIAL_ECHO(sizeof(cmdbuffer));
  45. SERIAL_ECHOLNPGM("");
  46. #endif /* CMDBUFFER_DEBUG */
  47. if (-- buflen == 0) {
  48. // Empty buffer.
  49. if (serial_count == 0)
  50. // No serial communication is pending. Reset both pointers to zero.
  51. bufindw = 0;
  52. bufindr = bufindw;
  53. } else {
  54. // There is at least one ready line in the buffer.
  55. // First skip the current command ID and iterate up to the end of the string.
  56. // for (++ bufindr; cmdbuffer[bufindr] != 0; ++ bufindr) ;
  57. for (bufindr += CMDHDRSIZE; cmdbuffer[bufindr] != 0; ++ bufindr) ;
  58. // Second, skip the end of string null character and iterate until a nonzero command ID is found.
  59. for (++ bufindr; bufindr < sizeof(cmdbuffer) && cmdbuffer[bufindr] == 0; ++ bufindr) ;
  60. // If the end of the buffer was empty,
  61. if (bufindr == sizeof(cmdbuffer)) {
  62. // skip to the start and find the nonzero command.
  63. for (bufindr = 0; cmdbuffer[bufindr] == 0; ++ bufindr) ;
  64. }
  65. #ifdef CMDBUFFER_DEBUG
  66. SERIAL_ECHOPGM("New indices: buflen ");
  67. SERIAL_ECHO(buflen);
  68. SERIAL_ECHOPGM(", bufindr ");
  69. SERIAL_ECHO(bufindr);
  70. SERIAL_ECHOPGM(", bufindw ");
  71. SERIAL_ECHO(bufindw);
  72. SERIAL_ECHOPGM(", serial_count ");
  73. SERIAL_ECHO(serial_count);
  74. SERIAL_ECHOPGM(" new command on the top: ");
  75. SERIAL_ECHO(cmdbuffer+bufindr+CMDHDRSIZE);
  76. SERIAL_ECHOLNPGM("");
  77. #endif /* CMDBUFFER_DEBUG */
  78. }
  79. return true;
  80. }
  81. return false;
  82. }
  83. void cmdqueue_reset()
  84. {
  85. while (cmdqueue_pop_front()) ;
  86. }
  87. // How long a string could be pushed to the front of the command queue?
  88. // If yes, adjust bufindr to the new position, where the new command could be enqued.
  89. // len_asked does not contain the zero terminator size.
  90. bool cmdqueue_could_enqueue_front(int len_asked)
  91. {
  92. // MAX_CMD_SIZE has to accommodate the zero terminator.
  93. if (len_asked >= MAX_CMD_SIZE)
  94. return false;
  95. // Remove the currently processed command from the queue.
  96. if (! cmdbuffer_front_already_processed) {
  97. cmdqueue_pop_front();
  98. cmdbuffer_front_already_processed = true;
  99. }
  100. if (bufindr == bufindw && buflen > 0)
  101. // Full buffer.
  102. return false;
  103. // Adjust the end of the write buffer based on whether a partial line is in the receive buffer.
  104. int endw = (serial_count > 0) ? (bufindw + MAX_CMD_SIZE + 1) : bufindw;
  105. if (bufindw < bufindr) {
  106. int bufindr_new = bufindr - len_asked - (1 + CMDHDRSIZE);
  107. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  108. if (endw <= bufindr_new) {
  109. bufindr = bufindr_new;
  110. return true;
  111. }
  112. } else {
  113. // Otherwise the free space is split between the start and end.
  114. if (len_asked + (1 + CMDHDRSIZE) <= bufindr) {
  115. // Could fit at the start.
  116. bufindr -= len_asked + (1 + CMDHDRSIZE);
  117. return true;
  118. }
  119. int bufindr_new = sizeof(cmdbuffer) - len_asked - (1 + CMDHDRSIZE);
  120. if (endw <= bufindr_new) {
  121. memset(cmdbuffer, 0, bufindr);
  122. bufindr = bufindr_new;
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. // Could one enqueue a command of lenthg len_asked into the buffer,
  129. // while leaving CMDBUFFER_RESERVE_FRONT at the start?
  130. // If yes, adjust bufindw to the new position, where the new command could be enqued.
  131. // len_asked does not contain the zero terminator size.
  132. bool cmdqueue_could_enqueue_back(int len_asked)
  133. {
  134. // MAX_CMD_SIZE has to accommodate the zero terminator.
  135. if (len_asked >= MAX_CMD_SIZE)
  136. return false;
  137. if (bufindr == bufindw && buflen > 0)
  138. // Full buffer.
  139. return false;
  140. if (serial_count > 0) {
  141. // If there is some data stored starting at bufindw, len_asked is certainly smaller than
  142. // the allocated data buffer. Try to reserve a new buffer and to move the already received
  143. // serial data.
  144. // How much memory to reserve for the commands pushed to the front?
  145. // End of the queue, when pushing to the end.
  146. int endw = bufindw + len_asked + (1 + CMDHDRSIZE);
  147. if (bufindw < bufindr)
  148. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  149. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  150. // Otherwise the free space is split between the start and end.
  151. if (// Could one fit to the end, including the reserve?
  152. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  153. // Could one fit to the end, and the reserve to the start?
  154. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  155. return true;
  156. // Could one fit both to the start?
  157. if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  158. // Mark the rest of the buffer as used.
  159. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  160. // and point to the start.
  161. bufindw = 0;
  162. return true;
  163. }
  164. } else {
  165. // How much memory to reserve for the commands pushed to the front?
  166. // End of the queue, when pushing to the end.
  167. int endw = bufindw + len_asked + (1 + CMDHDRSIZE);
  168. if (bufindw < bufindr)
  169. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  170. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  171. // Otherwise the free space is split between the start and end.
  172. if (// Could one fit to the end, including the reserve?
  173. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  174. // Could one fit to the end, and the reserve to the start?
  175. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  176. return true;
  177. // Could one fit both to the start?
  178. if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  179. // Mark the rest of the buffer as used.
  180. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  181. // and point to the start.
  182. bufindw = 0;
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. #ifdef CMDBUFFER_DEBUG
  189. void cmdqueue_dump_to_serial_single_line(int nr, const char *p)
  190. {
  191. SERIAL_ECHOPGM("Entry nr: ");
  192. SERIAL_ECHO(nr);
  193. SERIAL_ECHOPGM(", type: ");
  194. SERIAL_ECHO(int(*p));
  195. SERIAL_ECHOPGM(", cmd: ");
  196. SERIAL_ECHO(p+1);
  197. SERIAL_ECHOLNPGM("");
  198. }
  199. void cmdqueue_dump_to_serial()
  200. {
  201. if (buflen == 0) {
  202. SERIAL_ECHOLNPGM("The command buffer is empty.");
  203. } else {
  204. SERIAL_ECHOPGM("Content of the buffer: entries ");
  205. SERIAL_ECHO(buflen);
  206. SERIAL_ECHOPGM(", indr ");
  207. SERIAL_ECHO(bufindr);
  208. SERIAL_ECHOPGM(", indw ");
  209. SERIAL_ECHO(bufindw);
  210. SERIAL_ECHOLNPGM("");
  211. int nr = 0;
  212. if (bufindr < bufindw) {
  213. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + bufindw; ++ nr) {
  214. cmdqueue_dump_to_serial_single_line(nr, p);
  215. // Skip the command.
  216. for (++p; *p != 0; ++ p);
  217. // Skip the gaps.
  218. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  219. }
  220. } else {
  221. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + sizeof(cmdbuffer); ++ nr) {
  222. cmdqueue_dump_to_serial_single_line(nr, p);
  223. // Skip the command.
  224. for (++p; *p != 0; ++ p);
  225. // Skip the gaps.
  226. for (++p; p < cmdbuffer + sizeof(cmdbuffer) && *p == 0; ++ p);
  227. }
  228. for (const char *p = cmdbuffer; p < cmdbuffer + bufindw; ++ nr) {
  229. cmdqueue_dump_to_serial_single_line(nr, p);
  230. // Skip the command.
  231. for (++p; *p != 0; ++ p);
  232. // Skip the gaps.
  233. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  234. }
  235. }
  236. SERIAL_ECHOLNPGM("End of the buffer.");
  237. }
  238. }
  239. #endif /* CMDBUFFER_DEBUG */
  240. //adds an command to the main command buffer
  241. //thats really done in a non-safe way.
  242. //needs overworking someday
  243. // Currently the maximum length of a command piped through this function is around 20 characters
  244. void enquecommand(const char *cmd, bool from_progmem)
  245. {
  246. int len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  247. // Does cmd fit the queue while leaving sufficient space at the front for the chained commands?
  248. // If it fits, it may move bufindw, so it points to a contiguous buffer, which fits cmd.
  249. if (cmdqueue_could_enqueue_back(len)) {
  250. // This is dangerous if a mixing of serial and this happens
  251. // This may easily be tested: If serial_count > 0, we have a problem.
  252. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_UI;
  253. if (from_progmem)
  254. strcpy_P(cmdbuffer + bufindw + CMDHDRSIZE, cmd);
  255. else
  256. strcpy(cmdbuffer + bufindw + CMDHDRSIZE, cmd);
  257. SERIAL_ECHO_START;
  258. SERIAL_ECHORPGM(MSG_Enqueing);
  259. SERIAL_ECHO(cmdbuffer + bufindw + CMDHDRSIZE);
  260. SERIAL_ECHOLNPGM("\"");
  261. bufindw += len + (CMDHDRSIZE + 1);
  262. if (bufindw == sizeof(cmdbuffer))
  263. bufindw = 0;
  264. ++ buflen;
  265. #ifdef CMDBUFFER_DEBUG
  266. cmdqueue_dump_to_serial();
  267. #endif /* CMDBUFFER_DEBUG */
  268. } else {
  269. SERIAL_ERROR_START;
  270. SERIAL_ECHORPGM(MSG_Enqueing);
  271. if (from_progmem)
  272. SERIAL_PROTOCOLRPGM(cmd);
  273. else
  274. SERIAL_ECHO(cmd);
  275. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  276. #ifdef CMDBUFFER_DEBUG
  277. cmdqueue_dump_to_serial();
  278. #endif /* CMDBUFFER_DEBUG */
  279. }
  280. }
  281. void enquecommand_front(const char *cmd, bool from_progmem)
  282. {
  283. int 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. bool is_buffer_empty()
  319. {
  320. if (buflen == 0) return true;
  321. else return false;
  322. }
  323. void get_command()
  324. {
  325. // Test and reserve space for the new command string.
  326. if (!cmdqueue_could_enqueue_back(MAX_CMD_SIZE - 1))
  327. return;
  328. bool rx_buffer_full = false; //flag that serial rx buffer is full
  329. while (MYSERIAL.available() > 0) {
  330. if (MYSERIAL.available() == RX_BUFFER_SIZE - 1) { //compare number of chars buffered in rx buffer with rx buffer size
  331. SERIAL_ECHOLNPGM("Full RX Buffer"); //if buffer was full, there is danger that reading of last gcode will not be completed
  332. rx_buffer_full = true; //sets flag that buffer was full
  333. }
  334. char serial_char = MYSERIAL.read();
  335. if (selectedSerialPort == 1)
  336. {
  337. selectedSerialPort = 0;
  338. MYSERIAL.write(serial_char); // for debuging serial line 2 in farm_mode
  339. selectedSerialPort = 1;
  340. }
  341. TimeSent = millis();
  342. TimeNow = millis();
  343. if (serial_char < 0)
  344. // Ignore extended ASCII characters. These characters have no meaning in the G-code apart from the file names
  345. // and Marlin does not support such file names anyway.
  346. // Serial characters with a highest bit set to 1 are generated when the USB cable is unplugged, leading
  347. // to a hang-up of the print process from an SD card.
  348. continue;
  349. if(serial_char == '\n' ||
  350. serial_char == '\r' ||
  351. (serial_char == ':' && comment_mode == false) ||
  352. serial_count >= (MAX_CMD_SIZE - 1) )
  353. {
  354. if(!serial_count) { //if empty line
  355. comment_mode = false; //for new command
  356. return;
  357. }
  358. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; //terminate string
  359. if(!comment_mode){
  360. comment_mode = false; //for new command
  361. if ((strchr_pointer = strstr(cmdbuffer+bufindw+CMDHDRSIZE, "PRUSA")) == NULL && (strchr_pointer = strchr(cmdbuffer+bufindw+CMDHDRSIZE, 'N')) != NULL) {
  362. if ((strchr_pointer = strchr(cmdbuffer+bufindw+CMDHDRSIZE, 'N')) != NULL)
  363. {
  364. // Line number met. When sending a G-code over a serial line, each line may be stamped with its index,
  365. // and Marlin tests, whether the successive lines are stamped with an increasing line number ID.
  366. gcode_N = (strtol(strchr_pointer+1, NULL, 10));
  367. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer+bufindw+CMDHDRSIZE, PSTR("M110")) == NULL) ) {
  368. // M110 - set current line number.
  369. // Line numbers not sent in succession.
  370. SERIAL_ERROR_START;
  371. SERIAL_ERRORRPGM(MSG_ERR_LINE_NO);
  372. SERIAL_ERRORLN(gcode_LastN);
  373. //Serial.println(gcode_N);
  374. FlushSerialRequestResend();
  375. serial_count = 0;
  376. return;
  377. }
  378. if((strchr_pointer = strchr(cmdbuffer+bufindw+CMDHDRSIZE, '*')) != NULL)
  379. {
  380. byte checksum = 0;
  381. char *p = cmdbuffer+bufindw+CMDHDRSIZE;
  382. while (p != strchr_pointer)
  383. checksum = checksum^(*p++);
  384. if (int(strtol(strchr_pointer+1, NULL, 10)) != int(checksum)) {
  385. SERIAL_ERROR_START;
  386. SERIAL_ERRORRPGM(MSG_ERR_CHECKSUM_MISMATCH);
  387. SERIAL_ERRORLN(gcode_LastN);
  388. FlushSerialRequestResend();
  389. serial_count = 0;
  390. return;
  391. }
  392. // If no errors, remove the checksum and continue parsing.
  393. *strchr_pointer = 0;
  394. }
  395. else
  396. {
  397. SERIAL_ERROR_START;
  398. SERIAL_ERRORRPGM(MSG_ERR_NO_CHECKSUM);
  399. SERIAL_ERRORLN(gcode_LastN);
  400. FlushSerialRequestResend();
  401. serial_count = 0;
  402. return;
  403. }
  404. gcode_LastN = gcode_N;
  405. //if no errors, continue parsing
  406. } // end of 'N' command
  407. }
  408. else // if we don't receive 'N' but still see '*'
  409. {
  410. if((strchr(cmdbuffer+bufindw+CMDHDRSIZE, '*') != NULL))
  411. {
  412. SERIAL_ERROR_START;
  413. SERIAL_ERRORRPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  414. SERIAL_ERRORLN(gcode_LastN);
  415. serial_count = 0;
  416. return;
  417. }
  418. } // end of '*' command
  419. if ((strchr_pointer = strchr(cmdbuffer+bufindw+CMDHDRSIZE, 'G')) != NULL) {
  420. if (! IS_SD_PRINTING) {
  421. usb_printing_counter = 10;
  422. is_usb_printing = true;
  423. }
  424. if (Stopped == true) {
  425. int gcode = strtol(strchr_pointer+1, NULL, 10);
  426. if (gcode >= 0 && gcode <= 3) {
  427. SERIAL_ERRORLNRPGM(MSG_ERR_STOPPED);
  428. LCD_MESSAGERPGM(MSG_STOPPED);
  429. }
  430. }
  431. } // end of 'G' command
  432. //If command was e-stop process now
  433. if(strcmp(cmdbuffer+bufindw+CMDHDRSIZE, "M112") == 0)
  434. kill("", 2);
  435. // Store the current line into buffer, move to the next line.
  436. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_USB;
  437. #ifdef CMDBUFFER_DEBUG
  438. SERIAL_ECHO_START;
  439. SERIAL_ECHOPGM("Storing a command line to buffer: ");
  440. SERIAL_ECHO(cmdbuffer+bufindw+CMDHDRSIZE);
  441. SERIAL_ECHOLNPGM("");
  442. #endif /* CMDBUFFER_DEBUG */
  443. bufindw += strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  444. if (bufindw == sizeof(cmdbuffer))
  445. bufindw = 0;
  446. ++ buflen;
  447. #ifdef CMDBUFFER_DEBUG
  448. SERIAL_ECHOPGM("Number of commands in the buffer: ");
  449. SERIAL_ECHO(buflen);
  450. SERIAL_ECHOLNPGM("");
  451. #endif /* CMDBUFFER_DEBUG */
  452. } // end of 'not comment mode'
  453. serial_count = 0; //clear buffer
  454. // Don't call cmdqueue_could_enqueue_back if there are no characters waiting
  455. // in the queue, as this function will reserve the memory.
  456. if (MYSERIAL.available() == 0 || ! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1))
  457. return;
  458. } // end of "end of line" processing
  459. else {
  460. // Not an "end of line" symbol. Store the new character into a buffer.
  461. if(serial_char == ';') comment_mode = true;
  462. if(!comment_mode) cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char;
  463. }
  464. } // end of serial line processing loop
  465. if(farm_mode){
  466. TimeNow = millis();
  467. if ( ((TimeNow - TimeSent) > 800) && (serial_count > 0) ) {
  468. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0;
  469. bufindw += strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  470. if (bufindw == sizeof(cmdbuffer))
  471. bufindw = 0;
  472. ++ buflen;
  473. serial_count = 0;
  474. SERIAL_ECHOPGM("TIMEOUT:");
  475. //memset(cmdbuffer, 0 , sizeof(cmdbuffer));
  476. return;
  477. }
  478. }
  479. //add comment
  480. if (rx_buffer_full == true && serial_count > 0) { //if rx buffer was full and string was not properly terminated
  481. rx_buffer_full = false;
  482. bufindw = bufindw - serial_count; //adjust tail of the buffer to prepare buffer for writing new command
  483. serial_count = 0;
  484. }
  485. #ifdef SDSUPPORT
  486. if(!card.sdprinting || serial_count!=0){
  487. // If there is a half filled buffer from serial line, wait until return before
  488. // continuing with the serial line.
  489. return;
  490. }
  491. //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible
  492. // if it occurs, stop_buffering is triggered and the buffer is ran dry.
  493. // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing
  494. static bool stop_buffering=false;
  495. if(buflen==0) stop_buffering=false;
  496. unsigned char sd_count = 0;
  497. // Reads whole lines from the SD card. Never leaves a half-filled line in the cmdbuffer.
  498. while( !card.eof() && !stop_buffering) {
  499. int16_t n=card.get();
  500. sd_count++;
  501. char serial_char = (char)n;
  502. if(serial_char == '\n' ||
  503. serial_char == '\r' ||
  504. (serial_char == '#' && comment_mode == false) ||
  505. (serial_char == ':' && comment_mode == false) ||
  506. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  507. {
  508. if(card.eof()){
  509. SERIAL_PROTOCOLLNRPGM(MSG_FILE_PRINTED);
  510. stoptime=millis();
  511. char time[30];
  512. unsigned long t=(stoptime-starttime-pause_time)/1000;
  513. pause_time = 0;
  514. int hours, minutes;
  515. minutes=(t/60)%60;
  516. hours=t/60/60;
  517. save_statistics(total_filament_used, t);
  518. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  519. SERIAL_ECHO_START;
  520. SERIAL_ECHOLN(time);
  521. lcd_setstatus(time);
  522. card.printingHasFinished();
  523. card.checkautostart(true);
  524. if (farm_mode)
  525. {
  526. prusa_statistics(6);
  527. lcd_commands_type = LCD_COMMAND_FARM_MODE_CONFIRM;
  528. }
  529. }
  530. if(serial_char=='#')
  531. stop_buffering=true;
  532. if(!serial_count)
  533. {
  534. comment_mode = false; //for new command
  535. return; //if empty line
  536. }
  537. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; //terminate string
  538. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_SDCARD;
  539. cmdbuffer[bufindw+1] = sd_count;
  540. /* SERIAL_ECHOPGM("SD cmd(");
  541. MYSERIAL.print(sd_count, DEC);
  542. SERIAL_ECHOPGM(") ");
  543. SERIAL_ECHOLN(cmdbuffer+bufindw+CMDHDRSIZE);*/
  544. // SERIAL_ECHOPGM("cmdbuffer:");
  545. // MYSERIAL.print(cmdbuffer);
  546. ++ buflen;
  547. // SERIAL_ECHOPGM("buflen:");
  548. // MYSERIAL.print(buflen);
  549. bufindw += strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  550. if (bufindw == sizeof(cmdbuffer))
  551. bufindw = 0;
  552. comment_mode = false; //for new command
  553. serial_count = 0; //clear buffer
  554. // The following line will reserve buffer space if available.
  555. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1))
  556. return;
  557. }
  558. else
  559. {
  560. if(serial_char == ';') comment_mode = true;
  561. if(!comment_mode) cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char;
  562. }
  563. }
  564. #endif //SDSUPPORT
  565. }
  566. uint16_t cmdqueue_calc_sd_length()
  567. {
  568. int _buflen = buflen;
  569. int _bufindr = bufindr;
  570. uint16_t sdlen = 0;
  571. while (_buflen--)
  572. {
  573. if (cmdbuffer[_bufindr] == CMDBUFFER_CURRENT_TYPE_SDCARD)
  574. sdlen += cmdbuffer[_bufindr + 1];
  575. //skip header, skip command
  576. for (_bufindr += CMDHDRSIZE; cmdbuffer[_bufindr] != 0; ++ _bufindr) ;
  577. //skip zeros
  578. for (++ _bufindr; _bufindr < sizeof(cmdbuffer) && cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  579. }
  580. return sdlen;
  581. }