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.

DGUSScreenHandler.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../../../inc/MarlinConfigPre.h"
  23. #if ENABLED(DGUS_LCD_UI_ORIGIN)
  24. #include "../DGUSScreenHandler.h"
  25. #include "../../../../MarlinCore.h"
  26. #include "../../../../gcode/queue.h"
  27. #include "../../../../libs/duration_t.h"
  28. #include "../../../../module/settings.h"
  29. #include "../../../../module/temperature.h"
  30. #include "../../../../module/motion.h"
  31. #include "../../../../module/planner.h"
  32. #include "../../../../module/printcounter.h"
  33. #include "../../../../sd/cardreader.h"
  34. #if ENABLED(POWER_LOSS_RECOVERY)
  35. #include "../../../../feature/powerloss.h"
  36. #endif
  37. #if ENABLED(SDSUPPORT)
  38. static ExtUI::FileList filelist;
  39. void DGUSScreenHandler::DGUSLCD_SD_FileSelected(DGUS_VP_Variable &var, void *val_ptr) {
  40. uint16_t touched_nr = (int16_t)swap16(*(uint16_t*)val_ptr) + top_file;
  41. if (touched_nr > filelist.count()) return;
  42. if (!filelist.seek(touched_nr)) return;
  43. if (filelist.isDir()) {
  44. filelist.changeDir(filelist.filename());
  45. top_file = 0;
  46. ForceCompleteUpdate();
  47. return;
  48. }
  49. #if ENABLED(DGUS_PRINT_FILENAME)
  50. // Send print filename
  51. dgusdisplay.WriteVariable(VP_SD_Print_Filename, filelist.filename(), VP_SD_FileName_LEN, true);
  52. #endif
  53. // Setup Confirmation screen
  54. file_to_print = touched_nr;
  55. HandleUserConfirmationPopUp(VP_SD_FileSelectConfirm, nullptr, PSTR("Print file"), filelist.filename(), PSTR("from SD Card?"), true, true, false, true);
  56. }
  57. void DGUSScreenHandler::DGUSLCD_SD_StartPrint(DGUS_VP_Variable &var, void *val_ptr) {
  58. if (!filelist.seek(file_to_print)) return;
  59. ExtUI::printFile(filelist.shortFilename());
  60. GotoScreen(DGUSLCD_SCREEN_STATUS);
  61. }
  62. void DGUSScreenHandler::DGUSLCD_SD_ResumePauseAbort(DGUS_VP_Variable &var, void *val_ptr) {
  63. if (!ExtUI::isPrintingFromMedia()) return; // avoid race condition when user stays in this menu and printer finishes.
  64. switch (swap16(*(uint16_t*)val_ptr)) {
  65. case 0: { // Resume
  66. if (ExtUI::isPrintingFromMediaPaused()) {
  67. ExtUI::resumePrint();
  68. }
  69. } break;
  70. case 1: // Pause
  71. GotoScreen(MKSLCD_SCREEN_PAUSE);
  72. if (!ExtUI::isPrintingFromMediaPaused()) {
  73. ExtUI::pausePrint();
  74. //ExtUI::mks_pausePrint();
  75. }
  76. break;
  77. case 2: // Abort
  78. HandleUserConfirmationPopUp(VP_SD_AbortPrintConfirmed, nullptr, PSTR("Abort printing"), filelist.filename(), PSTR("?"), true, true, false, true);
  79. break;
  80. }
  81. }
  82. void DGUSScreenHandler::DGUSLCD_SD_SendFilename(DGUS_VP_Variable& var) {
  83. uint16_t target_line = (var.VP - VP_SD_FileName0) / VP_SD_FileName_LEN;
  84. if (target_line > DGUS_SD_FILESPERSCREEN) return;
  85. char tmpfilename[VP_SD_FileName_LEN + 1] = "";
  86. var.memadr = (void*)tmpfilename;
  87. if (filelist.seek(top_file + target_line)) {
  88. snprintf_P(tmpfilename, VP_SD_FileName_LEN, PSTR("%s%c"), filelist.filename(), filelist.isDir() ? '/' : 0); // snprintf_P(tmpfilename, VP_SD_FileName_LEN, PSTR("%s"), filelist.filename());
  89. }
  90. DGUSLCD_SendStringToDisplay(var);
  91. }
  92. void DGUSScreenHandler::SDCardInserted() {
  93. top_file = 0;
  94. filelist.refresh();
  95. auto cs = getCurrentScreen();
  96. if (cs == DGUSLCD_SCREEN_MAIN || cs == DGUSLCD_SCREEN_STATUS)
  97. GotoScreen(DGUSLCD_SCREEN_SDFILELIST);
  98. }
  99. void DGUSScreenHandler::SDCardRemoved() {
  100. if (current_screen == DGUSLCD_SCREEN_SDFILELIST
  101. || (current_screen == DGUSLCD_SCREEN_CONFIRM && (ConfirmVP == VP_SD_AbortPrintConfirmed || ConfirmVP == VP_SD_FileSelectConfirm))
  102. || current_screen == DGUSLCD_SCREEN_SDPRINTMANIPULATION
  103. ) GotoScreen(DGUSLCD_SCREEN_MAIN);
  104. }
  105. #endif // SDSUPPORT
  106. void DGUSScreenHandler::ScreenChangeHook(DGUS_VP_Variable &var, void *val_ptr) {
  107. uint8_t *tmp = (uint8_t*)val_ptr;
  108. // The keycode in target is coded as <from-frame><to-frame>, so 0x0100A means
  109. // from screen 1 (main) to 10 (temperature). DGUSLCD_SCREEN_POPUP is special,
  110. // meaning "return to previous screen"
  111. DGUSLCD_Screens target = (DGUSLCD_Screens)tmp[1];
  112. DEBUG_ECHOLNPAIR("\n DEBUG target", target);
  113. if (target == DGUSLCD_SCREEN_POPUP) {
  114. // Special handling for popup is to return to previous menu
  115. if (current_screen == DGUSLCD_SCREEN_POPUP && confirm_action_cb) confirm_action_cb();
  116. PopToOldScreen();
  117. return;
  118. }
  119. UpdateNewScreen(target);
  120. #ifdef DEBUG_DGUSLCD
  121. if (!DGUSLCD_FindScreenVPMapList(target)) DEBUG_ECHOLNPAIR("WARNING: No screen Mapping found for ", target);
  122. #endif
  123. }
  124. void DGUSScreenHandler::HandleManualMove(DGUS_VP_Variable &var, void *val_ptr) {
  125. DEBUG_ECHOLNPGM("HandleManualMove");
  126. int16_t movevalue = swap16(*(uint16_t*)val_ptr);
  127. #if ENABLED(DGUS_UI_MOVE_DIS_OPTION)
  128. if (movevalue) {
  129. const uint16_t choice = *(uint16_t*)var.memadr;
  130. movevalue = movevalue < 0 ? -choice : choice;
  131. }
  132. #endif
  133. char axiscode;
  134. unsigned int speed = 1500; // FIXME: get default feedrate for manual moves, dont hardcode.
  135. switch (var.VP) {
  136. default: return;
  137. case VP_MOVE_X:
  138. axiscode = 'X';
  139. if (!ExtUI::canMove(ExtUI::axis_t::X)) goto cannotmove;
  140. break;
  141. case VP_MOVE_Y:
  142. axiscode = 'Y';
  143. if (!ExtUI::canMove(ExtUI::axis_t::Y)) goto cannotmove;
  144. break;
  145. case VP_MOVE_Z:
  146. axiscode = 'Z';
  147. speed = 300; // default to 5mm/s
  148. if (!ExtUI::canMove(ExtUI::axis_t::Z)) goto cannotmove;
  149. break;
  150. case VP_HOME_ALL: // only used for homing
  151. axiscode = '\0';
  152. movevalue = 0; // ignore value sent from display, this VP is _ONLY_ for homing.
  153. break;
  154. }
  155. if (!movevalue) {
  156. // homing
  157. DEBUG_ECHOPAIR(" homing ", AS_CHAR(axiscode));
  158. char buf[6] = "G28 X";
  159. buf[4] = axiscode;
  160. //DEBUG_ECHOPAIR(" ", buf);
  161. queue.enqueue_one_now(buf);
  162. //DEBUG_ECHOLNPGM(" ✓");
  163. ForceCompleteUpdate();
  164. return;
  165. }
  166. else {
  167. // movement
  168. DEBUG_ECHOPAIR(" move ", AS_CHAR(axiscode));
  169. bool old_relative_mode = relative_mode;
  170. if (!relative_mode) {
  171. //DEBUG_ECHOPGM(" G91");
  172. queue.enqueue_now_P(PSTR("G91"));
  173. //DEBUG_ECHOPGM(" ✓ ");
  174. }
  175. char buf[32]; // G1 X9999.99 F12345
  176. unsigned int backup_speed = MMS_TO_MMM(feedrate_mm_s);
  177. char sign[] = "\0";
  178. int16_t value = movevalue / 100;
  179. if (movevalue < 0) { value = -value; sign[0] = '-'; }
  180. int16_t fraction = ABS(movevalue) % 100;
  181. snprintf_P(buf, 32, PSTR("G0 %c%s%d.%02d F%d"), axiscode, sign, value, fraction, speed);
  182. //DEBUG_ECHOPAIR(" ", buf);
  183. queue.enqueue_one_now(buf);
  184. //DEBUG_ECHOLNPGM(" ✓ ");
  185. if (backup_speed != speed) {
  186. snprintf_P(buf, 32, PSTR("G0 F%d"), backup_speed);
  187. queue.enqueue_one_now(buf);
  188. //DEBUG_ECHOPAIR(" ", buf);
  189. }
  190. // while (!enqueue_and_echo_command(buf)) idle();
  191. //DEBUG_ECHOLNPGM(" ✓ ");
  192. if (!old_relative_mode) {
  193. //DEBUG_ECHOPGM("G90");
  194. queue.enqueue_now_P(PSTR("G90"));
  195. //DEBUG_ECHOPGM(" ✓ ");
  196. }
  197. }
  198. ForceCompleteUpdate();
  199. DEBUG_ECHOLNPGM("manmv done.");
  200. return;
  201. cannotmove:
  202. DEBUG_ECHOLNPAIR(" cannot move ", AS_CHAR(axiscode));
  203. return;
  204. }
  205. #if HAS_PID_HEATING
  206. void DGUSScreenHandler::HandleTemperaturePIDChanged(DGUS_VP_Variable &var, void *val_ptr) {
  207. uint16_t rawvalue = swap16(*(uint16_t*)val_ptr);
  208. DEBUG_ECHOLNPAIR("V1:", rawvalue);
  209. float value = (float)rawvalue / 10;
  210. DEBUG_ECHOLNPAIR("V2:", value);
  211. float newvalue = 0;
  212. switch (var.VP) {
  213. default: return;
  214. #if HOTENDS >= 1
  215. case VP_E0_PID_P: newvalue = value; break;
  216. case VP_E0_PID_I: newvalue = scalePID_i(value); break;
  217. case VP_E0_PID_D: newvalue = scalePID_d(value); break;
  218. #endif
  219. #if HOTENDS >= 2
  220. case VP_E1_PID_P: newvalue = value; break;
  221. case VP_E1_PID_I: newvalue = scalePID_i(value); break;
  222. case VP_E1_PID_D: newvalue = scalePID_d(value); break;
  223. #endif
  224. #if HAS_HEATED_BED
  225. case VP_BED_PID_P: newvalue = value; break;
  226. case VP_BED_PID_I: newvalue = scalePID_i(value); break;
  227. case VP_BED_PID_D: newvalue = scalePID_d(value); break;
  228. #endif
  229. }
  230. DEBUG_ECHOLNPAIR_F("V3:", newvalue);
  231. *(float *)var.memadr = newvalue;
  232. skipVP = var.VP; // don't overwrite value the next update time as the display might autoincrement in parallel
  233. }
  234. #endif // HAS_PID_HEATING
  235. #if ENABLED(BABYSTEPPING)
  236. void DGUSScreenHandler::HandleLiveAdjustZ(DGUS_VP_Variable &var, void *val_ptr) {
  237. DEBUG_ECHOLNPGM("HandleLiveAdjustZ");
  238. int16_t flag = swap16(*(uint16_t*)val_ptr),
  239. steps = flag ? -20 : 20;
  240. ExtUI::smartAdjustAxis_steps(steps, ExtUI::axis_t::Z, true);
  241. ForceCompleteUpdate();
  242. }
  243. #endif
  244. #if ENABLED(DGUS_FILAMENT_LOADUNLOAD)
  245. void DGUSScreenHandler::HandleFilamentOption(DGUS_VP_Variable &var, void *val_ptr) {
  246. DEBUG_ECHOLNPGM("HandleFilamentOption");
  247. uint8_t e_temp = 0;
  248. filament_data.heated = false;
  249. uint16_t preheat_option = swap16(*(uint16_t*)val_ptr);
  250. if (preheat_option <= 8) { // Load filament type
  251. filament_data.action = 1;
  252. }
  253. else if (preheat_option >= 10) { // Unload filament type
  254. preheat_option -= 10;
  255. filament_data.action = 2;
  256. filament_data.purge_length = DGUS_FILAMENT_PURGE_LENGTH;
  257. }
  258. else { // Cancel filament operation
  259. filament_data.action = 0;
  260. }
  261. switch (preheat_option) {
  262. case 0: // Load PLA
  263. #ifdef PREHEAT_1_TEMP_HOTEND
  264. e_temp = PREHEAT_1_TEMP_HOTEND;
  265. #endif
  266. break;
  267. case 1: // Load ABS
  268. TERN_(PREHEAT_2_TEMP_HOTEND, e_temp = PREHEAT_2_TEMP_HOTEND);
  269. break;
  270. case 2: // Load PET
  271. #ifdef PREHEAT_3_TEMP_HOTEND
  272. e_temp = PREHEAT_3_TEMP_HOTEND;
  273. #endif
  274. break;
  275. case 3: // Load FLEX
  276. #ifdef PREHEAT_4_TEMP_HOTEND
  277. e_temp = PREHEAT_4_TEMP_HOTEND;
  278. #endif
  279. break;
  280. case 9: // Cool down
  281. default:
  282. e_temp = 0;
  283. break;
  284. }
  285. if (filament_data.action == 0) { // Go back to utility screen
  286. #if HOTENDS >= 1
  287. thermalManager.setTargetHotend(e_temp, ExtUI::extruder_t::E0);
  288. #endif
  289. #if HOTENDS >= 2
  290. thermalManager.setTargetHotend(e_temp, ExtUI::extruder_t::E1);
  291. #endif
  292. GotoScreen(DGUSLCD_SCREEN_UTILITY);
  293. }
  294. else { // Go to the preheat screen to show the heating progress
  295. switch (var.VP) {
  296. default: return;
  297. #if HOTENDS >= 1
  298. case VP_E0_FILAMENT_LOAD_UNLOAD:
  299. filament_data.extruder = ExtUI::extruder_t::E0;
  300. thermalManager.setTargetHotend(e_temp, filament_data.extruder);
  301. break;
  302. #endif
  303. #if HOTENDS >= 2
  304. case VP_E1_FILAMENT_LOAD_UNLOAD:
  305. filament_data.extruder = ExtUI::extruder_t::E1;
  306. thermalManager.setTargetHotend(e_temp, filament_data.extruder);
  307. break;
  308. #endif
  309. }
  310. GotoScreen(DGUSLCD_SCREEN_FILAMENT_HEATING);
  311. }
  312. }
  313. void DGUSScreenHandler::HandleFilamentLoadUnload(DGUS_VP_Variable &var) {
  314. DEBUG_ECHOLNPGM("HandleFilamentLoadUnload");
  315. if (filament_data.action <= 0) return;
  316. // If we close to the target temperature, we can start load or unload the filament
  317. if (thermalManager.hotEnoughToExtrude(filament_data.extruder) && \
  318. thermalManager.targetHotEnoughToExtrude(filament_data.extruder)) {
  319. float movevalue = DGUS_FILAMENT_LOAD_LENGTH_PER_TIME;
  320. if (filament_data.action == 1) { // load filament
  321. if (!filament_data.heated) {
  322. //GotoScreen(DGUSLCD_SCREEN_FILAMENT_LOADING);
  323. filament_data.heated = true;
  324. }
  325. movevalue = ExtUI::getAxisPosition_mm(filament_data.extruder) + movevalue;
  326. }
  327. else { // unload filament
  328. if (!filament_data.heated) {
  329. GotoScreen(DGUSLCD_SCREEN_FILAMENT_UNLOADING);
  330. filament_data.heated = true;
  331. }
  332. // Before unloading extrude to prevent jamming
  333. if (filament_data.purge_length >= 0) {
  334. movevalue = ExtUI::getAxisPosition_mm(filament_data.extruder) + movevalue;
  335. filament_data.purge_length -= movevalue;
  336. }
  337. else {
  338. movevalue = ExtUI::getAxisPosition_mm(filament_data.extruder) - movevalue;
  339. }
  340. }
  341. ExtUI::setAxisPosition_mm(movevalue, filament_data.extruder);
  342. }
  343. }
  344. #endif // DGUS_FILAMENT_LOADUNLOAD
  345. bool DGUSScreenHandler::loop() {
  346. dgusdisplay.loop();
  347. const millis_t ms = millis();
  348. static millis_t next_event_ms = 0;
  349. if (!IsScreenComplete() || ELAPSED(ms, next_event_ms)) {
  350. next_event_ms = ms + DGUS_UPDATE_INTERVAL_MS;
  351. UpdateScreenVPData();
  352. }
  353. #if ENABLED(SHOW_BOOTSCREEN)
  354. static bool booted = false;
  355. if (!booted && TERN0(POWER_LOSS_RECOVERY, recovery.valid()))
  356. booted = true;
  357. if (!booted && ELAPSED(ms, TERN(USE_MKS_GREEN_UI, 1000, BOOTSCREEN_TIMEOUT)))
  358. booted = true;
  359. #endif
  360. return IsScreenComplete();
  361. }
  362. #endif // DGUS_LCD_UI_ORIGIN