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

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