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.

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