My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

marlinui.cpp 62KB

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