My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

marlinui.cpp 55KB

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