My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

anycubic_i3mega_lcd.cpp 36KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /**
  2. * anycubic_i3mega_lcd.cpp --- Support for Anycubic i3 Mega TFT
  3. * Created by Christian Hopp on 09.12.17.
  4. * Improved by David Ramiro
  5. * Converted to ExtUI by John BouAntoun 21 June 2020
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "../../../../inc/MarlinConfigPre.h"
  22. #if ENABLED(ANYCUBIC_LCD_I3MEGA)
  23. #include "anycubic_i3mega_lcd.h"
  24. #include "../../ui_api.h"
  25. #include "../../../../libs/numtostr.h"
  26. #include "../../../../module/motion.h" // for quickstop_stepper, A20 read printing speed, feedrate_percentage
  27. #include "../../../../MarlinCore.h" // for disable_steppers
  28. #include "../../../../inc/MarlinConfig.h"
  29. // command sending macro's with debugging capability
  30. #define SEND_PGM(x) send_P(PSTR(x))
  31. #define SENDLINE_PGM(x) sendLine_P(PSTR(x))
  32. #define SEND_PGM_VAL(x,y) (send_P(PSTR(x)), sendLine(i16tostr3rj(y)))
  33. #define SEND(x) send(x)
  34. #define SENDLINE(x) sendLine(x)
  35. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  36. #define SENDLINE_DBG_PGM(x,y) (sendLine_P(PSTR(x)), SERIAL_ECHOLNPGM(y))
  37. #define SENDLINE_DBG_PGM_VAL(x,y,z) (sendLine_P(PSTR(x)), SERIAL_ECHOPGM(y), SERIAL_ECHOLN(z))
  38. #else
  39. #define SENDLINE_DBG_PGM(x,y) sendLine_P(PSTR(x))
  40. #define SENDLINE_DBG_PGM_VAL(x,y,z) sendLine_P(PSTR(x))
  41. #endif
  42. AnycubicTFTClass AnycubicTFT;
  43. char AnycubicTFTClass::TFTcmdbuffer[TFTBUFSIZE][TFT_MAX_CMD_SIZE];
  44. int AnycubicTFTClass::TFTbuflen = 0,
  45. AnycubicTFTClass::TFTbufindr = 0,
  46. AnycubicTFTClass::TFTbufindw = 0;
  47. char AnycubicTFTClass::serial3_char;
  48. int AnycubicTFTClass::serial3_count = 0;
  49. char* AnycubicTFTClass::TFTstrchr_pointer;
  50. uint8_t AnycubicTFTClass::SpecialMenu = false;
  51. AnycubicMediaPrintState AnycubicTFTClass::mediaPrintingState = AMPRINTSTATE_NOT_PRINTING;
  52. AnycubicMediaPauseState AnycubicTFTClass::mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
  53. char AnycubicTFTClass::SelectedDirectory[30];
  54. char AnycubicTFTClass::SelectedFile[FILENAME_LENGTH];
  55. // Serial helpers
  56. static void sendNewLine(void) { LCD_SERIAL.write('\r'); LCD_SERIAL.write('\n'); }
  57. static void send(const char *str) { LCD_SERIAL.print(str); }
  58. static void send_P(PGM_P str) {
  59. while (const char c = pgm_read_byte(str++))
  60. LCD_SERIAL.write(c);
  61. }
  62. static void sendLine(const char *str) { send(str); sendNewLine(); }
  63. static void sendLine_P(PGM_P str) { send_P(str); sendNewLine(); }
  64. using namespace ExtUI;
  65. AnycubicTFTClass::AnycubicTFTClass() {}
  66. void AnycubicTFTClass::OnSetup() {
  67. #ifndef LCD_BAUDRATE
  68. #define LCD_BAUDRATE 115200
  69. #endif
  70. LCD_SERIAL.begin(LCD_BAUDRATE);
  71. SENDLINE_DBG_PGM("J17", "TFT Serial Debug: Main board reset... J17"); // J17 Main board reset
  72. delay_ms(10);
  73. // initialise the state of the key pins running on the tft
  74. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  75. SET_INPUT_PULLUP(SD_DETECT_PIN);
  76. #endif
  77. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  78. SET_INPUT_PULLUP(FIL_RUNOUT1_PIN);
  79. #endif
  80. mediaPrintingState = AMPRINTSTATE_NOT_PRINTING;
  81. mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
  82. // DoSDCardStateCheck();
  83. SENDLINE_DBG_PGM("J12", "TFT Serial Debug: Ready... J12"); // J12 Ready
  84. delay_ms(10);
  85. DoFilamentRunoutCheck();
  86. SelectedFile[0] = 0;
  87. #if ENABLED(STARTUP_CHIME)
  88. injectCommands_P(PSTR("M300 P250 S554\nM300 P250 S554\nM300 P250 S740\nM300 P250 S554\nM300 P250 S740\nM300 P250 S554\nM300 P500 S831"));
  89. #endif
  90. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  91. SERIAL_ECHOLNPGM("TFT Serial Debug: Finished startup");
  92. #endif
  93. }
  94. void AnycubicTFTClass::OnCommandScan() {
  95. static millis_t nextStopCheck = 0; // used to slow the stopped print check down to reasonable times
  96. const millis_t ms = millis();
  97. if (ELAPSED(ms, nextStopCheck)) {
  98. nextStopCheck = ms + 1000UL;
  99. if (mediaPrintingState == AMPRINTSTATE_STOP_REQUESTED && IsNozzleHomed()) {
  100. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  101. SERIAL_ECHOLNPGM("TFT Serial Debug: Finished stopping print, releasing motors ...");
  102. #endif
  103. mediaPrintingState = AMPRINTSTATE_NOT_PRINTING;
  104. mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
  105. injectCommands_P(PSTR("M84\nM27")); // disable stepper motors and force report of SD status
  106. delay_ms(200);
  107. // tell printer to release resources of print to indicate it is done
  108. SENDLINE_DBG_PGM("J14", "TFT Serial Debug: SD Print Stopped... J14");
  109. }
  110. }
  111. if (TFTbuflen < (TFTBUFSIZE - 1))
  112. GetCommandFromTFT();
  113. if (TFTbuflen) {
  114. TFTbuflen = (TFTbuflen - 1);
  115. TFTbufindr = (TFTbufindr + 1) % TFTBUFSIZE;
  116. }
  117. }
  118. void AnycubicTFTClass::OnKillTFT() {
  119. SENDLINE_DBG_PGM("J11", "TFT Serial Debug: Kill command... J11");
  120. }
  121. void AnycubicTFTClass::OnSDCardStateChange(bool isInserted) {
  122. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  123. SERIAL_ECHOLNPAIR("TFT Serial Debug: OnSDCardStateChange event triggered...", isInserted);
  124. #endif
  125. DoSDCardStateCheck();
  126. }
  127. void AnycubicTFTClass::OnSDCardError() {
  128. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  129. SERIAL_ECHOLNPGM("TFT Serial Debug: OnSDCardError event triggered...");
  130. #endif
  131. SENDLINE_DBG_PGM("J21", "TFT Serial Debug: On SD Card Error ... J21");
  132. }
  133. void AnycubicTFTClass::OnFilamentRunout() {
  134. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  135. SERIAL_ECHOLNPGM("TFT Serial Debug: FilamentRunout triggered...");
  136. #endif
  137. DoFilamentRunoutCheck();
  138. }
  139. void AnycubicTFTClass::OnUserConfirmRequired(const char * const msg) {
  140. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  141. SERIAL_ECHOLNPAIR("TFT Serial Debug: OnUserConfirmRequired triggered... ", msg);
  142. #endif
  143. #if ENABLED(SDSUPPORT)
  144. /**
  145. * Need to handle the process of following states
  146. * "Nozzle Parked"
  147. * "Load Filament"
  148. * "Filament Purging..."
  149. * "HeaterTimeout"
  150. * "Reheat finished."
  151. *
  152. * NOTE: The only way to handle these states is strcmp_P with the msg unfortunately (very expensive)
  153. */
  154. if (strcmp_P(msg, PSTR("Nozzle Parked")) == 0) {
  155. mediaPrintingState = AMPRINTSTATE_PAUSED;
  156. mediaPauseState = AMPAUSESTATE_PARKED;
  157. // enable continue button
  158. SENDLINE_DBG_PGM("J18", "TFT Serial Debug: UserConfirm SD print paused done... J18");
  159. }
  160. else if (strcmp_P(msg, PSTR("Load Filament")) == 0) {
  161. mediaPrintingState = AMPRINTSTATE_PAUSED;
  162. mediaPauseState = AMPAUSESTATE_FILAMENT_OUT;
  163. // enable continue button
  164. SENDLINE_DBG_PGM("J18", "TFT Serial Debug: UserConfirm Filament is out... J18");
  165. SENDLINE_DBG_PGM("J23", "TFT Serial Debug: UserConfirm Blocking filament prompt... J23");
  166. }
  167. else if (strcmp_P(msg, PSTR("Filament Purging...")) == 0) {
  168. mediaPrintingState = AMPRINTSTATE_PAUSED;
  169. mediaPauseState = AMPAUSESTATE_PARKING;
  170. // TODO: JBA I don't think J05 just disables the continue button, i think it injects a rogue M25. So taking this out
  171. // disable continue button
  172. // SENDLINE_DBG_PGM("J05", "TFT Serial Debug: UserConfirm SD Filament Purging... J05"); // J05 printing pause
  173. // enable continue button
  174. SENDLINE_DBG_PGM("J18", "TFT Serial Debug: UserConfirm Filament is purging... J18");
  175. }
  176. else if (strcmp_P(msg, PSTR("HeaterTimeout")) == 0) {
  177. mediaPrintingState = AMPRINTSTATE_PAUSED;
  178. mediaPauseState = AMPAUSESTATE_HEATER_TIMEOUT;
  179. // enable continue button
  180. SENDLINE_DBG_PGM("J18", "TFT Serial Debug: UserConfirm SD Heater timeout... J18");
  181. }
  182. else if (strcmp_P(msg, PSTR("Reheat finished.")) == 0) {
  183. mediaPrintingState = AMPRINTSTATE_PAUSED;
  184. mediaPauseState = AMPAUSESTATE_REHEAT_FINISHED;
  185. // enable continue button
  186. SENDLINE_DBG_PGM("J18", "TFT Serial Debug: UserConfirm SD Reheat done... J18");
  187. }
  188. #endif
  189. }
  190. float AnycubicTFTClass::CodeValue() {
  191. return (strtod(&TFTcmdbuffer[TFTbufindr][TFTstrchr_pointer - TFTcmdbuffer[TFTbufindr] + 1], nullptr));
  192. }
  193. bool AnycubicTFTClass::CodeSeen(char code) {
  194. TFTstrchr_pointer = strchr(TFTcmdbuffer[TFTbufindr], code);
  195. return !!TFTstrchr_pointer; // Return True if a character was found
  196. }
  197. bool AnycubicTFTClass::IsNozzleHomed() {
  198. const float xPosition = getAxisPosition_mm((axis_t) X);
  199. const float yPosition = getAxisPosition_mm((axis_t) Y);
  200. return WITHIN(xPosition, X_MIN_POS - 0.1, X_MIN_POS + 0.1) &&
  201. WITHIN(yPosition, Y_MIN_POS - 0.1, Y_MIN_POS + 0.1);
  202. }
  203. void AnycubicTFTClass::HandleSpecialMenu() {
  204. /**
  205. * NOTE: that the file selection command actual lowercases the entire selected file/foldername, so charracter comparisons need to be lowercase.
  206. */
  207. if (SelectedDirectory[0] == '<') {
  208. switch (SelectedDirectory[1]) {
  209. case 'e': // "<exit>"
  210. SpecialMenu = false;
  211. return;
  212. break;
  213. #if ENABLED(PROBE_MANUALLY)
  214. case '0':
  215. switch (SelectedDirectory[2]) {
  216. case '1': // "<01ZUp0.1>"
  217. SERIAL_ECHOLNPGM("Special Menu: Z Up 0.1");
  218. injectCommands_P(PSTR("G91\nG1 Z+0.1\nG90"));
  219. break;
  220. case '2': // "<02ZUp0.02>"
  221. SERIAL_ECHOLNPGM("Special Menu: Z Up 0.02");
  222. injectCommands_P(PSTR("G91\nG1 Z+0.02\nG90"));
  223. break;
  224. case '3': // "<03ZDn0.02>"
  225. SERIAL_ECHOLNPGM("Special Menu: Z Down 0.02");
  226. injectCommands_P(PSTR("G91\nG1 Z-0.02\nG90"));
  227. break;
  228. case '4': // "<04ZDn0.1>"
  229. SERIAL_ECHOLNPGM("Special Menu: Z Down 0.1");
  230. injectCommands_P(PSTR("G91\nG1 Z-0.1\nG90"));
  231. break;
  232. case '5': // "<05PrehtBed>"
  233. SERIAL_ECHOLNPGM("Special Menu: Preheat Bed");
  234. injectCommands_P(PSTR("M140 S65"));
  235. break;
  236. case '6': // "<06SMeshLvl>"
  237. SERIAL_ECHOLNPGM("Special Menu: Start Mesh Leveling");
  238. injectCommands_P(PSTR("G29S1"));
  239. break;
  240. case '7': // "<07MeshNPnt>"
  241. SERIAL_ECHOLNPGM("Special Menu: Next Mesh Point");
  242. injectCommands_P(PSTR("G29S2"));
  243. break;
  244. case '8': // "<08HtEndPID>"
  245. SERIAL_ECHOLNPGM("Special Menu: Auto Tune Hotend PID");
  246. // need to dwell for half a second to give the fan a chance to start before the pid tuning starts
  247. injectCommands_P(PSTR("M106 S204\nG4 P500\nM303 E0 S215 C15 U1"));
  248. break;
  249. case '9': // "<09HtBedPID>"
  250. SERIAL_ECHOLNPGM("Special Menu: Auto Tune Hotbed Pid");
  251. injectCommands_P(PSTR("M303 E-1 S65 C6 U1"));
  252. break;
  253. default:
  254. break;
  255. }
  256. break;
  257. case '1':
  258. switch (SelectedDirectory[2]) {
  259. case '0': // "<10FWDeflts>"
  260. SERIAL_ECHOLNPGM("Special Menu: Load FW Defaults");
  261. injectCommands_P(PSTR("M502\nM300 P105 S1661\nM300 P210 S1108"));
  262. break;
  263. case '1': // "<11SvEEPROM>"
  264. SERIAL_ECHOLNPGM("Special Menu: Save EEPROM");
  265. injectCommands_P(PSTR("M500\nM300 P105 S1108\nM300 P210 S1661"));
  266. break;
  267. default:
  268. break;
  269. }
  270. break;
  271. #else // if ENABLED(PROBE_MANUALLY)
  272. case '0':
  273. switch (SelectedDirectory[2]) {
  274. case '1': // "<01PrehtBed>"
  275. SERIAL_ECHOLNPGM("Special Menu: Preheat Bed");
  276. injectCommands_P(PSTR("M140 S65"));
  277. break;
  278. case '2': // "<02ABL>"
  279. SERIAL_ECHOLNPGM("Special Menu: Auto Bed Leveling");
  280. injectCommands_P(PSTR("G29N"));
  281. break;
  282. case '3': // "<03HtendPID>"
  283. SERIAL_ECHOLNPGM("Special Menu: Auto Tune Hotend PID");
  284. // need to dwell for half a second to give the fan a chance to start before the pid tuning starts
  285. injectCommands_P(PSTR("M106 S204\nG4 P500\nM303 E0 S215 C15 U1"));
  286. break;
  287. case '4': // "<04HtbedPID>"
  288. SERIAL_ECHOLNPGM("Special Menu: Auto Tune Hotbed Pid");
  289. injectCommands_P(PSTR("M303 E-1 S65 C6 U1"));
  290. break;
  291. case '5': // "<05FWDeflts>"
  292. SERIAL_ECHOLNPGM("Special Menu: Load FW Defaults");
  293. injectCommands_P(PSTR("M502\nM300 P105 S1661\nM300 P210 S1108"));
  294. break;
  295. case '6': // "<06SvEEPROM>"
  296. SERIAL_ECHOLNPGM("Special Menu: Save EEPROM");
  297. injectCommands_P(PSTR("M500\nM300 P105 S1108\nM300 P210 S1661"));
  298. break;
  299. case '7': // <07SendM108>
  300. SERIAL_ECHOLNPGM("Special Menu: Send User Confirmation");
  301. injectCommands_P(PSTR("M108"));
  302. break;
  303. default:
  304. break;
  305. }
  306. break;
  307. #endif // PROBE_MANUALLY
  308. default:
  309. break;
  310. }
  311. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  312. }
  313. else {
  314. SERIAL_ECHOPGM("TFT Serial Debug: Attempted to HandleSpecialMenu on non-special menu... ");
  315. SERIAL_ECHOLN(SelectedDirectory);
  316. #endif
  317. }
  318. }
  319. void AnycubicTFTClass::RenderCurrentFileList() {
  320. #if ENABLED(SDSUPPORT)
  321. uint16_t selectedNumber = 0;
  322. SelectedDirectory[0] = 0;
  323. SelectedFile[0] = 0;
  324. FileList currentFileList;
  325. SENDLINE_PGM("FN "); // Filelist start
  326. if (!isMediaInserted() && !SpecialMenu) {
  327. SENDLINE_DBG_PGM("J02", "TFT Serial Debug: No SD Card mounted to render Current File List... J02");
  328. SENDLINE_PGM("<Special_Menu>");
  329. SENDLINE_PGM("<Special_Menu>");
  330. }
  331. else {
  332. if (CodeSeen('S'))
  333. selectedNumber = CodeValue();
  334. if (SpecialMenu)
  335. RenderSpecialMenu(selectedNumber);
  336. else if (selectedNumber <= currentFileList.count())
  337. RenderCurrentFolder(selectedNumber);
  338. }
  339. SENDLINE_PGM("END"); // Filelist stop
  340. #endif // SDSUPPORT
  341. }
  342. void AnycubicTFTClass::RenderSpecialMenu(uint16_t selectedNumber) {
  343. switch (selectedNumber) {
  344. #if ENABLED(PROBE_MANUALLY)
  345. case 0: // First Page
  346. SENDLINE_PGM("<01ZUp0.1>");
  347. SENDLINE_PGM("<Z Up 0.1>");
  348. SENDLINE_PGM("<02ZUp0.02>");
  349. SENDLINE_PGM("<Z Up 0.02>");
  350. SENDLINE_PGM("<03ZDn0.02>");
  351. SENDLINE_PGM("<Z Down 0.02>");
  352. SENDLINE_PGM("<04ZDn0.1>");
  353. SENDLINE_PGM("<Z Down 0.1>");
  354. break;
  355. case 4: // Second Page
  356. SENDLINE_PGM("<05PrehtBed>");
  357. SENDLINE_PGM("<Preheat bed>");
  358. SENDLINE_PGM("<06SMeshLvl>");
  359. SENDLINE_PGM("<Start Mesh Leveling>");
  360. SENDLINE_PGM("<07MeshNPnt>");
  361. SENDLINE_PGM("<Next Mesh Point>");
  362. SENDLINE_PGM("<08HtEndPID>");
  363. SENDLINE_PGM("<Auto Tune Hotend PID>");
  364. break;
  365. case 8: // Third Page
  366. SENDLINE_PGM("<09HtBedPID>");
  367. SENDLINE_PGM("<Auto Tune Hotbed PID>");
  368. SENDLINE_PGM("<10FWDeflts>");
  369. SENDLINE_PGM("<Load FW Defaults>");
  370. SENDLINE_PGM("<11SvEEPROM>");
  371. SENDLINE_PGM("<Save EEPROM>");
  372. SENDLINE_PGM("<Exit>");
  373. SENDLINE_PGM("<Exit>");
  374. break;
  375. #else
  376. case 0: // First Page
  377. SENDLINE_PGM("<01PrehtBed>");
  378. SENDLINE_PGM("<Preheat bed>");
  379. SENDLINE_PGM("<02ABL>");
  380. SENDLINE_PGM("<Auto Bed Leveling>");
  381. SENDLINE_PGM("<03HtEndPID>");
  382. SENDLINE_PGM("<Auto Tune Hotend PID>");
  383. SENDLINE_PGM("<04HtBedPID>");
  384. SENDLINE_PGM("<Auto Tune Hotbed PID>");
  385. break;
  386. case 4: // Second Page
  387. SENDLINE_PGM("<05FWDeflts>");
  388. SENDLINE_PGM("<Load FW Defaults>");
  389. SENDLINE_PGM("<06SvEEPROM>");
  390. SENDLINE_PGM("<Save EEPROM>");
  391. SENDLINE_PGM("<07SendM108>");
  392. SENDLINE_PGM("<Send User Confirmation>");
  393. SENDLINE_PGM("<Exit>");
  394. SENDLINE_PGM("<Exit>");
  395. break;
  396. #endif // PROBE_MANUALLY
  397. default:
  398. break;
  399. }
  400. }
  401. void AnycubicTFTClass::RenderCurrentFolder(uint16_t selectedNumber) {
  402. FileList currentFileList;
  403. uint16_t cnt = selectedNumber;
  404. uint16_t max_files;
  405. uint16_t dir_files = currentFileList.count();
  406. if ((dir_files - selectedNumber) < 4)
  407. max_files = dir_files;
  408. else
  409. max_files = selectedNumber + 3;
  410. for (cnt = selectedNumber; cnt <= max_files; cnt++) {
  411. if (cnt == 0) { // Special Entry
  412. if (currentFileList.isAtRootDir()) {
  413. SENDLINE_PGM("<specialmnu>");
  414. SENDLINE_PGM("<Special Menu>");
  415. }
  416. else {
  417. SENDLINE_PGM("/..");
  418. SENDLINE_PGM("/..");
  419. }
  420. }
  421. else {
  422. currentFileList.seek(cnt - 1, false);
  423. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  424. SERIAL_ECHOLN(currentFileList.filename());
  425. #endif
  426. if (currentFileList.isDir()) {
  427. SEND_PGM("/");
  428. SENDLINE(currentFileList.shortFilename());
  429. SEND_PGM("/");
  430. SENDLINE(currentFileList.filename());
  431. }
  432. else {
  433. SENDLINE(currentFileList.shortFilename());
  434. SENDLINE(currentFileList.filename());
  435. }
  436. }
  437. }
  438. }
  439. void AnycubicTFTClass::OnPrintTimerStarted() {
  440. #if ENABLED(SDSUPPORT)
  441. if (mediaPrintingState == AMPRINTSTATE_PRINTING)
  442. SENDLINE_DBG_PGM("J04", "TFT Serial Debug: Starting SD Print... J04"); // J04 Starting Print
  443. #endif
  444. }
  445. void AnycubicTFTClass::OnPrintTimerPaused() {
  446. #if ENABLED(SDSUPPORT)
  447. if (isPrintingFromMedia()) {
  448. mediaPrintingState = AMPRINTSTATE_PAUSED;
  449. mediaPauseState = AMPAUSESTATE_PARKING;
  450. }
  451. #endif
  452. }
  453. void AnycubicTFTClass::OnPrintTimerStopped() {
  454. #if ENABLED(SDSUPPORT)
  455. if (mediaPrintingState == AMPRINTSTATE_PRINTING) {
  456. mediaPrintingState = AMPRINTSTATE_NOT_PRINTING;
  457. mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
  458. SENDLINE_DBG_PGM("J14", "TFT Serial Debug: SD Print Completed... J14");
  459. }
  460. // otherwise it was stopped by the printer so don't send print completed signal to TFT
  461. #endif
  462. }
  463. void AnycubicTFTClass::GetCommandFromTFT() {
  464. char *starpos = nullptr;
  465. while (LCD_SERIAL.available() > 0 && TFTbuflen < TFTBUFSIZE) {
  466. serial3_char = LCD_SERIAL.read();
  467. if (serial3_char == '\n' ||
  468. serial3_char == '\r' ||
  469. serial3_char == ':' ||
  470. serial3_count >= (TFT_MAX_CMD_SIZE - 1)
  471. ) {
  472. if (!serial3_count) return; // if empty line
  473. TFTcmdbuffer[TFTbufindw][serial3_count] = 0; // terminate string
  474. if ((strchr(TFTcmdbuffer[TFTbufindw], 'A') != nullptr)) {
  475. int16_t a_command;
  476. TFTstrchr_pointer = strchr(TFTcmdbuffer[TFTbufindw], 'A');
  477. a_command = ((int)((strtod(&TFTcmdbuffer[TFTbufindw][TFTstrchr_pointer - TFTcmdbuffer[TFTbufindw] + 1], nullptr))));
  478. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  479. if ((a_command > 7) && (a_command != 20)) // No debugging of status polls, please!
  480. SERIAL_ECHOLNPAIR("TFT Serial Command: ", TFTcmdbuffer[TFTbufindw]);
  481. #endif
  482. switch (a_command) {
  483. case 0: { // A0 GET HOTEND TEMP
  484. const float hotendActualTemp = getActualTemp_celsius(E0);
  485. SEND_PGM_VAL("A0V ", int(hotendActualTemp + 0.5));
  486. }
  487. break;
  488. case 1: { // A1 GET HOTEND TARGET TEMP
  489. const float hotendTargetTemp = getTargetTemp_celsius(E0);
  490. SEND_PGM_VAL("A1V ", int(hotendTargetTemp + 0.5));
  491. }
  492. break;
  493. case 2: { // A2 GET HOTBED TEMP
  494. const float heatedBedActualTemp = getActualTemp_celsius(BED);
  495. SEND_PGM_VAL("A2V ", int(heatedBedActualTemp + 0.5));
  496. }
  497. break;
  498. case 3: { // A3 GET HOTBED TARGET TEMP
  499. const float heatedBedTargetTemp = getTargetTemp_celsius(BED);
  500. SEND_PGM_VAL("A3V ", int(heatedBedTargetTemp + 0.5));
  501. } break;
  502. case 4: { // A4 GET FAN SPEED
  503. const float fanPercent = getActualFan_percent(FAN0);
  504. SEND_PGM_VAL("A4V ", int(LIMIT(fanPercent, 0, 100)));
  505. } break;
  506. case 5: { // A5 GET CURRENT COORDINATE
  507. const float xPosition = getAxisPosition_mm(X),
  508. yPosition = getAxisPosition_mm(Y),
  509. zPosition = getAxisPosition_mm(Z);
  510. SEND_PGM("A5V X: "); LCD_SERIAL.print(xPosition);
  511. SEND_PGM( " Y: "); LCD_SERIAL.print(yPosition);
  512. SEND_PGM( " Z: "); LCD_SERIAL.print(zPosition);
  513. SENDLINE_PGM("");
  514. } break;
  515. case 6: // A6 GET SD CARD PRINTING STATUS
  516. #if ENABLED(SDSUPPORT)
  517. if (isPrintingFromMedia()) {
  518. SEND_PGM("A6V ");
  519. if (isMediaInserted())
  520. SENDLINE(ui8tostr3rj(getProgress_percent()));
  521. else
  522. SENDLINE_DBG_PGM("J02", "TFT Serial Debug: No SD Card mounted to return printing status... J02");
  523. }
  524. else
  525. SENDLINE_PGM("A6V ---");
  526. #endif
  527. break;
  528. case 7: { // A7 GET PRINTING TIME
  529. const uint32_t elapsedSeconds = getProgress_seconds_elapsed();
  530. SEND_PGM("A7V ");
  531. if (elapsedSeconds != 0) { // print time
  532. const uint32_t elapsedMinutes = elapsedSeconds / 60;
  533. SEND(ui8tostr2(elapsedMinutes / 60));
  534. SEND_PGM(" H ");
  535. SEND(ui8tostr2(elapsedMinutes % 60));
  536. SENDLINE_PGM(" M");
  537. }
  538. else
  539. SENDLINE_PGM(" 999:999");
  540. }
  541. break;
  542. case 8: // A8 GET SD LIST
  543. #if ENABLED(SDSUPPORT)
  544. SelectedFile[0] = 0;
  545. RenderCurrentFileList();
  546. #endif
  547. break;
  548. case 9: // A9 pause sd print
  549. #if ENABLED(SDSUPPORT)
  550. if (isPrintingFromMedia())
  551. PausePrint();
  552. #endif
  553. break;
  554. case 10: // A10 resume sd print
  555. #if ENABLED(SDSUPPORT)
  556. if (isPrintingFromMediaPaused())
  557. ResumePrint();
  558. #endif
  559. break;
  560. case 11: // A11 STOP SD PRINT
  561. TERN_(SDSUPPORT, StopPrint());
  562. break;
  563. case 12: // A12 kill
  564. kill(PSTR(STR_ERR_KILLED));
  565. break;
  566. case 13: // A13 SELECTION FILE
  567. #if ENABLED(SDSUPPORT)
  568. if (isMediaInserted()) {
  569. starpos = (strchr(TFTstrchr_pointer + 4, '*'));
  570. if (TFTstrchr_pointer[4] == '/') {
  571. strcpy(SelectedDirectory, TFTstrchr_pointer + 5);
  572. SelectedFile[0] = 0;
  573. SENDLINE_DBG_PGM("J21", "TFT Serial Debug: Clear file selection... J21 "); // J21 Not File Selected
  574. SENDLINE_PGM("");
  575. }
  576. else if (TFTstrchr_pointer[4] == '<') {
  577. strcpy(SelectedDirectory, TFTstrchr_pointer + 4);
  578. SpecialMenu = true;
  579. SelectedFile[0] = 0;
  580. SENDLINE_DBG_PGM("J21", "TFT Serial Debug: Clear file selection... J21 "); // J21 Not File Selected
  581. SENDLINE_PGM("");
  582. }
  583. else {
  584. SelectedDirectory[0] = 0;
  585. if (starpos) *(starpos - 1) = '\0';
  586. strcpy(SelectedFile, TFTstrchr_pointer + 4);
  587. SENDLINE_DBG_PGM_VAL("J20", "TFT Serial Debug: File Selected... J20 ", SelectedFile); // J20 File Selected
  588. }
  589. }
  590. #endif
  591. break;
  592. case 14: // A14 START PRINTING
  593. #if ENABLED(SDSUPPORT)
  594. if (!isPrinting() && strlen(SelectedFile) > 0)
  595. StartPrint();
  596. #endif
  597. break;
  598. case 15: // A15 RESUMING FROM OUTAGE
  599. // TODO: JBA implement resume form outage
  600. break;
  601. case 16: { // A16 set hotend temp
  602. unsigned int tempvalue;
  603. if (CodeSeen('S')) {
  604. tempvalue = constrain(CodeValue(), 0, 275);
  605. setTargetTemp_celsius(tempvalue, (extruder_t) E0);
  606. }
  607. else if (CodeSeen('C') && !isPrinting()) {
  608. if (getAxisPosition_mm(Z) < 10)
  609. injectCommands_P(PSTR("G1 Z10")); // RASE Z AXIS
  610. tempvalue = constrain(CodeValue(), 0, 275);
  611. setTargetTemp_celsius(tempvalue, (extruder_t) E0);
  612. }
  613. }
  614. break;
  615. case 17: { // A17 set heated bed temp
  616. unsigned int tempbed;
  617. if (CodeSeen('S')) {
  618. tempbed = constrain(CodeValue(), 0, 100);
  619. setTargetTemp_celsius(tempbed, (heater_t)BED);
  620. }
  621. }
  622. break;
  623. case 18: { // A18 set fan speed
  624. float fanPercent;
  625. if (CodeSeen('S')) {
  626. fanPercent = CodeValue();
  627. fanPercent = constrain(fanPercent, 0, 100);
  628. setTargetFan_percent(fanPercent, FAN0);
  629. }
  630. else
  631. fanPercent = 100;
  632. setTargetFan_percent(fanPercent, FAN0);
  633. SENDLINE_PGM("");
  634. }
  635. break;
  636. case 19: // A19 stop stepper drivers - sent on stop extrude command and on turn motors off command
  637. if (!isPrinting()) {
  638. quickstop_stepper();
  639. disable_all_steppers();
  640. }
  641. SENDLINE_PGM("");
  642. break;
  643. case 20: // A20 read printing speed
  644. if (CodeSeen('S'))
  645. feedrate_percentage = constrain(CodeValue(), 40, 999);
  646. else
  647. SEND_PGM_VAL("A20V ", feedrate_percentage);
  648. break;
  649. case 21: // A21 all home
  650. if (!isPrinting() && !isPrintingFromMediaPaused()) {
  651. if (CodeSeen('X') || CodeSeen('Y') || CodeSeen('Z')) {
  652. if (CodeSeen('X'))
  653. injectCommands_P(PSTR("G28X"));
  654. if (CodeSeen('Y'))
  655. injectCommands_P(PSTR("G28Y"));
  656. if (CodeSeen('Z'))
  657. injectCommands_P(PSTR("G28Z"));
  658. }
  659. else if (CodeSeen('C')) {
  660. injectCommands_P(G28_STR);
  661. }
  662. }
  663. break;
  664. case 22: // A22 move X/Y/Z or extrude
  665. if (!isPrinting()) {
  666. float coorvalue;
  667. unsigned int movespeed = 0;
  668. char commandStr[30];
  669. char fullCommandStr[38];
  670. commandStr[0] = 0; // empty string
  671. if (CodeSeen('F')) // Set feedrate
  672. movespeed = CodeValue();
  673. if (CodeSeen('X')) { // Move in X direction
  674. coorvalue = CodeValue();
  675. if ((coorvalue <= 0.2) && coorvalue > 0)
  676. sprintf_P(commandStr, PSTR("G1 X0.1F%i"), movespeed);
  677. else if ((coorvalue <= -0.1) && coorvalue > -1)
  678. sprintf_P(commandStr, PSTR("G1 X-0.1F%i"), movespeed);
  679. else
  680. sprintf_P(commandStr, PSTR("G1 X%iF%i"), int(coorvalue), movespeed);
  681. }
  682. else if (CodeSeen('Y')) { // Move in Y direction
  683. coorvalue = CodeValue();
  684. if ((coorvalue <= 0.2) && coorvalue > 0)
  685. sprintf_P(commandStr, PSTR("G1 Y0.1F%i"), movespeed);
  686. else if ((coorvalue <= -0.1) && coorvalue > -1)
  687. sprintf_P(commandStr, PSTR("G1 Y-0.1F%i"), movespeed);
  688. else
  689. sprintf_P(commandStr, PSTR("G1 Y%iF%i"), int(coorvalue), movespeed);
  690. }
  691. else if (CodeSeen('Z')) { // Move in Z direction
  692. coorvalue = CodeValue();
  693. if ((coorvalue <= 0.2) && coorvalue > 0)
  694. sprintf_P(commandStr, PSTR("G1 Z0.1F%i"), movespeed);
  695. else if ((coorvalue <= -0.1) && coorvalue > -1)
  696. sprintf_P(commandStr, PSTR("G1 Z-0.1F%i"), movespeed);
  697. else
  698. sprintf_P(commandStr, PSTR("G1 Z%iF%i"), int(coorvalue), movespeed);
  699. }
  700. else if (CodeSeen('E')) { // Extrude
  701. coorvalue = CodeValue();
  702. if ((coorvalue <= 0.2) && coorvalue > 0)
  703. sprintf_P(commandStr, PSTR("G1 E0.1F%i"), movespeed);
  704. else if ((coorvalue <= -0.1) && coorvalue > -1)
  705. sprintf_P(commandStr, PSTR("G1 E-0.1F%i"), movespeed);
  706. else
  707. sprintf_P(commandStr, PSTR("G1 E%iF500"), int(coorvalue));
  708. }
  709. if (strlen(commandStr) > 0) {
  710. sprintf_P(fullCommandStr, PSTR("G91\n%s\nG90"), commandStr);
  711. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  712. SERIAL_ECHOPGM("TFT Serial Debug: A22 Move final request with gcode... ");
  713. SERIAL_ECHOLN(fullCommandStr);
  714. #endif
  715. injectCommands(fullCommandStr);
  716. }
  717. }
  718. SENDLINE_PGM("");
  719. break;
  720. case 23: // A23 preheat pla
  721. if (!isPrinting()) {
  722. if (getAxisPosition_mm(Z) < 10)
  723. injectCommands_P(PSTR("G1 Z10")); // RASE Z AXIS
  724. setTargetTemp_celsius(PREHEAT_1_TEMP_BED, (heater_t) BED);
  725. setTargetTemp_celsius(PREHEAT_1_TEMP_HOTEND, (extruder_t) E0);
  726. SENDLINE_PGM("OK");
  727. }
  728. break;
  729. case 24:// A24 preheat abs
  730. if (!isPrinting()) {
  731. if (getAxisPosition_mm(Z) < 10)
  732. injectCommands_P(PSTR("G1 Z10")); // RASE Z AXIS
  733. setTargetTemp_celsius(PREHEAT_2_TEMP_BED, (heater_t) BED);
  734. setTargetTemp_celsius(PREHEAT_2_TEMP_HOTEND, (extruder_t) E0);
  735. SENDLINE_PGM("OK");
  736. }
  737. break;
  738. case 25: // A25 cool down
  739. if (!isPrinting()) {
  740. setTargetTemp_celsius(0, (heater_t) BED);
  741. setTargetTemp_celsius(0, (extruder_t) E0);
  742. SENDLINE_DBG_PGM("J12", "TFT Serial Debug: Cooling down... J12"); // J12 cool down
  743. }
  744. break;
  745. case 26: // A26 refresh SD
  746. #if ENABLED(SDSUPPORT)
  747. if (isMediaInserted()) {
  748. if (strlen(SelectedDirectory) > 0) {
  749. FileList currentFileList;
  750. if ((SelectedDirectory[0] == '.') && (SelectedDirectory[1] == '.')) {
  751. currentFileList.upDir();
  752. }
  753. else {
  754. if (SelectedDirectory[0] == '<')
  755. HandleSpecialMenu();
  756. else
  757. currentFileList.changeDir(SelectedDirectory);
  758. }
  759. }
  760. }
  761. else {
  762. SENDLINE_DBG_PGM("J02", "TFT Serial Debug: No SD Card mounted to refresh SD A26... J02");
  763. }
  764. SelectedDirectory[0] = 0;
  765. #endif
  766. break;
  767. #if ENABLED(SERVO_ENDSTOPS)
  768. case 27: break; // A27 servos angles adjust
  769. #endif
  770. case 28: // A28 filament test
  771. if (CodeSeen('O'))
  772. NOOP;
  773. else if (CodeSeen('C'))
  774. NOOP;
  775. SENDLINE_PGM("");
  776. break;
  777. case 33: // A33 get version info
  778. SEND_PGM("J33 ");
  779. SENDLINE_PGM(DETAILED_BUILD_VERSION);
  780. break;
  781. default:
  782. break;
  783. }
  784. }
  785. TFTbufindw = (TFTbufindw + 1) % TFTBUFSIZE;
  786. TFTbuflen += 1;
  787. serial3_count = 0; // clear buffer
  788. }
  789. else {
  790. TFTcmdbuffer[TFTbufindw][serial3_count++] = serial3_char;
  791. }
  792. }
  793. }
  794. void AnycubicTFTClass::DoSDCardStateCheck() {
  795. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  796. bool isInserted = isMediaInserted();
  797. if (isInserted)
  798. SENDLINE_DBG_PGM("J00", "TFT Serial Debug: SD card state changed... isInserted");
  799. else
  800. SENDLINE_DBG_PGM("J01", "TFT Serial Debug: SD card state changed... !isInserted");
  801. #endif
  802. }
  803. void AnycubicTFTClass::DoFilamentRunoutCheck() {
  804. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  805. // NOTE: getFilamentRunoutState() only returns the runout state if the job is printing
  806. // we want to actually check the status of the pin here, regardless of printstate
  807. if (READ(FIL_RUNOUT1_PIN)) {
  808. if (mediaPrintingState == AMPRINTSTATE_PRINTING || mediaPrintingState == AMPRINTSTATE_PAUSED || mediaPrintingState == AMPRINTSTATE_PAUSE_REQUESTED) {
  809. // play tone to indicate filament is out
  810. injectCommands_P(PSTR("\nM300 P200 S1567\nM300 P200 S1174\nM300 P200 S1567\nM300 P200 S1174\nM300 P2000 S1567"));
  811. // tell the user that the filament has run out and wait
  812. SENDLINE_DBG_PGM("J23", "TFT Serial Debug: Blocking filament prompt... J23");
  813. }
  814. else {
  815. SENDLINE_DBG_PGM("J15", "TFT Serial Debug: Non blocking filament runout... J15");
  816. }
  817. }
  818. #endif // FILAMENT_RUNOUT_SENSOR
  819. }
  820. void AnycubicTFTClass::StartPrint() {
  821. #if ENABLED(SDSUPPORT)
  822. if (!isPrinting() && strlen(SelectedFile) > 0) {
  823. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  824. SERIAL_ECHOPGM("TFT Serial Debug: About to print file ... ");
  825. SERIAL_ECHO(isPrinting());
  826. SERIAL_ECHOPGM(" ");
  827. SERIAL_ECHOLN(SelectedFile);
  828. #endif
  829. mediaPrintingState = AMPRINTSTATE_PRINTING;
  830. mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
  831. printFile(SelectedFile);
  832. }
  833. #endif // SDUPPORT
  834. }
  835. void AnycubicTFTClass::PausePrint() {
  836. #if ENABLED(SDSUPPORT)
  837. if (isPrintingFromMedia() && mediaPrintingState != AMPRINTSTATE_STOP_REQUESTED && mediaPauseState == AMPAUSESTATE_NOT_PAUSED) {
  838. mediaPrintingState = AMPRINTSTATE_PAUSE_REQUESTED;
  839. mediaPauseState = AMPAUSESTATE_NOT_PAUSED; // need the userconfirm method to update pause state
  840. SENDLINE_DBG_PGM("J05", "TFT Serial Debug: SD print pause started... J05"); // J05 printing pause
  841. // for some reason pausing the print doesn't retract the extruder so force a manual one here
  842. injectCommands_P(PSTR("G91\nG1 E-2 F1800\nG90"));
  843. pausePrint();
  844. }
  845. #endif
  846. }
  847. void AnycubicTFTClass::ResumePrint() {
  848. #if ENABLED(SDSUPPORT)
  849. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  850. if (READ(FIL_RUNOUT1_PIN)) {
  851. #if ENABLED(ANYCUBIC_LCD_DEBUG)
  852. SERIAL_ECHOLNPGM("TFT Serial Debug: Resume Print with filament sensor still tripped... ");
  853. #endif
  854. // trigger the user message box
  855. DoFilamentRunoutCheck();
  856. // re-enable the continue button
  857. SENDLINE_DBG_PGM("J18", "TFT Serial Debug: Resume Print with filament sensor still tripped... J18");
  858. return;
  859. }
  860. #endif
  861. if (mediaPauseState == AMPAUSESTATE_HEATER_TIMEOUT) {
  862. mediaPauseState = AMPAUSESTATE_REHEATING;
  863. // TODO: JBA I don't think J05 just disables the continue button, i think it injects a rogue M25. So taking this out
  864. // // disable the continue button
  865. // SENDLINE_DBG_PGM("J05", "TFT Serial Debug: Resume called with heater timeout... J05"); // J05 printing pause
  866. // reheat the nozzle
  867. setUserConfirmed();
  868. }
  869. else {
  870. mediaPrintingState = AMPRINTSTATE_PRINTING;
  871. mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
  872. SENDLINE_DBG_PGM("J04", "TFT Serial Debug: SD print resumed... J04"); // J04 printing form sd card now
  873. resumePrint();
  874. }
  875. #endif
  876. }
  877. void AnycubicTFTClass::StopPrint() {
  878. #if ENABLED(SDSUPPORT)
  879. mediaPrintingState = AMPRINTSTATE_STOP_REQUESTED;
  880. mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
  881. SENDLINE_DBG_PGM("J16", "TFT Serial Debug: SD print stop called... J16");
  882. // for some reason stopping the print doesn't retract the extruder so force a manual one here
  883. injectCommands_P(PSTR("G91\nG1 E-2 F1800\nG90"));
  884. stopPrint();
  885. #endif
  886. }
  887. #endif // ANYCUBIC_LCD_I3MEGA