My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

marlinui.cpp 62KB

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