cmdqueue.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. size_t bufindr = 0;
  9. // Tail of the buffer, where to write.
  10. static size_t 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. // Used for temporarely preventing accidental adding of Serial commands to the queue.
  19. // For now only check_file and the fancheck pause use this.
  20. bool cmdqueue_serial_disabled = false;
  21. int serial_count = 0; //index of character read from serial line
  22. boolean comment_mode = false;
  23. char *strchr_pointer; // just a pointer to find chars in the command string like X, Y, Z, E, etc
  24. ShortTimer farm_incomplete_command_timeout_timer;
  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. farm_incomplete_command_timeout_timer.start();
  373. if (serial_char < 0)
  374. // Ignore extended ASCII characters. These characters have no meaning in the G-code apart from the file names
  375. // and Marlin does not support such file names anyway.
  376. // Serial characters with a highest bit set to 1 are generated when the USB cable is unplugged, leading
  377. // to a hang-up of the print process from an SD card.
  378. continue;
  379. if(serial_char == '\n' ||
  380. serial_char == '\r' ||
  381. serial_count >= (MAX_CMD_SIZE - 1) )
  382. {
  383. if(!serial_count) { //if empty line
  384. comment_mode = false; //for new command
  385. return;
  386. }
  387. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; //terminate string
  388. if(!comment_mode){
  389. gcode_N = 0;
  390. // Line numbers must be first in buffer
  391. if ((strstr(cmdbuffer+bufindw+CMDHDRSIZE, "PRUSA") == NULL) &&
  392. (cmdbuffer[bufindw+CMDHDRSIZE] == 'N')) {
  393. // Line number met. When sending a G-code over a serial line, each line may be stamped with its index,
  394. // and Marlin tests, whether the successive lines are stamped with an increasing line number ID
  395. gcode_N = (strtol(cmdbuffer+bufindw+CMDHDRSIZE+1, NULL, 10));
  396. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer+bufindw+CMDHDRSIZE, PSTR("M110")) == NULL) ) {
  397. // M110 - set current line number.
  398. // Line numbers not sent in succession.
  399. SERIAL_ERROR_START;
  400. SERIAL_ERRORRPGM(_n("Line Number is not Last Line Number+1, Last Line: "));////MSG_ERR_LINE_NO
  401. SERIAL_ERRORLN(gcode_LastN);
  402. //Serial.println(gcode_N);
  403. FlushSerialRequestResend();
  404. serial_count = 0;
  405. return;
  406. }
  407. if((strchr_pointer = strchr(cmdbuffer+bufindw+CMDHDRSIZE, '*')) != NULL)
  408. {
  409. byte checksum = 0;
  410. char *p = cmdbuffer+bufindw+CMDHDRSIZE;
  411. while (p != strchr_pointer)
  412. checksum = checksum^(*p++);
  413. if (code_value_short() != (int16_t)checksum) {
  414. SERIAL_ERROR_START;
  415. SERIAL_ERRORRPGM(_n("checksum mismatch, Last Line: "));////MSG_ERR_CHECKSUM_MISMATCH
  416. SERIAL_ERRORLN(gcode_LastN);
  417. FlushSerialRequestResend();
  418. serial_count = 0;
  419. return;
  420. }
  421. // If no errors, remove the checksum and continue parsing.
  422. *strchr_pointer = 0;
  423. }
  424. else
  425. {
  426. SERIAL_ERROR_START;
  427. SERIAL_ERRORRPGM(_n("No Checksum with line number, Last Line: "));////MSG_ERR_NO_CHECKSUM
  428. SERIAL_ERRORLN(gcode_LastN);
  429. FlushSerialRequestResend();
  430. serial_count = 0;
  431. return;
  432. }
  433. // Don't parse N again with code_seen('N')
  434. cmdbuffer[bufindw + CMDHDRSIZE] = '$';
  435. //if no errors, continue parsing
  436. gcode_LastN = gcode_N;
  437. }
  438. // if we don't receive 'N' but still see '*'
  439. if ((cmdbuffer[bufindw + CMDHDRSIZE] != 'N') && (cmdbuffer[bufindw + CMDHDRSIZE] != '$') && (strchr(cmdbuffer+bufindw+CMDHDRSIZE, '*') != NULL))
  440. {
  441. SERIAL_ERROR_START;
  442. SERIAL_ERRORRPGM(_n("No Line Number with checksum, Last Line: "));////MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM
  443. SERIAL_ERRORLN(gcode_LastN);
  444. FlushSerialRequestResend();
  445. serial_count = 0;
  446. return;
  447. }
  448. if ((strchr_pointer = strchr(cmdbuffer+bufindw+CMDHDRSIZE, 'G')) != NULL) {
  449. if (! IS_SD_PRINTING) {
  450. usb_printing_counter = 10;
  451. is_usb_printing = true;
  452. }
  453. if (Stopped == true) {
  454. if (code_value_uint8() <= 3) {
  455. SERIAL_ERRORLNRPGM(MSG_ERR_STOPPED);
  456. LCD_MESSAGERPGM(_T(MSG_STOPPED));
  457. }
  458. }
  459. } // end of 'G' command
  460. //If command was e-stop process now
  461. if(strcmp(cmdbuffer+bufindw+CMDHDRSIZE, "M112") == 0)
  462. kill(MSG_M112_KILL, 2);
  463. // Store the current line into buffer, move to the next line.
  464. // Store type of entry
  465. cmdbuffer[bufindw] = gcode_N ? CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR : CMDBUFFER_CURRENT_TYPE_USB;
  466. #ifdef CMDBUFFER_DEBUG
  467. SERIAL_ECHO_START;
  468. SERIAL_ECHOPGM("Storing a command line to buffer: ");
  469. SERIAL_ECHO(cmdbuffer+bufindw+CMDHDRSIZE);
  470. SERIAL_ECHOLNPGM("");
  471. #endif /* CMDBUFFER_DEBUG */
  472. bufindw += strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  473. if (bufindw == sizeof(cmdbuffer))
  474. bufindw = 0;
  475. ++ buflen;
  476. #ifdef CMDBUFFER_DEBUG
  477. SERIAL_ECHOPGM("Number of commands in the buffer: ");
  478. SERIAL_ECHO(buflen);
  479. SERIAL_ECHOLNPGM("");
  480. #endif /* CMDBUFFER_DEBUG */
  481. } // end of 'not comment mode'
  482. serial_count = 0; //clear buffer
  483. // Don't call cmdqueue_could_enqueue_back if there are no characters waiting
  484. // in the queue, as this function will reserve the memory.
  485. if (MYSERIAL.available() == 0 || ! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1, true))
  486. return;
  487. } // end of "end of line" processing
  488. else {
  489. // Not an "end of line" symbol. Store the new character into a buffer.
  490. if(serial_char == ';') comment_mode = true;
  491. if(!comment_mode) cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char;
  492. }
  493. } // end of serial line processing loop
  494. if(farm_mode && (serial_count > 0)){
  495. if (farm_incomplete_command_timeout_timer.expired(800)) {
  496. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0;
  497. bufindw += strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  498. if (bufindw == sizeof(cmdbuffer))
  499. bufindw = 0;
  500. ++ buflen;
  501. serial_count = 0;
  502. SERIAL_ECHOPGM("TIMEOUT:");
  503. //memset(cmdbuffer, 0 , sizeof(cmdbuffer));
  504. return;
  505. }
  506. }
  507. #ifdef SDSUPPORT
  508. if(!card.sdprinting || serial_count!=0){
  509. // If there is a half filled buffer from serial line, wait until return before
  510. // continuing with the serial line.
  511. return;
  512. }
  513. //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible
  514. // if it occurs, stop_buffering is triggered and the buffer is ran dry.
  515. // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing
  516. static bool stop_buffering=false;
  517. if(buflen==0) stop_buffering=false;
  518. union {
  519. struct {
  520. char lo;
  521. char hi;
  522. } lohi;
  523. uint16_t value;
  524. } sd_count;
  525. sd_count.value = 0;
  526. // Reads whole lines from the SD card. Never leaves a half-filled line in the cmdbuffer.
  527. while( !card.eof() && !stop_buffering) {
  528. int16_t n=card.getFilteredGcodeChar();
  529. char serial_char = (char)n;
  530. if( serial_char == '\n'
  531. || serial_char == '\r'
  532. || ((serial_char == '#' || serial_char == ':') )
  533. || serial_count >= (MAX_CMD_SIZE - 1)
  534. || n==-1
  535. ){
  536. if(serial_char=='#')
  537. stop_buffering=true;
  538. if(!serial_count)
  539. {
  540. // This is either an empty line, or a line with just a comment.
  541. // Continue to the following line, and continue accumulating the number of bytes
  542. // read from the sdcard into sd_count,
  543. // so that the lenght of the already read empty lines and comments will be added
  544. // to the following non-empty line.
  545. return; // prevent cycling indefinitely - let manage_heaters do their job
  546. }
  547. // The new command buffer could be updated non-atomically, because it is not yet considered
  548. // to be inside the active queue.
  549. sd_count.value = card.get_sdpos() - sdpos_atomic;
  550. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_SDCARD;
  551. cmdbuffer[bufindw+1] = sd_count.lohi.lo;
  552. cmdbuffer[bufindw+2] = sd_count.lohi.hi;
  553. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; //terminate string
  554. // Calculate the length before disabling the interrupts.
  555. uint8_t len = strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  556. // SERIAL_ECHOPGM("SD cmd(");
  557. // MYSERIAL.print(sd_count.value, DEC);
  558. // SERIAL_ECHOPGM(") ");
  559. // SERIAL_ECHOLN(cmdbuffer+bufindw+CMDHDRSIZE);
  560. // SERIAL_ECHOPGM("cmdbuffer:");
  561. // MYSERIAL.print(cmdbuffer);
  562. // SERIAL_ECHOPGM("buflen:");
  563. // MYSERIAL.print(buflen+1);
  564. sd_count.value = 0;
  565. cli();
  566. // This block locks the interrupts globally for 3.56 us,
  567. // which corresponds to a maximum repeat frequency of 280.70 kHz.
  568. // This blocking is safe in the context of a 10kHz stepper driver interrupt
  569. // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz.
  570. ++ buflen;
  571. bufindw += len;
  572. sdpos_atomic = card.get_sdpos();
  573. if (bufindw == sizeof(cmdbuffer))
  574. bufindw = 0;
  575. sei();
  576. comment_mode = false; //for new command
  577. serial_count = 0; //clear buffer
  578. if(card.eof()) break;
  579. // The following line will reserve buffer space if available.
  580. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1, true))
  581. return;
  582. }
  583. else
  584. {
  585. // there are no comments coming from the filtered file
  586. cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char;
  587. }
  588. }
  589. if(card.eof())
  590. {
  591. // file was fully buffered, but commands might still need to be planned!
  592. // do *not* clear sdprinting until all SD commands are consumed to ensure
  593. // SD state can be resumed from a saved printing state. sdprinting is only
  594. // cleared by printingHasFinished after peforming all remaining moves.
  595. if(!cmdqueue_calc_sd_length())
  596. {
  597. SERIAL_PROTOCOLLNRPGM(_n("Done printing file"));////MSG_FILE_PRINTED
  598. stoptime=_millis();
  599. char time[30];
  600. unsigned long t=(stoptime-starttime-pause_time)/1000;
  601. pause_time = 0;
  602. int hours, minutes;
  603. minutes=(t/60)%60;
  604. hours=t/60/60;
  605. save_statistics(total_filament_used, t);
  606. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  607. SERIAL_ECHO_START;
  608. SERIAL_ECHOLN(time);
  609. lcd_setstatus(time);
  610. card.printingHasFinished();
  611. card.checkautostart(true);
  612. if (farm_mode)
  613. {
  614. prusa_statistics(6);
  615. lcd_commands_type = LcdCommands::FarmModeConfirm;
  616. }
  617. }
  618. }
  619. #endif //SDSUPPORT
  620. }
  621. uint16_t cmdqueue_calc_sd_length()
  622. {
  623. if (buflen == 0)
  624. return 0;
  625. union {
  626. struct {
  627. char lo;
  628. char hi;
  629. } lohi;
  630. uint16_t value;
  631. } sdlen_single;
  632. uint16_t sdlen = 0;
  633. for (size_t _buflen = buflen, _bufindr = bufindr;;) {
  634. if (cmdbuffer[_bufindr] == CMDBUFFER_CURRENT_TYPE_SDCARD) {
  635. sdlen_single.lohi.lo = cmdbuffer[_bufindr + 1];
  636. sdlen_single.lohi.hi = cmdbuffer[_bufindr + 2];
  637. sdlen += sdlen_single.value;
  638. }
  639. if (-- _buflen == 0)
  640. break;
  641. // First skip the current command ID and iterate up to the end of the string.
  642. for (_bufindr += CMDHDRSIZE; cmdbuffer[_bufindr] != 0; ++ _bufindr) ;
  643. // Second, skip the end of string null character and iterate until a nonzero command ID is found.
  644. for (++ _bufindr; _bufindr < sizeof(cmdbuffer) && cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  645. // If the end of the buffer was empty,
  646. if (_bufindr == sizeof(cmdbuffer)) {
  647. // skip to the start and find the nonzero command.
  648. for (_bufindr = 0; cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  649. }
  650. }
  651. return sdlen;
  652. }