My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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