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

marlinui.cpp 62KB

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