My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

marlinui.cpp 62KB

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