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

marlinui.cpp 58KB

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