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.

marlinui.cpp 60KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  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/MarlinConfig.h"
  23. #include "../MarlinCore.h" // for printingIsPaused
  24. #ifdef LED_BACKLIGHT_TIMEOUT
  25. #include "../feature/leds/leds.h"
  26. #endif
  27. #if ENABLED(HOST_ACTION_COMMANDS)
  28. #include "../feature/host_actions.h"
  29. #endif
  30. #if BOTH(BROWSE_MEDIA_ON_INSERT, PASSWORD_ON_SD_PRINT_MENU)
  31. #include "../feature/password/password.h"
  32. #endif
  33. // All displays share the MarlinUI class
  34. #include "marlinui.h"
  35. MarlinUI ui;
  36. #if HAS_DISPLAY
  37. #include "../gcode/queue.h"
  38. #include "fontutils.h"
  39. #endif
  40. #if ENABLED(DWIN_CREALITY_LCD)
  41. #include "e3v2/creality/dwin.h"
  42. #elif ENABLED(DWIN_CREALITY_LCD_ENHANCED)
  43. #include "fontutils.h"
  44. #include "e3v2/enhanced/dwin.h"
  45. #elif ENABLED(DWIN_CREALITY_LCD_JYERSUI)
  46. #include "e3v2/jyersui/dwin.h"
  47. #endif
  48. #if ENABLED(LCD_PROGRESS_BAR) && !IS_TFTGLCD_PANEL
  49. #define BASIC_PROGRESS_BAR 1
  50. #endif
  51. #if ANY(HAS_DISPLAY, HAS_STATUS_MESSAGE, BASIC_PROGRESS_BAR)
  52. #include "../module/printcounter.h"
  53. #endif
  54. #if LCD_HAS_WAIT_FOR_MOVE
  55. bool MarlinUI::wait_for_move; // = false
  56. #endif
  57. constexpr uint8_t epps = ENCODER_PULSES_PER_STEP;
  58. #if HAS_STATUS_MESSAGE
  59. #if ENABLED(STATUS_MESSAGE_SCROLLING) && EITHER(HAS_WIRED_LCD, DWIN_CREALITY_LCD_ENHANCED)
  60. uint8_t MarlinUI::status_scroll_offset; // = 0
  61. #endif
  62. char MarlinUI::status_message[MAX_MESSAGE_LENGTH + 1];
  63. uint8_t MarlinUI::alert_level; // = 0
  64. #endif
  65. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  66. MarlinUI::progress_t MarlinUI::progress_override; // = 0
  67. #if ENABLED(USE_M73_REMAINING_TIME)
  68. uint32_t MarlinUI::remaining_time;
  69. #endif
  70. #endif
  71. #if HAS_MULTI_LANGUAGE
  72. uint8_t MarlinUI::language; // Initialized by settings.load()
  73. void MarlinUI::set_language(const uint8_t lang) {
  74. if (lang < NUM_LANGUAGES) {
  75. language = lang;
  76. TERN_(HAS_MARLINUI_U8GLIB, update_language_font());
  77. return_to_status();
  78. refresh();
  79. }
  80. }
  81. #endif
  82. #if HAS_LCD_CONTRAST
  83. uint8_t MarlinUI::contrast; // Initialized by settings.load()
  84. void MarlinUI::set_contrast(const uint8_t value) {
  85. contrast = constrain(value, LCD_CONTRAST_MIN, LCD_CONTRAST_MAX);
  86. _set_contrast();
  87. }
  88. #endif
  89. #if HAS_LCD_BRIGHTNESS
  90. uint8_t MarlinUI::brightness = DEFAULT_LCD_BRIGHTNESS;
  91. bool MarlinUI::backlight = true;
  92. void MarlinUI::set_brightness(const uint8_t value) {
  93. backlight = !!value;
  94. if (backlight) brightness = constrain(value, LCD_BRIGHTNESS_MIN, LCD_BRIGHTNESS_MAX);
  95. _set_brightness();
  96. }
  97. #endif
  98. #if ENABLED(SOUND_MENU_ITEM)
  99. bool MarlinUI::buzzer_enabled = true;
  100. #endif
  101. #if EITHER(PCA9632_BUZZER, USE_BEEPER)
  102. #include "../libs/buzzer.h" // for BUZZ() macro
  103. #if ENABLED(PCA9632_BUZZER)
  104. #include "../feature/leds/pca9632.h"
  105. #endif
  106. void MarlinUI::buzz(const long duration, const uint16_t freq) {
  107. if (!buzzer_enabled) return;
  108. #if ENABLED(PCA9632_BUZZER)
  109. PCA9632_buzz(duration, freq);
  110. #elif USE_BEEPER
  111. buzzer.tone(duration, freq);
  112. #endif
  113. }
  114. #endif
  115. #if HAS_PREHEAT
  116. #include "../module/temperature.h"
  117. preheat_t MarlinUI::material_preset[PREHEAT_COUNT]; // Initialized by settings.load()
  118. PGM_P MarlinUI::get_preheat_label(const uint8_t m) {
  119. #define _PDEF(N) static PGMSTR(preheat_##N##_label, PREHEAT_##N##_LABEL);
  120. #define _PLBL(N) preheat_##N##_label,
  121. REPEAT_1(PREHEAT_COUNT, _PDEF);
  122. static PGM_P const preheat_labels[PREHEAT_COUNT] PROGMEM = { REPEAT_1(PREHEAT_COUNT, _PLBL) };
  123. return (PGM_P)pgm_read_ptr(&preheat_labels[m]);
  124. }
  125. void MarlinUI::apply_preheat(const uint8_t m, const uint8_t pmask, const uint8_t e/*=active_extruder*/) {
  126. const preheat_t &pre = material_preset[m];
  127. TERN_(HAS_HOTEND, if (TEST(pmask, PT_HOTEND)) thermalManager.setTargetHotend(pre.hotend_temp, e));
  128. TERN_(HAS_HEATED_BED, if (TEST(pmask, PT_BED)) thermalManager.setTargetBed(pre.bed_temp));
  129. //TERN_(HAS_HEATED_CHAMBER, if (TEST(pmask, PT_CHAMBER)) thermalManager.setTargetBed(pre.chamber_temp));
  130. TERN_(HAS_FAN, if (TEST(pmask, PT_FAN)) thermalManager.set_fan_speed(0, pre.fan_speed));
  131. }
  132. #endif
  133. #if EITHER(HAS_LCD_MENU, EXTENSIBLE_UI)
  134. bool MarlinUI::lcd_clicked;
  135. #endif
  136. #if EITHER(HAS_WIRED_LCD, DWIN_CREALITY_LCD_JYERSUI)
  137. bool MarlinUI::get_blink() {
  138. static uint8_t blink = 0;
  139. static millis_t next_blink_ms = 0;
  140. millis_t ms = millis();
  141. if (ELAPSED(ms, next_blink_ms)) {
  142. blink ^= 0xFF;
  143. next_blink_ms = ms + 1000 - (LCD_UPDATE_INTERVAL) / 2;
  144. }
  145. return blink != 0;
  146. }
  147. #endif
  148. // Encoder Handling
  149. #if HAS_ENCODER_ACTION
  150. uint32_t MarlinUI::encoderPosition;
  151. volatile int8_t encoderDiff; // Updated in update_buttons, added to encoderPosition every LCD update
  152. #endif
  153. void MarlinUI::init() {
  154. init_lcd();
  155. #if HAS_DIGITAL_BUTTONS
  156. #if BUTTON_EXISTS(EN1)
  157. SET_INPUT_PULLUP(BTN_EN1);
  158. #endif
  159. #if BUTTON_EXISTS(EN2)
  160. SET_INPUT_PULLUP(BTN_EN2);
  161. #endif
  162. #if BUTTON_EXISTS(ENC)
  163. SET_INPUT_PULLUP(BTN_ENC);
  164. #endif
  165. #if BUTTON_EXISTS(ENC_EN)
  166. SET_INPUT_PULLUP(BTN_ENC_EN);
  167. #endif
  168. #if BUTTON_EXISTS(BACK)
  169. SET_INPUT_PULLUP(BTN_BACK);
  170. #endif
  171. #if BUTTON_EXISTS(UP)
  172. SET_INPUT(BTN_UP);
  173. #endif
  174. #if BUTTON_EXISTS(DWN)
  175. SET_INPUT(BTN_DWN);
  176. #endif
  177. #if BUTTON_EXISTS(LFT)
  178. SET_INPUT(BTN_LFT);
  179. #endif
  180. #if BUTTON_EXISTS(RT)
  181. SET_INPUT(BTN_RT);
  182. #endif
  183. #endif
  184. #if HAS_SHIFT_ENCODER
  185. #if ENABLED(SR_LCD_2W_NL) // Non latching 2 wire shift register
  186. SET_OUTPUT(SR_DATA_PIN);
  187. SET_OUTPUT(SR_CLK_PIN);
  188. #elif PIN_EXISTS(SHIFT_CLK)
  189. SET_OUTPUT(SHIFT_CLK_PIN);
  190. OUT_WRITE(SHIFT_LD_PIN, HIGH);
  191. #if PIN_EXISTS(SHIFT_EN)
  192. OUT_WRITE(SHIFT_EN_PIN, LOW);
  193. #endif
  194. SET_INPUT_PULLUP(SHIFT_OUT_PIN);
  195. #endif
  196. #endif // HAS_SHIFT_ENCODER
  197. #if BOTH(HAS_ENCODER_ACTION, HAS_SLOW_BUTTONS)
  198. slow_buttons = 0;
  199. #endif
  200. update_buttons();
  201. TERN_(HAS_ENCODER_ACTION, encoderDiff = 0);
  202. reset_status(); // Set welcome message
  203. }
  204. #if HAS_WIRED_LCD
  205. #if HAS_MARLINUI_U8GLIB
  206. #include "dogm/marlinui_DOGM.h"
  207. #endif
  208. #include "lcdprint.h"
  209. #include "../module/temperature.h"
  210. #include "../module/planner.h"
  211. #include "../module/motion.h"
  212. #if HAS_LCD_MENU
  213. #include "../module/settings.h"
  214. #endif
  215. #if ENABLED(AUTO_BED_LEVELING_UBL)
  216. #include "../feature/bedlevel/bedlevel.h"
  217. #endif
  218. #if HAS_TRINAMIC_CONFIG
  219. #include "../feature/tmc_util.h"
  220. #endif
  221. #if HAS_ADC_BUTTONS
  222. #include "../module/thermistor/thermistors.h"
  223. #endif
  224. #if HAS_POWER_MONITOR
  225. #include "../feature/power_monitor.h"
  226. #endif
  227. #if ENABLED(PSU_CONTROL) && defined(LED_BACKLIGHT_TIMEOUT)
  228. #include "../feature/power.h"
  229. #endif
  230. #if HAS_ENCODER_ACTION
  231. volatile uint8_t MarlinUI::buttons;
  232. #if HAS_SLOW_BUTTONS
  233. volatile uint8_t MarlinUI::slow_buttons;
  234. #endif
  235. #if HAS_TOUCH_BUTTONS
  236. #include "touch/touch_buttons.h"
  237. bool MarlinUI::on_edit_screen = false;
  238. #endif
  239. #endif
  240. #if SCREENS_CAN_TIME_OUT
  241. bool MarlinUI::defer_return_to_status;
  242. millis_t MarlinUI::return_to_status_ms = 0;
  243. #endif
  244. uint8_t MarlinUI::lcd_status_update_delay = 1; // First update one loop delayed
  245. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  246. millis_t MarlinUI::next_filament_display; // = 0
  247. #endif
  248. millis_t MarlinUI::next_button_update_ms; // = 0
  249. #if HAS_MARLINUI_U8GLIB
  250. bool MarlinUI::drawing_screen, MarlinUI::first_page; // = false
  251. #endif
  252. #if IS_DWIN_MARLINUI
  253. bool MarlinUI::did_first_redraw;
  254. bool MarlinUI::old_is_printing;
  255. #endif
  256. #if ENABLED(SDSUPPORT)
  257. #if MARLINUI_SCROLL_NAME
  258. uint8_t MarlinUI::filename_scroll_pos, MarlinUI::filename_scroll_max;
  259. #endif
  260. const char * MarlinUI::scrolled_filename(CardReader &theCard, const uint8_t maxlen, uint8_t hash, const bool doScroll) {
  261. const char *outstr = theCard.longest_filename();
  262. if (theCard.longFilename[0]) {
  263. #if MARLINUI_SCROLL_NAME
  264. if (doScroll) {
  265. for (uint8_t l = FILENAME_LENGTH; l--;)
  266. hash = ((hash << 1) | (hash >> 7)) ^ theCard.filename[l]; // rotate, xor
  267. static uint8_t filename_scroll_hash;
  268. if (filename_scroll_hash != hash) { // If the hash changed...
  269. filename_scroll_hash = hash; // Save the new hash
  270. filename_scroll_max = _MAX(0, utf8_strlen(theCard.longFilename) - maxlen); // Update the scroll limit
  271. filename_scroll_pos = 0; // Reset scroll to the start
  272. lcd_status_update_delay = 8; // Don't scroll right away
  273. }
  274. // Advance byte position corresponding to filename_scroll_pos char position
  275. outstr += TERN(UTF_FILENAME_SUPPORT, utf8_byte_pos_by_char_num(outstr, filename_scroll_pos), filename_scroll_pos);
  276. }
  277. #else
  278. theCard.longFilename[
  279. TERN(UTF_FILENAME_SUPPORT, utf8_byte_pos_by_char_num(theCard.longFilename, maxlen), maxlen)
  280. ] = '\0'; // cutoff at screen edge
  281. #endif
  282. }
  283. return outstr;
  284. }
  285. #endif
  286. #if HAS_LCD_MENU
  287. #include "menu/menu.h"
  288. screenFunc_t MarlinUI::currentScreen; // Initialized in CTOR
  289. bool MarlinUI::screen_changed;
  290. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  291. bool MarlinUI::encoderRateMultiplierEnabled;
  292. millis_t MarlinUI::lastEncoderMovementMillis = 0;
  293. void MarlinUI::enable_encoder_multiplier(const bool onoff) {
  294. encoderRateMultiplierEnabled = onoff;
  295. lastEncoderMovementMillis = 0;
  296. }
  297. #endif
  298. #if EITHER(REVERSE_MENU_DIRECTION, REVERSE_SELECT_DIRECTION)
  299. int8_t MarlinUI::encoderDirection = ENCODERBASE;
  300. #endif
  301. #if HAS_TOUCH_BUTTONS
  302. uint8_t MarlinUI::touch_buttons;
  303. uint8_t MarlinUI::repeat_delay;
  304. #endif
  305. #if EITHER(AUTO_BED_LEVELING_UBL, G26_MESH_VALIDATION)
  306. bool MarlinUI::external_control; // = false
  307. void MarlinUI::wait_for_release() {
  308. while (button_pressed()) safe_delay(50);
  309. safe_delay(50);
  310. }
  311. #endif
  312. #if !HAS_GRAPHICAL_TFT
  313. void _wrap_string(uint8_t &col, uint8_t &row, const char * const string, read_byte_cb_t cb_read_byte, bool wordwrap/*=false*/) {
  314. SETCURSOR(col, row);
  315. if (!string) return;
  316. auto _newline = [&col, &row]{
  317. col = 0; row++; // Move col to string len (plus space)
  318. SETCURSOR(0, row); // Simulate carriage return
  319. };
  320. uint8_t *p = (uint8_t*)string;
  321. wchar_t ch;
  322. if (wordwrap) {
  323. uint8_t *wrd = nullptr, c = 0;
  324. // find the end of the part
  325. for (;;) {
  326. if (!wrd) wrd = p; // Get word start /before/ advancing
  327. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  328. const bool eol = !ch; // zero ends the string
  329. // End or a break between phrases?
  330. if (eol || ch == ' ' || ch == '-' || ch == '+' || ch == '.') {
  331. if (!c && ch == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
  332. // Past the right and the word is not too long?
  333. if (col + c > LCD_WIDTH && col >= (LCD_WIDTH) / 4) _newline(); // should it wrap?
  334. c += !eol; // +1 so the space will be printed
  335. col += c; // advance col to new position
  336. while (c) { // character countdown
  337. --c; // count down to zero
  338. wrd = get_utf8_value_cb(wrd, cb_read_byte, &ch); // get characters again
  339. lcd_put_wchar(ch); // character to the LCD
  340. }
  341. if (eol) break; // all done!
  342. wrd = nullptr; // set up for next word
  343. }
  344. else c++; // count word characters
  345. }
  346. }
  347. else {
  348. for (;;) {
  349. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  350. if (!ch) break;
  351. lcd_put_wchar(ch);
  352. col++;
  353. if (col >= LCD_WIDTH) _newline();
  354. }
  355. }
  356. }
  357. void MarlinUI::draw_select_screen_prompt(PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) {
  358. const uint8_t plen = utf8_strlen_P(pref), slen = suff ? utf8_strlen_P(suff) : 0;
  359. uint8_t col = 0, row = 0;
  360. if (!string && plen + slen <= LCD_WIDTH) {
  361. col = (LCD_WIDTH - plen - slen) / 2;
  362. row = LCD_HEIGHT > 3 ? 1 : 0;
  363. }
  364. if (LCD_HEIGHT >= 8) row = LCD_HEIGHT / 2 - 2;
  365. wrap_string_P(col, row, pref, true);
  366. if (string) {
  367. if (col) { col = 0; row++; } // Move to the start of the next line
  368. wrap_string(col, row, string);
  369. }
  370. if (suff) wrap_string_P(col, row, suff);
  371. }
  372. #endif // !HAS_GRAPHICAL_TFT
  373. #endif // HAS_LCD_MENU
  374. ////////////////////////////////////////////
  375. ///////////// Keypad Handling //////////////
  376. ////////////////////////////////////////////
  377. #if IS_RRW_KEYPAD && HAS_ENCODER_ACTION
  378. volatile uint8_t MarlinUI::keypad_buttons;
  379. #if HAS_LCD_MENU && !HAS_ADC_BUTTONS
  380. void lcd_move_x();
  381. void lcd_move_y();
  382. void lcd_move_z();
  383. void _reprapworld_keypad_move(const AxisEnum axis, const int16_t dir) {
  384. ui.manual_move.menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  385. ui.encoderPosition = dir;
  386. switch (axis) {
  387. case X_AXIS: lcd_move_x(); break;
  388. case Y_AXIS: lcd_move_y(); break;
  389. case Z_AXIS: lcd_move_z();
  390. default: break;
  391. }
  392. }
  393. #endif
  394. bool MarlinUI::handle_keypad() {
  395. #if HAS_ADC_BUTTONS
  396. #define ADC_MIN_KEY_DELAY 100
  397. if (keypad_buttons) {
  398. #if HAS_ENCODER_ACTION
  399. refresh(LCDVIEW_REDRAW_NOW);
  400. #if HAS_LCD_MENU
  401. if (encoderDirection == -(ENCODERBASE)) { // HAS_ADC_BUTTONS forces REVERSE_MENU_DIRECTION, so this indicates menu navigation
  402. if (RRK(EN_KEYPAD_UP)) encoderPosition += ENCODER_STEPS_PER_MENU_ITEM;
  403. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition -= ENCODER_STEPS_PER_MENU_ITEM;
  404. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  405. else if (RRK(EN_KEYPAD_RIGHT)) { return_to_status(); quick_feedback(); }
  406. }
  407. else
  408. #endif
  409. {
  410. #if HAS_LCD_MENU
  411. if (RRK(EN_KEYPAD_UP)) encoderPosition -= epps;
  412. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition += epps;
  413. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  414. else if (RRK(EN_KEYPAD_RIGHT)) encoderPosition = 0;
  415. #else
  416. if (RRK(EN_KEYPAD_UP) || RRK(EN_KEYPAD_LEFT)) encoderPosition -= epps;
  417. else if (RRK(EN_KEYPAD_DOWN) || RRK(EN_KEYPAD_RIGHT)) encoderPosition += epps;
  418. #endif
  419. }
  420. #endif
  421. next_button_update_ms = millis() + ADC_MIN_KEY_DELAY;
  422. return true;
  423. }
  424. #else // !HAS_ADC_BUTTONS
  425. static uint8_t keypad_debounce = 0;
  426. if (!RRK( EN_KEYPAD_F1 | EN_KEYPAD_F2
  427. | EN_KEYPAD_F3 | EN_KEYPAD_DOWN
  428. | EN_KEYPAD_RIGHT | EN_KEYPAD_MIDDLE
  429. | EN_KEYPAD_UP | EN_KEYPAD_LEFT )
  430. ) {
  431. if (keypad_debounce > 0) keypad_debounce--;
  432. }
  433. else if (!keypad_debounce) {
  434. keypad_debounce = 2;
  435. const bool homed = all_axes_homed();
  436. #if HAS_LCD_MENU
  437. if (RRK(EN_KEYPAD_MIDDLE)) goto_screen(menu_move);
  438. #if NONE(DELTA, Z_HOME_TO_MAX)
  439. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  440. #endif
  441. if (homed) {
  442. #if EITHER(DELTA, Z_HOME_TO_MAX)
  443. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  444. #endif
  445. if (RRK(EN_KEYPAD_F3)) _reprapworld_keypad_move(Z_AXIS, -1);
  446. if (RRK(EN_KEYPAD_LEFT)) _reprapworld_keypad_move(X_AXIS, -1);
  447. if (RRK(EN_KEYPAD_RIGHT)) _reprapworld_keypad_move(X_AXIS, 1);
  448. if (RRK(EN_KEYPAD_DOWN)) _reprapworld_keypad_move(Y_AXIS, 1);
  449. if (RRK(EN_KEYPAD_UP)) _reprapworld_keypad_move(Y_AXIS, -1);
  450. }
  451. #endif // HAS_LCD_MENU
  452. if (!homed && RRK(EN_KEYPAD_F1)) queue.inject_P(G28_STR);
  453. return true;
  454. }
  455. #endif // !HAS_ADC_BUTTONS
  456. return false;
  457. }
  458. #endif // IS_RRW_KEYPAD && HAS_ENCODER_ACTION
  459. /**
  460. * Status Screen
  461. *
  462. * This is very display-dependent, so the lcd implementation draws this.
  463. */
  464. #if BASIC_PROGRESS_BAR
  465. millis_t MarlinUI::progress_bar_ms; // = 0
  466. #if PROGRESS_MSG_EXPIRE > 0
  467. millis_t MarlinUI::expire_status_ms; // = 0
  468. #endif
  469. #endif
  470. void MarlinUI::status_screen() {
  471. TERN_(HAS_LCD_MENU, ENCODER_RATE_MULTIPLY(false));
  472. #if BASIC_PROGRESS_BAR
  473. //
  474. // HD44780 implements the following message blinking and
  475. // message expiration because Status Line and Progress Bar
  476. // share the same line on the display.
  477. //
  478. #if DISABLED(PROGRESS_MSG_ONCE) || (PROGRESS_MSG_EXPIRE > 0)
  479. #define GOT_MS
  480. const millis_t ms = millis();
  481. #endif
  482. // If the message will blink rather than expire...
  483. #if DISABLED(PROGRESS_MSG_ONCE)
  484. if (ELAPSED(ms, progress_bar_ms + PROGRESS_BAR_MSG_TIME + PROGRESS_BAR_BAR_TIME))
  485. progress_bar_ms = ms;
  486. #endif
  487. #if PROGRESS_MSG_EXPIRE > 0
  488. // Handle message expire
  489. if (expire_status_ms) {
  490. // Expire the message if a job is active and the bar has ticks
  491. if (get_progress_percent() > 2 && !print_job_timer.isPaused()) {
  492. if (ELAPSED(ms, expire_status_ms)) {
  493. status_message[0] = '\0';
  494. expire_status_ms = 0;
  495. }
  496. }
  497. else {
  498. // Defer message expiration before bar appears
  499. // and during any pause (not just SD)
  500. expire_status_ms += LCD_UPDATE_INTERVAL;
  501. }
  502. }
  503. #endif // PROGRESS_MSG_EXPIRE
  504. #endif // BASIC_PROGRESS_BAR
  505. #if HAS_LCD_MENU
  506. if (use_click()) {
  507. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  508. next_filament_display = millis() + 5000UL; // Show status message for 5s
  509. #endif
  510. goto_screen(menu_main);
  511. IF_DISABLED(NO_LCD_REINIT, init_lcd()); // May revive the LCD if static electricity killed it
  512. return;
  513. }
  514. #endif
  515. #if ENABLED(ULTIPANEL_FEEDMULTIPLY)
  516. const int16_t old_frm = feedrate_percentage;
  517. int16_t new_frm = old_frm + int16_t(encoderPosition);
  518. // Dead zone at 100% feedrate
  519. if (old_frm == 100) {
  520. if (int16_t(encoderPosition) > ENCODER_FEEDRATE_DEADZONE)
  521. new_frm -= ENCODER_FEEDRATE_DEADZONE;
  522. else if (int16_t(encoderPosition) < -(ENCODER_FEEDRATE_DEADZONE))
  523. new_frm += ENCODER_FEEDRATE_DEADZONE;
  524. else
  525. new_frm = old_frm;
  526. }
  527. else if ((old_frm < 100 && new_frm > 100) || (old_frm > 100 && new_frm < 100))
  528. new_frm = 100;
  529. LIMIT(new_frm, 10, 999);
  530. if (old_frm != new_frm) {
  531. feedrate_percentage = new_frm;
  532. encoderPosition = 0;
  533. #if BOTH(HAS_BUZZER, BEEP_ON_FEEDRATE_CHANGE)
  534. static millis_t next_beep;
  535. #ifndef GOT_MS
  536. const millis_t ms = millis();
  537. #endif
  538. if (ELAPSED(ms, next_beep)) {
  539. buzz(FEEDRATE_CHANGE_BEEP_DURATION, FEEDRATE_CHANGE_BEEP_FREQUENCY);
  540. next_beep = ms + 500UL;
  541. }
  542. #endif
  543. }
  544. #endif // ULTIPANEL_FEEDMULTIPLY
  545. draw_status_screen();
  546. }
  547. void MarlinUI::kill_screen(FSTR_P const lcd_error, FSTR_P const lcd_component) {
  548. init();
  549. status_printf(1, F(S_FMT ": " S_FMT), FTOP(lcd_error), FTOP(lcd_component));
  550. TERN_(HAS_LCD_MENU, return_to_status());
  551. // RED ALERT. RED ALERT.
  552. #ifdef LED_BACKLIGHT_TIMEOUT
  553. leds.set_color(LEDColorRed());
  554. #ifdef NEOPIXEL_BKGD_INDEX_FIRST
  555. neo.set_background_color(255, 0, 0, 0);
  556. neo.show();
  557. #endif
  558. #endif
  559. draw_kill_screen();
  560. }
  561. #if HAS_TOUCH_SLEEP
  562. #if HAS_TOUCH_BUTTONS
  563. #include "touch/touch_buttons.h"
  564. #else
  565. #include "tft/touch.h"
  566. #endif
  567. // Wake up a sleeping TFT
  568. void MarlinUI::wakeup_screen() {
  569. TERN(HAS_TOUCH_BUTTONS, touchBt.wakeUp(), touch.wakeUp());
  570. }
  571. #endif
  572. void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
  573. TERN_(HAS_TOUCH_SLEEP, wakeup_screen()); // Wake up the TFT with most buttons
  574. TERN_(HAS_LCD_MENU, refresh());
  575. #if HAS_ENCODER_ACTION
  576. if (clear_buttons) buttons = 0;
  577. next_button_update_ms = millis() + 500;
  578. #else
  579. UNUSED(clear_buttons);
  580. #endif
  581. #if HAS_CHIRP
  582. chirp(); // Buzz and wait. Is the delay needed for buttons to settle?
  583. #if BOTH(HAS_LCD_MENU, USE_BEEPER)
  584. for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
  585. #elif HAS_LCD_MENU
  586. delay(10);
  587. #endif
  588. #endif
  589. }
  590. ////////////////////////////////////////////
  591. /////////////// Manual Move ////////////////
  592. ////////////////////////////////////////////
  593. #if HAS_LCD_MENU
  594. ManualMove MarlinUI::manual_move{};
  595. millis_t ManualMove::start_time = 0;
  596. float ManualMove::menu_scale = 1;
  597. #if IS_KINEMATIC
  598. float ManualMove::offset = 0;
  599. xyze_pos_t ManualMove::all_axes_destination = { 0 };
  600. bool ManualMove::processing = false;
  601. #endif
  602. #if MULTI_E_MANUAL
  603. int8_t ManualMove::e_index = 0;
  604. #endif
  605. AxisEnum ManualMove::axis = NO_AXIS_ENUM;
  606. /**
  607. * If a manual move has been posted and its time has arrived, and if the planner
  608. * has a space for it, then add a linear move to current_position the planner.
  609. *
  610. * If any manual move needs to be interrupted, make sure to force a manual move
  611. * by setting manual_move.start_time to millis() after updating current_position.
  612. *
  613. * To post a manual move:
  614. * - Update current_position to the new place you want to go.
  615. * - Set manual_move.axis to an axis like X_AXIS. Use ALL_AXES_ENUM for diagonal moves.
  616. * - Set manual_move.start_time to a point in the future (in ms) when the move should be done.
  617. *
  618. * For kinematic machines:
  619. * - Set manual_move.offset to modify one axis and post the move.
  620. * This is used to achieve more rapid stepping on kinematic machines.
  621. *
  622. * Currently used by the _lcd_move_xyz function in menu_motion.cpp
  623. * and the ubl_map_move_to_xy function in menu_ubl.cpp.
  624. */
  625. void ManualMove::task() {
  626. if (processing) return; // Prevent re-entry from idle() calls
  627. // Add a manual move to the queue?
  628. if (axis != NO_AXIS_ENUM && ELAPSED(millis(), start_time) && !planner.is_full()) {
  629. const feedRate_t fr_mm_s = (axis < LOGICAL_AXES) ? manual_feedrate_mm_s[axis] : XY_PROBE_FEEDRATE_MM_S;
  630. #if IS_KINEMATIC
  631. #if HAS_MULTI_EXTRUDER
  632. REMEMBER(ae, active_extruder);
  633. #if MULTI_E_MANUAL
  634. if (axis == E_AXIS) active_extruder = e_index;
  635. #endif
  636. #endif
  637. // Apply a linear offset to a single axis
  638. if (axis == ALL_AXES_ENUM)
  639. destination = all_axes_destination;
  640. else if (axis <= XYZE) {
  641. destination = current_position;
  642. destination[axis] += offset;
  643. }
  644. // Reset for the next move
  645. offset = 0;
  646. axis = NO_AXIS_ENUM;
  647. // DELTA and SCARA machines use segmented moves, which could fill the planner during the call to
  648. // move_to_destination. This will cause idle() to be called, which can then call this function while the
  649. // previous invocation is being blocked. Modifications to offset shouldn't be made while
  650. // processing is true or the planner will get out of sync.
  651. processing = true;
  652. prepare_internal_move_to_destination(fr_mm_s); // will set current_position from destination
  653. processing = false;
  654. #else
  655. // For Cartesian / Core motion simply move to the current_position
  656. planner.buffer_line(current_position, fr_mm_s,
  657. TERN_(MULTI_E_MANUAL, axis == E_AXIS ? e_index :) active_extruder
  658. );
  659. //SERIAL_ECHOLNPGM("Add planner.move with Axis ", AS_CHAR(axis_codes[axis]), " at FR ", fr_mm_s);
  660. axis = NO_AXIS_ENUM;
  661. #endif
  662. }
  663. }
  664. //
  665. // Tell ui.update() to start a move to current_position after a short delay.
  666. //
  667. void ManualMove::soon(const AxisEnum move_axis
  668. OPTARG(MULTI_E_MANUAL, const int8_t eindex/*=active_extruder*/)
  669. ) {
  670. TERN_(MULTI_E_MANUAL, if (move_axis == E_AXIS) e_index = eindex);
  671. start_time = millis() + (menu_scale < 0.99f ? 0UL : 250UL); // delay for bigger moves
  672. axis = move_axis;
  673. //SERIAL_ECHOLNPGM("Post Move with Axis ", AS_CHAR(axis_codes[axis]), " soon.");
  674. }
  675. #if ENABLED(AUTO_BED_LEVELING_UBL)
  676. void MarlinUI::external_encoder() {
  677. if (external_control && encoderDiff) {
  678. ubl.encoder_diff += encoderDiff; // Encoder for UBL G29 mesh editing
  679. encoderDiff = 0; // Hide encoder events from the screen handler
  680. refresh(LCDVIEW_REDRAW_NOW); // ...but keep the refresh.
  681. }
  682. }
  683. #endif
  684. #endif // HAS_LCD_MENU
  685. /**
  686. * Update the LCD, read encoder buttons, etc.
  687. * - Read button states
  688. * - Check the SD Card slot state
  689. * - Act on RepRap World keypad input
  690. * - Update the encoder position
  691. * - Apply acceleration to the encoder position
  692. * - Do refresh(LCDVIEW_CALL_REDRAW_NOW) on controller events
  693. * - Reset the Info Screen timeout if there's any input
  694. * - Update status indicators, if any
  695. *
  696. * Run the current LCD menu handler callback function:
  697. * - Call the handler only if lcdDrawUpdate != LCDVIEW_NONE
  698. * - Before calling the handler, LCDVIEW_CALL_NO_REDRAW => LCDVIEW_NONE
  699. * - Call the menu handler. Menu handlers should do the following:
  700. * - If a value changes, set lcdDrawUpdate to LCDVIEW_REDRAW_NOW and draw the value
  701. * (Encoder events automatically set lcdDrawUpdate for you.)
  702. * - if (should_draw()) { redraw }
  703. * - Before exiting the handler set lcdDrawUpdate to:
  704. * - LCDVIEW_CLEAR_CALL_REDRAW to clear screen and set LCDVIEW_CALL_REDRAW_NEXT.
  705. * - LCDVIEW_REDRAW_NOW to draw now (including remaining stripes).
  706. * - LCDVIEW_CALL_REDRAW_NEXT to draw now and get LCDVIEW_REDRAW_NOW on the next loop.
  707. * - LCDVIEW_CALL_NO_REDRAW to draw now and get LCDVIEW_NONE on the next loop.
  708. * - NOTE: For graphical displays menu handlers may be called 2 or more times per loop,
  709. * so don't change lcdDrawUpdate without considering this.
  710. *
  711. * After the menu handler callback runs (or not):
  712. * - Clear the LCD if lcdDrawUpdate == LCDVIEW_CLEAR_CALL_REDRAW
  713. * - Update lcdDrawUpdate for the next loop (i.e., move one state down, usually)
  714. *
  715. * This function is only called from the main thread.
  716. */
  717. LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
  718. millis_t next_lcd_update_ms;
  719. inline bool can_encode() {
  720. return !BUTTON_PRESSED(ENC_EN); // Update encoder only when ENC_EN is not LOW (pressed)
  721. }
  722. void MarlinUI::update() {
  723. static uint16_t max_display_update_time = 0;
  724. millis_t ms = millis();
  725. #if ENABLED(PSU_CONTROL) && defined(LED_BACKLIGHT_TIMEOUT)
  726. leds.update_timeout(powerManager.psu_on);
  727. #endif
  728. #if HAS_LCD_MENU
  729. // Handle any queued Move Axis motion
  730. manual_move.task();
  731. // Update button states for button_pressed(), etc.
  732. // If the state changes the next update may be delayed 300-500ms.
  733. update_buttons();
  734. // If the action button is pressed...
  735. static bool wait_for_unclick; // = false
  736. auto do_click = [&]{
  737. wait_for_unclick = true; // - Set debounce flag to ignore continuous clicks
  738. lcd_clicked = !wait_for_user; // - Keep the click if not waiting for a user-click
  739. wait_for_user = false; // - Any click clears wait for user
  740. quick_feedback(); // - Always make a click sound
  741. };
  742. #if HAS_TOUCH_BUTTONS
  743. if (touch_buttons) {
  744. reset_status_timeout(ms);
  745. if (touch_buttons & (EN_A | EN_B)) { // Menu arrows, in priority
  746. if (ELAPSED(ms, next_button_update_ms)) {
  747. encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * epps * encoderDirection;
  748. if (touch_buttons & EN_A) encoderDiff *= -1;
  749. TERN_(AUTO_BED_LEVELING_UBL, external_encoder());
  750. next_button_update_ms = ms + repeat_delay; // Assume the repeat delay
  751. if (!wait_for_unclick) {
  752. next_button_update_ms += 250; // Longer delay on first press
  753. wait_for_unclick = true; // Avoid Back/Select click while repeating
  754. chirp();
  755. }
  756. }
  757. }
  758. else if (!wait_for_unclick && (buttons & EN_C)) // OK button, if not waiting for a debounce release:
  759. do_click();
  760. }
  761. // keep wait_for_unclick value
  762. #endif
  763. if (!touch_buttons) {
  764. // Integrated LCD click handling via button_pressed
  765. if (!external_control && button_pressed()) {
  766. if (!wait_for_unclick) do_click(); // Handle the click
  767. }
  768. else
  769. wait_for_unclick = false;
  770. }
  771. if (LCD_BACK_CLICKED()) {
  772. quick_feedback();
  773. goto_previous_screen();
  774. }
  775. #endif // HAS_LCD_MENU
  776. if (ELAPSED(ms, next_lcd_update_ms) || TERN0(HAS_MARLINUI_U8GLIB, drawing_screen)) {
  777. next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL;
  778. #if HAS_TOUCH_BUTTONS
  779. if (on_status_screen()) next_lcd_update_ms += (LCD_UPDATE_INTERVAL) * 2;
  780. TERN_(HAS_ENCODER_ACTION, touch_buttons = touchBt.read_buttons());
  781. #endif
  782. TERN_(LCD_HAS_STATUS_INDICATORS, update_indicators());
  783. #if HAS_ENCODER_ACTION
  784. TERN_(HAS_SLOW_BUTTONS, slow_buttons = read_slow_buttons()); // Buttons that take too long to read in interrupt context
  785. if (TERN0(IS_RRW_KEYPAD, handle_keypad()))
  786. reset_status_timeout(ms);
  787. uint8_t abs_diff = ABS(encoderDiff);
  788. #if ENCODER_PULSES_PER_STEP > 1
  789. // When reversing the encoder direction, a movement step can be missed because
  790. // encoderDiff has a non-zero residual value, making the controller unresponsive.
  791. // The fix clears the residual value when the encoder is idle.
  792. // Also check if past half the threshold to compensate for missed single steps.
  793. static int8_t lastEncoderDiff;
  794. // Timeout? No decoder change since last check. 10 or 20 times per second.
  795. if (encoderDiff == lastEncoderDiff && abs_diff <= epps / 2) // Same direction & size but not over a half-step?
  796. encoderDiff = 0; // Clear residual pulses.
  797. else if (WITHIN(abs_diff, epps / 2 + 1, epps - 1)) { // Past half of threshold?
  798. abs_diff = epps; // Treat as a full step size
  799. encoderDiff = (encoderDiff < 0 ? -1 : 1) * abs_diff; // ...in the spin direction.
  800. }
  801. TERN_(HAS_TOUCH_SLEEP, if (lastEncoderDiff != encoderDiff) wakeup_screen());
  802. lastEncoderDiff = encoderDiff;
  803. #endif
  804. const bool encoderPastThreshold = (abs_diff >= epps);
  805. if (encoderPastThreshold || lcd_clicked) {
  806. if (encoderPastThreshold && TERN1(IS_TFTGLCD_PANEL, !external_control)) {
  807. #if BOTH(HAS_LCD_MENU, ENCODER_RATE_MULTIPLIER)
  808. int32_t encoderMultiplier = 1;
  809. if (encoderRateMultiplierEnabled) {
  810. const float encoderMovementSteps = float(abs_diff) / epps;
  811. if (lastEncoderMovementMillis) {
  812. // Note that the rate is always calculated between two passes through the
  813. // loop and that the abs of the encoderDiff value is tracked.
  814. const float encoderStepRate = encoderMovementSteps / float(ms - lastEncoderMovementMillis) * 1000;
  815. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  816. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  817. // Enable to output the encoder steps per second value
  818. //#define ENCODER_RATE_MULTIPLIER_DEBUG
  819. #if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG)
  820. SERIAL_ECHO_START();
  821. SERIAL_ECHOPGM("Enc Step Rate: ", encoderStepRate);
  822. SERIAL_ECHOPGM(" Multiplier: ", encoderMultiplier);
  823. SERIAL_ECHOPGM(" ENCODER_10X_STEPS_PER_SEC: ", ENCODER_10X_STEPS_PER_SEC);
  824. SERIAL_ECHOPGM(" ENCODER_100X_STEPS_PER_SEC: ", ENCODER_100X_STEPS_PER_SEC);
  825. SERIAL_EOL();
  826. #endif
  827. }
  828. lastEncoderMovementMillis = ms;
  829. } // encoderRateMultiplierEnabled
  830. #else
  831. constexpr int32_t encoderMultiplier = 1;
  832. #endif // ENCODER_RATE_MULTIPLIER
  833. if (can_encode()) encoderPosition += (encoderDiff * encoderMultiplier) / epps;
  834. encoderDiff = 0;
  835. }
  836. reset_status_timeout(ms);
  837. refresh(LCDVIEW_REDRAW_NOW);
  838. #if ENABLED(PSU_CONTROL) && defined(LED_BACKLIGHT_TIMEOUT)
  839. if (!powerManager.psu_on) leds.reset_timeout(ms);
  840. #endif
  841. }
  842. #endif
  843. // This runs every ~100ms when idling often enough.
  844. // Instead of tracking changes just redraw the Status Screen once per second.
  845. if (on_status_screen() && !lcd_status_update_delay--) {
  846. lcd_status_update_delay = TERN(HAS_MARLINUI_U8GLIB, 12, 9);
  847. if (max_display_update_time) max_display_update_time--; // Be sure never go to a very big number
  848. refresh(LCDVIEW_REDRAW_NOW);
  849. }
  850. #if BOTH(HAS_LCD_MENU, SCROLL_LONG_FILENAMES)
  851. // If scrolling of long file names is enabled and we are in the sd card menu,
  852. // cause a refresh to occur until all the text has scrolled into view.
  853. if (currentScreen == menu_media && !lcd_status_update_delay--) {
  854. lcd_status_update_delay = ++filename_scroll_pos >= filename_scroll_max ? 12 : 4; // Long delay at end and start
  855. if (filename_scroll_pos > filename_scroll_max) filename_scroll_pos = 0;
  856. refresh(LCDVIEW_REDRAW_NOW);
  857. reset_status_timeout(ms);
  858. }
  859. #endif
  860. // Then we want to use only 50% of the time
  861. const uint16_t bbr2 = planner.block_buffer_runtime() >> 1;
  862. if ((should_draw() || drawing_screen) && (!bbr2 || bbr2 > max_display_update_time)) {
  863. // Change state of drawing flag between screen updates
  864. if (!drawing_screen) switch (lcdDrawUpdate) {
  865. case LCDVIEW_CALL_NO_REDRAW:
  866. refresh(LCDVIEW_NONE);
  867. break;
  868. case LCDVIEW_CLEAR_CALL_REDRAW:
  869. case LCDVIEW_CALL_REDRAW_NEXT:
  870. refresh(LCDVIEW_REDRAW_NOW);
  871. case LCDVIEW_REDRAW_NOW: // set above, or by a handler through LCDVIEW_CALL_REDRAW_NEXT
  872. case LCDVIEW_NONE:
  873. break;
  874. } // switch
  875. TERN_(HAS_ADC_BUTTONS, keypad_buttons = 0);
  876. #if HAS_MARLINUI_U8GLIB
  877. #if ENABLED(LIGHTWEIGHT_UI)
  878. const bool in_status = on_status_screen(),
  879. do_u8g_loop = !in_status;
  880. lcd_in_status(in_status);
  881. if (in_status) status_screen();
  882. #else
  883. constexpr bool do_u8g_loop = true;
  884. #endif
  885. if (do_u8g_loop) {
  886. if (!drawing_screen) { // If not already drawing pages
  887. u8g.firstPage(); // Start the first page
  888. drawing_screen = first_page = true; // Flag as drawing pages
  889. }
  890. set_font(FONT_MENU); // Setup font for every page draw
  891. u8g.setColorIndex(1); // And reset the color
  892. run_current_screen(); // Draw and process the current screen
  893. first_page = false;
  894. // The screen handler can clear drawing_screen for an action that changes the screen.
  895. // If still drawing and there's another page, update max-time and return now.
  896. // The nextPage will already be set up on the next call.
  897. if (drawing_screen && (drawing_screen = u8g.nextPage())) {
  898. if (on_status_screen())
  899. NOLESS(max_display_update_time, millis() - ms);
  900. return;
  901. }
  902. }
  903. #else
  904. run_current_screen();
  905. // Apply all DWIN drawing after processing
  906. TERN_(IS_DWIN_MARLINUI, DWIN_UpdateLCD());
  907. #endif
  908. TERN_(HAS_LCD_MENU, lcd_clicked = false);
  909. // Keeping track of the longest time for an individual LCD update.
  910. // Used to do screen throttling when the planner starts to fill up.
  911. if (on_status_screen())
  912. NOLESS(max_display_update_time, millis() - ms);
  913. }
  914. #if SCREENS_CAN_TIME_OUT
  915. // Return to Status Screen after a timeout
  916. if (on_status_screen() || defer_return_to_status)
  917. reset_status_timeout(ms);
  918. else if (ELAPSED(ms, return_to_status_ms))
  919. return_to_status();
  920. #endif
  921. // Change state of drawing flag between screen updates
  922. if (!drawing_screen) switch (lcdDrawUpdate) {
  923. case LCDVIEW_CLEAR_CALL_REDRAW:
  924. clear_lcd(); break;
  925. case LCDVIEW_REDRAW_NOW:
  926. refresh(LCDVIEW_NONE);
  927. case LCDVIEW_NONE:
  928. case LCDVIEW_CALL_REDRAW_NEXT:
  929. case LCDVIEW_CALL_NO_REDRAW:
  930. default: break;
  931. } // switch
  932. } // ELAPSED(ms, next_lcd_update_ms)
  933. TERN_(HAS_GRAPHICAL_TFT, tft_idle());
  934. }
  935. #if HAS_ADC_BUTTONS
  936. typedef struct {
  937. uint16_t ADCKeyValueMin, ADCKeyValueMax;
  938. uint8_t ADCKeyNo;
  939. } _stADCKeypadTable_;
  940. #ifndef ADC_BUTTONS_VALUE_SCALE
  941. #define ADC_BUTTONS_VALUE_SCALE 1.0 // for the power voltage equal to the reference voltage
  942. #endif
  943. #ifndef ADC_BUTTONS_R_PULLUP
  944. #define ADC_BUTTONS_R_PULLUP 4.7 // common pull-up resistor in the voltage divider
  945. #endif
  946. #ifndef ADC_BUTTONS_LEFT_R_PULLDOWN
  947. #define ADC_BUTTONS_LEFT_R_PULLDOWN 0.47 // pull-down resistor for LEFT button voltage divider
  948. #endif
  949. #ifndef ADC_BUTTONS_RIGHT_R_PULLDOWN
  950. #define ADC_BUTTONS_RIGHT_R_PULLDOWN 4.7 // pull-down resistor for RIGHT button voltage divider
  951. #endif
  952. #ifndef ADC_BUTTONS_UP_R_PULLDOWN
  953. #define ADC_BUTTONS_UP_R_PULLDOWN 1.0 // pull-down resistor for UP button voltage divider
  954. #endif
  955. #ifndef ADC_BUTTONS_DOWN_R_PULLDOWN
  956. #define ADC_BUTTONS_DOWN_R_PULLDOWN 10.0 // pull-down resistor for DOWN button voltage divider
  957. #endif
  958. #ifndef ADC_BUTTONS_MIDDLE_R_PULLDOWN
  959. #define ADC_BUTTONS_MIDDLE_R_PULLDOWN 2.2 // pull-down resistor for MIDDLE button voltage divider
  960. #endif
  961. // Calculate the ADC value for the voltage divider with specified pull-down resistor value
  962. #define ADC_BUTTON_VALUE(r) int(HAL_ADC_RANGE * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP))
  963. static constexpr uint16_t adc_button_tolerance = HAL_ADC_RANGE * 25 / 1024,
  964. adc_other_button = HAL_ADC_RANGE * 1000 / 1024;
  965. static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
  966. // VALUE_MIN, VALUE_MAX, KEY
  967. { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F1 }, // F1
  968. { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F2 }, // F2
  969. { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F3 }, // F3
  970. { ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) - adc_button_tolerance,
  971. ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_LEFT }, // LEFT ( 272 ... 472)
  972. { ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) - adc_button_tolerance,
  973. ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_RIGHT }, // RIGHT (1948 ... 2148)
  974. { ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) - adc_button_tolerance,
  975. ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_UP }, // UP ( 618 ... 818)
  976. { ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) - adc_button_tolerance,
  977. ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_DOWN }, // DOWN (2686 ... 2886)
  978. { ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) - adc_button_tolerance,
  979. ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_MIDDLE }, // ENTER (1205 ... 1405)
  980. };
  981. uint8_t get_ADC_keyValue() {
  982. if (thermalManager.ADCKey_count >= 16) {
  983. const uint16_t currentkpADCValue = thermalManager.current_ADCKey_raw;
  984. thermalManager.current_ADCKey_raw = HAL_ADC_RANGE;
  985. thermalManager.ADCKey_count = 0;
  986. if (currentkpADCValue < adc_other_button)
  987. LOOP_L_N(i, ADC_KEY_NUM) {
  988. const uint16_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
  989. hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
  990. if (WITHIN(currentkpADCValue, lo, hi)) return pgm_read_byte(&stADCKeyTable[i].ADCKeyNo);
  991. }
  992. }
  993. return 0;
  994. }
  995. #endif // HAS_ADC_BUTTONS
  996. #if HAS_ENCODER_ACTION
  997. /**
  998. * Read encoder buttons from the hardware registers
  999. * Warning: This function is called from interrupt context!
  1000. */
  1001. void MarlinUI::update_buttons() {
  1002. const millis_t now = millis();
  1003. if (ELAPSED(now, next_button_update_ms)) {
  1004. #if HAS_DIGITAL_BUTTONS
  1005. #if ANY_BUTTON(EN1, EN2, ENC, BACK)
  1006. uint8_t newbutton = 0;
  1007. if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
  1008. if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
  1009. if (can_encode() && BUTTON_PRESSED(ENC)) newbutton |= EN_C;
  1010. if (BUTTON_PRESSED(BACK)) newbutton |= EN_D;
  1011. #else
  1012. constexpr uint8_t newbutton = 0;
  1013. #endif
  1014. //
  1015. // Directional buttons
  1016. //
  1017. #if ANY_BUTTON(UP, DWN, LFT, RT)
  1018. const int8_t pulses = epps * encoderDirection;
  1019. if (BUTTON_PRESSED(UP)) {
  1020. encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * pulses;
  1021. next_button_update_ms = now + 300;
  1022. }
  1023. else if (BUTTON_PRESSED(DWN)) {
  1024. encoderDiff = -(ENCODER_STEPS_PER_MENU_ITEM) * pulses;
  1025. next_button_update_ms = now + 300;
  1026. }
  1027. else if (BUTTON_PRESSED(LFT)) {
  1028. encoderDiff = -pulses;
  1029. next_button_update_ms = now + 300;
  1030. }
  1031. else if (BUTTON_PRESSED(RT)) {
  1032. encoderDiff = pulses;
  1033. next_button_update_ms = now + 300;
  1034. }
  1035. #endif // UP || DWN || LFT || RT
  1036. buttons = (newbutton | TERN0(HAS_SLOW_BUTTONS, slow_buttons)
  1037. #if BOTH(HAS_TOUCH_BUTTONS, HAS_ENCODER_ACTION)
  1038. | (touch_buttons & TERN(HAS_ENCODER_WHEEL, ~(EN_A | EN_B), 0xFF))
  1039. #endif
  1040. );
  1041. #elif HAS_ADC_BUTTONS
  1042. buttons = 0;
  1043. #endif
  1044. #if HAS_ADC_BUTTONS
  1045. if (keypad_buttons == 0) {
  1046. const uint8_t b = get_ADC_keyValue();
  1047. if (WITHIN(b, 1, 8)) keypad_buttons = _BV(b - 1);
  1048. }
  1049. #endif
  1050. #if HAS_SHIFT_ENCODER
  1051. /**
  1052. * Set up Rotary Encoder bit values (for two pin encoders to indicate movement).
  1053. * These values are independent of which pins are used for EN_A / EN_B indications.
  1054. * The rotary encoder part is also independent of the LCD chipset.
  1055. */
  1056. uint8_t val = 0;
  1057. WRITE(SHIFT_LD_PIN, LOW);
  1058. WRITE(SHIFT_LD_PIN, HIGH);
  1059. LOOP_L_N(i, 8) {
  1060. val >>= 1;
  1061. if (READ(SHIFT_OUT_PIN)) SBI(val, 7);
  1062. WRITE(SHIFT_CLK_PIN, HIGH);
  1063. WRITE(SHIFT_CLK_PIN, LOW);
  1064. }
  1065. TERN(REPRAPWORLD_KEYPAD, keypad_buttons, buttons) = ~val;
  1066. #endif
  1067. #if IS_TFTGLCD_PANEL
  1068. next_button_update_ms = now + (LCD_UPDATE_INTERVAL / 2);
  1069. buttons = slow_buttons;
  1070. TERN_(AUTO_BED_LEVELING_UBL, external_encoder());
  1071. #endif
  1072. } // next_button_update_ms
  1073. #if HAS_ENCODER_WHEEL
  1074. static uint8_t lastEncoderBits;
  1075. // Manage encoder rotation
  1076. #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; }
  1077. uint8_t enc = 0;
  1078. if (buttons & EN_A) enc |= B01;
  1079. if (buttons & EN_B) enc |= B10;
  1080. if (enc != lastEncoderBits) {
  1081. switch (enc) {
  1082. case ENCODER_PHASE_0: ENCODER_SPIN(ENCODER_PHASE_3, ENCODER_PHASE_1); break;
  1083. case ENCODER_PHASE_1: ENCODER_SPIN(ENCODER_PHASE_0, ENCODER_PHASE_2); break;
  1084. case ENCODER_PHASE_2: ENCODER_SPIN(ENCODER_PHASE_1, ENCODER_PHASE_3); break;
  1085. case ENCODER_PHASE_3: ENCODER_SPIN(ENCODER_PHASE_2, ENCODER_PHASE_0); break;
  1086. }
  1087. #if BOTH(HAS_LCD_MENU, AUTO_BED_LEVELING_UBL)
  1088. external_encoder();
  1089. #endif
  1090. lastEncoderBits = enc;
  1091. }
  1092. #endif // HAS_ENCODER_WHEEL
  1093. }
  1094. #endif // HAS_ENCODER_ACTION
  1095. #endif // HAS_WIRED_LCD
  1096. #if HAS_STATUS_MESSAGE
  1097. ////////////////////////////////////////////
  1098. ////////////// Status Message //////////////
  1099. ////////////////////////////////////////////
  1100. #if ENABLED(EXTENSIBLE_UI)
  1101. #include "extui/ui_api.h"
  1102. #endif
  1103. bool MarlinUI::has_status() { return (status_message[0] != '\0'); }
  1104. void MarlinUI::set_status(const char * const cstr, const bool persist) {
  1105. if (alert_level) return;
  1106. TERN_(HOST_PROMPT_SUPPORT, hostui.notify(cstr));
  1107. // Here we have a problem. The message is encoded in UTF8, so
  1108. // arbitrarily cutting it will be a problem. We MUST be sure
  1109. // that there is no cutting in the middle of a multibyte character!
  1110. // Get a pointer to the null terminator
  1111. const char* pend = cstr + strlen(cstr);
  1112. // If length of supplied UTF8 string is greater than
  1113. // our buffer size, start cutting whole UTF8 chars
  1114. while ((pend - cstr) > MAX_MESSAGE_LENGTH) {
  1115. --pend;
  1116. while (!START_OF_UTF8_CHAR(*pend)) --pend;
  1117. };
  1118. // At this point, we have the proper cut point. Use it
  1119. uint8_t maxLen = pend - cstr;
  1120. strncpy(status_message, cstr, maxLen);
  1121. status_message[maxLen] = '\0';
  1122. finish_status(persist);
  1123. }
  1124. /**
  1125. * Reset the status message
  1126. */
  1127. void MarlinUI::reset_status(const bool no_welcome) {
  1128. #if SERVICE_INTERVAL_1 > 0
  1129. static PGMSTR(service1, "> " SERVICE_NAME_1 "!");
  1130. #endif
  1131. #if SERVICE_INTERVAL_2 > 0
  1132. static PGMSTR(service2, "> " SERVICE_NAME_2 "!");
  1133. #endif
  1134. #if SERVICE_INTERVAL_3 > 0
  1135. static PGMSTR(service3, "> " SERVICE_NAME_3 "!");
  1136. #endif
  1137. FSTR_P msg;
  1138. if (printingIsPaused())
  1139. msg = GET_TEXT_F(MSG_PRINT_PAUSED);
  1140. #if ENABLED(SDSUPPORT)
  1141. else if (IS_SD_PRINTING())
  1142. return set_status(card.longest_filename(), true);
  1143. #endif
  1144. else if (print_job_timer.isRunning())
  1145. msg = GET_TEXT_F(MSG_PRINTING);
  1146. #if SERVICE_INTERVAL_1 > 0
  1147. else if (print_job_timer.needsService(1)) msg = FPSTR(service1);
  1148. #endif
  1149. #if SERVICE_INTERVAL_2 > 0
  1150. else if (print_job_timer.needsService(2)) msg = FPSTR(service2);
  1151. #endif
  1152. #if SERVICE_INTERVAL_3 > 0
  1153. else if (print_job_timer.needsService(3)) msg = FPSTR(service3);
  1154. #endif
  1155. else if (!no_welcome)
  1156. msg = GET_TEXT_F(WELCOME_MSG);
  1157. else
  1158. return;
  1159. set_status(msg, -1);
  1160. }
  1161. void MarlinUI::set_status(FSTR_P const fstr, int8_t level) {
  1162. PGM_P const pstr = FTOP(fstr);
  1163. if (level < 0) level = alert_level = 0;
  1164. if (level < alert_level) return;
  1165. alert_level = level;
  1166. TERN_(HOST_PROMPT_SUPPORT, hostui.notify(fstr));
  1167. // Since the message is encoded in UTF8 it must
  1168. // only be cut on a character boundary.
  1169. // Get a pointer to the null terminator
  1170. PGM_P pend = pstr + strlen_P(pstr);
  1171. // If length of supplied UTF8 string is greater than
  1172. // the buffer size, start cutting whole UTF8 chars
  1173. while ((pend - pstr) > MAX_MESSAGE_LENGTH) {
  1174. --pend;
  1175. while (!START_OF_UTF8_CHAR(pgm_read_byte(pend))) --pend;
  1176. };
  1177. // At this point, we have the proper cut point. Use it
  1178. uint8_t maxLen = pend - pstr;
  1179. strncpy_P(status_message, pstr, maxLen);
  1180. status_message[maxLen] = '\0';
  1181. finish_status(level > 0);
  1182. }
  1183. void MarlinUI::set_alert_status(FSTR_P const fstr) {
  1184. set_status(fstr, 1);
  1185. TERN_(HAS_TOUCH_SLEEP, wakeup_screen());
  1186. TERN_(HAS_LCD_MENU, return_to_status());
  1187. }
  1188. #include <stdarg.h>
  1189. void MarlinUI::status_printf(const uint8_t level, FSTR_P const fmt, ...) {
  1190. if (level < alert_level) return;
  1191. alert_level = level;
  1192. va_list args;
  1193. va_start(args, FTOP(fmt));
  1194. vsnprintf_P(status_message, MAX_MESSAGE_LENGTH, FTOP(fmt), args);
  1195. va_end(args);
  1196. finish_status(level > 0);
  1197. }
  1198. void MarlinUI::finish_status(const bool persist) {
  1199. UNUSED(persist);
  1200. #if HAS_WIRED_LCD
  1201. #if BASIC_PROGRESS_BAR || BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  1202. const millis_t ms = millis();
  1203. #endif
  1204. #if BASIC_PROGRESS_BAR
  1205. progress_bar_ms = ms;
  1206. #if PROGRESS_MSG_EXPIRE > 0
  1207. expire_status_ms = persist ? 0 : ms + PROGRESS_MSG_EXPIRE;
  1208. #endif
  1209. #endif
  1210. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  1211. next_filament_display = ms + 5000UL; // Show status message for 5s
  1212. #endif
  1213. #endif
  1214. #if ENABLED(STATUS_MESSAGE_SCROLLING) && EITHER(HAS_WIRED_LCD, DWIN_CREALITY_LCD_ENHANCED)
  1215. status_scroll_offset = 0;
  1216. #endif
  1217. TERN_(EXTENSIBLE_UI, ExtUI::onStatusChanged(status_message));
  1218. TERN_(DWIN_CREALITY_LCD, DWIN_StatusChanged(status_message));
  1219. TERN_(DWIN_CREALITY_LCD_ENHANCED, DWIN_CheckStatusMessage());
  1220. TERN_(DWIN_CREALITY_LCD_JYERSUI, CrealityDWIN.Update_Status(status_message));
  1221. }
  1222. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  1223. void MarlinUI::advance_status_scroll() {
  1224. // Advance by one UTF8 code-word
  1225. if (status_scroll_offset < utf8_strlen(status_message))
  1226. while (!START_OF_UTF8_CHAR(status_message[++status_scroll_offset]));
  1227. else
  1228. status_scroll_offset = 0;
  1229. }
  1230. char* MarlinUI::status_and_len(uint8_t &len) {
  1231. char *out = status_message + status_scroll_offset;
  1232. len = utf8_strlen(out);
  1233. return out;
  1234. }
  1235. #endif
  1236. #endif
  1237. #if HAS_DISPLAY
  1238. #if ENABLED(SDSUPPORT)
  1239. extern bool wait_for_user, wait_for_heatup;
  1240. #endif
  1241. void MarlinUI::abort_print() {
  1242. #if ENABLED(SDSUPPORT)
  1243. wait_for_heatup = wait_for_user = false;
  1244. card.abortFilePrintSoon();
  1245. #endif
  1246. #ifdef ACTION_ON_CANCEL
  1247. hostui.cancel();
  1248. #endif
  1249. IF_DISABLED(SDSUPPORT, print_job_timer.stop());
  1250. TERN_(HOST_PROMPT_SUPPORT, hostui.prompt_open(PROMPT_INFO, F("UI Aborted"), FPSTR(DISMISS_STR)));
  1251. LCD_MESSAGE(MSG_PRINT_ABORTED);
  1252. TERN_(HAS_LCD_MENU, return_to_status());
  1253. }
  1254. #if BOTH(HAS_LCD_MENU, PSU_CONTROL)
  1255. void MarlinUI::poweroff() {
  1256. queue.inject(F("M81" TERN_(POWER_OFF_WAIT_FOR_COOLDOWN, "S")));
  1257. return_to_status();
  1258. }
  1259. #endif
  1260. void MarlinUI::flow_fault() {
  1261. LCD_ALERTMESSAGE(MSG_FLOWMETER_FAULT);
  1262. TERN_(HAS_BUZZER, buzz(1000, 440));
  1263. TERN_(HAS_LCD_MENU, return_to_status());
  1264. }
  1265. void MarlinUI::pause_print() {
  1266. #if HAS_LCD_MENU
  1267. synchronize(GET_TEXT(MSG_PAUSING));
  1268. defer_status_screen();
  1269. #endif
  1270. TERN_(HAS_TOUCH_SLEEP, wakeup_screen());
  1271. TERN_(HOST_PROMPT_SUPPORT, hostui.prompt_open(PROMPT_PAUSE_RESUME, F("UI Pause"), F("Resume")));
  1272. LCD_MESSAGE(MSG_PRINT_PAUSED);
  1273. #if ENABLED(PARK_HEAD_ON_PAUSE)
  1274. pause_show_message(PAUSE_MESSAGE_PARKING, PAUSE_MODE_PAUSE_PRINT); // Show message immediately to let user know about pause in progress
  1275. queue.inject(F("M25 P\nM24"));
  1276. #elif ENABLED(SDSUPPORT)
  1277. queue.inject(F("M25"));
  1278. #elif defined(ACTION_ON_PAUSE)
  1279. hostui.pause();
  1280. #endif
  1281. }
  1282. void MarlinUI::resume_print() {
  1283. reset_status();
  1284. TERN_(PARK_HEAD_ON_PAUSE, wait_for_heatup = wait_for_user = false);
  1285. TERN_(SDSUPPORT, if (IS_SD_PAUSED()) queue.inject_P(M24_STR));
  1286. #ifdef ACTION_ON_RESUME
  1287. hostui.resume();
  1288. #endif
  1289. print_job_timer.start(); // Also called by M24
  1290. }
  1291. #if HAS_PRINT_PROGRESS
  1292. MarlinUI::progress_t MarlinUI::_get_progress() {
  1293. return (
  1294. TERN0(LCD_SET_PROGRESS_MANUALLY, (progress_override & PROGRESS_MASK))
  1295. #if ENABLED(SDSUPPORT)
  1296. ?: TERN(HAS_PRINT_PROGRESS_PERMYRIAD, card.permyriadDone(), card.percentDone())
  1297. #endif
  1298. );
  1299. }
  1300. #endif
  1301. #if HAS_TOUCH_BUTTONS
  1302. //
  1303. // Screen Click
  1304. // - On menu screens move directly to the touched item
  1305. // - On menu screens, right side (last 3 cols) acts like a scroll - half up => prev page, half down = next page
  1306. // - On select screens (and others) touch the Right Half for +, Left Half for -
  1307. // - On edit screens, touch Up Half for -, Bottom Half to +
  1308. //
  1309. void MarlinUI::screen_click(const uint8_t row, const uint8_t col, const uint8_t, const uint8_t) {
  1310. const millis_t now = millis();
  1311. if (PENDING(now, next_button_update_ms)) return;
  1312. next_button_update_ms = now + repeat_delay; // Assume the repeat delay
  1313. const int8_t xdir = col < (LCD_WIDTH ) / 2 ? -1 : 1,
  1314. ydir = row < (LCD_HEIGHT) / 2 ? -1 : 1;
  1315. if (on_edit_screen)
  1316. encoderDiff = epps * ydir;
  1317. else if (screen_items > 0) {
  1318. // Last 5 cols act as a scroll :-)
  1319. if (col > (LCD_WIDTH) - 5)
  1320. // 2 * LCD_HEIGHT to scroll to bottom of next page. (LCD_HEIGHT would only go 1 item down.)
  1321. encoderDiff = epps * (encoderLine - encoderTopLine + 2 * (LCD_HEIGHT)) * ydir;
  1322. else
  1323. encoderDiff = epps * (row - encoderPosition + encoderTopLine);
  1324. }
  1325. else if (!on_status_screen())
  1326. encoderDiff = epps * xdir;
  1327. }
  1328. #endif
  1329. #elif !HAS_STATUS_MESSAGE // && !HAS_DISPLAY
  1330. //
  1331. // Send the status line as a host notification
  1332. //
  1333. void MarlinUI::set_status(const char * const cstr, const bool) {
  1334. TERN(HOST_PROMPT_SUPPORT, hostui.notify(cstr), UNUSED(cstr));
  1335. }
  1336. void MarlinUI::set_status(FSTR_P const fstr, const int8_t) {
  1337. TERN(HOST_PROMPT_SUPPORT, hostui.notify(fstr), UNUSED(fstr));
  1338. }
  1339. void MarlinUI::status_printf(const uint8_t, FSTR_P const fstr, ...) {
  1340. TERN(HOST_PROMPT_SUPPORT, hostui.notify(fstr), UNUSED(fstr));
  1341. }
  1342. #endif // !HAS_DISPLAY && !HAS_STATUS_MESSAGE
  1343. #if ENABLED(SDSUPPORT)
  1344. #if ENABLED(EXTENSIBLE_UI)
  1345. #include "extui/ui_api.h"
  1346. #endif
  1347. void MarlinUI::media_changed(const uint8_t old_status, const uint8_t status) {
  1348. if (old_status == status) {
  1349. TERN_(EXTENSIBLE_UI, ExtUI::onMediaError()); // Failed to mount/unmount
  1350. return;
  1351. }
  1352. if (status) {
  1353. if (old_status < 2) {
  1354. #if ENABLED(EXTENSIBLE_UI)
  1355. ExtUI::onMediaInserted();
  1356. #elif ENABLED(BROWSE_MEDIA_ON_INSERT)
  1357. clear_menu_history();
  1358. quick_feedback();
  1359. goto_screen(MEDIA_MENU_GATEWAY);
  1360. #else
  1361. LCD_MESSAGE(MSG_MEDIA_INSERTED);
  1362. #endif
  1363. }
  1364. }
  1365. else {
  1366. if (old_status < 2) {
  1367. #if ENABLED(EXTENSIBLE_UI)
  1368. ExtUI::onMediaRemoved();
  1369. #elif PIN_EXISTS(SD_DETECT)
  1370. LCD_MESSAGE(MSG_MEDIA_REMOVED);
  1371. #if HAS_LCD_MENU
  1372. if (!defer_return_to_status) return_to_status();
  1373. #endif
  1374. #endif
  1375. }
  1376. }
  1377. #if PIN_EXISTS(SD_DETECT) && DISABLED(NO_LCD_REINIT)
  1378. init_lcd(); // Revive a noisy shared SPI LCD
  1379. #endif
  1380. refresh();
  1381. #if HAS_WIRED_LCD || defined(LED_BACKLIGHT_TIMEOUT)
  1382. const millis_t ms = millis();
  1383. #endif
  1384. TERN_(HAS_WIRED_LCD, next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL); // Delay LCD update for SD activity
  1385. #ifdef LED_BACKLIGHT_TIMEOUT
  1386. leds.reset_timeout(ms);
  1387. #endif
  1388. }
  1389. #endif // SDSUPPORT
  1390. #if HAS_LCD_MENU
  1391. void MarlinUI::reset_settings() {
  1392. settings.reset();
  1393. completion_feedback();
  1394. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  1395. if (touch_calibration.need_calibration()) ui.goto_screen(touch_screen_calibration);
  1396. #endif
  1397. }
  1398. #if EITHER(BABYSTEP_ZPROBE_GFX_OVERLAY, MESH_EDIT_GFX_OVERLAY)
  1399. void MarlinUI::zoffset_overlay(const_float_t zvalue) {
  1400. // Determine whether the user is raising or lowering the nozzle.
  1401. static int8_t dir;
  1402. static float old_zvalue;
  1403. if (zvalue != old_zvalue) {
  1404. dir = zvalue ? zvalue < old_zvalue ? -1 : 1 : 0;
  1405. old_zvalue = zvalue;
  1406. }
  1407. zoffset_overlay(dir);
  1408. }
  1409. #endif
  1410. #endif
  1411. #if BOTH(EXTENSIBLE_UI, ADVANCED_PAUSE_FEATURE)
  1412. void MarlinUI::pause_show_message(
  1413. const PauseMessage message,
  1414. const PauseMode mode/*=PAUSE_MODE_SAME*/,
  1415. const uint8_t extruder/*=active_extruder*/
  1416. ) {
  1417. pause_mode = mode;
  1418. ExtUI::pauseModeStatus = message;
  1419. switch (message) {
  1420. case PAUSE_MESSAGE_PARKING: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_PAUSE_PRINT_PARKING)); break;
  1421. case PAUSE_MESSAGE_CHANGING: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_INIT)); break;
  1422. case PAUSE_MESSAGE_UNLOAD: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_UNLOAD)); break;
  1423. case PAUSE_MESSAGE_WAITING: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_ADVANCED_PAUSE_WAITING)); break;
  1424. case PAUSE_MESSAGE_INSERT: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_INSERT)); break;
  1425. case PAUSE_MESSAGE_LOAD: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_LOAD)); break;
  1426. case PAUSE_MESSAGE_PURGE:
  1427. ExtUI::onUserConfirmRequired(GET_TEXT_F(TERN(ADVANCED_PAUSE_CONTINUOUS_PURGE, MSG_FILAMENT_CHANGE_CONT_PURGE, MSG_FILAMENT_CHANGE_PURGE)));
  1428. break;
  1429. case PAUSE_MESSAGE_RESUME: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_RESUME)); break;
  1430. case PAUSE_MESSAGE_HEAT: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_HEAT)); break;
  1431. case PAUSE_MESSAGE_HEATING: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_HEATING)); break;
  1432. case PAUSE_MESSAGE_OPTION: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_OPTION_HEADER)); break;
  1433. case PAUSE_MESSAGE_STATUS: break;
  1434. default: break;
  1435. }
  1436. }
  1437. #endif
  1438. #if ENABLED(EEPROM_SETTINGS)
  1439. #if HAS_LCD_MENU
  1440. void MarlinUI::init_eeprom() {
  1441. const bool good = settings.init_eeprom();
  1442. completion_feedback(good);
  1443. return_to_status();
  1444. }
  1445. void MarlinUI::load_settings() {
  1446. const bool good = settings.load();
  1447. completion_feedback(good);
  1448. }
  1449. void MarlinUI::store_settings() {
  1450. const bool good = settings.save();
  1451. completion_feedback(good);
  1452. }
  1453. #endif
  1454. #if DISABLED(EEPROM_AUTO_INIT)
  1455. static inline PGM_P eeprom_err(const uint8_t msgid) {
  1456. switch (msgid) {
  1457. default:
  1458. case 0: return GET_TEXT(MSG_ERR_EEPROM_CRC);
  1459. case 1: return GET_TEXT(MSG_ERR_EEPROM_INDEX);
  1460. case 2: return GET_TEXT(MSG_ERR_EEPROM_VERSION);
  1461. }
  1462. }
  1463. void MarlinUI::eeprom_alert(const uint8_t msgid) {
  1464. #if HAS_LCD_MENU
  1465. editable.uint8 = msgid;
  1466. goto_screen([]{
  1467. PGM_P const restore_msg = GET_TEXT(MSG_INIT_EEPROM);
  1468. char msg[utf8_strlen_P(restore_msg) + 1];
  1469. strcpy_P(msg, restore_msg);
  1470. MenuItem_confirm::select_screen(
  1471. GET_TEXT(MSG_BUTTON_RESET), GET_TEXT(MSG_BUTTON_IGNORE),
  1472. init_eeprom, return_to_status,
  1473. eeprom_err(editable.uint8), msg, PSTR("?")
  1474. );
  1475. });
  1476. #else
  1477. set_status(FPSTR(eeprom_err(msgid)));
  1478. #endif
  1479. }
  1480. #endif // EEPROM_AUTO_INIT
  1481. #endif // EEPROM_SETTINGS