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.

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. #elif ENABLED(DWIN_CREALITY_LCD_JYERSUI)
  45. #include "e3v2/jyersui/dwin.h"
  46. #endif
  47. #if ENABLED(LCD_PROGRESS_BAR) && !IS_TFTGLCD_PANEL
  48. #define BASIC_PROGRESS_BAR 1
  49. #endif
  50. #if ANY(HAS_DISPLAY, HAS_STATUS_MESSAGE, BASIC_PROGRESS_BAR)
  51. #include "../module/printcounter.h"
  52. #endif
  53. #if LCD_HAS_WAIT_FOR_MOVE
  54. bool MarlinUI::wait_for_move; // = false
  55. #endif
  56. constexpr uint8_t epps = ENCODER_PULSES_PER_STEP;
  57. #if HAS_STATUS_MESSAGE
  58. #if ENABLED(STATUS_MESSAGE_SCROLLING) && EITHER(HAS_WIRED_LCD, DWIN_LCD_PROUI)
  59. uint8_t MarlinUI::status_scroll_offset; // = 0
  60. #endif
  61. char MarlinUI::status_message[MAX_MESSAGE_LENGTH + 1];
  62. uint8_t MarlinUI::alert_level; // = 0
  63. #if HAS_STATUS_MESSAGE_TIMEOUT
  64. millis_t MarlinUI::status_message_expire_ms; // = 0
  65. #endif
  66. statusResetFunc_t MarlinUI::status_reset_callback; // = nullptr
  67. #endif
  68. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  69. MarlinUI::progress_t MarlinUI::progress_override; // = 0
  70. #if ENABLED(USE_M73_REMAINING_TIME)
  71. uint32_t MarlinUI::remaining_time;
  72. #endif
  73. #endif
  74. #if HAS_MULTI_LANGUAGE
  75. uint8_t MarlinUI::language; // Initialized by settings.load()
  76. void MarlinUI::set_language(const uint8_t lang) {
  77. if (lang < NUM_LANGUAGES) {
  78. language = lang;
  79. TERN_(HAS_MARLINUI_U8GLIB, update_language_font());
  80. return_to_status();
  81. refresh();
  82. }
  83. }
  84. #endif
  85. #if HAS_LCD_CONTRAST
  86. uint8_t MarlinUI::contrast = LCD_CONTRAST_DEFAULT; // Initialized by settings.load()
  87. void MarlinUI::set_contrast(const uint8_t value) {
  88. contrast = constrain(value, LCD_CONTRAST_MIN, LCD_CONTRAST_MAX);
  89. _set_contrast();
  90. }
  91. #endif
  92. #if HAS_LCD_BRIGHTNESS
  93. uint8_t MarlinUI::brightness = LCD_BRIGHTNESS_DEFAULT;
  94. bool MarlinUI::backlight = true;
  95. void MarlinUI::set_brightness(const uint8_t value) {
  96. backlight = !!value;
  97. if (backlight) brightness = constrain(value, LCD_BRIGHTNESS_MIN, LCD_BRIGHTNESS_MAX);
  98. _set_brightness();
  99. }
  100. #endif
  101. #if ENABLED(SOUND_MENU_ITEM)
  102. bool MarlinUI::sound_on = ENABLED(SOUND_ON_DEFAULT);
  103. #endif
  104. #if ENABLED(PCA9632_BUZZER)
  105. void MarlinUI::buzz(const long duration, const uint16_t freq) {
  106. if (sound_on) PCA9632_buzz(duration, freq);
  107. }
  108. #endif
  109. #if HAS_PREHEAT
  110. #include "../module/temperature.h"
  111. preheat_t MarlinUI::material_preset[PREHEAT_COUNT]; // Initialized by settings.load()
  112. FSTR_P MarlinUI::get_preheat_label(const uint8_t m) {
  113. #define _PDEF(N) static PGMSTR(preheat_##N##_label, PREHEAT_##N##_LABEL);
  114. #define _PLBL(N) preheat_##N##_label,
  115. REPEAT_1(PREHEAT_COUNT, _PDEF);
  116. static PGM_P const preheat_labels[PREHEAT_COUNT] PROGMEM = { REPEAT_1(PREHEAT_COUNT, _PLBL) };
  117. return FPSTR((PGM_P)pgm_read_ptr(&preheat_labels[m]));
  118. }
  119. void MarlinUI::apply_preheat(const uint8_t m, const uint8_t pmask, const uint8_t e/*=active_extruder*/) {
  120. const preheat_t &pre = material_preset[m];
  121. TERN_(HAS_HOTEND, if (TEST(pmask, PT_HOTEND)) thermalManager.setTargetHotend(pre.hotend_temp, e));
  122. TERN_(HAS_HEATED_BED, if (TEST(pmask, PT_BED)) thermalManager.setTargetBed(pre.bed_temp));
  123. //TERN_(HAS_HEATED_CHAMBER, if (TEST(pmask, PT_CHAMBER)) thermalManager.setTargetBed(pre.chamber_temp));
  124. TERN_(HAS_FAN, if (TEST(pmask, PT_FAN)) thermalManager.set_fan_speed(0, pre.fan_speed));
  125. }
  126. #endif
  127. #if EITHER(HAS_MARLINUI_MENU, EXTENSIBLE_UI)
  128. bool MarlinUI::lcd_clicked;
  129. #endif
  130. #if EITHER(HAS_WIRED_LCD, DWIN_CREALITY_LCD_JYERSUI)
  131. bool MarlinUI::get_blink() {
  132. static uint8_t blink = 0;
  133. static millis_t next_blink_ms = 0;
  134. millis_t ms = millis();
  135. if (ELAPSED(ms, next_blink_ms)) {
  136. blink ^= 0xFF;
  137. next_blink_ms = ms + 1000 - (LCD_UPDATE_INTERVAL) / 2;
  138. }
  139. return blink != 0;
  140. }
  141. #endif
  142. // Encoder Handling
  143. #if HAS_ENCODER_ACTION
  144. uint32_t MarlinUI::encoderPosition;
  145. volatile int8_t encoderDiff; // Updated in update_buttons, added to encoderPosition every LCD update
  146. #endif
  147. #if LCD_BACKLIGHT_TIMEOUT_MINS
  148. constexpr uint8_t MarlinUI::backlight_timeout_min, MarlinUI::backlight_timeout_max;
  149. uint8_t MarlinUI::backlight_timeout_minutes; // Initialized by settings.load()
  150. millis_t MarlinUI::backlight_off_ms = 0;
  151. void MarlinUI::refresh_backlight_timeout() {
  152. backlight_off_ms = backlight_timeout_minutes ? millis() + backlight_timeout_minutes * 60UL * 1000UL : 0;
  153. WRITE(LCD_BACKLIGHT_PIN, HIGH);
  154. }
  155. #elif HAS_DISPLAY_SLEEP
  156. constexpr uint8_t MarlinUI::sleep_timeout_min, MarlinUI::sleep_timeout_max;
  157. uint8_t MarlinUI::sleep_timeout_minutes; // Initialized by settings.load()
  158. millis_t MarlinUI::screen_timeout_millis = 0;
  159. void MarlinUI::refresh_screen_timeout() {
  160. screen_timeout_millis = sleep_timeout_minutes ? millis() + sleep_timeout_minutes * 60UL * 1000UL : 0;
  161. sleep_display(false);
  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. const uint8_t *p = (uint8_t*)string;
  332. lchar_t wc;
  333. if (wordwrap) {
  334. const uint8_t *wrd = nullptr;
  335. uint8_t c = 0;
  336. // find the end of the part
  337. for (;;) {
  338. if (!wrd) wrd = p; // Get word start /before/ advancing
  339. p = get_utf8_value_cb(p, cb_read_byte, wc);
  340. const bool eol = !wc; // zero ends the string
  341. // End or a break between phrases?
  342. if (eol || wc == ' ' || wc == '-' || wc == '+' || wc == '.') {
  343. if (!c && wc == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
  344. // Past the right and the word is not too long?
  345. if (col + c > LCD_WIDTH && col >= (LCD_WIDTH) / 4) _newline(); // should it wrap?
  346. c += !eol; // +1 so the space will be printed
  347. col += c; // advance col to new position
  348. while (c) { // character countdown
  349. --c; // count down to zero
  350. wrd = get_utf8_value_cb(wrd, cb_read_byte, wc); // get characters again
  351. lcd_put_lchar(wc); // character to the LCD
  352. }
  353. if (eol) break; // all done!
  354. wrd = nullptr; // set up for next word
  355. }
  356. else c++; // count word characters
  357. }
  358. }
  359. else {
  360. for (;;) {
  361. p = get_utf8_value_cb(p, cb_read_byte, wc);
  362. if (!wc) break;
  363. lcd_put_lchar(wc);
  364. col++;
  365. if (col >= LCD_WIDTH) _newline();
  366. }
  367. }
  368. }
  369. void MarlinUI::draw_select_screen_prompt(FSTR_P const pref, const char * const string/*=nullptr*/, FSTR_P const suff/*=nullptr*/) {
  370. const uint8_t plen = utf8_strlen(pref), slen = suff ? utf8_strlen(suff) : 0;
  371. uint8_t col = 0, row = 0;
  372. if (!string && plen + slen <= LCD_WIDTH) {
  373. col = (LCD_WIDTH - plen - slen) / 2;
  374. row = LCD_HEIGHT > 3 ? 1 : 0;
  375. }
  376. if (LCD_HEIGHT >= 8) row = LCD_HEIGHT / 2 - 2;
  377. wrap_string_P(col, row, FTOP(pref), true);
  378. if (string) {
  379. if (col) { col = 0; row++; } // Move to the start of the next line
  380. wrap_string(col, row, string);
  381. }
  382. if (suff) wrap_string_P(col, row, FTOP(suff));
  383. }
  384. #endif // !HAS_GRAPHICAL_TFT
  385. #endif // HAS_MARLINUI_MENU
  386. ////////////////////////////////////////////
  387. ///////////// Keypad Handling //////////////
  388. ////////////////////////////////////////////
  389. #if IS_RRW_KEYPAD && HAS_ENCODER_ACTION
  390. volatile uint8_t MarlinUI::keypad_buttons;
  391. #if HAS_MARLINUI_MENU && !HAS_ADC_BUTTONS
  392. void _reprapworld_keypad_move(const AxisEnum axis, const int16_t dir) {
  393. ui.manual_move.menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  394. ui.encoderPosition = dir;
  395. switch (axis) {
  396. case X_AXIS:
  397. TERN_(HAS_Y_AXIS, case Y_AXIS:)
  398. TERN_(HAS_Z_AXIS, case Z_AXIS:)
  399. lcd_move_axis(axis);
  400. default: break;
  401. }
  402. }
  403. #endif
  404. bool MarlinUI::handle_keypad() {
  405. #if HAS_ADC_BUTTONS
  406. #define ADC_MIN_KEY_DELAY 100
  407. if (keypad_buttons) {
  408. #if HAS_ENCODER_ACTION
  409. refresh(LCDVIEW_REDRAW_NOW);
  410. #if HAS_MARLINUI_MENU
  411. if (encoderDirection == -(ENCODERBASE)) { // HAS_ADC_BUTTONS forces REVERSE_MENU_DIRECTION, so this indicates menu navigation
  412. if (RRK(EN_KEYPAD_UP)) encoderPosition += ENCODER_STEPS_PER_MENU_ITEM;
  413. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition -= ENCODER_STEPS_PER_MENU_ITEM;
  414. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  415. else if (RRK(EN_KEYPAD_RIGHT)) { return_to_status(); quick_feedback(); }
  416. }
  417. else
  418. #endif
  419. {
  420. #if HAS_MARLINUI_MENU
  421. if (RRK(EN_KEYPAD_UP)) encoderPosition -= epps;
  422. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition += epps;
  423. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  424. else if (RRK(EN_KEYPAD_RIGHT)) encoderPosition = 0;
  425. #else
  426. if (RRK(EN_KEYPAD_UP) || RRK(EN_KEYPAD_LEFT)) encoderPosition -= epps;
  427. else if (RRK(EN_KEYPAD_DOWN) || RRK(EN_KEYPAD_RIGHT)) encoderPosition += epps;
  428. #endif
  429. }
  430. #endif
  431. next_button_update_ms = millis() + ADC_MIN_KEY_DELAY;
  432. return true;
  433. }
  434. #else // !HAS_ADC_BUTTONS
  435. static uint8_t keypad_debounce = 0;
  436. if (!RRK( EN_KEYPAD_F1 | EN_KEYPAD_F2
  437. | EN_KEYPAD_F3 | EN_KEYPAD_DOWN
  438. | EN_KEYPAD_RIGHT | EN_KEYPAD_MIDDLE
  439. | EN_KEYPAD_UP | EN_KEYPAD_LEFT )
  440. ) {
  441. if (keypad_debounce > 0) keypad_debounce--;
  442. }
  443. else if (!keypad_debounce) {
  444. keypad_debounce = 2;
  445. const bool homed = all_axes_homed();
  446. #if HAS_MARLINUI_MENU
  447. if (RRK(EN_KEYPAD_MIDDLE)) goto_screen(menu_move);
  448. #if NONE(DELTA, Z_HOME_TO_MAX)
  449. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  450. #endif
  451. if (homed) {
  452. #if EITHER(DELTA, Z_HOME_TO_MAX)
  453. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  454. #endif
  455. if (RRK(EN_KEYPAD_F3)) _reprapworld_keypad_move(Z_AXIS, -1);
  456. if (RRK(EN_KEYPAD_LEFT)) _reprapworld_keypad_move(X_AXIS, -1);
  457. if (RRK(EN_KEYPAD_RIGHT)) _reprapworld_keypad_move(X_AXIS, 1);
  458. if (RRK(EN_KEYPAD_DOWN)) _reprapworld_keypad_move(Y_AXIS, 1);
  459. if (RRK(EN_KEYPAD_UP)) _reprapworld_keypad_move(Y_AXIS, -1);
  460. }
  461. #endif // HAS_MARLINUI_MENU
  462. if (!homed && RRK(EN_KEYPAD_F1)) queue.inject_P(G28_STR);
  463. return true;
  464. }
  465. #endif // !HAS_ADC_BUTTONS
  466. return false;
  467. }
  468. #endif // IS_RRW_KEYPAD && HAS_ENCODER_ACTION
  469. /**
  470. * Status Screen
  471. *
  472. * This is very display-dependent, so the lcd implementation draws this.
  473. */
  474. #if BASIC_PROGRESS_BAR
  475. millis_t MarlinUI::progress_bar_ms; // = 0
  476. #if PROGRESS_MSG_EXPIRE > 0
  477. millis_t MarlinUI::expire_status_ms; // = 0
  478. #endif
  479. #endif
  480. void MarlinUI::status_screen() {
  481. TERN_(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLY(false));
  482. #if BASIC_PROGRESS_BAR
  483. //
  484. // HD44780 implements the following message blinking and
  485. // message expiration because Status Line and Progress Bar
  486. // share the same line on the display.
  487. //
  488. #if DISABLED(PROGRESS_MSG_ONCE) || PROGRESS_MSG_EXPIRE > 0
  489. #define GOT_MS
  490. const millis_t ms = millis();
  491. #endif
  492. // If the message will blink rather than expire...
  493. #if DISABLED(PROGRESS_MSG_ONCE)
  494. if (ELAPSED(ms, progress_bar_ms + PROGRESS_BAR_MSG_TIME + PROGRESS_BAR_BAR_TIME))
  495. progress_bar_ms = ms;
  496. #endif
  497. #if PROGRESS_MSG_EXPIRE > 0
  498. // Handle message expire
  499. if (expire_status_ms) {
  500. // Expire the message if a job is active and the bar has ticks
  501. if (get_progress_percent() > 2 && !print_job_timer.isPaused()) {
  502. if (ELAPSED(ms, expire_status_ms)) {
  503. status_message[0] = '\0';
  504. expire_status_ms = 0;
  505. }
  506. }
  507. else {
  508. // Defer message expiration before bar appears
  509. // and during any pause (not just SD)
  510. expire_status_ms += LCD_UPDATE_INTERVAL;
  511. }
  512. }
  513. #endif // PROGRESS_MSG_EXPIRE
  514. #endif // BASIC_PROGRESS_BAR
  515. bool did_expire = status_reset_callback && (*status_reset_callback)();
  516. #if HAS_STATUS_MESSAGE_TIMEOUT
  517. #ifndef GOT_MS
  518. #define GOT_MS
  519. const millis_t ms = millis();
  520. #endif
  521. did_expire |= status_message_expire_ms && ELAPSED(ms, status_message_expire_ms);
  522. #endif
  523. if (did_expire) reset_status();
  524. #if HAS_MARLINUI_MENU
  525. if (use_click()) {
  526. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  527. next_filament_display = millis() + 5000UL; // Show status message for 5s
  528. #endif
  529. goto_screen(menu_main);
  530. reinit_lcd(); // Revive a noisy shared SPI LCD
  531. return;
  532. }
  533. #endif
  534. #if ENABLED(ULTIPANEL_FEEDMULTIPLY)
  535. const int16_t old_frm = feedrate_percentage;
  536. int16_t new_frm = old_frm + int16_t(encoderPosition);
  537. // Dead zone at 100% feedrate
  538. if (old_frm == 100) {
  539. if (int16_t(encoderPosition) > ENCODER_FEEDRATE_DEADZONE)
  540. new_frm -= ENCODER_FEEDRATE_DEADZONE;
  541. else if (int16_t(encoderPosition) < -(ENCODER_FEEDRATE_DEADZONE))
  542. new_frm += ENCODER_FEEDRATE_DEADZONE;
  543. else
  544. new_frm = old_frm;
  545. }
  546. else if ((old_frm < 100 && new_frm > 100) || (old_frm > 100 && new_frm < 100))
  547. new_frm = 100;
  548. LIMIT(new_frm, 10, 999);
  549. if (old_frm != new_frm) {
  550. feedrate_percentage = new_frm;
  551. encoderPosition = 0;
  552. #if BOTH(HAS_SOUND, BEEP_ON_FEEDRATE_CHANGE)
  553. static millis_t next_beep;
  554. #ifndef GOT_MS
  555. const millis_t ms = millis();
  556. #endif
  557. if (ELAPSED(ms, next_beep)) {
  558. BUZZ(FEEDRATE_CHANGE_BEEP_DURATION, FEEDRATE_CHANGE_BEEP_FREQUENCY);
  559. next_beep = ms + 500UL;
  560. }
  561. #endif
  562. }
  563. #endif // ULTIPANEL_FEEDMULTIPLY
  564. draw_status_screen();
  565. }
  566. void MarlinUI::kill_screen(FSTR_P const lcd_error, FSTR_P const lcd_component) {
  567. init();
  568. status_printf(1, F(S_FMT ": " S_FMT), FTOP(lcd_error), FTOP(lcd_component));
  569. TERN_(HAS_MARLINUI_MENU, return_to_status());
  570. // RED ALERT. RED ALERT.
  571. #if ENABLED(PRINTER_EVENT_LEDS)
  572. leds.set_color(LEDColorRed());
  573. #ifdef NEOPIXEL_BKGD_INDEX_FIRST
  574. neo.set_background_color(255, 0, 0, 0);
  575. neo.show();
  576. #endif
  577. #endif
  578. draw_kill_screen();
  579. }
  580. #if HAS_TOUCH_SLEEP
  581. #if HAS_TOUCH_BUTTONS
  582. #include "touch/touch_buttons.h"
  583. #else
  584. #include "tft/touch.h"
  585. #endif
  586. // Wake up a sleeping TFT
  587. void MarlinUI::wakeup_screen() {
  588. TERN(HAS_TOUCH_BUTTONS, touchBt.wakeUp(), touch.wakeUp());
  589. }
  590. #endif
  591. void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
  592. TERN_(HAS_TOUCH_SLEEP, wakeup_screen()); // Wake up the TFT with most buttons
  593. TERN_(HAS_MARLINUI_MENU, refresh());
  594. #if HAS_ENCODER_ACTION
  595. if (clear_buttons)
  596. TERN_(HAS_ADC_BUTTONS, keypad_buttons =) buttons = 0;
  597. next_button_update_ms = millis() + 500;
  598. #else
  599. UNUSED(clear_buttons);
  600. #endif
  601. chirp(); // Buzz and wait. Is the delay needed for buttons to settle?
  602. #if HAS_CHIRP && HAS_MARLINUI_MENU
  603. #if HAS_BEEPER
  604. for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
  605. #else
  606. delay(10);
  607. #endif
  608. #endif
  609. }
  610. ////////////////////////////////////////////
  611. /////////////// Manual Move ////////////////
  612. ////////////////////////////////////////////
  613. #if HAS_MARLINUI_MENU
  614. ManualMove MarlinUI::manual_move{};
  615. millis_t ManualMove::start_time = 0;
  616. float ManualMove::menu_scale = 1;
  617. screenFunc_t ManualMove::screen_ptr;
  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. #if ENABLED(MANUAL_E_MOVES_RELATIVE)
  628. float ManualMove::e_origin = 0;
  629. #endif
  630. /**
  631. * If a manual move has been posted and its time has arrived, and if the planner
  632. * has a space for it, then add a linear move to current_position the planner.
  633. *
  634. * If any manual move needs to be interrupted, make sure to force a manual move
  635. * by setting manual_move.start_time to millis() after updating current_position.
  636. *
  637. * To post a manual move:
  638. * - Update current_position to the new place you want to go.
  639. * - Set manual_move.axis to an axis like X_AXIS. Use ALL_AXES_ENUM for diagonal moves.
  640. * - Set manual_move.start_time to a point in the future (in ms) when the move should be done.
  641. *
  642. * For kinematic machines:
  643. * - Set manual_move.offset to modify one axis and post the move.
  644. * This is used to achieve more rapid stepping on kinematic machines.
  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_CHAR(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_CHAR(axis)), " soon.");
  695. }
  696. #if ENABLED(AUTO_BED_LEVELING_UBL)
  697. void MarlinUI::external_encoder() {
  698. if (external_control && encoderDiff) {
  699. bedlevel.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_MINS
  859. refresh_backlight_timeout();
  860. #elif HAS_DISPLAY_SLEEP
  861. refresh_screen_timeout();
  862. #endif
  863. refresh(LCDVIEW_REDRAW_NOW);
  864. #if LED_POWEROFF_TIMEOUT > 0
  865. if (!powerManager.psu_on) leds.reset_timeout(ms);
  866. #endif
  867. } // encoder activity
  868. #endif // HAS_ENCODER_ACTION
  869. // This runs every ~100ms when idling often enough.
  870. // Instead of tracking changes just redraw the Status Screen once per second.
  871. if (on_status_screen() && !lcd_status_update_delay--) {
  872. lcd_status_update_delay = TERN(HAS_MARLINUI_U8GLIB, 12, 9);
  873. if (max_display_update_time) max_display_update_time--; // Be sure never go to a very big number
  874. refresh(LCDVIEW_REDRAW_NOW);
  875. }
  876. #if BOTH(HAS_MARLINUI_MENU, SCROLL_LONG_FILENAMES)
  877. // If scrolling of long file names is enabled and we are in the sd card menu,
  878. // cause a refresh to occur until all the text has scrolled into view.
  879. if (currentScreen == menu_media && !lcd_status_update_delay--) {
  880. lcd_status_update_delay = ++filename_scroll_pos >= filename_scroll_max ? 12 : 4; // Long delay at end and start
  881. if (filename_scroll_pos > filename_scroll_max) filename_scroll_pos = 0;
  882. refresh(LCDVIEW_REDRAW_NOW);
  883. reset_status_timeout(ms);
  884. }
  885. #endif
  886. // Then we want to use only 50% of the time
  887. const uint16_t bbr2 = planner.block_buffer_runtime() >> 1;
  888. if ((should_draw() || drawing_screen) && (!bbr2 || bbr2 > max_display_update_time)) {
  889. // Change state of drawing flag between screen updates
  890. if (!drawing_screen) switch (lcdDrawUpdate) {
  891. case LCDVIEW_CALL_NO_REDRAW:
  892. refresh(LCDVIEW_NONE);
  893. break;
  894. case LCDVIEW_CLEAR_CALL_REDRAW:
  895. case LCDVIEW_CALL_REDRAW_NEXT:
  896. refresh(LCDVIEW_REDRAW_NOW);
  897. case LCDVIEW_REDRAW_NOW: // set above, or by a handler through LCDVIEW_CALL_REDRAW_NEXT
  898. case LCDVIEW_NONE:
  899. break;
  900. } // switch
  901. TERN_(HAS_ADC_BUTTONS, keypad_buttons = 0);
  902. #if HAS_MARLINUI_U8GLIB
  903. #if ENABLED(LIGHTWEIGHT_UI)
  904. const bool in_status = on_status_screen(),
  905. do_u8g_loop = !in_status;
  906. lcd_in_status(in_status);
  907. if (in_status) status_screen();
  908. #else
  909. constexpr bool do_u8g_loop = true;
  910. #endif
  911. if (do_u8g_loop) {
  912. if (!drawing_screen) { // If not already drawing pages
  913. u8g.firstPage(); // Start the first page
  914. drawing_screen = first_page = true; // Flag as drawing pages
  915. }
  916. set_font(FONT_MENU); // Setup font for every page draw
  917. u8g.setColorIndex(1); // And reset the color
  918. run_current_screen(); // Draw and process the current screen
  919. first_page = false;
  920. // The screen handler can clear drawing_screen for an action that changes the screen.
  921. // If still drawing and there's another page, update max-time and return now.
  922. // The nextPage will already be set up on the next call.
  923. if (drawing_screen && (drawing_screen = u8g.nextPage())) {
  924. if (on_status_screen())
  925. NOLESS(max_display_update_time, millis() - ms);
  926. return;
  927. }
  928. }
  929. #else
  930. run_current_screen();
  931. // Apply all DWIN drawing after processing
  932. TERN_(IS_DWIN_MARLINUI, DWIN_UpdateLCD());
  933. #endif
  934. TERN_(HAS_MARLINUI_MENU, lcd_clicked = false);
  935. // Keeping track of the longest time for an individual LCD update.
  936. // Used to do screen throttling when the planner starts to fill up.
  937. if (on_status_screen())
  938. NOLESS(max_display_update_time, millis() - ms);
  939. }
  940. #if SCREENS_CAN_TIME_OUT
  941. // Return to Status Screen after a timeout
  942. if (on_status_screen() || defer_return_to_status)
  943. reset_status_timeout(ms);
  944. else if (ELAPSED(ms, return_to_status_ms))
  945. return_to_status();
  946. #endif
  947. #if LCD_BACKLIGHT_TIMEOUT_MINS
  948. if (backlight_off_ms && ELAPSED(ms, backlight_off_ms)) {
  949. WRITE(LCD_BACKLIGHT_PIN, LOW); // Backlight off
  950. backlight_off_ms = 0;
  951. }
  952. #elif HAS_DISPLAY_SLEEP
  953. if (screen_timeout_millis && ELAPSED(ms, screen_timeout_millis))
  954. sleep_display();
  955. #endif
  956. // Change state of drawing flag between screen updates
  957. if (!drawing_screen) switch (lcdDrawUpdate) {
  958. case LCDVIEW_CLEAR_CALL_REDRAW:
  959. clear_lcd(); break;
  960. case LCDVIEW_REDRAW_NOW:
  961. refresh(LCDVIEW_NONE);
  962. case LCDVIEW_NONE:
  963. case LCDVIEW_CALL_REDRAW_NEXT:
  964. case LCDVIEW_CALL_NO_REDRAW:
  965. default: break;
  966. } // switch
  967. } // ELAPSED(ms, next_lcd_update_ms)
  968. TERN_(HAS_GRAPHICAL_TFT, tft_idle());
  969. }
  970. #if HAS_ADC_BUTTONS
  971. typedef struct {
  972. raw_adc_t ADCKeyValueMin, ADCKeyValueMax;
  973. uint8_t ADCKeyNo;
  974. } _stADCKeypadTable_;
  975. #ifndef ADC_BUTTONS_VALUE_SCALE
  976. #define ADC_BUTTONS_VALUE_SCALE 1.0 // for the power voltage equal to the reference voltage
  977. #endif
  978. #ifndef ADC_BUTTONS_R_PULLUP
  979. #define ADC_BUTTONS_R_PULLUP 4.7 // common pull-up resistor in the voltage divider
  980. #endif
  981. #ifndef ADC_BUTTONS_LEFT_R_PULLDOWN
  982. #define ADC_BUTTONS_LEFT_R_PULLDOWN 0.47 // pull-down resistor for LEFT button voltage divider
  983. #endif
  984. #ifndef ADC_BUTTONS_RIGHT_R_PULLDOWN
  985. #define ADC_BUTTONS_RIGHT_R_PULLDOWN 4.7 // pull-down resistor for RIGHT button voltage divider
  986. #endif
  987. #ifndef ADC_BUTTONS_UP_R_PULLDOWN
  988. #define ADC_BUTTONS_UP_R_PULLDOWN 1.0 // pull-down resistor for UP button voltage divider
  989. #endif
  990. #ifndef ADC_BUTTONS_DOWN_R_PULLDOWN
  991. #define ADC_BUTTONS_DOWN_R_PULLDOWN 10.0 // pull-down resistor for DOWN button voltage divider
  992. #endif
  993. #ifndef ADC_BUTTONS_MIDDLE_R_PULLDOWN
  994. #define ADC_BUTTONS_MIDDLE_R_PULLDOWN 2.2 // pull-down resistor for MIDDLE button voltage divider
  995. #endif
  996. // Calculate the ADC value for the voltage divider with specified pull-down resistor value
  997. #define ADC_BUTTON_VALUE(r) raw_adc_t(HAL_ADC_RANGE * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP))
  998. static constexpr raw_adc_t adc_button_tolerance = HAL_ADC_RANGE * 25 / 1024,
  999. adc_other_button = raw_adc_t(uint32_t(HAL_ADC_RANGE * 1000UL) / 1024UL);
  1000. static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
  1001. // VALUE_MIN, VALUE_MAX, KEY
  1002. { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F1 }, // F1
  1003. { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F2 }, // F2
  1004. { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F3 }, // F3
  1005. { ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) - adc_button_tolerance,
  1006. ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_LEFT }, // LEFT ( 272 ... 472)
  1007. { ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) - adc_button_tolerance,
  1008. ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_RIGHT }, // RIGHT (1948 ... 2148)
  1009. { ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) - adc_button_tolerance,
  1010. ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_UP }, // UP ( 618 ... 818)
  1011. { ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) - adc_button_tolerance,
  1012. ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_DOWN }, // DOWN (2686 ... 2886)
  1013. { ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) - adc_button_tolerance,
  1014. ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) + adc_button_tolerance, 1 + BLEN_KEYPAD_MIDDLE }, // ENTER (1205 ... 1405)
  1015. };
  1016. uint8_t get_ADC_keyValue() {
  1017. if (thermalManager.ADCKey_count >= 16) {
  1018. const raw_adc_t currentkpADCValue = thermalManager.current_ADCKey_raw;
  1019. thermalManager.current_ADCKey_raw = HAL_ADC_RANGE;
  1020. thermalManager.ADCKey_count = 0;
  1021. if (currentkpADCValue < adc_other_button)
  1022. LOOP_L_N(i, ADC_KEY_NUM) {
  1023. const raw_adc_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
  1024. hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
  1025. if (WITHIN(currentkpADCValue, lo, hi)) return pgm_read_byte(&stADCKeyTable[i].ADCKeyNo);
  1026. }
  1027. }
  1028. return 0;
  1029. }
  1030. #endif // HAS_ADC_BUTTONS
  1031. #if HAS_ENCODER_ACTION
  1032. /**
  1033. * Read encoder buttons from the hardware registers
  1034. * Warning: This function is called from interrupt context!
  1035. */
  1036. void MarlinUI::update_buttons() {
  1037. const millis_t now = millis();
  1038. if (ELAPSED(now, next_button_update_ms)) {
  1039. #if HAS_DIGITAL_BUTTONS
  1040. #if ANY_BUTTON(EN1, EN2, ENC, BACK)
  1041. uint8_t newbutton = 0;
  1042. if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
  1043. if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
  1044. if (can_encode() && BUTTON_PRESSED(ENC)) newbutton |= EN_C;
  1045. if (BUTTON_PRESSED(BACK)) newbutton |= EN_D;
  1046. #else
  1047. constexpr uint8_t newbutton = 0;
  1048. #endif
  1049. //
  1050. // Directional buttons
  1051. //
  1052. #if ANY_BUTTON(UP, DWN, LFT, RT)
  1053. const int8_t pulses = epps * encoderDirection;
  1054. if (BUTTON_PRESSED(UP)) {
  1055. encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * pulses;
  1056. next_button_update_ms = now + 300;
  1057. }
  1058. else if (BUTTON_PRESSED(DWN)) {
  1059. encoderDiff = -(ENCODER_STEPS_PER_MENU_ITEM) * pulses;
  1060. next_button_update_ms = now + 300;
  1061. }
  1062. else if (BUTTON_PRESSED(LFT)) {
  1063. encoderDiff = -pulses;
  1064. next_button_update_ms = now + 300;
  1065. }
  1066. else if (BUTTON_PRESSED(RT)) {
  1067. encoderDiff = pulses;
  1068. next_button_update_ms = now + 300;
  1069. }
  1070. #endif // UP || DWN || LFT || RT
  1071. buttons = (newbutton | TERN0(HAS_SLOW_BUTTONS, slow_buttons)
  1072. #if BOTH(HAS_TOUCH_BUTTONS, HAS_ENCODER_ACTION)
  1073. | (touch_buttons & TERN(HAS_ENCODER_WHEEL, ~(EN_A | EN_B), 0xFF))
  1074. #endif
  1075. );
  1076. #elif HAS_ADC_BUTTONS
  1077. buttons = 0;
  1078. #endif
  1079. #if HAS_ADC_BUTTONS
  1080. if (keypad_buttons == 0) {
  1081. const uint8_t b = get_ADC_keyValue();
  1082. if (WITHIN(b, 1, 8)) keypad_buttons = _BV(b - 1);
  1083. }
  1084. #endif
  1085. #if HAS_SHIFT_ENCODER
  1086. /**
  1087. * Set up Rotary Encoder bit values (for two pin encoders to indicate movement).
  1088. * These values are independent of which pins are used for EN_A / EN_B indications.
  1089. * The rotary encoder part is also independent of the LCD chipset.
  1090. */
  1091. uint8_t val = 0;
  1092. WRITE(SHIFT_LD_PIN, LOW);
  1093. WRITE(SHIFT_LD_PIN, HIGH);
  1094. LOOP_L_N(i, 8) {
  1095. val >>= 1;
  1096. if (READ(SHIFT_OUT_PIN)) SBI(val, 7);
  1097. WRITE(SHIFT_CLK_PIN, HIGH);
  1098. WRITE(SHIFT_CLK_PIN, LOW);
  1099. }
  1100. TERN(REPRAPWORLD_KEYPAD, keypad_buttons, buttons) = ~val;
  1101. #endif
  1102. #if IS_TFTGLCD_PANEL
  1103. next_button_update_ms = now + (LCD_UPDATE_INTERVAL / 2);
  1104. buttons = slow_buttons;
  1105. TERN_(AUTO_BED_LEVELING_UBL, external_encoder());
  1106. #endif
  1107. } // next_button_update_ms
  1108. #if HAS_ENCODER_WHEEL
  1109. static uint8_t lastEncoderBits;
  1110. // Manage encoder rotation
  1111. #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; }
  1112. uint8_t enc = 0;
  1113. if (buttons & EN_A) enc |= B01;
  1114. if (buttons & EN_B) enc |= B10;
  1115. if (enc != lastEncoderBits) {
  1116. switch (enc) {
  1117. case ENCODER_PHASE_0: ENCODER_SPIN(ENCODER_PHASE_3, ENCODER_PHASE_1); break;
  1118. case ENCODER_PHASE_1: ENCODER_SPIN(ENCODER_PHASE_0, ENCODER_PHASE_2); break;
  1119. case ENCODER_PHASE_2: ENCODER_SPIN(ENCODER_PHASE_1, ENCODER_PHASE_3); break;
  1120. case ENCODER_PHASE_3: ENCODER_SPIN(ENCODER_PHASE_2, ENCODER_PHASE_0); break;
  1121. }
  1122. #if BOTH(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL)
  1123. external_encoder();
  1124. #endif
  1125. lastEncoderBits = enc;
  1126. }
  1127. #endif // HAS_ENCODER_WHEEL
  1128. }
  1129. #endif // HAS_ENCODER_ACTION
  1130. #endif // HAS_WIRED_LCD
  1131. #if HAS_STATUS_MESSAGE
  1132. ////////////////////////////////////////////
  1133. ////////////// Status Message //////////////
  1134. ////////////////////////////////////////////
  1135. #if ENABLED(EXTENSIBLE_UI)
  1136. #include "extui/ui_api.h"
  1137. #endif
  1138. bool MarlinUI::has_status() { return (status_message[0] != '\0'); }
  1139. void MarlinUI::set_status(const char * const cstr, const bool persist) {
  1140. if (alert_level) return;
  1141. TERN_(HOST_STATUS_NOTIFICATIONS, hostui.notify(cstr));
  1142. // Here we have a problem. The message is encoded in UTF8, so
  1143. // arbitrarily cutting it will be a problem. We MUST be sure
  1144. // that there is no cutting in the middle of a multibyte character!
  1145. // Get a pointer to the null terminator
  1146. const char* pend = cstr + strlen(cstr);
  1147. // If length of supplied UTF8 string is greater than
  1148. // our buffer size, start cutting whole UTF8 chars
  1149. while ((pend - cstr) > MAX_MESSAGE_LENGTH) {
  1150. --pend;
  1151. while (!START_OF_UTF8_CHAR(*pend)) --pend;
  1152. };
  1153. // At this point, we have the proper cut point. Use it
  1154. uint8_t maxLen = pend - cstr;
  1155. strncpy(status_message, cstr, maxLen);
  1156. status_message[maxLen] = '\0';
  1157. finish_status(persist);
  1158. }
  1159. /**
  1160. * Reset the status message
  1161. */
  1162. void MarlinUI::reset_status(const bool no_welcome) {
  1163. #if SERVICE_INTERVAL_1 > 0
  1164. static PGMSTR(service1, "> " SERVICE_NAME_1 "!");
  1165. #endif
  1166. #if SERVICE_INTERVAL_2 > 0
  1167. static PGMSTR(service2, "> " SERVICE_NAME_2 "!");
  1168. #endif
  1169. #if SERVICE_INTERVAL_3 > 0
  1170. static PGMSTR(service3, "> " SERVICE_NAME_3 "!");
  1171. #endif
  1172. FSTR_P msg;
  1173. if (printingIsPaused())
  1174. msg = GET_TEXT_F(MSG_PRINT_PAUSED);
  1175. #if ENABLED(SDSUPPORT)
  1176. else if (IS_SD_PRINTING())
  1177. return set_status(card.longest_filename(), true);
  1178. #endif
  1179. else if (print_job_timer.isRunning())
  1180. msg = GET_TEXT_F(MSG_PRINTING);
  1181. #if SERVICE_INTERVAL_1 > 0
  1182. else if (print_job_timer.needsService(1)) msg = FPSTR(service1);
  1183. #endif
  1184. #if SERVICE_INTERVAL_2 > 0
  1185. else if (print_job_timer.needsService(2)) msg = FPSTR(service2);
  1186. #endif
  1187. #if SERVICE_INTERVAL_3 > 0
  1188. else if (print_job_timer.needsService(3)) msg = FPSTR(service3);
  1189. #endif
  1190. else if (!no_welcome) msg = GET_TEXT_F(WELCOME_MSG);
  1191. else if (ENABLED(DWIN_LCD_PROUI))
  1192. msg = F("");
  1193. else
  1194. return;
  1195. set_status(msg, -1);
  1196. }
  1197. /**
  1198. * Set Status with a fixed string and alert level.
  1199. * @param fstr A constant F-string to set as the status.
  1200. * @param level Alert level. Negative to ignore and reset the level. Non-zero never expires.
  1201. */
  1202. void MarlinUI::set_status(FSTR_P const fstr, int8_t level) {
  1203. // Alerts block lower priority messages
  1204. if (level < 0) level = alert_level = 0;
  1205. if (level < alert_level) return;
  1206. alert_level = level;
  1207. PGM_P const pstr = FTOP(fstr);
  1208. // Since the message is encoded in UTF8 it must
  1209. // only be cut on a character boundary.
  1210. // Get a pointer to the null terminator
  1211. PGM_P pend = pstr + strlen_P(pstr);
  1212. // If length of supplied UTF8 string is greater than
  1213. // the buffer size, start cutting whole UTF8 chars
  1214. while ((pend - pstr) > MAX_MESSAGE_LENGTH) {
  1215. --pend;
  1216. while (!START_OF_UTF8_CHAR(pgm_read_byte(pend))) --pend;
  1217. };
  1218. // At this point, we have the proper cut point. Use it
  1219. uint8_t maxLen = pend - pstr;
  1220. strncpy_P(status_message, pstr, maxLen);
  1221. status_message[maxLen] = '\0';
  1222. TERN_(HOST_STATUS_NOTIFICATIONS, hostui.notify(fstr));
  1223. finish_status(level > 0);
  1224. }
  1225. void MarlinUI::set_alert_status(FSTR_P const fstr) {
  1226. set_status(fstr, 1);
  1227. TERN_(HAS_TOUCH_SLEEP, wakeup_screen());
  1228. TERN_(HAS_MARLINUI_MENU, return_to_status());
  1229. }
  1230. #include <stdarg.h>
  1231. void MarlinUI::status_printf(int8_t level, FSTR_P const fmt, ...) {
  1232. // Alerts block lower priority messages
  1233. if (level < 0) level = alert_level = 0;
  1234. if (level < alert_level) return;
  1235. alert_level = level;
  1236. va_list args;
  1237. va_start(args, FTOP(fmt));
  1238. vsnprintf_P(status_message, MAX_MESSAGE_LENGTH, FTOP(fmt), args);
  1239. va_end(args);
  1240. TERN_(HOST_STATUS_NOTIFICATIONS, hostui.notify(status_message));
  1241. finish_status(level > 0);
  1242. }
  1243. void MarlinUI::finish_status(const bool persist) {
  1244. UNUSED(persist);
  1245. set_status_reset_fn();
  1246. TERN_(HAS_STATUS_MESSAGE_TIMEOUT, status_message_expire_ms = persist ? 0 : millis() + (STATUS_MESSAGE_TIMEOUT_SEC) * 1000UL);
  1247. #if HAS_WIRED_LCD
  1248. #if BASIC_PROGRESS_BAR || BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  1249. const millis_t ms = millis();
  1250. #endif
  1251. #if BASIC_PROGRESS_BAR
  1252. progress_bar_ms = ms;
  1253. #if PROGRESS_MSG_EXPIRE > 0
  1254. expire_status_ms = persist ? 0 : ms + PROGRESS_MSG_EXPIRE;
  1255. #endif
  1256. #endif
  1257. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  1258. next_filament_display = ms + 5000UL; // Show status message for 5s
  1259. #endif
  1260. #endif
  1261. #if ENABLED(STATUS_MESSAGE_SCROLLING) && EITHER(HAS_WIRED_LCD, DWIN_LCD_PROUI)
  1262. status_scroll_offset = 0;
  1263. #endif
  1264. TERN_(EXTENSIBLE_UI, ExtUI::onStatusChanged(status_message));
  1265. TERN_(DWIN_CREALITY_LCD, DWIN_StatusChanged(status_message));
  1266. TERN_(DWIN_LCD_PROUI, DWIN_CheckStatusMessage());
  1267. TERN_(DWIN_CREALITY_LCD_JYERSUI, CrealityDWIN.Update_Status(status_message));
  1268. }
  1269. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  1270. void MarlinUI::advance_status_scroll() {
  1271. // Advance by one UTF8 code-word
  1272. if (status_scroll_offset < utf8_strlen(status_message))
  1273. while (!START_OF_UTF8_CHAR(status_message[++status_scroll_offset]));
  1274. else
  1275. status_scroll_offset = 0;
  1276. }
  1277. char* MarlinUI::status_and_len(uint8_t &len) {
  1278. char *out = status_message + status_scroll_offset;
  1279. len = utf8_strlen(out);
  1280. return out;
  1281. }
  1282. #endif
  1283. #else // !HAS_STATUS_MESSAGE
  1284. //
  1285. // Send the status line as a host notification
  1286. //
  1287. void MarlinUI::set_status(const char * const cstr, const bool) {
  1288. TERN(HOST_PROMPT_SUPPORT, hostui.notify(cstr), UNUSED(cstr));
  1289. }
  1290. void MarlinUI::set_status(FSTR_P const fstr, const int8_t) {
  1291. TERN(HOST_PROMPT_SUPPORT, hostui.notify(fstr), UNUSED(fstr));
  1292. }
  1293. void MarlinUI::status_printf(int8_t, FSTR_P const fstr, ...) {
  1294. TERN(HOST_PROMPT_SUPPORT, hostui.notify(fstr), UNUSED(fstr));
  1295. }
  1296. #endif // !HAS_STATUS_MESSAGE
  1297. #if HAS_DISPLAY
  1298. #if ENABLED(SDSUPPORT)
  1299. extern bool wait_for_user, wait_for_heatup;
  1300. #endif
  1301. void MarlinUI::abort_print() {
  1302. #if ENABLED(SDSUPPORT)
  1303. wait_for_heatup = wait_for_user = false;
  1304. card.abortFilePrintSoon();
  1305. #endif
  1306. #ifdef ACTION_ON_CANCEL
  1307. hostui.cancel();
  1308. #endif
  1309. IF_DISABLED(SDSUPPORT, print_job_timer.stop());
  1310. TERN_(HOST_PROMPT_SUPPORT, hostui.prompt_open(PROMPT_INFO, F("UI Aborted"), FPSTR(DISMISS_STR)));
  1311. LCD_MESSAGE(MSG_PRINT_ABORTED);
  1312. TERN_(HAS_MARLINUI_MENU, return_to_status());
  1313. }
  1314. #if BOTH(HAS_MARLINUI_MENU, PSU_CONTROL)
  1315. void MarlinUI::poweroff() {
  1316. queue.inject(F("M81" TERN_(POWER_OFF_WAIT_FOR_COOLDOWN, "S")));
  1317. return_to_status();
  1318. }
  1319. #endif
  1320. void MarlinUI::flow_fault() {
  1321. LCD_ALERTMESSAGE(MSG_FLOWMETER_FAULT);
  1322. BUZZ(1000, 440);
  1323. TERN_(HAS_MARLINUI_MENU, return_to_status());
  1324. }
  1325. void MarlinUI::pause_print() {
  1326. #if HAS_MARLINUI_MENU
  1327. synchronize(GET_TEXT_F(MSG_PAUSING));
  1328. defer_status_screen();
  1329. #endif
  1330. TERN_(HAS_TOUCH_SLEEP, wakeup_screen());
  1331. TERN_(HOST_PROMPT_SUPPORT, hostui.prompt_open(PROMPT_PAUSE_RESUME, F("UI Pause"), F("Resume")));
  1332. LCD_MESSAGE(MSG_PRINT_PAUSED);
  1333. #if ENABLED(PARK_HEAD_ON_PAUSE)
  1334. pause_show_message(PAUSE_MESSAGE_PARKING, PAUSE_MODE_PAUSE_PRINT); // Show message immediately to let user know about pause in progress
  1335. queue.inject(F("M25 P\nM24"));
  1336. #elif ENABLED(SDSUPPORT)
  1337. queue.inject(F("M25"));
  1338. #elif defined(ACTION_ON_PAUSE)
  1339. hostui.pause();
  1340. #endif
  1341. }
  1342. void MarlinUI::resume_print() {
  1343. reset_status();
  1344. TERN_(PARK_HEAD_ON_PAUSE, wait_for_heatup = wait_for_user = false);
  1345. TERN_(SDSUPPORT, if (IS_SD_PAUSED()) queue.inject_P(M24_STR));
  1346. #ifdef ACTION_ON_RESUME
  1347. hostui.resume();
  1348. #endif
  1349. print_job_timer.start(); // Also called by M24
  1350. }
  1351. #if HAS_PRINT_PROGRESS
  1352. MarlinUI::progress_t MarlinUI::_get_progress() {
  1353. return (
  1354. TERN0(LCD_SET_PROGRESS_MANUALLY, (progress_override & PROGRESS_MASK))
  1355. #if ENABLED(SDSUPPORT)
  1356. ?: TERN(HAS_PRINT_PROGRESS_PERMYRIAD, card.permyriadDone(), card.percentDone())
  1357. #endif
  1358. );
  1359. }
  1360. #endif
  1361. #if HAS_TOUCH_BUTTONS
  1362. //
  1363. // Screen Click
  1364. // - On menu screens move directly to the touched item
  1365. // - On menu screens, right side (last 3 cols) acts like a scroll - half up => prev page, half down = next page
  1366. // - On select screens (and others) touch the Right Half for +, Left Half for -
  1367. // - On edit screens, touch Up Half for -, Bottom Half to +
  1368. //
  1369. void MarlinUI::screen_click(const uint8_t row, const uint8_t col, const uint8_t, const uint8_t) {
  1370. const millis_t now = millis();
  1371. if (PENDING(now, next_button_update_ms)) return;
  1372. next_button_update_ms = now + repeat_delay; // Assume the repeat delay
  1373. const int8_t xdir = col < (LCD_WIDTH ) / 2 ? -1 : 1,
  1374. ydir = row < (LCD_HEIGHT) / 2 ? -1 : 1;
  1375. if (on_edit_screen)
  1376. encoderDiff = epps * ydir;
  1377. else if (screen_items > 0) {
  1378. // Last 5 cols act as a scroll :-)
  1379. if (col > (LCD_WIDTH) - 5)
  1380. // 2 * LCD_HEIGHT to scroll to bottom of next page. (LCD_HEIGHT would only go 1 item down.)
  1381. encoderDiff = epps * (encoderLine - encoderTopLine + 2 * (LCD_HEIGHT)) * ydir;
  1382. else
  1383. encoderDiff = epps * (row - encoderPosition + encoderTopLine);
  1384. }
  1385. else if (!on_status_screen())
  1386. encoderDiff = epps * xdir;
  1387. }
  1388. #endif
  1389. #endif // HAS_DISPLAY
  1390. #if ENABLED(SDSUPPORT)
  1391. #if ENABLED(EXTENSIBLE_UI)
  1392. #include "extui/ui_api.h"
  1393. #endif
  1394. void MarlinUI::media_changed(const uint8_t old_status, const uint8_t status) {
  1395. if (old_status == status) {
  1396. TERN_(EXTENSIBLE_UI, ExtUI::onMediaError()); // Failed to mount/unmount
  1397. return;
  1398. }
  1399. if (status) {
  1400. if (old_status < 2) {
  1401. #if ENABLED(EXTENSIBLE_UI)
  1402. ExtUI::onMediaInserted();
  1403. #elif ENABLED(BROWSE_MEDIA_ON_INSERT)
  1404. clear_menu_history();
  1405. quick_feedback();
  1406. goto_screen(MEDIA_MENU_GATEWAY);
  1407. #else
  1408. LCD_MESSAGE(MSG_MEDIA_INSERTED);
  1409. #endif
  1410. }
  1411. }
  1412. else {
  1413. if (old_status < 2) {
  1414. #if ENABLED(EXTENSIBLE_UI)
  1415. ExtUI::onMediaRemoved();
  1416. #elif HAS_SD_DETECT
  1417. LCD_MESSAGE(MSG_MEDIA_REMOVED);
  1418. #if HAS_MARLINUI_MENU
  1419. if (!defer_return_to_status) return_to_status();
  1420. #endif
  1421. #endif
  1422. }
  1423. }
  1424. reinit_lcd(); // Revive a noisy shared SPI LCD
  1425. refresh();
  1426. #if HAS_WIRED_LCD || LED_POWEROFF_TIMEOUT > 0
  1427. const millis_t ms = millis();
  1428. #endif
  1429. TERN_(HAS_WIRED_LCD, next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL); // Delay LCD update for SD activity
  1430. #if LED_POWEROFF_TIMEOUT > 0
  1431. leds.reset_timeout(ms);
  1432. #endif
  1433. }
  1434. #endif // SDSUPPORT
  1435. #if HAS_MARLINUI_MENU
  1436. void MarlinUI::reset_settings() {
  1437. settings.reset();
  1438. completion_feedback();
  1439. #if ENABLED(TOUCH_SCREEN_CALIBRATION)
  1440. if (touch_calibration.need_calibration()) ui.goto_screen(touch_screen_calibration);
  1441. #endif
  1442. }
  1443. #if EITHER(BABYSTEP_ZPROBE_GFX_OVERLAY, MESH_EDIT_GFX_OVERLAY)
  1444. void MarlinUI::zoffset_overlay(const_float_t zvalue) {
  1445. // Determine whether the user is raising or lowering the nozzle.
  1446. static int8_t dir;
  1447. static float old_zvalue;
  1448. if (zvalue != old_zvalue) {
  1449. dir = zvalue ? zvalue < old_zvalue ? -1 : 1 : 0;
  1450. old_zvalue = zvalue;
  1451. }
  1452. zoffset_overlay(dir);
  1453. }
  1454. #endif
  1455. #endif
  1456. #if BOTH(EXTENSIBLE_UI, ADVANCED_PAUSE_FEATURE)
  1457. void MarlinUI::pause_show_message(
  1458. const PauseMessage message,
  1459. const PauseMode mode/*=PAUSE_MODE_SAME*/,
  1460. const uint8_t extruder/*=active_extruder*/
  1461. ) {
  1462. pause_mode = mode;
  1463. ExtUI::pauseModeStatus = message;
  1464. switch (message) {
  1465. case PAUSE_MESSAGE_PARKING: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_PAUSE_PRINT_PARKING)); break;
  1466. case PAUSE_MESSAGE_CHANGING: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_INIT)); break;
  1467. case PAUSE_MESSAGE_UNLOAD: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_UNLOAD)); break;
  1468. case PAUSE_MESSAGE_WAITING: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_ADVANCED_PAUSE_WAITING)); break;
  1469. case PAUSE_MESSAGE_INSERT: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_INSERT)); break;
  1470. case PAUSE_MESSAGE_LOAD: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_LOAD)); break;
  1471. case PAUSE_MESSAGE_PURGE:
  1472. ExtUI::onUserConfirmRequired(GET_TEXT_F(TERN(ADVANCED_PAUSE_CONTINUOUS_PURGE, MSG_FILAMENT_CHANGE_CONT_PURGE, MSG_FILAMENT_CHANGE_PURGE)));
  1473. break;
  1474. case PAUSE_MESSAGE_RESUME: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_RESUME)); break;
  1475. case PAUSE_MESSAGE_HEAT: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_HEAT)); break;
  1476. case PAUSE_MESSAGE_HEATING: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_HEATING)); break;
  1477. case PAUSE_MESSAGE_OPTION: ExtUI::onUserConfirmRequired(GET_TEXT_F(MSG_FILAMENT_CHANGE_OPTION_HEADER)); break;
  1478. case PAUSE_MESSAGE_STATUS: break;
  1479. default: break;
  1480. }
  1481. }
  1482. #endif
  1483. #if ENABLED(EEPROM_SETTINGS)
  1484. #if HAS_MARLINUI_MENU
  1485. void MarlinUI::init_eeprom() {
  1486. const bool good = settings.init_eeprom();
  1487. completion_feedback(good);
  1488. return_to_status();
  1489. }
  1490. void MarlinUI::load_settings() {
  1491. const bool good = settings.load();
  1492. completion_feedback(good);
  1493. }
  1494. void MarlinUI::store_settings() {
  1495. const bool good = settings.save();
  1496. completion_feedback(good);
  1497. }
  1498. #endif
  1499. #if DISABLED(EEPROM_AUTO_INIT)
  1500. static inline FSTR_P eeprom_err(const uint8_t msgid) {
  1501. switch (msgid) {
  1502. default:
  1503. case 0: return GET_TEXT_F(MSG_ERR_EEPROM_CRC);
  1504. case 1: return GET_TEXT_F(MSG_ERR_EEPROM_INDEX);
  1505. case 2: return GET_TEXT_F(MSG_ERR_EEPROM_VERSION);
  1506. }
  1507. }
  1508. void MarlinUI::eeprom_alert(const uint8_t msgid) {
  1509. #if HAS_MARLINUI_MENU
  1510. editable.uint8 = msgid;
  1511. goto_screen([]{
  1512. FSTR_P const restore_msg = GET_TEXT_F(MSG_INIT_EEPROM);
  1513. char msg[utf8_strlen(restore_msg) + 1];
  1514. strcpy_P(msg, FTOP(restore_msg));
  1515. MenuItem_confirm::select_screen(
  1516. GET_TEXT_F(MSG_BUTTON_RESET), GET_TEXT_F(MSG_BUTTON_IGNORE),
  1517. init_eeprom, return_to_status,
  1518. eeprom_err(editable.uint8), msg, F("?")
  1519. );
  1520. });
  1521. #else
  1522. set_status(eeprom_err(msgid));
  1523. #endif
  1524. }
  1525. #endif // EEPROM_AUTO_INIT
  1526. #endif // EEPROM_SETTINGS