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.

ultralcd.cpp 53KB

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