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.

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