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

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