cmdqueue.cpp 27 KB

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