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 54KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  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_WIRED_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_WIRED_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_WIRED_LCD
  103. #if HAS_MARLINUI_U8GLIB
  104. #include "dogm/ultralcd_DOGM.h"
  105. #endif
  106. #include "lcdprint.h"
  107. #include "../sd/cardreader.h"
  108. #include "../module/temperature.h"
  109. #include "../module/planner.h"
  110. #include "../module/motion.h"
  111. #if HAS_LCD_MENU
  112. #include "../module/settings.h"
  113. #endif
  114. #if ENABLED(AUTO_BED_LEVELING_UBL)
  115. #include "../feature/bedlevel/bedlevel.h"
  116. #endif
  117. #if HAS_TRINAMIC_CONFIG
  118. #include "../feature/tmc_util.h"
  119. #endif
  120. #if HAS_ADC_BUTTONS
  121. #include "../module/thermistor/thermistors.h"
  122. #endif
  123. #if HAS_POWER_MONITOR
  124. #include "../feature/power_monitor.h"
  125. #endif
  126. #if HAS_ENCODER_ACTION
  127. volatile uint8_t MarlinUI::buttons;
  128. #if HAS_SLOW_BUTTONS
  129. volatile uint8_t MarlinUI::slow_buttons;
  130. #endif
  131. #if HAS_TOUCH_XPT2046
  132. #include "touch/touch_buttons.h"
  133. bool MarlinUI::on_edit_screen = false;
  134. #endif
  135. #endif
  136. #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS > 0
  137. bool MarlinUI::defer_return_to_status;
  138. #endif
  139. uint8_t MarlinUI::lcd_status_update_delay = 1; // First update one loop delayed
  140. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  141. millis_t MarlinUI::next_filament_display; // = 0
  142. #endif
  143. millis_t MarlinUI::next_button_update_ms; // = 0
  144. #if HAS_MARLINUI_U8GLIB
  145. bool MarlinUI::drawing_screen, MarlinUI::first_page; // = false
  146. #endif
  147. // Encoder Handling
  148. #if HAS_ENCODER_ACTION
  149. uint32_t MarlinUI::encoderPosition;
  150. volatile int8_t encoderDiff; // Updated in update_buttons, added to encoderPosition every LCD update
  151. #endif
  152. #if ENABLED(SDSUPPORT)
  153. #include "../sd/cardreader.h"
  154. #if MARLINUI_SCROLL_NAME
  155. uint8_t MarlinUI::filename_scroll_pos, MarlinUI::filename_scroll_max;
  156. #endif
  157. const char * MarlinUI::scrolled_filename(CardReader &theCard, const uint8_t maxlen, uint8_t hash, const bool doScroll) {
  158. const char *outstr = theCard.longest_filename();
  159. if (theCard.longFilename[0]) {
  160. #if MARLINUI_SCROLL_NAME
  161. if (doScroll) {
  162. for (uint8_t l = FILENAME_LENGTH; l--;)
  163. hash = ((hash << 1) | (hash >> 7)) ^ theCard.filename[l]; // rotate, xor
  164. static uint8_t filename_scroll_hash;
  165. if (filename_scroll_hash != hash) { // If the hash changed...
  166. filename_scroll_hash = hash; // Save the new hash
  167. filename_scroll_max = _MAX(0, utf8_strlen(theCard.longFilename) - maxlen); // Update the scroll limit
  168. filename_scroll_pos = 0; // Reset scroll to the start
  169. lcd_status_update_delay = 8; // Don't scroll right away
  170. }
  171. outstr += filename_scroll_pos;
  172. }
  173. #else
  174. theCard.longFilename[maxlen] = '\0'; // cutoff at screen edge
  175. #endif
  176. }
  177. return outstr;
  178. }
  179. #endif
  180. #if HAS_LCD_MENU
  181. #include "menu/menu.h"
  182. screenFunc_t MarlinUI::currentScreen; // Initialized in CTOR
  183. bool MarlinUI::screen_changed;
  184. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  185. bool MarlinUI::encoderRateMultiplierEnabled;
  186. millis_t MarlinUI::lastEncoderMovementMillis = 0;
  187. void MarlinUI::enable_encoder_multiplier(const bool onoff) {
  188. encoderRateMultiplierEnabled = onoff;
  189. lastEncoderMovementMillis = 0;
  190. }
  191. #endif
  192. #if EITHER(REVERSE_MENU_DIRECTION, REVERSE_SELECT_DIRECTION)
  193. int8_t MarlinUI::encoderDirection = ENCODERBASE;
  194. #endif
  195. #if HAS_TOUCH_XPT2046
  196. uint8_t MarlinUI::touch_buttons;
  197. uint8_t MarlinUI::repeat_delay;
  198. #endif
  199. bool MarlinUI::lcd_clicked;
  200. bool MarlinUI::use_click() {
  201. const bool click = lcd_clicked;
  202. lcd_clicked = false;
  203. return click;
  204. }
  205. #if EITHER(AUTO_BED_LEVELING_UBL, G26_MESH_VALIDATION)
  206. bool MarlinUI::external_control; // = false
  207. void MarlinUI::wait_for_release() {
  208. while (button_pressed()) safe_delay(50);
  209. safe_delay(50);
  210. }
  211. #endif
  212. void _wrap_string(uint8_t &col, uint8_t &row, const char * const string, read_byte_cb_t cb_read_byte, bool wordwrap/*=false*/) {
  213. SETCURSOR(col, row);
  214. if (!string) return;
  215. auto _newline = [&col, &row]{
  216. col = 0; row++; // Move col to string len (plus space)
  217. SETCURSOR(0, row); // Simulate carriage return
  218. };
  219. uint8_t *p = (uint8_t*)string;
  220. wchar_t ch;
  221. if (wordwrap) {
  222. uint8_t *wrd = nullptr, c = 0;
  223. // find the end of the part
  224. for (;;) {
  225. if (!wrd) wrd = p; // Get word start /before/ advancing
  226. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  227. const bool eol = !ch; // zero ends the string
  228. // End or a break between phrases?
  229. if (eol || ch == ' ' || ch == '-' || ch == '+' || ch == '.') {
  230. if (!c && ch == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
  231. // Past the right and the word is not too long?
  232. if (col + c > LCD_WIDTH && col >= (LCD_WIDTH) / 4) _newline(); // should it wrap?
  233. c += !eol; // +1 so the space will be printed
  234. col += c; // advance col to new position
  235. while (c) { // character countdown
  236. --c; // count down to zero
  237. wrd = get_utf8_value_cb(wrd, cb_read_byte, &ch); // get characters again
  238. lcd_put_wchar(ch); // character to the LCD
  239. }
  240. if (eol) break; // all done!
  241. wrd = nullptr; // set up for next word
  242. }
  243. else c++; // count word characters
  244. }
  245. }
  246. else {
  247. for (;;) {
  248. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  249. if (!ch) break;
  250. lcd_put_wchar(ch);
  251. col++;
  252. if (col >= LCD_WIDTH) _newline();
  253. }
  254. }
  255. }
  256. void MarlinUI::draw_select_screen_prompt(PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) {
  257. const uint8_t plen = utf8_strlen_P(pref), slen = suff ? utf8_strlen_P(suff) : 0;
  258. uint8_t col = 0, row = 0;
  259. if (!string && plen + slen <= LCD_WIDTH) {
  260. col = (LCD_WIDTH - plen - slen) / 2;
  261. row = LCD_HEIGHT > 3 ? 1 : 0;
  262. }
  263. wrap_string_P(col, row, pref, true);
  264. if (string) {
  265. if (col) { col = 0; row++; } // Move to the start of the next line
  266. wrap_string(col, row, string);
  267. }
  268. if (suff) wrap_string_P(col, row, suff);
  269. }
  270. #endif // HAS_LCD_MENU
  271. void MarlinUI::init() {
  272. init_lcd();
  273. #if HAS_DIGITAL_BUTTONS
  274. #if BUTTON_EXISTS(EN1)
  275. SET_INPUT_PULLUP(BTN_EN1);
  276. #endif
  277. #if BUTTON_EXISTS(EN2)
  278. SET_INPUT_PULLUP(BTN_EN2);
  279. #endif
  280. #if BUTTON_EXISTS(ENC)
  281. SET_INPUT_PULLUP(BTN_ENC);
  282. #endif
  283. #if BUTTON_EXISTS(BACK)
  284. SET_INPUT_PULLUP(BTN_BACK);
  285. #endif
  286. #if BUTTON_EXISTS(UP)
  287. SET_INPUT(BTN_UP);
  288. #endif
  289. #if BUTTON_EXISTS(DWN)
  290. SET_INPUT(BTN_DWN);
  291. #endif
  292. #if BUTTON_EXISTS(LFT)
  293. SET_INPUT(BTN_LFT);
  294. #endif
  295. #if BUTTON_EXISTS(RT)
  296. SET_INPUT(BTN_RT);
  297. #endif
  298. #endif // !HAS_DIGITAL_BUTTONS
  299. #if HAS_SHIFT_ENCODER
  300. #if ENABLED(SR_LCD_2W_NL) // Non latching 2 wire shift register
  301. SET_OUTPUT(SR_DATA_PIN);
  302. SET_OUTPUT(SR_CLK_PIN);
  303. #elif defined(SHIFT_CLK)
  304. SET_OUTPUT(SHIFT_CLK);
  305. OUT_WRITE(SHIFT_LD, HIGH);
  306. #if defined(SHIFT_EN) && SHIFT_EN >= 0
  307. OUT_WRITE(SHIFT_EN, LOW);
  308. #endif
  309. SET_INPUT_PULLUP(SHIFT_OUT);
  310. #endif
  311. #endif // HAS_SHIFT_ENCODER
  312. #if BOTH(HAS_ENCODER_ACTION, HAS_SLOW_BUTTONS)
  313. slow_buttons = 0;
  314. #endif
  315. update_buttons();
  316. TERN_(HAS_ENCODER_ACTION, encoderDiff = 0);
  317. }
  318. bool MarlinUI::get_blink() {
  319. static uint8_t blink = 0;
  320. static millis_t next_blink_ms = 0;
  321. millis_t ms = millis();
  322. if (ELAPSED(ms, next_blink_ms)) {
  323. blink ^= 0xFF;
  324. next_blink_ms = ms + 1000 - (LCD_UPDATE_INTERVAL) / 2;
  325. }
  326. return blink != 0;
  327. }
  328. ////////////////////////////////////////////
  329. ///////////// Keypad Handling //////////////
  330. ////////////////////////////////////////////
  331. #if BOTH(REPRAPWORLD_KEYPAD, HAS_ENCODER_ACTION)
  332. volatile uint8_t MarlinUI::keypad_buttons;
  333. #if HAS_LCD_MENU && !HAS_ADC_BUTTONS
  334. void lcd_move_x();
  335. void lcd_move_y();
  336. void lcd_move_z();
  337. void _reprapworld_keypad_move(const AxisEnum axis, const int16_t dir) {
  338. ui.manual_move.menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  339. ui.encoderPosition = dir;
  340. switch (axis) {
  341. case X_AXIS: lcd_move_x(); break;
  342. case Y_AXIS: lcd_move_y(); break;
  343. case Z_AXIS: lcd_move_z();
  344. default: break;
  345. }
  346. }
  347. #endif
  348. bool MarlinUI::handle_keypad() {
  349. #if HAS_ADC_BUTTONS
  350. #define ADC_MIN_KEY_DELAY 100
  351. if (keypad_buttons) {
  352. #if HAS_ENCODER_ACTION
  353. refresh(LCDVIEW_REDRAW_NOW);
  354. #if HAS_LCD_MENU
  355. if (encoderDirection == -(ENCODERBASE)) { // ADC_KEYPAD forces REVERSE_MENU_DIRECTION, so this indicates menu navigation
  356. if (RRK(EN_KEYPAD_UP)) encoderPosition += ENCODER_STEPS_PER_MENU_ITEM;
  357. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition -= ENCODER_STEPS_PER_MENU_ITEM;
  358. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  359. else if (RRK(EN_KEYPAD_RIGHT)) { return_to_status(); quick_feedback(); }
  360. }
  361. else
  362. #endif
  363. {
  364. #if HAS_LCD_MENU
  365. if (RRK(EN_KEYPAD_UP)) encoderPosition -= epps;
  366. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition += epps;
  367. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  368. else if (RRK(EN_KEYPAD_RIGHT)) encoderPosition = 0;
  369. #else
  370. if (RRK(EN_KEYPAD_UP) || RRK(EN_KEYPAD_LEFT)) encoderPosition -= epps;
  371. else if (RRK(EN_KEYPAD_DOWN) || RRK(EN_KEYPAD_RIGHT)) encoderPosition += epps;
  372. #endif
  373. }
  374. #endif
  375. next_button_update_ms = millis() + ADC_MIN_KEY_DELAY;
  376. return true;
  377. }
  378. #else // !HAS_ADC_BUTTONS
  379. static uint8_t keypad_debounce = 0;
  380. if (!RRK( EN_KEYPAD_F1 | EN_KEYPAD_F2
  381. | EN_KEYPAD_F3 | EN_KEYPAD_DOWN
  382. | EN_KEYPAD_RIGHT | EN_KEYPAD_MIDDLE
  383. | EN_KEYPAD_UP | EN_KEYPAD_LEFT )
  384. ) {
  385. if (keypad_debounce > 0) keypad_debounce--;
  386. }
  387. else if (!keypad_debounce) {
  388. keypad_debounce = 2;
  389. const bool homed = all_axes_homed();
  390. #if HAS_LCD_MENU
  391. if (RRK(EN_KEYPAD_MIDDLE)) goto_screen(menu_move);
  392. #if DISABLED(DELTA) && Z_HOME_DIR < 0
  393. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  394. #endif
  395. if (homed) {
  396. #if ENABLED(DELTA) || Z_HOME_DIR != -1
  397. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  398. #endif
  399. if (RRK(EN_KEYPAD_F3)) _reprapworld_keypad_move(Z_AXIS, -1);
  400. if (RRK(EN_KEYPAD_LEFT)) _reprapworld_keypad_move(X_AXIS, -1);
  401. if (RRK(EN_KEYPAD_RIGHT)) _reprapworld_keypad_move(X_AXIS, 1);
  402. if (RRK(EN_KEYPAD_DOWN)) _reprapworld_keypad_move(Y_AXIS, 1);
  403. if (RRK(EN_KEYPAD_UP)) _reprapworld_keypad_move(Y_AXIS, -1);
  404. }
  405. #endif // HAS_LCD_MENU
  406. if (!homed && RRK(EN_KEYPAD_F1)) queue.inject_P(G28_STR);
  407. return true;
  408. }
  409. #endif // !ADC_KEYPAD
  410. return false;
  411. }
  412. #endif // REPRAPWORLD_KEYPAD
  413. /**
  414. * Status Screen
  415. *
  416. * This is very display-dependent, so the lcd implementation draws this.
  417. */
  418. #if ENABLED(LCD_PROGRESS_BAR) && !IS_TFTGLCD_PANEL
  419. millis_t MarlinUI::progress_bar_ms; // = 0
  420. #if PROGRESS_MSG_EXPIRE > 0
  421. millis_t MarlinUI::expire_status_ms; // = 0
  422. #endif
  423. #endif
  424. void MarlinUI::status_screen() {
  425. TERN_(HAS_LCD_MENU, ENCODER_RATE_MULTIPLY(false));
  426. #if ENABLED(LCD_PROGRESS_BAR) && !IS_TFTGLCD_PANEL
  427. //
  428. // HD44780 implements the following message blinking and
  429. // message expiration because Status Line and Progress Bar
  430. // share the same line on the display.
  431. //
  432. #if DISABLED(PROGRESS_MSG_ONCE) || (PROGRESS_MSG_EXPIRE > 0)
  433. #define GOT_MS
  434. const millis_t ms = millis();
  435. #endif
  436. // If the message will blink rather than expire...
  437. #if DISABLED(PROGRESS_MSG_ONCE)
  438. if (ELAPSED(ms, progress_bar_ms + PROGRESS_BAR_MSG_TIME + PROGRESS_BAR_BAR_TIME))
  439. progress_bar_ms = ms;
  440. #endif
  441. #if PROGRESS_MSG_EXPIRE > 0
  442. // Handle message expire
  443. if (expire_status_ms) {
  444. // Expire the message if a job is active and the bar has ticks
  445. if (get_progress_percent() > 2 && !print_job_timer.isPaused()) {
  446. if (ELAPSED(ms, expire_status_ms)) {
  447. status_message[0] = '\0';
  448. expire_status_ms = 0;
  449. }
  450. }
  451. else {
  452. // Defer message expiration before bar appears
  453. // and during any pause (not just SD)
  454. expire_status_ms += LCD_UPDATE_INTERVAL;
  455. }
  456. }
  457. #endif // PROGRESS_MSG_EXPIRE
  458. #endif // LCD_PROGRESS_BAR
  459. #if HAS_LCD_MENU
  460. if (use_click()) {
  461. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  462. next_filament_display = millis() + 5000UL; // Show status message for 5s
  463. #endif
  464. goto_screen(menu_main);
  465. #if DISABLED(NO_LCD_REINIT)
  466. init_lcd(); // May revive the LCD if static electricity killed it
  467. #endif
  468. return;
  469. }
  470. #endif
  471. #if ENABLED(ULTIPANEL_FEEDMULTIPLY)
  472. const int16_t old_frm = feedrate_percentage;
  473. int16_t new_frm = old_frm + int16_t(encoderPosition);
  474. // Dead zone at 100% feedrate
  475. if (old_frm == 100) {
  476. if (int16_t(encoderPosition) > ENCODER_FEEDRATE_DEADZONE)
  477. new_frm -= ENCODER_FEEDRATE_DEADZONE;
  478. else if (int16_t(encoderPosition) < -(ENCODER_FEEDRATE_DEADZONE))
  479. new_frm += ENCODER_FEEDRATE_DEADZONE;
  480. else
  481. new_frm = old_frm;
  482. }
  483. else if ((old_frm < 100 && new_frm > 100) || (old_frm > 100 && new_frm < 100))
  484. new_frm = 100;
  485. LIMIT(new_frm, 10, 999);
  486. if (old_frm != new_frm) {
  487. feedrate_percentage = new_frm;
  488. encoderPosition = 0;
  489. #if BOTH(HAS_BUZZER, BEEP_ON_FEEDRATE_CHANGE)
  490. static millis_t next_beep;
  491. #ifndef GOT_MS
  492. const millis_t ms = millis();
  493. #endif
  494. if (ELAPSED(ms, next_beep)) {
  495. buzz(FEEDRATE_CHANGE_BEEP_DURATION, FEEDRATE_CHANGE_BEEP_FREQUENCY);
  496. next_beep = ms + 500UL;
  497. }
  498. #endif
  499. }
  500. #endif // ULTIPANEL_FEEDMULTIPLY
  501. draw_status_screen();
  502. }
  503. void MarlinUI::kill_screen(PGM_P lcd_error, PGM_P lcd_component) {
  504. init();
  505. status_printf_P(1, PSTR(S_FMT ": " S_FMT), lcd_error, lcd_component);
  506. TERN_(HAS_LCD_MENU, return_to_status());
  507. // RED ALERT. RED ALERT.
  508. #ifdef LED_BACKLIGHT_TIMEOUT
  509. leds.set_color(LEDColorRed());
  510. #ifdef NEOPIXEL_BKGD_LED_INDEX
  511. neo.set_pixel_color(NEOPIXEL_BKGD_LED_INDEX, 255, 0, 0, 0);
  512. neo.show();
  513. #endif
  514. #endif
  515. draw_kill_screen();
  516. }
  517. void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
  518. TERN_(HAS_LCD_MENU, refresh());
  519. #if HAS_ENCODER_ACTION
  520. if (clear_buttons) buttons = 0;
  521. next_button_update_ms = millis() + 500;
  522. #else
  523. UNUSED(clear_buttons);
  524. #endif
  525. #if HAS_CHIRP
  526. chirp(); // Buzz and wait. Is the delay needed for buttons to settle?
  527. #if BOTH(HAS_LCD_MENU, USE_BEEPER)
  528. for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
  529. #elif HAS_LCD_MENU
  530. delay(10);
  531. #endif
  532. #endif
  533. }
  534. ////////////////////////////////////////////
  535. /////////////// Manual Move ////////////////
  536. ////////////////////////////////////////////
  537. #if HAS_LCD_MENU
  538. ManualMove MarlinUI::manual_move{};
  539. millis_t ManualMove::start_time = 0;
  540. float ManualMove::menu_scale = 1;
  541. TERN_(IS_KINEMATIC, float ManualMove::offset = 0);
  542. TERN_(IS_KINEMATIC, bool ManualMove::processing = false);
  543. TERN_(MULTI_MANUAL, int8_t ManualMove::e_index = 0);
  544. uint8_t ManualMove::axis = (uint8_t)NO_AXIS;
  545. /**
  546. * If a manual move has been posted and its time has arrived, and if the planner
  547. * has a space for it, then add a linear move to current_position the planner.
  548. *
  549. * If any manual move needs to be interrupted, make sure to force a manual move
  550. * by setting manual_move.start_time to millis() after updating current_position.
  551. *
  552. * To post a manual move:
  553. * - Update current_position to the new place you want to go.
  554. * - Set manual_move.axis to an axis like X_AXIS. Use ALL_AXES for diagonal moves.
  555. * - Set manual_move.start_time to a point in the future (in ms) when the move should be done.
  556. *
  557. * For kinematic machines:
  558. * - Set manual_move.offset to modify one axis and post the move.
  559. * This is used to achieve more rapid stepping on kinematic machines.
  560. *
  561. * Currently used by the _lcd_move_xyz function in menu_motion.cpp
  562. * and the ubl_map_move_to_xy funtion in menu_ubl.cpp.
  563. */
  564. void ManualMove::task() {
  565. if (processing) return; // Prevent re-entry from idle() calls
  566. // Add a manual move to the queue?
  567. if (axis != (uint8_t)NO_AXIS && ELAPSED(millis(), start_time) && !planner.is_full()) {
  568. const feedRate_t fr_mm_s = (uint8_t(axis) <= E_AXIS) ? manual_feedrate_mm_s[axis] : XY_PROBE_FEEDRATE_MM_S;
  569. #if IS_KINEMATIC
  570. #if HAS_MULTI_EXTRUDER
  571. const int8_t old_extruder = active_extruder;
  572. if (axis == E_AXIS) active_extruder = e_index;
  573. #endif
  574. // Apply a linear offset to a single axis
  575. destination = current_position;
  576. if (axis <= XYZE) destination[axis] += offset;
  577. // Reset for the next move
  578. offset = 0;
  579. axis = (uint8_t)NO_AXIS;
  580. // DELTA and SCARA machines use segmented moves, which could fill the planner during the call to
  581. // move_to_destination. This will cause idle() to be called, which can then call this function while the
  582. // previous invocation is being blocked. Modifications to offset shouldn't be made while
  583. // processing is true or the planner will get out of sync.
  584. processing = true;
  585. prepare_internal_move_to_destination(fr_mm_s); // will set current_position from destination
  586. processing = false;
  587. TERN_(HAS_MULTI_EXTRUDER, active_extruder = old_extruder);
  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_MARLINUI_U8GLIB, 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 && TERN1(IS_TFTGLCD_PANEL, !external_control)) {
  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_MARLINUI_U8GLIB, 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_MARLINUI_U8GLIB
  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. #if IS_TFTGLCD_PANEL
  1031. next_button_update_ms = now + (LCD_UPDATE_INTERVAL / 2);
  1032. buttons = slow_buttons;
  1033. TERN_(AUTO_BED_LEVELING_UBL, external_encoder());
  1034. #endif
  1035. } // next_button_update_ms
  1036. #if HAS_ENCODER_WHEEL
  1037. static uint8_t lastEncoderBits;
  1038. #define encrot0 0
  1039. #define encrot1 2
  1040. #define encrot2 3
  1041. #define encrot3 1
  1042. // Manage encoder rotation
  1043. #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; }
  1044. uint8_t enc = 0;
  1045. if (buttons & EN_A) enc |= B01;
  1046. if (buttons & EN_B) enc |= B10;
  1047. if (enc != lastEncoderBits) {
  1048. switch (enc) {
  1049. case encrot0: ENCODER_SPIN(encrot3, encrot1); break;
  1050. case encrot1: ENCODER_SPIN(encrot0, encrot2); break;
  1051. case encrot2: ENCODER_SPIN(encrot1, encrot3); break;
  1052. case encrot3: ENCODER_SPIN(encrot2, encrot0); break;
  1053. }
  1054. #if BOTH(HAS_LCD_MENU, AUTO_BED_LEVELING_UBL)
  1055. external_encoder();
  1056. #endif
  1057. lastEncoderBits = enc;
  1058. }
  1059. #endif // HAS_ENCODER_WHEEL
  1060. }
  1061. #endif // HAS_ENCODER_ACTION
  1062. #endif // HAS_WIRED_LCD
  1063. #if HAS_DISPLAY
  1064. #if ENABLED(EXTENSIBLE_UI)
  1065. #include "extui/ui_api.h"
  1066. #endif
  1067. ////////////////////////////////////////////
  1068. /////////////// Status Line ////////////////
  1069. ////////////////////////////////////////////
  1070. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  1071. void MarlinUI::advance_status_scroll() {
  1072. // Advance by one UTF8 code-word
  1073. if (status_scroll_offset < utf8_strlen(status_message))
  1074. while (!START_OF_UTF8_CHAR(status_message[++status_scroll_offset]));
  1075. else
  1076. status_scroll_offset = 0;
  1077. }
  1078. char* MarlinUI::status_and_len(uint8_t &len) {
  1079. char *out = status_message + status_scroll_offset;
  1080. len = utf8_strlen(out);
  1081. return out;
  1082. }
  1083. #endif
  1084. void MarlinUI::finish_status(const bool persist) {
  1085. #if !(ENABLED(LCD_PROGRESS_BAR) && (PROGRESS_MSG_EXPIRE) > 0)
  1086. UNUSED(persist);
  1087. #endif
  1088. #if ENABLED(LCD_PROGRESS_BAR) || BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  1089. const millis_t ms = millis();
  1090. #endif
  1091. #if ENABLED(LCD_PROGRESS_BAR) && !IS_TFTGLCD_PANEL
  1092. progress_bar_ms = ms;
  1093. #if PROGRESS_MSG_EXPIRE > 0
  1094. expire_status_ms = persist ? 0 : ms + PROGRESS_MSG_EXPIRE;
  1095. #endif
  1096. #endif
  1097. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  1098. next_filament_display = ms + 5000UL; // Show status message for 5s
  1099. #endif
  1100. #if BOTH(HAS_WIRED_LCD, STATUS_MESSAGE_SCROLLING)
  1101. status_scroll_offset = 0;
  1102. #endif
  1103. TERN_(EXTENSIBLE_UI, ExtUI::onStatusChanged(status_message));
  1104. }
  1105. bool MarlinUI::has_status() { return (status_message[0] != '\0'); }
  1106. void MarlinUI::set_status(const char * const message, const bool persist) {
  1107. if (alert_level) return;
  1108. TERN_(HOST_PROMPT_SUPPORT, host_action_notify(message));
  1109. // Here we have a problem. The message is encoded in UTF8, so
  1110. // arbitrarily cutting it will be a problem. We MUST be sure
  1111. // that there is no cutting in the middle of a multibyte character!
  1112. // Get a pointer to the null terminator
  1113. const char* pend = message + strlen(message);
  1114. // If length of supplied UTF8 string is greater than
  1115. // our buffer size, start cutting whole UTF8 chars
  1116. while ((pend - message) > MAX_MESSAGE_LENGTH) {
  1117. --pend;
  1118. while (!START_OF_UTF8_CHAR(*pend)) --pend;
  1119. };
  1120. // At this point, we have the proper cut point. Use it
  1121. uint8_t maxLen = pend - message;
  1122. strncpy(status_message, message, maxLen);
  1123. status_message[maxLen] = '\0';
  1124. finish_status(persist);
  1125. }
  1126. #include <stdarg.h>
  1127. void MarlinUI::status_printf_P(const uint8_t level, PGM_P const fmt, ...) {
  1128. if (level < alert_level) return;
  1129. alert_level = level;
  1130. va_list args;
  1131. va_start(args, fmt);
  1132. vsnprintf_P(status_message, MAX_MESSAGE_LENGTH, fmt, args);
  1133. va_end(args);
  1134. finish_status(level > 0);
  1135. }
  1136. void MarlinUI::set_status_P(PGM_P const message, int8_t level) {
  1137. if (level < 0) level = alert_level = 0;
  1138. if (level < alert_level) return;
  1139. alert_level = level;
  1140. TERN_(HOST_PROMPT_SUPPORT, host_action_notify_P(message));
  1141. // Since the message is encoded in UTF8 it must
  1142. // only be cut on a character boundary.
  1143. // Get a pointer to the null terminator
  1144. PGM_P pend = message + strlen_P(message);
  1145. // If length of supplied UTF8 string is greater than
  1146. // the buffer size, start cutting whole UTF8 chars
  1147. while ((pend - message) > MAX_MESSAGE_LENGTH) {
  1148. --pend;
  1149. while (!START_OF_UTF8_CHAR(pgm_read_byte(pend))) --pend;
  1150. };
  1151. // At this point, we have the proper cut point. Use it
  1152. uint8_t maxLen = pend - message;
  1153. strncpy_P(status_message, message, maxLen);
  1154. status_message[maxLen] = '\0';
  1155. finish_status(level > 0);
  1156. }
  1157. void MarlinUI::set_alert_status_P(PGM_P const message) {
  1158. set_status_P(message, 1);
  1159. TERN_(HAS_LCD_MENU, return_to_status());
  1160. }
  1161. PGM_P print_paused = GET_TEXT(MSG_PRINT_PAUSED);
  1162. /**
  1163. * Reset the status message
  1164. */
  1165. void MarlinUI::reset_status(const bool no_welcome) {
  1166. PGM_P printing = GET_TEXT(MSG_PRINTING);
  1167. PGM_P welcome = GET_TEXT(WELCOME_MSG);
  1168. #if SERVICE_INTERVAL_1 > 0
  1169. static PGMSTR(service1, "> " SERVICE_NAME_1 "!");
  1170. #endif
  1171. #if SERVICE_INTERVAL_2 > 0
  1172. static PGMSTR(service2, "> " SERVICE_NAME_2 "!");
  1173. #endif
  1174. #if SERVICE_INTERVAL_3 > 0
  1175. static PGMSTR(service3, "> " SERVICE_NAME_3 "!");
  1176. #endif
  1177. PGM_P msg;
  1178. if (printingIsPaused())
  1179. msg = print_paused;
  1180. #if ENABLED(SDSUPPORT)
  1181. else if (IS_SD_PRINTING())
  1182. return set_status(card.longest_filename(), true);
  1183. #endif
  1184. else if (print_job_timer.isRunning())
  1185. msg = printing;
  1186. #if SERVICE_INTERVAL_1 > 0
  1187. else if (print_job_timer.needsService(1)) msg = service1;
  1188. #endif
  1189. #if SERVICE_INTERVAL_2 > 0
  1190. else if (print_job_timer.needsService(2)) msg = service2;
  1191. #endif
  1192. #if SERVICE_INTERVAL_3 > 0
  1193. else if (print_job_timer.needsService(3)) msg = service3;
  1194. #endif
  1195. else if (!no_welcome)
  1196. msg = welcome;
  1197. else
  1198. return;
  1199. set_status_P(msg, -1);
  1200. }
  1201. #if ENABLED(SDSUPPORT)
  1202. extern bool wait_for_user, wait_for_heatup;
  1203. #endif
  1204. void MarlinUI::abort_print() {
  1205. #if ENABLED(SDSUPPORT)
  1206. wait_for_heatup = wait_for_user = false;
  1207. card.flag.abort_sd_printing = true;
  1208. #endif
  1209. #ifdef ACTION_ON_CANCEL
  1210. host_action_cancel();
  1211. #endif
  1212. TERN_(HOST_PROMPT_SUPPORT, host_prompt_open(PROMPT_INFO, PSTR("UI Aborted"), DISMISS_STR));
  1213. print_job_timer.stop();
  1214. set_status_P(GET_TEXT(MSG_PRINT_ABORTED));
  1215. TERN_(HAS_LCD_MENU, return_to_status());
  1216. }
  1217. #if ANY(PARK_HEAD_ON_PAUSE, SDSUPPORT)
  1218. #include "../gcode/queue.h"
  1219. #endif
  1220. void MarlinUI::pause_print() {
  1221. #if HAS_LCD_MENU
  1222. synchronize(GET_TEXT(MSG_PAUSING));
  1223. defer_status_screen();
  1224. #endif
  1225. TERN_(HOST_PROMPT_SUPPORT, host_prompt_open(PROMPT_PAUSE_RESUME, PSTR("UI Pause"), PSTR("Resume")));
  1226. set_status_P(print_paused);
  1227. #if ENABLED(PARK_HEAD_ON_PAUSE)
  1228. TERN_(HAS_WIRED_LCD, lcd_pause_show_message(PAUSE_MESSAGE_PARKING, PAUSE_MODE_PAUSE_PRINT)); // Show message immediately to let user know about pause in progress
  1229. queue.inject_P(PSTR("M25 P\nM24"));
  1230. #elif ENABLED(SDSUPPORT)
  1231. queue.inject_P(PSTR("M25"));
  1232. #elif defined(ACTION_ON_PAUSE)
  1233. host_action_pause();
  1234. #endif
  1235. }
  1236. void MarlinUI::resume_print() {
  1237. reset_status();
  1238. TERN_(PARK_HEAD_ON_PAUSE, wait_for_heatup = wait_for_user = false);
  1239. if (IS_SD_PAUSED()) queue.inject_P(M24_STR);
  1240. #ifdef ACTION_ON_RESUME
  1241. host_action_resume();
  1242. #endif
  1243. print_job_timer.start(); // Also called by M24
  1244. }
  1245. #if HAS_PRINT_PROGRESS
  1246. MarlinUI::progress_t MarlinUI::_get_progress() {
  1247. return (
  1248. TERN0(LCD_SET_PROGRESS_MANUALLY, (progress_override & PROGRESS_MASK))
  1249. #if ENABLED(SDSUPPORT)
  1250. ?: TERN(HAS_PRINT_PROGRESS_PERMYRIAD, card.permyriadDone(), card.percentDone())
  1251. #endif
  1252. );
  1253. }
  1254. #endif
  1255. #if HAS_TOUCH_XPT2046
  1256. //
  1257. // Screen Click
  1258. // - On menu screens move directly to the touched item
  1259. // - On menu screens, right side (last 3 cols) acts like a scroll - half up => prev page, half down = next page
  1260. // - On select screens (and others) touch the Right Half for +, Left Half for -
  1261. // - On edit screens, touch Up Half for -, Bottom Half to +
  1262. //
  1263. void MarlinUI::screen_click(const uint8_t row, const uint8_t col, const uint8_t, const uint8_t) {
  1264. const millis_t now = millis();
  1265. if (PENDING(now, next_button_update_ms)) return;
  1266. next_button_update_ms = now + repeat_delay; // Assume the repeat delay
  1267. const int8_t xdir = col < (LCD_WIDTH ) / 2 ? -1 : 1,
  1268. ydir = row < (LCD_HEIGHT) / 2 ? -1 : 1;
  1269. if (on_edit_screen)
  1270. encoderDiff = epps * ydir;
  1271. else if (screen_items > 0) {
  1272. // Last 5 cols act as a scroll :-)
  1273. if (col > (LCD_WIDTH) - 5)
  1274. // 2 * LCD_HEIGHT to scroll to bottom of next page. (LCD_HEIGHT would only go 1 item down.)
  1275. encoderDiff = epps * (encoderLine - encoderTopLine + 2 * (LCD_HEIGHT)) * ydir;
  1276. else
  1277. encoderDiff = epps * (row - encoderPosition + encoderTopLine);
  1278. }
  1279. else if (!on_status_screen())
  1280. encoderDiff = epps * xdir;
  1281. }
  1282. #endif
  1283. #else // !HAS_DISPLAY
  1284. //
  1285. // Send the status line as a host notification
  1286. //
  1287. void MarlinUI::set_status(const char * const message, const bool) {
  1288. TERN(HOST_PROMPT_SUPPORT, host_action_notify(message), UNUSED(message));
  1289. }
  1290. void MarlinUI::set_status_P(PGM_P message, const int8_t) {
  1291. TERN(HOST_PROMPT_SUPPORT, host_action_notify_P(message), UNUSED(message));
  1292. }
  1293. void MarlinUI::status_printf_P(const uint8_t, PGM_P const message, ...) {
  1294. TERN(HOST_PROMPT_SUPPORT, host_action_notify_P(message), UNUSED(message));
  1295. }
  1296. #endif // !HAS_DISPLAY
  1297. #if ENABLED(SDSUPPORT)
  1298. void MarlinUI::media_changed(const uint8_t old_status, const uint8_t status) {
  1299. if (old_status == status) {
  1300. TERN_(EXTENSIBLE_UI, ExtUI::onMediaError()); // Failed to mount/unmount
  1301. return;
  1302. }
  1303. if (status) {
  1304. if (old_status < 2) {
  1305. TERN_(EXTENSIBLE_UI, ExtUI::onMediaInserted()); // ExtUI response
  1306. set_status_P(GET_TEXT(MSG_MEDIA_INSERTED));
  1307. }
  1308. }
  1309. else {
  1310. if (old_status < 2) {
  1311. TERN_(EXTENSIBLE_UI, ExtUI::onMediaRemoved()); // ExtUI response
  1312. #if PIN_EXISTS(SD_DETECT)
  1313. set_status_P(GET_TEXT(MSG_MEDIA_REMOVED));
  1314. #if HAS_LCD_MENU
  1315. if (!defer_return_to_status) return_to_status();
  1316. #endif
  1317. #endif
  1318. }
  1319. }
  1320. #if PIN_EXISTS(SD_DETECT) && DISABLED(NO_LCD_REINIT)
  1321. init_lcd(); // Revive a noisy shared SPI LCD
  1322. #endif
  1323. refresh();
  1324. #if HAS_WIRED_LCD || defined(LED_BACKLIGHT_TIMEOUT)
  1325. const millis_t ms = millis();
  1326. #endif
  1327. TERN_(HAS_WIRED_LCD, next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL); // Delay LCD update for SD activity
  1328. #ifdef LED_BACKLIGHT_TIMEOUT
  1329. leds.reset_timeout(ms);
  1330. #endif
  1331. }
  1332. #endif // SDSUPPORT
  1333. #if HAS_LCD_MENU
  1334. void MarlinUI::reset_settings() { settings.reset(); completion_feedback(); }
  1335. #endif
  1336. #if ENABLED(EEPROM_SETTINGS)
  1337. #if HAS_LCD_MENU
  1338. void MarlinUI::init_eeprom() {
  1339. const bool good = settings.init_eeprom();
  1340. completion_feedback(good);
  1341. return_to_status();
  1342. }
  1343. void MarlinUI::load_settings() {
  1344. const bool good = settings.load();
  1345. completion_feedback(good);
  1346. }
  1347. void MarlinUI::store_settings() {
  1348. const bool good = settings.save();
  1349. completion_feedback(good);
  1350. }
  1351. #endif
  1352. #if DISABLED(EEPROM_AUTO_INIT)
  1353. static inline PGM_P eeprom_err(const uint8_t msgid) {
  1354. switch (msgid) {
  1355. default:
  1356. case 0: return GET_TEXT(MSG_ERR_EEPROM_CRC);
  1357. case 1: return GET_TEXT(MSG_ERR_EEPROM_INDEX);
  1358. case 2: return GET_TEXT(MSG_ERR_EEPROM_VERSION);
  1359. }
  1360. }
  1361. void MarlinUI::eeprom_alert(const uint8_t msgid) {
  1362. #if HAS_LCD_MENU
  1363. editable.uint8 = msgid;
  1364. goto_screen([]{
  1365. PGM_P const restore_msg = GET_TEXT(MSG_INIT_EEPROM);
  1366. char msg[utf8_strlen_P(restore_msg) + 1];
  1367. strcpy_P(msg, restore_msg);
  1368. MenuItem_confirm::select_screen(
  1369. GET_TEXT(MSG_BUTTON_RESET), GET_TEXT(MSG_BUTTON_IGNORE),
  1370. init_eeprom, return_to_status,
  1371. eeprom_err(editable.uint8), msg, PSTR("?")
  1372. );
  1373. });
  1374. #else
  1375. set_status_P(eeprom_err(msgid));
  1376. #endif
  1377. }
  1378. #endif // EEPROM_AUTO_INIT
  1379. #endif // EEPROM_SETTINGS