My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DGUSScreenHandler.cpp 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../../../inc/MarlinConfigPre.h"
  23. #if HAS_DGUS_LCD
  24. #include "DGUSScreenHandler.h"
  25. #include "DGUSDisplay.h"
  26. #include "DGUSVPVariable.h"
  27. #include "DGUSDisplayDef.h"
  28. #include "../../ui_api.h"
  29. #include "../../../../MarlinCore.h"
  30. #include "../../../../module/temperature.h"
  31. #include "../../../../module/motion.h"
  32. #include "../../../../gcode/queue.h"
  33. #include "../../../../module/planner.h"
  34. #include "../../../../sd/cardreader.h"
  35. #include "../../../../libs/duration_t.h"
  36. #include "../../../../module/printcounter.h"
  37. #if ENABLED(POWER_LOSS_RECOVERY)
  38. #include "../../../../feature/powerloss.h"
  39. #endif
  40. uint16_t DGUSScreenHandler::ConfirmVP;
  41. #if ENABLED(SDSUPPORT)
  42. int16_t DGUSScreenHandler::top_file = 0;
  43. int16_t DGUSScreenHandler::file_to_print = 0;
  44. static ExtUI::FileList filelist;
  45. #endif
  46. void (*DGUSScreenHandler::confirm_action_cb)() = nullptr;
  47. //DGUSScreenHandler ScreenHandler;
  48. DGUSLCD_Screens DGUSScreenHandler::current_screen;
  49. DGUSLCD_Screens DGUSScreenHandler::past_screens[NUM_PAST_SCREENS];
  50. uint8_t DGUSScreenHandler::update_ptr;
  51. uint16_t DGUSScreenHandler::skipVP;
  52. bool DGUSScreenHandler::ScreenComplete;
  53. //DGUSDisplay dgusdisplay;
  54. // endianness swap
  55. uint16_t swap16(const uint16_t value) { return (value & 0xffU) << 8U | (value >> 8U); }
  56. void DGUSScreenHandler::sendinfoscreen(const char* line1, const char* line2, const char* line3, const char* line4, bool l1inflash, bool l2inflash, bool l3inflash, bool l4inflash) {
  57. DGUS_VP_Variable ramcopy;
  58. if (populate_VPVar(VP_MSGSTR1, &ramcopy)) {
  59. ramcopy.memadr = (void*) line1;
  60. l1inflash ? DGUSScreenHandler::DGUSLCD_SendStringToDisplayPGM(ramcopy) : DGUSScreenHandler::DGUSLCD_SendStringToDisplay(ramcopy);
  61. }
  62. if (populate_VPVar(VP_MSGSTR2, &ramcopy)) {
  63. ramcopy.memadr = (void*) line2;
  64. l2inflash ? DGUSScreenHandler::DGUSLCD_SendStringToDisplayPGM(ramcopy) : DGUSScreenHandler::DGUSLCD_SendStringToDisplay(ramcopy);
  65. }
  66. if (populate_VPVar(VP_MSGSTR3, &ramcopy)) {
  67. ramcopy.memadr = (void*) line3;
  68. l3inflash ? DGUSScreenHandler::DGUSLCD_SendStringToDisplayPGM(ramcopy) : DGUSScreenHandler::DGUSLCD_SendStringToDisplay(ramcopy);
  69. }
  70. if (populate_VPVar(VP_MSGSTR4, &ramcopy)) {
  71. ramcopy.memadr = (void*) line4;
  72. l4inflash ? DGUSScreenHandler::DGUSLCD_SendStringToDisplayPGM(ramcopy) : DGUSScreenHandler::DGUSLCD_SendStringToDisplay(ramcopy);
  73. }
  74. }
  75. void DGUSScreenHandler::HandleUserConfirmationPopUp(uint16_t VP, const char* line1, const char* line2, const char* line3, const char* line4, bool l1, bool l2, bool l3, bool l4) {
  76. if (current_screen == DGUSLCD_SCREEN_CONFIRM) {
  77. // Already showing a pop up, so we need to cancel that first.
  78. PopToOldScreen();
  79. }
  80. ConfirmVP = VP;
  81. sendinfoscreen(line1, line2, line3, line4, l1, l2, l3, l4);
  82. ScreenHandler.GotoScreen(DGUSLCD_SCREEN_CONFIRM);
  83. }
  84. void DGUSScreenHandler::setstatusmessage(const char *msg) {
  85. DGUS_VP_Variable ramcopy;
  86. if (populate_VPVar(VP_M117, &ramcopy)) {
  87. ramcopy.memadr = (void*) msg;
  88. DGUSLCD_SendStringToDisplay(ramcopy);
  89. }
  90. }
  91. void DGUSScreenHandler::setstatusmessagePGM(PGM_P const msg) {
  92. DGUS_VP_Variable ramcopy;
  93. if (populate_VPVar(VP_M117, &ramcopy)) {
  94. ramcopy.memadr = (void*) msg;
  95. DGUSLCD_SendStringToDisplayPGM(ramcopy);
  96. }
  97. }
  98. // Send an 8 bit or 16 bit value to the display.
  99. void DGUSScreenHandler::DGUSLCD_SendWordValueToDisplay(DGUS_VP_Variable &var) {
  100. if (var.memadr) {
  101. //DEBUG_ECHOPAIR(" DGUS_LCD_SendWordValueToDisplay ", var.VP);
  102. //DEBUG_ECHOLNPAIR(" data ", *(uint16_t *)var.memadr);
  103. uint8_t *tmp = (uint8_t *) var.memadr;
  104. uint16_t data_to_send = (tmp[0] << 8);
  105. if (var.size >= 1) data_to_send |= tmp[1];
  106. dgusdisplay.WriteVariable(var.VP, data_to_send);
  107. }
  108. }
  109. // Send an uint8_t between 0 and 255 to the display, but scale to a percentage (0..100)
  110. void DGUSScreenHandler::DGUSLCD_SendPercentageToDisplay(DGUS_VP_Variable &var) {
  111. if (var.memadr) {
  112. //DEBUG_ECHOPAIR(" DGUS_LCD_SendWordValueToDisplay ", var.VP);
  113. //DEBUG_ECHOLNPAIR(" data ", *(uint16_t *)var.memadr);
  114. uint16_t tmp = *(uint8_t *) var.memadr +1 ; // +1 -> avoid rounding issues for the display.
  115. tmp = map(tmp, 0, 255, 0, 100);
  116. uint16_t data_to_send = swap16(tmp);
  117. dgusdisplay.WriteVariable(var.VP, data_to_send);
  118. }
  119. }
  120. // Send the current print progress to the display.
  121. void DGUSScreenHandler::DGUSLCD_SendPrintProgressToDisplay(DGUS_VP_Variable &var) {
  122. //DEBUG_ECHOPAIR(" DGUSLCD_SendPrintProgressToDisplay ", var.VP);
  123. uint16_t tmp = ExtUI::getProgress_percent();
  124. //DEBUG_ECHOLNPAIR(" data ", tmp);
  125. uint16_t data_to_send = swap16(tmp);
  126. dgusdisplay.WriteVariable(var.VP, data_to_send);
  127. }
  128. // Send the current print time to the display.
  129. // It is using a hex display for that: It expects BSD coded data in the format xxyyzz
  130. void DGUSScreenHandler::DGUSLCD_SendPrintTimeToDisplay(DGUS_VP_Variable &var) {
  131. duration_t elapsed = print_job_timer.duration();
  132. char buf[32];
  133. elapsed.toString(buf);
  134. dgusdisplay.WriteVariable(VP_PrintTime, buf, var.size, true);
  135. }
  136. // Send an uint8_t between 0 and 100 to a variable scale to 0..255
  137. void DGUSScreenHandler::DGUSLCD_PercentageToUint8(DGUS_VP_Variable &var, void *val_ptr) {
  138. if (var.memadr) {
  139. uint16_t value = swap16(*(uint16_t*)val_ptr);
  140. *(uint8_t*)var.memadr = map(constrain(value, 0, 100), 0, 100, 0, 255);
  141. }
  142. }
  143. // Sends a (RAM located) string to the DGUS Display
  144. // (Note: The DGUS Display does not clear after the \0, you have to
  145. // overwrite the remainings with spaces.// var.size has the display buffer size!
  146. void DGUSScreenHandler::DGUSLCD_SendStringToDisplay(DGUS_VP_Variable &var) {
  147. char *tmp = (char*) var.memadr;
  148. dgusdisplay.WriteVariable(var.VP, tmp, var.size, true);
  149. }
  150. // Sends a (flash located) string to the DGUS Display
  151. // (Note: The DGUS Display does not clear after the \0, you have to
  152. // overwrite the remainings with spaces.// var.size has the display buffer size!
  153. void DGUSScreenHandler::DGUSLCD_SendStringToDisplayPGM(DGUS_VP_Variable &var) {
  154. char *tmp = (char*) var.memadr;
  155. dgusdisplay.WriteVariablePGM(var.VP, tmp, var.size, true);
  156. }
  157. #if HAS_PID_HEATING
  158. void DGUSScreenHandler::DGUSLCD_SendTemperaturePID(DGUS_VP_Variable &var) {
  159. float value = *(float *)var.memadr;
  160. float valuesend = 0;
  161. switch (var.VP) {
  162. default: return;
  163. #if HOTENDS >= 1
  164. case VP_E0_PID_P: valuesend = value; break;
  165. case VP_E0_PID_I: valuesend = unscalePID_i(value); break;
  166. case VP_E0_PID_D: valuesend = unscalePID_d(value); break;
  167. #endif
  168. #if HOTENDS >= 2
  169. case VP_E1_PID_P: valuesend = value; break;
  170. case VP_E1_PID_I: valuesend = unscalePID_i(value); break;
  171. case VP_E1_PID_D: valuesend = unscalePID_d(value); break;
  172. #endif
  173. #if HAS_HEATED_BED
  174. case VP_BED_PID_P: valuesend = value; break;
  175. case VP_BED_PID_I: valuesend = unscalePID_i(value); break;
  176. case VP_BED_PID_D: valuesend = unscalePID_d(value); break;
  177. #endif
  178. }
  179. valuesend *= cpow(10, 1);
  180. union { int16_t i; char lb[2]; } endian;
  181. char tmp[2];
  182. endian.i = valuesend;
  183. tmp[0] = endian.lb[1];
  184. tmp[1] = endian.lb[0];
  185. dgusdisplay.WriteVariable(var.VP, tmp, 2);
  186. }
  187. #endif
  188. #if ENABLED(PRINTCOUNTER)
  189. // Send the accumulate print time to the display.
  190. // It is using a hex display for that: It expects BSD coded data in the format xxyyzz
  191. void DGUSScreenHandler::DGUSLCD_SendPrintAccTimeToDisplay(DGUS_VP_Variable &var) {
  192. printStatistics state = print_job_timer.getStats();
  193. char buf[21];
  194. duration_t elapsed = state.printTime;
  195. elapsed.toString(buf);
  196. dgusdisplay.WriteVariable(VP_PrintAccTime, buf, var.size, true);
  197. }
  198. void DGUSScreenHandler::DGUSLCD_SendPrintsTotalToDisplay(DGUS_VP_Variable &var) {
  199. printStatistics state = print_job_timer.getStats();
  200. char buf[21];
  201. sprintf_P(buf, PSTR("%u"), state.totalPrints);
  202. dgusdisplay.WriteVariable(VP_PrintsTotal, buf, var.size, true);
  203. }
  204. #endif
  205. // Send fan status value to the display.
  206. #if HAS_FAN
  207. void DGUSScreenHandler::DGUSLCD_SendFanStatusToDisplay(DGUS_VP_Variable &var) {
  208. if (var.memadr) {
  209. DEBUG_ECHOPAIR(" DGUSLCD_SendFanStatusToDisplay ", var.VP);
  210. DEBUG_ECHOLNPAIR(" data ", *(uint8_t *)var.memadr);
  211. uint16_t data_to_send = 0;
  212. if (*(uint8_t *) var.memadr) data_to_send = 1;
  213. data_to_send = swap16(data_to_send);
  214. dgusdisplay.WriteVariable(var.VP, data_to_send);
  215. }
  216. }
  217. #endif
  218. // Send heater status value to the display.
  219. void DGUSScreenHandler::DGUSLCD_SendHeaterStatusToDisplay(DGUS_VP_Variable &var) {
  220. if (var.memadr) {
  221. DEBUG_ECHOPAIR(" DGUSLCD_SendHeaterStatusToDisplay ", var.VP);
  222. DEBUG_ECHOLNPAIR(" data ", *(int16_t *)var.memadr);
  223. uint16_t data_to_send = 0;
  224. if (*(int16_t *) var.memadr) data_to_send = 1;
  225. data_to_send = swap16(data_to_send);
  226. dgusdisplay.WriteVariable(var.VP, data_to_send);
  227. }
  228. }
  229. #if ENABLED(DGUS_UI_WAITING)
  230. void DGUSScreenHandler::DGUSLCD_SendWaitingStatusToDisplay(DGUS_VP_Variable &var) {
  231. // In FYSETC UI design there are 10 statuses to loop
  232. static uint16_t period = 0;
  233. static uint16_t index = 0;
  234. //DEBUG_ECHOPAIR(" DGUSLCD_SendWaitingStatusToDisplay ", var.VP);
  235. //DEBUG_ECHOLNPAIR(" data ", swap16(index));
  236. if (period++ > DGUS_UI_WAITING_STATUS_PERIOD) {
  237. dgusdisplay.WriteVariable(var.VP, swap16(index));
  238. //DEBUG_ECHOLNPAIR(" data ", swap16(index));
  239. if (++index >= DGUS_UI_WAITING_STATUS) index = 0;
  240. period = 0;
  241. }
  242. }
  243. #endif
  244. #if ENABLED(SDSUPPORT)
  245. void DGUSScreenHandler::ScreenChangeHookIfSD(DGUS_VP_Variable &var, void *val_ptr) {
  246. // default action executed when there is a SD card, but not printing
  247. if (ExtUI::isMediaInserted() && !ExtUI::isPrintingFromMedia()) {
  248. ScreenChangeHook(var, val_ptr);
  249. dgusdisplay.RequestScreen(current_screen);
  250. return;
  251. }
  252. // if we are printing, we jump to two screens after the requested one.
  253. // This should host e.g a print pause / print abort / print resume dialog.
  254. // This concept allows to recycle this hook for other file
  255. if (ExtUI::isPrintingFromMedia() && !card.flag.abort_sd_printing) {
  256. GotoScreen(DGUSLCD_SCREEN_SDPRINTMANIPULATION);
  257. return;
  258. }
  259. // Don't let the user in the dark why there is no reaction.
  260. if (!ExtUI::isMediaInserted()) {
  261. setstatusmessagePGM(GET_TEXT(MSG_NO_MEDIA));
  262. return;
  263. }
  264. if (card.flag.abort_sd_printing) {
  265. setstatusmessagePGM(GET_TEXT(MSG_MEDIA_ABORTING));
  266. return;
  267. }
  268. }
  269. void DGUSScreenHandler::DGUSLCD_SD_ScrollFilelist(DGUS_VP_Variable& var, void *val_ptr) {
  270. auto old_top = top_file;
  271. const int16_t scroll = (int16_t)swap16(*(uint16_t*)val_ptr);
  272. if (scroll) {
  273. top_file += scroll;
  274. DEBUG_ECHOPAIR("new topfile calculated:", top_file);
  275. if (top_file < 0) {
  276. top_file = 0;
  277. DEBUG_ECHOLNPGM("Top of filelist reached");
  278. }
  279. else {
  280. int16_t max_top = filelist.count() - DGUS_SD_FILESPERSCREEN;
  281. NOLESS(max_top, 0);
  282. NOMORE(top_file, max_top);
  283. }
  284. DEBUG_ECHOPAIR("new topfile adjusted:", top_file);
  285. }
  286. else if (!filelist.isAtRootDir()) {
  287. filelist.upDir();
  288. top_file = 0;
  289. ForceCompleteUpdate();
  290. }
  291. if (old_top != top_file) ForceCompleteUpdate();
  292. }
  293. void DGUSScreenHandler::DGUSLCD_SD_FileSelected(DGUS_VP_Variable &var, void *val_ptr) {
  294. uint16_t touched_nr = (int16_t)swap16(*(uint16_t*)val_ptr) + top_file;
  295. if (touched_nr > filelist.count()) return;
  296. if (!filelist.seek(touched_nr)) return;
  297. if (filelist.isDir()) {
  298. filelist.changeDir(filelist.filename());
  299. top_file = 0;
  300. ForceCompleteUpdate();
  301. return;
  302. }
  303. #if ENABLED(DGUS_PRINT_FILENAME)
  304. // Send print filename
  305. dgusdisplay.WriteVariable(VP_SD_Print_Filename, filelist.filename(), VP_SD_FileName_LEN, true);
  306. #endif
  307. // Setup Confirmation screen
  308. file_to_print = touched_nr;
  309. HandleUserConfirmationPopUp(VP_SD_FileSelectConfirm, nullptr, PSTR("Print file"), filelist.filename(), PSTR("from SD Card?"), true, true, false, true);
  310. }
  311. void DGUSScreenHandler::DGUSLCD_SD_StartPrint(DGUS_VP_Variable &var, void *val_ptr) {
  312. if (!filelist.seek(file_to_print)) return;
  313. ExtUI::printFile(filelist.shortFilename());
  314. ScreenHandler.GotoScreen(
  315. #if ENABLED(DGUS_LCD_UI_ORIGIN)
  316. DGUSLCD_SCREEN_STATUS
  317. #else
  318. DGUSLCD_SCREEN_SDPRINTMANIPULATION
  319. #endif
  320. );
  321. }
  322. void DGUSScreenHandler::DGUSLCD_SD_ResumePauseAbort(DGUS_VP_Variable &var, void *val_ptr) {
  323. if (!ExtUI::isPrintingFromMedia()) return; // avoid race condition when user stays in this menu and printer finishes.
  324. switch (swap16(*(uint16_t*)val_ptr)) {
  325. case 0: // Resume
  326. if (ExtUI::isPrintingFromMediaPaused()) ExtUI::resumePrint();
  327. break;
  328. case 1: // Pause
  329. if (!ExtUI::isPrintingFromMediaPaused()) ExtUI::pausePrint();
  330. break;
  331. case 2: // Abort
  332. ScreenHandler.HandleUserConfirmationPopUp(VP_SD_AbortPrintConfirmed, nullptr, PSTR("Abort printing"), filelist.filename(), PSTR("?"), true, true, false, true);
  333. break;
  334. }
  335. }
  336. void DGUSScreenHandler::DGUSLCD_SD_ReallyAbort(DGUS_VP_Variable &var, void *val_ptr) {
  337. ExtUI::stopPrint();
  338. GotoScreen(DGUSLCD_SCREEN_MAIN);
  339. }
  340. void DGUSScreenHandler::DGUSLCD_SD_PrintTune(DGUS_VP_Variable &var, void *val_ptr) {
  341. if (!ExtUI::isPrintingFromMedia()) return; // avoid race condition when user stays in this menu and printer finishes.
  342. GotoScreen(DGUSLCD_SCREEN_SDPRINTTUNE);
  343. }
  344. void DGUSScreenHandler::DGUSLCD_SD_SendFilename(DGUS_VP_Variable& var) {
  345. uint16_t target_line = (var.VP - VP_SD_FileName0) / VP_SD_FileName_LEN;
  346. if (target_line > DGUS_SD_FILESPERSCREEN) return;
  347. char tmpfilename[VP_SD_FileName_LEN + 1] = "";
  348. var.memadr = (void*)tmpfilename;
  349. if (filelist.seek(top_file + target_line))
  350. snprintf_P(tmpfilename, VP_SD_FileName_LEN, PSTR("%s%c"), filelist.filename(), filelist.isDir() ? '/' : 0);
  351. DGUSLCD_SendStringToDisplay(var);
  352. }
  353. void DGUSScreenHandler::SDCardInserted() {
  354. top_file = 0;
  355. filelist.refresh();
  356. auto cs = ScreenHandler.getCurrentScreen();
  357. if (cs == DGUSLCD_SCREEN_MAIN || cs == DGUSLCD_SCREEN_STATUS)
  358. ScreenHandler.GotoScreen(DGUSLCD_SCREEN_SDFILELIST);
  359. }
  360. void DGUSScreenHandler::SDCardRemoved() {
  361. if (current_screen == DGUSLCD_SCREEN_SDFILELIST
  362. || (current_screen == DGUSLCD_SCREEN_CONFIRM && (ConfirmVP == VP_SD_AbortPrintConfirmed || ConfirmVP == VP_SD_FileSelectConfirm))
  363. || current_screen == DGUSLCD_SCREEN_SDPRINTMANIPULATION
  364. ) ScreenHandler.GotoScreen(DGUSLCD_SCREEN_MAIN);
  365. }
  366. void DGUSScreenHandler::SDCardError() {
  367. DGUSScreenHandler::SDCardRemoved();
  368. ScreenHandler.sendinfoscreen(PSTR("NOTICE"), nullptr, PSTR("SD card error"), nullptr, true, true, true, true);
  369. ScreenHandler.SetupConfirmAction(nullptr);
  370. ScreenHandler.GotoScreen(DGUSLCD_SCREEN_POPUP);
  371. }
  372. #endif // SDSUPPORT
  373. void DGUSScreenHandler::ScreenConfirmedOK(DGUS_VP_Variable &var, void *val_ptr) {
  374. DGUS_VP_Variable ramcopy;
  375. if (!populate_VPVar(ConfirmVP, &ramcopy)) return;
  376. if (ramcopy.set_by_display_handler) ramcopy.set_by_display_handler(ramcopy, val_ptr);
  377. }
  378. const uint16_t* DGUSLCD_FindScreenVPMapList(uint8_t screen) {
  379. const uint16_t *ret;
  380. const struct VPMapping *map = VPMap;
  381. while (ret = (uint16_t*) pgm_read_ptr(&(map->VPList))) {
  382. if (pgm_read_byte(&(map->screen)) == screen) return ret;
  383. map++;
  384. }
  385. return nullptr;
  386. }
  387. const DGUS_VP_Variable* DGUSLCD_FindVPVar(const uint16_t vp) {
  388. const DGUS_VP_Variable *ret = ListOfVP;
  389. do {
  390. const uint16_t vpcheck = pgm_read_word(&(ret->VP));
  391. if (vpcheck == 0) break;
  392. if (vpcheck == vp) return ret;
  393. ++ret;
  394. } while (1);
  395. DEBUG_ECHOLNPAIR("FindVPVar NOT FOUND ", vp);
  396. return nullptr;
  397. }
  398. void DGUSScreenHandler::ScreenChangeHookIfIdle(DGUS_VP_Variable &var, void *val_ptr) {
  399. if (!ExtUI::isPrinting()) {
  400. ScreenChangeHook(var, val_ptr);
  401. dgusdisplay.RequestScreen(current_screen);
  402. }
  403. }
  404. void DGUSScreenHandler::ScreenChangeHook(DGUS_VP_Variable &var, void *val_ptr) {
  405. uint8_t *tmp = (uint8_t*)val_ptr;
  406. // The keycode in target is coded as <from-frame><to-frame>, so 0x0100A means
  407. // from screen 1 (main) to 10 (temperature). DGUSLCD_SCREEN_POPUP is special,
  408. // meaning "return to previous screen"
  409. DGUSLCD_Screens target = (DGUSLCD_Screens)tmp[1];
  410. if (target == DGUSLCD_SCREEN_POPUP) {
  411. // special handling for popup is to return to previous menu
  412. if (current_screen == DGUSLCD_SCREEN_POPUP && confirm_action_cb) confirm_action_cb();
  413. PopToOldScreen();
  414. return;
  415. }
  416. UpdateNewScreen(target);
  417. #ifdef DEBUG_DGUSLCD
  418. if (!DGUSLCD_FindScreenVPMapList(target)) DEBUG_ECHOLNPAIR("WARNING: No screen Mapping found for ", target);
  419. #endif
  420. }
  421. void DGUSScreenHandler::HandleAllHeatersOff(DGUS_VP_Variable &var, void *val_ptr) {
  422. thermalManager.disable_all_heaters();
  423. ScreenHandler.ForceCompleteUpdate(); // hint to send all data.
  424. }
  425. void DGUSScreenHandler::HandleTemperatureChanged(DGUS_VP_Variable &var, void *val_ptr) {
  426. uint16_t newvalue = swap16(*(uint16_t*)val_ptr);
  427. uint16_t acceptedvalue;
  428. switch (var.VP) {
  429. default: return;
  430. #if HOTENDS >= 1
  431. case VP_T_E0_Set:
  432. thermalManager.setTargetHotend(newvalue, 0);
  433. acceptedvalue = thermalManager.temp_hotend[0].target;
  434. break;
  435. #endif
  436. #if HOTENDS >= 2
  437. case VP_T_E1_Set:
  438. thermalManager.setTargetHotend(newvalue, 1);
  439. acceptedvalue = thermalManager.temp_hotend[1].target;
  440. break;
  441. #endif
  442. #if HAS_HEATED_BED
  443. case VP_T_Bed_Set:
  444. thermalManager.setTargetBed(newvalue);
  445. acceptedvalue = thermalManager.temp_bed.target;
  446. break;
  447. #endif
  448. }
  449. // reply to display the new value to update the view if the new value was rejected by the Thermal Manager.
  450. if (newvalue != acceptedvalue && var.send_to_display_handler) var.send_to_display_handler(var);
  451. ScreenHandler.skipVP = var.VP; // don't overwrite value the next update time as the display might autoincrement in parallel
  452. }
  453. void DGUSScreenHandler::HandleFlowRateChanged(DGUS_VP_Variable &var, void *val_ptr) {
  454. #if EXTRUDERS
  455. uint16_t newvalue = swap16(*(uint16_t*)val_ptr);
  456. uint8_t target_extruder;
  457. switch (var.VP) {
  458. default: return;
  459. #if HOTENDS >= 1
  460. case VP_Flowrate_E0: target_extruder = 0; break;
  461. #endif
  462. #if HOTENDS >= 2
  463. case VP_Flowrate_E1: target_extruder = 1; break;
  464. #endif
  465. }
  466. planner.set_flow(target_extruder, newvalue);
  467. ScreenHandler.skipVP = var.VP; // don't overwrite value the next update time as the display might autoincrement in parallel
  468. #else
  469. UNUSED(var); UNUSED(val_ptr);
  470. #endif
  471. }
  472. void DGUSScreenHandler::HandleManualExtrude(DGUS_VP_Variable &var, void *val_ptr) {
  473. DEBUG_ECHOLNPGM("HandleManualExtrude");
  474. int16_t movevalue = swap16(*(uint16_t*)val_ptr);
  475. float target = movevalue * 0.01f;
  476. ExtUI::extruder_t target_extruder;
  477. switch (var.VP) {
  478. #if HOTENDS >= 1
  479. case VP_MOVE_E0: target_extruder = ExtUI::extruder_t::E0; break;
  480. #endif
  481. #if HOTENDS >= 2
  482. case VP_MOVE_E1: target_extruder = ExtUI::extruder_t::E1; break;
  483. #endif
  484. default: return;
  485. }
  486. target += ExtUI::getAxisPosition_mm(target_extruder);
  487. ExtUI::setAxisPosition_mm(target, target_extruder);
  488. skipVP = var.VP;
  489. }
  490. #if ENABLED(DGUS_UI_MOVE_DIS_OPTION)
  491. void DGUSScreenHandler::HandleManualMoveOption(DGUS_VP_Variable &var, void *val_ptr) {
  492. DEBUG_ECHOLNPGM("HandleManualMoveOption");
  493. *(uint16_t*)var.memadr = swap16(*(uint16_t*)val_ptr);
  494. }
  495. #endif
  496. void DGUSScreenHandler::HandleManualMove(DGUS_VP_Variable &var, void *val_ptr) {
  497. DEBUG_ECHOLNPGM("HandleManualMove");
  498. int16_t movevalue = swap16(*(uint16_t*)val_ptr);
  499. #if ENABLED(DGUS_UI_MOVE_DIS_OPTION)
  500. if (movevalue) {
  501. const uint16_t choice = *(uint16_t*)var.memadr;
  502. movevalue = movevalue < 0 ? -choice : choice;
  503. }
  504. #endif
  505. char axiscode;
  506. unsigned int speed = 1500; //FIXME: get default feedrate for manual moves, dont hardcode.
  507. switch (var.VP) {
  508. default: return;
  509. case VP_MOVE_X:
  510. axiscode = 'X';
  511. if (!ExtUI::canMove(ExtUI::axis_t::X)) goto cannotmove;
  512. break;
  513. case VP_MOVE_Y:
  514. axiscode = 'Y';
  515. if (!ExtUI::canMove(ExtUI::axis_t::Y)) goto cannotmove;
  516. break;
  517. case VP_MOVE_Z:
  518. axiscode = 'Z';
  519. speed = 300; // default to 5mm/s
  520. if (!ExtUI::canMove(ExtUI::axis_t::Z)) goto cannotmove;
  521. break;
  522. case VP_HOME_ALL: // only used for homing
  523. axiscode = '\0';
  524. movevalue = 0; // ignore value sent from display, this VP is _ONLY_ for homing.
  525. break;
  526. }
  527. if (!movevalue) {
  528. // homing
  529. DEBUG_ECHOPAIR(" homing ", axiscode);
  530. char buf[6] = "G28 X";
  531. buf[4] = axiscode;
  532. //DEBUG_ECHOPAIR(" ", buf);
  533. queue.enqueue_one_now(buf);
  534. //DEBUG_ECHOLNPGM(" ✓");
  535. ScreenHandler.ForceCompleteUpdate();
  536. return;
  537. }
  538. else {
  539. //movement
  540. DEBUG_ECHOPAIR(" move ", axiscode);
  541. bool old_relative_mode = relative_mode;
  542. if (!relative_mode) {
  543. //DEBUG_ECHOPGM(" G91");
  544. queue.enqueue_now_P(PSTR("G91"));
  545. //DEBUG_ECHOPGM(" ✓ ");
  546. }
  547. char buf[32]; // G1 X9999.99 F12345
  548. unsigned int backup_speed = MMS_TO_MMM(feedrate_mm_s);
  549. char sign[]="\0";
  550. int16_t value = movevalue / 100;
  551. if (movevalue < 0) { value = -value; sign[0] = '-'; }
  552. int16_t fraction = ABS(movevalue) % 100;
  553. snprintf_P(buf, 32, PSTR("G0 %c%s%d.%02d F%d"), axiscode, sign, value, fraction, speed);
  554. //DEBUG_ECHOPAIR(" ", buf);
  555. queue.enqueue_one_now(buf);
  556. //DEBUG_ECHOLNPGM(" ✓ ");
  557. if (backup_speed != speed) {
  558. snprintf_P(buf, 32, PSTR("G0 F%d"), backup_speed);
  559. queue.enqueue_one_now(buf);
  560. //DEBUG_ECHOPAIR(" ", buf);
  561. }
  562. //while (!enqueue_and_echo_command(buf)) idle();
  563. //DEBUG_ECHOLNPGM(" ✓ ");
  564. if (!old_relative_mode) {
  565. //DEBUG_ECHOPGM("G90");
  566. queue.enqueue_now_P(PSTR("G90"));
  567. //DEBUG_ECHOPGM(" ✓ ");
  568. }
  569. }
  570. ScreenHandler.ForceCompleteUpdate();
  571. DEBUG_ECHOLNPGM("manmv done.");
  572. return;
  573. cannotmove:
  574. DEBUG_ECHOLNPAIR(" cannot move ", axiscode);
  575. return;
  576. }
  577. void DGUSScreenHandler::HandleMotorLockUnlock(DGUS_VP_Variable &var, void *val_ptr) {
  578. DEBUG_ECHOLNPGM("HandleMotorLockUnlock");
  579. char buf[4];
  580. const int16_t lock = swap16(*(uint16_t*)val_ptr);
  581. strcpy_P(buf, lock ? PSTR("M18") : PSTR("M17"));
  582. //DEBUG_ECHOPAIR(" ", buf);
  583. queue.enqueue_one_now(buf);
  584. }
  585. #if ENABLED(POWER_LOSS_RECOVERY)
  586. void DGUSScreenHandler::HandlePowerLossRecovery(DGUS_VP_Variable &var, void *val_ptr) {
  587. uint16_t value = swap16(*(uint16_t*)val_ptr);
  588. if (value) {
  589. queue.inject_P(PSTR("M1000"));
  590. ScreenHandler.GotoScreen(DGUSLCD_SCREEN_SDPRINTMANIPULATION);
  591. }
  592. else {
  593. recovery.cancel();
  594. ScreenHandler.GotoScreen(DGUSLCD_SCREEN_STATUS);
  595. }
  596. }
  597. #endif
  598. void DGUSScreenHandler::HandleSettings(DGUS_VP_Variable &var, void *val_ptr) {
  599. DEBUG_ECHOLNPGM("HandleSettings");
  600. uint16_t value = swap16(*(uint16_t*)val_ptr);
  601. switch (value) {
  602. default: break;
  603. case 1:
  604. TERN_(PRINTCOUNTER, print_job_timer.initStats());
  605. queue.inject_P(PSTR("M502\nM500"));
  606. break;
  607. case 2: queue.inject_P(PSTR("M501")); break;
  608. case 3: queue.inject_P(PSTR("M500")); break;
  609. }
  610. }
  611. void DGUSScreenHandler::HandleStepPerMMChanged(DGUS_VP_Variable &var, void *val_ptr) {
  612. DEBUG_ECHOLNPGM("HandleStepPerMMChanged");
  613. uint16_t value_raw = swap16(*(uint16_t*)val_ptr);
  614. DEBUG_ECHOLNPAIR("value_raw:", value_raw);
  615. float value = (float)value_raw/10;
  616. ExtUI::axis_t axis;
  617. switch (var.VP) {
  618. case VP_X_STEP_PER_MM: axis = ExtUI::axis_t::X; break;
  619. case VP_Y_STEP_PER_MM: axis = ExtUI::axis_t::Y; break;
  620. case VP_Z_STEP_PER_MM: axis = ExtUI::axis_t::Z; break;
  621. default: return;
  622. }
  623. DEBUG_ECHOLNPAIR_F("value:", value);
  624. ExtUI::setAxisSteps_per_mm(value, axis);
  625. DEBUG_ECHOLNPAIR_F("value_set:", ExtUI::getAxisSteps_per_mm(axis));
  626. ScreenHandler.skipVP = var.VP; // don't overwrite value the next update time as the display might autoincrement in parallel
  627. return;
  628. }
  629. void DGUSScreenHandler::HandleStepPerMMExtruderChanged(DGUS_VP_Variable &var, void *val_ptr) {
  630. DEBUG_ECHOLNPGM("HandleStepPerMMExtruderChanged");
  631. uint16_t value_raw = swap16(*(uint16_t*)val_ptr);
  632. DEBUG_ECHOLNPAIR("value_raw:", value_raw);
  633. float value = (float)value_raw/10;
  634. ExtUI::extruder_t extruder;
  635. switch (var.VP) {
  636. default: return;
  637. #if HOTENDS >= 1
  638. case VP_E0_STEP_PER_MM: extruder = ExtUI::extruder_t::E0; break;
  639. #endif
  640. #if HOTENDS >= 2
  641. case VP_E1_STEP_PER_MM: extruder = ExtUI::extruder_t::E1; break;
  642. #endif
  643. }
  644. DEBUG_ECHOLNPAIR_F("value:", value);
  645. ExtUI::setAxisSteps_per_mm(value,extruder);
  646. DEBUG_ECHOLNPAIR_F("value_set:", ExtUI::getAxisSteps_per_mm(extruder));
  647. ScreenHandler.skipVP = var.VP; // don't overwrite value the next update time as the display might autoincrement in parallel
  648. return;
  649. }
  650. #if HAS_PID_HEATING
  651. void DGUSScreenHandler::HandleTemperaturePIDChanged(DGUS_VP_Variable &var, void *val_ptr) {
  652. uint16_t rawvalue = swap16(*(uint16_t*)val_ptr);
  653. DEBUG_ECHOLNPAIR("V1:", rawvalue);
  654. float value = (float)rawvalue / 10;
  655. DEBUG_ECHOLNPAIR("V2:", value);
  656. float newvalue = 0;
  657. switch (var.VP) {
  658. default: return;
  659. #if HOTENDS >= 1
  660. case VP_E0_PID_P: newvalue = value; break;
  661. case VP_E0_PID_I: newvalue = scalePID_i(value); break;
  662. case VP_E0_PID_D: newvalue = scalePID_d(value); break;
  663. #endif
  664. #if HOTENDS >= 2
  665. case VP_E1_PID_P: newvalue = value; break;
  666. case VP_E1_PID_I: newvalue = scalePID_i(value); break;
  667. case VP_E1_PID_D: newvalue = scalePID_d(value); break;
  668. #endif
  669. #if HAS_HEATED_BED
  670. case VP_BED_PID_P: newvalue = value; break;
  671. case VP_BED_PID_I: newvalue = scalePID_i(value); break;
  672. case VP_BED_PID_D: newvalue = scalePID_d(value); break;
  673. #endif
  674. }
  675. DEBUG_ECHOLNPAIR_F("V3:", newvalue);
  676. *(float *)var.memadr = newvalue;
  677. ScreenHandler.skipVP = var.VP; // don't overwrite value the next update time as the display might autoincrement in parallel
  678. }
  679. void DGUSScreenHandler::HandlePIDAutotune(DGUS_VP_Variable &var, void *val_ptr) {
  680. DEBUG_ECHOLNPGM("HandlePIDAutotune");
  681. char buf[32] = {0};
  682. switch (var.VP) {
  683. default: break;
  684. #if ENABLED(PIDTEMP)
  685. #if HOTENDS >= 1
  686. case VP_PID_AUTOTUNE_E0: // Autotune Extruder 0
  687. sprintf(buf, "M303 E%d C5 S210 U1", ExtUI::extruder_t::E0);
  688. break;
  689. #endif
  690. #if HOTENDS >= 2
  691. case VP_PID_AUTOTUNE_E1:
  692. sprintf(buf, "M303 E%d C5 S210 U1", ExtUI::extruder_t::E1);
  693. break;
  694. #endif
  695. #endif
  696. #if ENABLED(PIDTEMPBED)
  697. case VP_PID_AUTOTUNE_BED:
  698. sprintf(buf, "M303 E-1 C5 S70 U1");
  699. break;
  700. #endif
  701. }
  702. if (buf[0]) queue.enqueue_one_now(buf);
  703. #if ENABLED(DGUS_UI_WAITING)
  704. sendinfoscreen(PSTR("PID is autotuning"), PSTR("please wait"), NUL_STR, NUL_STR, true, true, true, true);
  705. GotoScreen(DGUSLCD_SCREEN_WAITING);
  706. #endif
  707. }
  708. #endif
  709. #if HAS_BED_PROBE
  710. void DGUSScreenHandler::HandleProbeOffsetZChanged(DGUS_VP_Variable &var, void *val_ptr) {
  711. DEBUG_ECHOLNPGM("HandleProbeOffsetZChanged");
  712. const float offset = float(int16_t(swap16(*(uint16_t*)val_ptr))) / 100.0f;
  713. ExtUI::setZOffset_mm(offset);
  714. ScreenHandler.skipVP = var.VP; // don't overwrite value the next update time as the display might autoincrement in parallel
  715. return;
  716. }
  717. #endif
  718. #if ENABLED(BABYSTEPPING)
  719. void DGUSScreenHandler::HandleLiveAdjustZ(DGUS_VP_Variable &var, void *val_ptr) {
  720. DEBUG_ECHOLNPGM("HandleLiveAdjustZ");
  721. int16_t flag = swap16(*(uint16_t*)val_ptr);
  722. int16_t steps = flag ? -20 : 20;
  723. ExtUI::smartAdjustAxis_steps(steps, ExtUI::axis_t::Z, true);
  724. ScreenHandler.ForceCompleteUpdate();
  725. return;
  726. }
  727. #endif
  728. #if HAS_FAN
  729. void DGUSScreenHandler::HandleFanControl(DGUS_VP_Variable &var, void *val_ptr) {
  730. DEBUG_ECHOLNPGM("HandleFanControl");
  731. *(uint8_t*)var.memadr = *(uint8_t*)var.memadr > 0 ? 0 : 255;
  732. }
  733. #endif
  734. void DGUSScreenHandler::HandleHeaterControl(DGUS_VP_Variable &var, void *val_ptr) {
  735. DEBUG_ECHOLNPGM("HandleHeaterControl");
  736. uint8_t preheat_temp = 0;
  737. switch (var.VP) {
  738. #if HOTENDS >= 1
  739. case VP_E0_CONTROL:
  740. #endif
  741. #if HOTENDS >= 2
  742. case VP_E1_CONTROL:
  743. #endif
  744. #if HOTENDS >= 3
  745. case VP_E2_CONTROL:
  746. #endif
  747. preheat_temp = PREHEAT_1_TEMP_HOTEND;
  748. break;
  749. case VP_BED_CONTROL:
  750. preheat_temp = PREHEAT_1_TEMP_BED;
  751. break;
  752. }
  753. *(int16_t*)var.memadr = *(int16_t*)var.memadr > 0 ? 0 : preheat_temp;
  754. }
  755. #if ENABLED(DGUS_PREHEAT_UI)
  756. void DGUSScreenHandler::HandlePreheat(DGUS_VP_Variable &var, void *val_ptr) {
  757. DEBUG_ECHOLNPGM("HandlePreheat");
  758. uint8_t e_temp = 0;
  759. TERN_(HAS_HEATED_BED, uint8_t bed_temp = 0);
  760. const uint16_t preheat_option = swap16(*(uint16_t*)val_ptr);
  761. switch (preheat_option) {
  762. default:
  763. case 0: // Preheat PLA
  764. #if defined(PREHEAT_1_TEMP_HOTEND) && defined(PREHEAT_1_TEMP_BED)
  765. e_temp = PREHEAT_1_TEMP_HOTEND;
  766. TERN_(HAS_HEATED_BED, bed_temp = PREHEAT_1_TEMP_BED);
  767. #endif
  768. break;
  769. case 1: // Preheat ABS
  770. #if defined(PREHEAT_2_TEMP_HOTEND) && defined(PREHEAT_2_TEMP_BED)
  771. e_temp = PREHEAT_2_TEMP_HOTEND;
  772. TERN_(HAS_HEATED_BED, bed_temp = PREHEAT_2_TEMP_BED);
  773. #endif
  774. break;
  775. case 2: // Preheat PET
  776. #if defined(PREHEAT_3_TEMP_HOTEND) && defined(PREHEAT_3_TEMP_BED)
  777. e_temp = PREHEAT_3_TEMP_HOTEND;
  778. TERN_(HAS_HEATED_BED, bed_temp = PREHEAT_3_TEMP_BED);
  779. #endif
  780. break;
  781. case 3: // Preheat FLEX
  782. #if defined(PREHEAT_4_TEMP_HOTEND) && defined(PREHEAT_4_TEMP_BED)
  783. e_temp = PREHEAT_4_TEMP_HOTEND;
  784. TERN_(HAS_HEATED_BED, bed_temp = PREHEAT_4_TEMP_BED);
  785. #endif
  786. break;
  787. case 7: break; // Custom preheat
  788. case 9: break; // Cool down
  789. }
  790. switch (var.VP) {
  791. default: return;
  792. #if HOTENDS >= 1
  793. case VP_E0_BED_PREHEAT:
  794. thermalManager.setTargetHotend(e_temp, 0);
  795. TERN_(HAS_HEATED_BED, thermalManager.setTargetBed(bed_temp));
  796. break;
  797. #endif
  798. #if HOTENDS >= 2
  799. case VP_E1_BED_PREHEAT:
  800. thermalManager.setTargetHotend(e_temp, 1);
  801. TERN_(HAS_HEATED_BED, thermalManager.setTargetBed(bed_temp));
  802. break;
  803. #endif
  804. }
  805. // Go to the preheat screen to show the heating progress
  806. GotoScreen(DGUSLCD_SCREEN_PREHEAT);
  807. }
  808. #endif
  809. #if ENABLED(DGUS_FILAMENT_LOADUNLOAD)
  810. typedef struct {
  811. ExtUI::extruder_t extruder; // which extruder to operate
  812. uint8_t action; // load or unload
  813. bool heated; // heating done ?
  814. float purge_length; // the length to extrude before unload, prevent filament jam
  815. } filament_data_t;
  816. static filament_data_t filament_data;
  817. void DGUSScreenHandler::HandleFilamentOption(DGUS_VP_Variable &var, void *val_ptr) {
  818. DEBUG_ECHOLNPGM("HandleFilamentOption");
  819. uint8_t e_temp = 0;
  820. filament_data.heated = false;
  821. uint16_t preheat_option = swap16(*(uint16_t*)val_ptr);
  822. if (preheat_option <= 8) // Load filament type
  823. filament_data.action = 1;
  824. else if (preheat_option >= 10) { // Unload filament type
  825. preheat_option -= 10;
  826. filament_data.action = 2;
  827. filament_data.purge_length = DGUS_FILAMENT_PURGE_LENGTH;
  828. }
  829. else // Cancel filament operation
  830. filament_data.action = 0;
  831. switch (preheat_option) {
  832. case 0: // Load PLA
  833. #ifdef PREHEAT_1_TEMP_HOTEND
  834. e_temp = PREHEAT_1_TEMP_HOTEND;
  835. #endif
  836. break;
  837. case 1: // Load ABS
  838. TERN_(PREHEAT_2_TEMP_HOTEND, e_temp = PREHEAT_2_TEMP_HOTEND);
  839. break;
  840. case 2: // Load PET
  841. #ifdef PREHEAT_3_TEMP_HOTEND
  842. e_temp = PREHEAT_3_TEMP_HOTEND;
  843. #endif
  844. break;
  845. case 3: // Load FLEX
  846. #ifdef PREHEAT_4_TEMP_HOTEND
  847. e_temp = PREHEAT_4_TEMP_HOTEND;
  848. #endif
  849. break;
  850. case 9: // Cool down
  851. default:
  852. e_temp = 0;
  853. break;
  854. }
  855. if (filament_data.action == 0) { // Go back to utility screen
  856. #if HOTENDS >= 1
  857. thermalManager.setTargetHotend(e_temp, ExtUI::extruder_t::E0);
  858. #endif
  859. #if HOTENDS >= 2
  860. thermalManager.setTargetHotend(e_temp, ExtUI::extruder_t::E1);
  861. #endif
  862. GotoScreen(DGUSLCD_SCREEN_UTILITY);
  863. }
  864. else { // Go to the preheat screen to show the heating progress
  865. switch (var.VP) {
  866. default: return;
  867. #if HOTENDS >= 1
  868. case VP_E0_FILAMENT_LOAD_UNLOAD:
  869. filament_data.extruder = ExtUI::extruder_t::E0;
  870. thermalManager.setTargetHotend(e_temp, filament_data.extruder);
  871. break;
  872. #endif
  873. #if HOTENDS >= 2
  874. case VP_E1_FILAMENT_LOAD_UNLOAD:
  875. filament_data.extruder = ExtUI::extruder_t::E1;
  876. thermalManager.setTargetHotend(e_temp, filament_data.extruder);
  877. break;
  878. #endif
  879. }
  880. GotoScreen(DGUSLCD_SCREEN_FILAMENT_HEATING);
  881. }
  882. }
  883. void DGUSScreenHandler::HandleFilamentLoadUnload(DGUS_VP_Variable &var) {
  884. DEBUG_ECHOLNPGM("HandleFilamentLoadUnload");
  885. if (filament_data.action <= 0) return;
  886. // If we close to the target temperature, we can start load or unload the filament
  887. if (thermalManager.hotEnoughToExtrude(filament_data.extruder) && \
  888. thermalManager.targetHotEnoughToExtrude(filament_data.extruder)) {
  889. float movevalue = DGUS_FILAMENT_LOAD_LENGTH_PER_TIME;
  890. if (filament_data.action == 1) { // load filament
  891. if (!filament_data.heated) {
  892. GotoScreen(DGUSLCD_SCREEN_FILAMENT_LOADING);
  893. filament_data.heated = true;
  894. }
  895. movevalue = ExtUI::getAxisPosition_mm(filament_data.extruder)+movevalue;
  896. }
  897. else { // unload filament
  898. if (!filament_data.heated) {
  899. GotoScreen(DGUSLCD_SCREEN_FILAMENT_UNLOADING);
  900. filament_data.heated = true;
  901. }
  902. // Before unloading extrude to prevent jamming
  903. if (filament_data.purge_length >= 0) {
  904. movevalue = ExtUI::getAxisPosition_mm(filament_data.extruder) + movevalue;
  905. filament_data.purge_length -= movevalue;
  906. }
  907. else
  908. movevalue = ExtUI::getAxisPosition_mm(filament_data.extruder) - movevalue;
  909. }
  910. ExtUI::setAxisPosition_mm(movevalue, filament_data.extruder);
  911. }
  912. }
  913. #endif
  914. void DGUSScreenHandler::UpdateNewScreen(DGUSLCD_Screens newscreen, bool popup) {
  915. DEBUG_ECHOLNPAIR("SetNewScreen: ", newscreen);
  916. if (!popup) {
  917. memmove(&past_screens[1], &past_screens[0], sizeof(past_screens) - 1);
  918. past_screens[0] = current_screen;
  919. }
  920. current_screen = newscreen;
  921. skipVP = 0;
  922. ForceCompleteUpdate();
  923. }
  924. void DGUSScreenHandler::PopToOldScreen() {
  925. DEBUG_ECHOLNPAIR("PopToOldScreen s=", past_screens[0]);
  926. GotoScreen(past_screens[0], true);
  927. memmove(&past_screens[0], &past_screens[1], sizeof(past_screens) - 1);
  928. past_screens[sizeof(past_screens) - 1] = DGUSLCD_SCREEN_MAIN;
  929. }
  930. void DGUSScreenHandler::UpdateScreenVPData() {
  931. DEBUG_ECHOPAIR(" UpdateScreenVPData Screen: ", current_screen);
  932. const uint16_t *VPList = DGUSLCD_FindScreenVPMapList(current_screen);
  933. if (!VPList) {
  934. DEBUG_ECHOLNPAIR(" NO SCREEN FOR: ", current_screen);
  935. ScreenComplete = true;
  936. return; // nothing to do, likely a bug or boring screen.
  937. }
  938. // Round-robin updating of all VPs.
  939. VPList += update_ptr;
  940. bool sent_one = false;
  941. do {
  942. uint16_t VP = pgm_read_word(VPList);
  943. DEBUG_ECHOPAIR(" VP: ", VP);
  944. if (!VP) {
  945. update_ptr = 0;
  946. DEBUG_ECHOLNPGM(" UpdateScreenVPData done");
  947. ScreenComplete = true;
  948. return; // Screen completed.
  949. }
  950. if (VP == skipVP) { skipVP = 0; continue; }
  951. DGUS_VP_Variable rcpy;
  952. if (populate_VPVar(VP, &rcpy)) {
  953. uint8_t expected_tx = 6 + rcpy.size; // expected overhead is 6 bytes + payload.
  954. // Send the VP to the display, but try to avoid overrunning the Tx Buffer.
  955. // But send at least one VP, to avoid getting stalled.
  956. if (rcpy.send_to_display_handler && (!sent_one || expected_tx <= dgusdisplay.GetFreeTxBuffer())) {
  957. //DEBUG_ECHOPAIR(" calling handler for ", rcpy.VP);
  958. sent_one = true;
  959. rcpy.send_to_display_handler(rcpy);
  960. }
  961. else {
  962. //auto x=dgusdisplay.GetFreeTxBuffer();
  963. //DEBUG_ECHOLNPAIR(" tx almost full: ", x);
  964. //DEBUG_ECHOPAIR(" update_ptr ", update_ptr);
  965. ScreenComplete = false;
  966. return; // please call again!
  967. }
  968. }
  969. } while (++update_ptr, ++VPList, true);
  970. }
  971. void DGUSScreenHandler::GotoScreen(DGUSLCD_Screens screen, bool ispopup) {
  972. dgusdisplay.RequestScreen(screen);
  973. UpdateNewScreen(screen, ispopup);
  974. }
  975. bool DGUSScreenHandler::loop() {
  976. dgusdisplay.loop();
  977. const millis_t ms = millis();
  978. static millis_t next_event_ms = 0;
  979. if (!IsScreenComplete() || ELAPSED(ms, next_event_ms)) {
  980. next_event_ms = ms + DGUS_UPDATE_INTERVAL_MS;
  981. UpdateScreenVPData();
  982. }
  983. #if ENABLED(SHOW_BOOTSCREEN)
  984. static bool booted = false;
  985. if (!booted && TERN0(POWER_LOSS_RECOVERY, recovery.valid()))
  986. booted = true;
  987. if (!booted && ELAPSED(ms, BOOTSCREEN_TIMEOUT)) {
  988. booted = true;
  989. GotoScreen(DGUSLCD_SCREEN_MAIN);
  990. }
  991. #endif
  992. return IsScreenComplete();
  993. }
  994. void DGUSDisplay::RequestScreen(DGUSLCD_Screens screen) {
  995. DEBUG_ECHOLNPAIR("GotoScreen ", screen);
  996. const unsigned char gotoscreen[] = { 0x5A, 0x01, (unsigned char) (screen >> 8U), (unsigned char) (screen & 0xFFU) };
  997. WriteVariable(0x84, gotoscreen, sizeof(gotoscreen));
  998. }
  999. #endif // HAS_DGUS_LCD