My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

marlinui.cpp 63KB

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