cmdqueue.cpp 27 KB

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