cmdqueue.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. #include "cmdqueue.h"
  2. #include "cardreader.h"
  3. #include "ultralcd.h"
  4. extern bool Stopped;
  5. // Reserve BUFSIZE lines of length MAX_CMD_SIZE plus CMDBUFFER_RESERVE_FRONT.
  6. char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT];
  7. // Head of the circular buffer, where to read.
  8. int bufindr = 0;
  9. // Tail of the buffer, where to write.
  10. int bufindw = 0;
  11. // Number of lines in cmdbuffer.
  12. int buflen = 0;
  13. // Flag for processing the current command inside the main Arduino loop().
  14. // If a new command was pushed to the front of a command buffer while
  15. // processing another command, this replaces the command on the top.
  16. // Therefore don't remove the command from the queue in the loop() function.
  17. bool cmdbuffer_front_already_processed = false;
  18. int serial_count = 0; //index of character read from serial line
  19. boolean comment_mode = false;
  20. char *strchr_pointer; // just a pointer to find chars in the command string like X, Y, Z, E, etc
  21. unsigned long TimeSent = millis();
  22. unsigned long TimeNow = millis();
  23. long gcode_N = 0;
  24. long gcode_LastN = 0;
  25. long Stopped_gcode_LastN = 0;
  26. 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. cmdbuffer_front_already_processed = false;
  89. }
  90. // How long a string could be pushed to the front of the command queue?
  91. // If yes, adjust bufindr to the new position, where the new command could be enqued.
  92. // len_asked does not contain the zero terminator size.
  93. bool cmdqueue_could_enqueue_front(int len_asked)
  94. {
  95. // MAX_CMD_SIZE has to accommodate the zero terminator.
  96. if (len_asked >= MAX_CMD_SIZE)
  97. return false;
  98. // Remove the currently processed command from the queue.
  99. if (! cmdbuffer_front_already_processed) {
  100. cmdqueue_pop_front();
  101. cmdbuffer_front_already_processed = true;
  102. }
  103. if (bufindr == bufindw && buflen > 0)
  104. // Full buffer.
  105. return false;
  106. // Adjust the end of the write buffer based on whether a partial line is in the receive buffer.
  107. int endw = (serial_count > 0) ? (bufindw + MAX_CMD_SIZE + 1) : bufindw;
  108. if (bufindw < bufindr) {
  109. int bufindr_new = bufindr - len_asked - (1 + CMDHDRSIZE);
  110. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  111. if (endw <= bufindr_new) {
  112. bufindr = bufindr_new;
  113. return true;
  114. }
  115. } else {
  116. // Otherwise the free space is split between the start and end.
  117. if (len_asked + (1 + CMDHDRSIZE) <= bufindr) {
  118. // Could fit at the start.
  119. bufindr -= len_asked + (1 + CMDHDRSIZE);
  120. return true;
  121. }
  122. int bufindr_new = sizeof(cmdbuffer) - len_asked - (1 + CMDHDRSIZE);
  123. if (endw <= bufindr_new) {
  124. memset(cmdbuffer, 0, bufindr);
  125. bufindr = bufindr_new;
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. // Could one enqueue a command of length len_asked into the buffer,
  132. // while leaving CMDBUFFER_RESERVE_FRONT at the start?
  133. // If yes, adjust bufindw to the new position, where the new command could be enqued.
  134. // len_asked does not contain the zero terminator size.
  135. // This function may update bufindw, therefore for the power panic to work, this function must be called
  136. // with the interrupts disabled!
  137. bool cmdqueue_could_enqueue_back(int len_asked, bool atomic_update)
  138. {
  139. // MAX_CMD_SIZE has to accommodate the zero terminator.
  140. if (len_asked >= MAX_CMD_SIZE)
  141. return false;
  142. if (bufindr == bufindw && buflen > 0)
  143. // Full buffer.
  144. return false;
  145. if (serial_count > 0) {
  146. // If there is some data stored starting at bufindw, len_asked is certainly smaller than
  147. // the allocated data buffer. Try to reserve a new buffer and to move the already received
  148. // serial data.
  149. // How much memory to reserve for the commands pushed to the front?
  150. // End of the queue, when pushing to the end.
  151. int endw = bufindw + len_asked + (1 + CMDHDRSIZE);
  152. if (bufindw < bufindr)
  153. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  154. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  155. // Otherwise the free space is split between the start and end.
  156. if (// Could one fit to the end, including the reserve?
  157. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  158. // Could one fit to the end, and the reserve to the start?
  159. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  160. return true;
  161. // Could one fit both to the start?
  162. if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  163. // Mark the rest of the buffer as used.
  164. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  165. // and point to the start.
  166. // Be careful! The bufindw needs to be changed atomically for the power panic & filament panic to work.
  167. if (atomic_update)
  168. cli();
  169. bufindw = 0;
  170. if (atomic_update)
  171. sei();
  172. return true;
  173. }
  174. } else {
  175. // How much memory to reserve for the commands pushed to the front?
  176. // End of the queue, when pushing to the end.
  177. int endw = bufindw + len_asked + (1 + CMDHDRSIZE);
  178. if (bufindw < bufindr)
  179. // Simple case. There is a contiguous space between the write buffer and the read buffer.
  180. return endw + CMDBUFFER_RESERVE_FRONT <= bufindr;
  181. // Otherwise the free space is split between the start and end.
  182. if (// Could one fit to the end, including the reserve?
  183. endw + CMDBUFFER_RESERVE_FRONT <= sizeof(cmdbuffer) ||
  184. // Could one fit to the end, and the reserve to the start?
  185. (endw <= sizeof(cmdbuffer) && CMDBUFFER_RESERVE_FRONT <= bufindr))
  186. return true;
  187. // Could one fit both to the start?
  188. if (len_asked + (1 + CMDHDRSIZE) + CMDBUFFER_RESERVE_FRONT <= bufindr) {
  189. // Mark the rest of the buffer as used.
  190. memset(cmdbuffer+bufindw, 0, sizeof(cmdbuffer)-bufindw);
  191. // and point to the start.
  192. // Be careful! The bufindw needs to be changed atomically for the power panic & filament panic to work.
  193. if (atomic_update)
  194. cli();
  195. bufindw = 0;
  196. if (atomic_update)
  197. sei();
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. #ifdef CMDBUFFER_DEBUG
  204. void cmdqueue_dump_to_serial_single_line(int nr, const char *p)
  205. {
  206. SERIAL_ECHOPGM("Entry nr: ");
  207. SERIAL_ECHO(nr);
  208. SERIAL_ECHOPGM(", type: ");
  209. SERIAL_ECHO(int(*p));
  210. SERIAL_ECHOPGM(", cmd: ");
  211. SERIAL_ECHO(p+1);
  212. SERIAL_ECHOLNPGM("");
  213. }
  214. void cmdqueue_dump_to_serial()
  215. {
  216. if (buflen == 0) {
  217. SERIAL_ECHOLNPGM("The command buffer is empty.");
  218. } else {
  219. SERIAL_ECHOPGM("Content of the buffer: entries ");
  220. SERIAL_ECHO(buflen);
  221. SERIAL_ECHOPGM(", indr ");
  222. SERIAL_ECHO(bufindr);
  223. SERIAL_ECHOPGM(", indw ");
  224. SERIAL_ECHO(bufindw);
  225. SERIAL_ECHOLNPGM("");
  226. int nr = 0;
  227. if (bufindr < bufindw) {
  228. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + bufindw; ++ nr) {
  229. cmdqueue_dump_to_serial_single_line(nr, p);
  230. // Skip the command.
  231. for (++p; *p != 0; ++ p);
  232. // Skip the gaps.
  233. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  234. }
  235. } else {
  236. for (const char *p = cmdbuffer + bufindr; p < cmdbuffer + sizeof(cmdbuffer); ++ nr) {
  237. cmdqueue_dump_to_serial_single_line(nr, p);
  238. // Skip the command.
  239. for (++p; *p != 0; ++ p);
  240. // Skip the gaps.
  241. for (++p; p < cmdbuffer + sizeof(cmdbuffer) && *p == 0; ++ p);
  242. }
  243. for (const char *p = cmdbuffer; p < cmdbuffer + bufindw; ++ nr) {
  244. cmdqueue_dump_to_serial_single_line(nr, p);
  245. // Skip the command.
  246. for (++p; *p != 0; ++ p);
  247. // Skip the gaps.
  248. for (++p; p < cmdbuffer + bufindw && *p == 0; ++ p);
  249. }
  250. }
  251. SERIAL_ECHOLNPGM("End of the buffer.");
  252. }
  253. }
  254. #endif /* CMDBUFFER_DEBUG */
  255. //adds an command to the main command buffer
  256. //thats really done in a non-safe way.
  257. //needs overworking someday
  258. // Currently the maximum length of a command piped through this function is around 20 characters
  259. void enquecommand(const char *cmd, bool from_progmem)
  260. {
  261. int len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  262. // Does cmd fit the queue while leaving sufficient space at the front for the chained commands?
  263. // If it fits, it may move bufindw, so it points to a contiguous buffer, which fits cmd.
  264. if (cmdqueue_could_enqueue_back(len)) {
  265. // This is dangerous if a mixing of serial and this happens
  266. // This may easily be tested: If serial_count > 0, we have a problem.
  267. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_UI;
  268. if (from_progmem)
  269. strcpy_P(cmdbuffer + bufindw + CMDHDRSIZE, cmd);
  270. else
  271. strcpy(cmdbuffer + bufindw + CMDHDRSIZE, cmd);
  272. SERIAL_ECHO_START;
  273. SERIAL_ECHORPGM(_T(MSG_Enqueing));
  274. SERIAL_ECHO(cmdbuffer + bufindw + CMDHDRSIZE);
  275. SERIAL_ECHOLNPGM("\"");
  276. bufindw += len + (CMDHDRSIZE + 1);
  277. if (bufindw == sizeof(cmdbuffer))
  278. bufindw = 0;
  279. ++ buflen;
  280. #ifdef CMDBUFFER_DEBUG
  281. cmdqueue_dump_to_serial();
  282. #endif /* CMDBUFFER_DEBUG */
  283. } else {
  284. SERIAL_ERROR_START;
  285. SERIAL_ECHORPGM(_T(MSG_Enqueing));
  286. if (from_progmem)
  287. SERIAL_PROTOCOLRPGM(cmd);
  288. else
  289. SERIAL_ECHO(cmd);
  290. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  291. #ifdef CMDBUFFER_DEBUG
  292. cmdqueue_dump_to_serial();
  293. #endif /* CMDBUFFER_DEBUG */
  294. }
  295. }
  296. bool cmd_buffer_empty()
  297. {
  298. return (buflen == 0);
  299. }
  300. void enquecommand_front(const char *cmd, bool from_progmem)
  301. {
  302. int len = from_progmem ? strlen_P(cmd) : strlen(cmd);
  303. // Does cmd fit the queue? This call shall move bufindr, so the command may be copied.
  304. if (cmdqueue_could_enqueue_front(len)) {
  305. cmdbuffer[bufindr] = CMDBUFFER_CURRENT_TYPE_UI;
  306. if (from_progmem)
  307. strcpy_P(cmdbuffer + bufindr + CMDHDRSIZE, cmd);
  308. else
  309. strcpy(cmdbuffer + bufindr + CMDHDRSIZE, cmd);
  310. ++ buflen;
  311. SERIAL_ECHO_START;
  312. SERIAL_ECHOPGM("Enqueing to the front: \"");
  313. SERIAL_ECHO(cmdbuffer + bufindr + CMDHDRSIZE);
  314. SERIAL_ECHOLNPGM("\"");
  315. #ifdef CMDBUFFER_DEBUG
  316. cmdqueue_dump_to_serial();
  317. #endif /* CMDBUFFER_DEBUG */
  318. } else {
  319. SERIAL_ERROR_START;
  320. SERIAL_ECHOPGM("Enqueing to the front: \"");
  321. if (from_progmem)
  322. SERIAL_PROTOCOLRPGM(cmd);
  323. else
  324. SERIAL_ECHO(cmd);
  325. SERIAL_ECHOLNPGM("\" failed: Buffer full!");
  326. #ifdef CMDBUFFER_DEBUG
  327. cmdqueue_dump_to_serial();
  328. #endif /* CMDBUFFER_DEBUG */
  329. }
  330. }
  331. // Mark the command at the top of the command queue as new.
  332. // Therefore it will not be removed from the queue.
  333. void repeatcommand_front()
  334. {
  335. cmdbuffer_front_already_processed = true;
  336. }
  337. bool is_buffer_empty()
  338. {
  339. if (buflen == 0) return true;
  340. else return false;
  341. }
  342. void proc_commands() {
  343. if (buflen)
  344. {
  345. process_commands();
  346. if (!cmdbuffer_front_already_processed)
  347. cmdqueue_pop_front();
  348. cmdbuffer_front_already_processed = false;
  349. }
  350. }
  351. void get_command()
  352. {
  353. // Test and reserve space for the new command string.
  354. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE - 1, true))
  355. return;
  356. bool rx_buffer_full = false; //flag that serial rx buffer is full
  357. if (MYSERIAL.available() == RX_BUFFER_SIZE - 1) { //compare number of chars buffered in rx buffer with rx buffer size
  358. MYSERIAL.flush();
  359. SERIAL_ECHOLNPGM("Full RX Buffer"); //if buffer was full, there is danger that reading of last gcode will not be completed
  360. rx_buffer_full = true; //sets flag that buffer was full
  361. }
  362. // start of serial line processing loop
  363. while (MYSERIAL.available() > 0 && !saved_printing) { //is print is saved (crash detection or filament detection), dont process data from serial line
  364. char serial_char = MYSERIAL.read();
  365. /* if (selectedSerialPort == 1)
  366. {
  367. selectedSerialPort = 0;
  368. MYSERIAL.write(serial_char); // for debuging serial line 2 in farm_mode
  369. selectedSerialPort = 1;
  370. } */ //RP - removed
  371. TimeSent = millis();
  372. TimeNow = millis();
  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 c=0 r=0
  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 (int(strtol(strchr_pointer+1, NULL, 10)) != int(checksum)) {
  414. SERIAL_ERROR_START;
  415. SERIAL_ERRORRPGM(_i("checksum mismatch, Last Line: "));////MSG_ERR_CHECKSUM_MISMATCH c=0 r=0
  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(_i("No Checksum with line number, Last Line: "));////MSG_ERR_NO_CHECKSUM c=0 r=0
  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 c=0 r=0
  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. int gcode = strtol(strchr_pointer+1, NULL, 10);
  455. if (gcode >= 0 && gcode <= 3) {
  456. SERIAL_ERRORLNRPGM(_T(MSG_ERR_STOPPED));
  457. LCD_MESSAGERPGM(_T(MSG_STOPPED));
  458. }
  459. }
  460. } // end of 'G' command
  461. //If command was e-stop process now
  462. if(strcmp(cmdbuffer+bufindw+CMDHDRSIZE, "M112") == 0)
  463. kill("", 2);
  464. // Store the current line into buffer, move to the next line.
  465. // Store type of entry
  466. cmdbuffer[bufindw] = gcode_N ? CMDBUFFER_CURRENT_TYPE_USB_WITH_LINENR : CMDBUFFER_CURRENT_TYPE_USB;
  467. #ifdef CMDBUFFER_DEBUG
  468. SERIAL_ECHO_START;
  469. SERIAL_ECHOPGM("Storing a command line to buffer: ");
  470. SERIAL_ECHO(cmdbuffer+bufindw+CMDHDRSIZE);
  471. SERIAL_ECHOLNPGM("");
  472. #endif /* CMDBUFFER_DEBUG */
  473. bufindw += strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  474. if (bufindw == sizeof(cmdbuffer))
  475. bufindw = 0;
  476. ++ buflen;
  477. #ifdef CMDBUFFER_DEBUG
  478. SERIAL_ECHOPGM("Number of commands in the buffer: ");
  479. SERIAL_ECHO(buflen);
  480. SERIAL_ECHOLNPGM("");
  481. #endif /* CMDBUFFER_DEBUG */
  482. } // end of 'not comment mode'
  483. serial_count = 0; //clear buffer
  484. // Don't call cmdqueue_could_enqueue_back if there are no characters waiting
  485. // in the queue, as this function will reserve the memory.
  486. if (MYSERIAL.available() == 0 || ! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1, true))
  487. return;
  488. } // end of "end of line" processing
  489. else {
  490. // Not an "end of line" symbol. Store the new character into a buffer.
  491. if(serial_char == ';') comment_mode = true;
  492. if(!comment_mode) cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char;
  493. }
  494. } // end of serial line processing loop
  495. if(farm_mode){
  496. TimeNow = millis();
  497. if ( ((TimeNow - TimeSent) > 800) && (serial_count > 0) ) {
  498. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0;
  499. bufindw += strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  500. if (bufindw == sizeof(cmdbuffer))
  501. bufindw = 0;
  502. ++ buflen;
  503. serial_count = 0;
  504. SERIAL_ECHOPGM("TIMEOUT:");
  505. //memset(cmdbuffer, 0 , sizeof(cmdbuffer));
  506. return;
  507. }
  508. }
  509. //add comment
  510. /*if (rx_buffer_full == true && serial_count > 0) { //if rx buffer was full and string was not properly terminated
  511. rx_buffer_full = false;
  512. bufindw = bufindw - serial_count; //adjust tail of the buffer to prepare buffer for writing new command
  513. serial_count = 0;
  514. }*/
  515. #ifdef SDSUPPORT
  516. if(!card.sdprinting || serial_count!=0){
  517. // If there is a half filled buffer from serial line, wait until return before
  518. // continuing with the serial line.
  519. return;
  520. }
  521. //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible
  522. // if it occurs, stop_buffering is triggered and the buffer is ran dry.
  523. // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing
  524. static bool stop_buffering=false;
  525. if(buflen==0) stop_buffering=false;
  526. union {
  527. struct {
  528. char lo;
  529. char hi;
  530. } lohi;
  531. uint16_t value;
  532. } sd_count;
  533. sd_count.value = 0;
  534. // Reads whole lines from the SD card. Never leaves a half-filled line in the cmdbuffer.
  535. while( !card.eof() && !stop_buffering) {
  536. int16_t n=card.get();
  537. char serial_char = (char)n;
  538. if(serial_char == '\n' ||
  539. serial_char == '\r' ||
  540. ((serial_char == '#' || serial_char == ':') && comment_mode == false) ||
  541. serial_count >= (MAX_CMD_SIZE - 1) || n==-1)
  542. {
  543. if(card.eof()){
  544. SERIAL_PROTOCOLLNRPGM(_n("Done printing file"));////MSG_FILE_PRINTED c=0 r=0
  545. stoptime=millis();
  546. char time[30];
  547. unsigned long t=(stoptime-starttime-pause_time)/1000;
  548. pause_time = 0;
  549. int hours, minutes;
  550. minutes=(t/60)%60;
  551. hours=t/60/60;
  552. save_statistics(total_filament_used, t);
  553. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  554. SERIAL_ECHO_START;
  555. SERIAL_ECHOLN(time);
  556. lcd_setstatus(time);
  557. card.printingHasFinished();
  558. card.checkautostart(true);
  559. if (farm_mode)
  560. {
  561. prusa_statistics(6);
  562. lcd_commands_type = LCD_COMMAND_FARM_MODE_CONFIRM;
  563. }
  564. }
  565. if(serial_char=='#')
  566. stop_buffering=true;
  567. if(!serial_count)
  568. {
  569. // This is either an empty line, or a line with just a comment.
  570. // Continue to the following line, and continue accumulating the number of bytes
  571. // read from the sdcard into sd_count,
  572. // so that the lenght of the already read empty lines and comments will be added
  573. // to the following non-empty line.
  574. comment_mode = false;
  575. continue; //if empty line
  576. }
  577. // The new command buffer could be updated non-atomically, because it is not yet considered
  578. // to be inside the active queue.
  579. sd_count.value = (card.get_sdpos()+1) - sdpos_atomic;
  580. cmdbuffer[bufindw] = CMDBUFFER_CURRENT_TYPE_SDCARD;
  581. cmdbuffer[bufindw+1] = sd_count.lohi.lo;
  582. cmdbuffer[bufindw+2] = sd_count.lohi.hi;
  583. cmdbuffer[bufindw+serial_count+CMDHDRSIZE] = 0; //terminate string
  584. // Calculate the length before disabling the interrupts.
  585. uint8_t len = strlen(cmdbuffer+bufindw+CMDHDRSIZE) + (1 + CMDHDRSIZE);
  586. // SERIAL_ECHOPGM("SD cmd(");
  587. // MYSERIAL.print(sd_count.value, DEC);
  588. // SERIAL_ECHOPGM(") ");
  589. // SERIAL_ECHOLN(cmdbuffer+bufindw+CMDHDRSIZE);
  590. // SERIAL_ECHOPGM("cmdbuffer:");
  591. // MYSERIAL.print(cmdbuffer);
  592. // SERIAL_ECHOPGM("buflen:");
  593. // MYSERIAL.print(buflen+1);
  594. sd_count.value = 0;
  595. cli();
  596. // This block locks the interrupts globally for 3.56 us,
  597. // which corresponds to a maximum repeat frequency of 280.70 kHz.
  598. // This blocking is safe in the context of a 10kHz stepper driver interrupt
  599. // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz.
  600. ++ buflen;
  601. bufindw += len;
  602. sdpos_atomic = card.get_sdpos()+1;
  603. if (bufindw == sizeof(cmdbuffer))
  604. bufindw = 0;
  605. sei();
  606. comment_mode = false; //for new command
  607. serial_count = 0; //clear buffer
  608. // The following line will reserve buffer space if available.
  609. if (! cmdqueue_could_enqueue_back(MAX_CMD_SIZE-1, true))
  610. return;
  611. }
  612. else
  613. {
  614. if(serial_char == ';') comment_mode = true;
  615. else if(!comment_mode) cmdbuffer[bufindw+CMDHDRSIZE+serial_count++] = serial_char;
  616. }
  617. }
  618. #endif //SDSUPPORT
  619. }
  620. uint16_t cmdqueue_calc_sd_length()
  621. {
  622. if (buflen == 0)
  623. return 0;
  624. union {
  625. struct {
  626. char lo;
  627. char hi;
  628. } lohi;
  629. uint16_t value;
  630. } sdlen_single;
  631. uint16_t sdlen = 0;
  632. for (int _buflen = buflen, _bufindr = bufindr;;) {
  633. if (cmdbuffer[_bufindr] == CMDBUFFER_CURRENT_TYPE_SDCARD) {
  634. sdlen_single.lohi.lo = cmdbuffer[_bufindr + 1];
  635. sdlen_single.lohi.hi = cmdbuffer[_bufindr + 2];
  636. sdlen += sdlen_single.value;
  637. }
  638. if (-- _buflen == 0)
  639. break;
  640. // First skip the current command ID and iterate up to the end of the string.
  641. for (_bufindr += CMDHDRSIZE; cmdbuffer[_bufindr] != 0; ++ _bufindr) ;
  642. // Second, skip the end of string null character and iterate until a nonzero command ID is found.
  643. for (++ _bufindr; _bufindr < sizeof(cmdbuffer) && cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  644. // If the end of the buffer was empty,
  645. if (_bufindr == sizeof(cmdbuffer)) {
  646. // skip to the start and find the nonzero command.
  647. for (_bufindr = 0; cmdbuffer[_bufindr] == 0; ++ _bufindr) ;
  648. }
  649. }
  650. return sdlen;
  651. }