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

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