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

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