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 63KB

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