cmdqueue.cpp 26 KB

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