My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

marlinui.cpp 55KB

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