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.

ultralcd.cpp 46KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfigPre.h"
  23. #ifdef LED_BACKLIGHT_TIMEOUT
  24. #include "../feature/leds/leds.h"
  25. #endif
  26. // These displays all share the MarlinUI class
  27. #if HAS_DISPLAY
  28. #include "ultralcd.h"
  29. #include "fontutils.h"
  30. MarlinUI ui;
  31. #include "../sd/cardreader.h"
  32. #if ENABLED(EXTENSIBLE_UI)
  33. #define START_OF_UTF8_CHAR(C) (((C) & 0xC0u) != 0x80u)
  34. #endif
  35. #if ENABLED(HOST_ACTION_COMMANDS)
  36. #include "../feature/host_actions.h"
  37. #endif
  38. #endif
  39. #if HAS_SPI_LCD
  40. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  41. uint8_t MarlinUI::status_scroll_offset; // = 0
  42. #if LONG_FILENAME_LENGTH > CHARSIZE * 2 * (LCD_WIDTH)
  43. #define MAX_MESSAGE_LENGTH LONG_FILENAME_LENGTH
  44. #else
  45. #define MAX_MESSAGE_LENGTH CHARSIZE * 2 * (LCD_WIDTH)
  46. #endif
  47. #else
  48. #define MAX_MESSAGE_LENGTH CHARSIZE * (LCD_WIDTH)
  49. #endif
  50. #elif ENABLED(EXTENSIBLE_UI)
  51. #define MAX_MESSAGE_LENGTH 63
  52. #endif
  53. #ifdef MAX_MESSAGE_LENGTH
  54. uint8_t MarlinUI::status_message_level; // = 0
  55. char MarlinUI::status_message[MAX_MESSAGE_LENGTH + 1];
  56. #endif
  57. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  58. uint8_t MarlinUI::progress_bar_percent; // = 0
  59. #endif
  60. #if HAS_SPI_LCD
  61. #if HAS_GRAPHICAL_LCD
  62. #include "dogm/ultralcd_DOGM.h"
  63. #endif
  64. #include "lcdprint.h"
  65. #include "../sd/cardreader.h"
  66. #include "../module/temperature.h"
  67. #include "../module/planner.h"
  68. #include "../module/printcounter.h"
  69. #include "../module/motion.h"
  70. #include "../gcode/queue.h"
  71. #include "../Marlin.h"
  72. #if ENABLED(POWER_LOSS_RECOVERY)
  73. #include "../feature/power_loss_recovery.h"
  74. #endif
  75. #if ENABLED(AUTO_BED_LEVELING_UBL)
  76. #include "../feature/bedlevel/bedlevel.h"
  77. #endif
  78. #if HAS_BUZZER
  79. #include "../libs/buzzer.h"
  80. #endif
  81. #if HAS_TRINAMIC
  82. #include "../feature/tmc_util.h"
  83. #endif
  84. #if HAS_ENCODER_ACTION
  85. volatile uint8_t MarlinUI::buttons;
  86. #if HAS_SLOW_BUTTONS
  87. volatile uint8_t MarlinUI::slow_buttons;
  88. #endif
  89. #endif
  90. #if ENABLED(INIT_SDCARD_ON_BOOT)
  91. uint8_t lcd_sd_status;
  92. #endif
  93. #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
  94. bool MarlinUI::defer_return_to_status;
  95. #endif
  96. uint8_t MarlinUI::lcd_status_update_delay = 1; // First update one loop delayed
  97. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  98. millis_t MarlinUI::next_filament_display; // = 0
  99. #endif
  100. millis_t next_button_update_ms;
  101. #if HAS_GRAPHICAL_LCD
  102. bool MarlinUI::drawing_screen, MarlinUI::first_page; // = false
  103. #endif
  104. // Encoder Handling
  105. #if HAS_ENCODER_ACTION
  106. uint16_t MarlinUI::encoderPosition;
  107. volatile int8_t encoderDiff; // Updated in update_buttons, added to encoderPosition every LCD update
  108. #endif
  109. #if HAS_LCD_MENU
  110. #include "menu/menu.h"
  111. #include "../sd/cardreader.h"
  112. #if ENABLED(SDSUPPORT)
  113. #if ENABLED(SCROLL_LONG_FILENAMES)
  114. uint8_t MarlinUI::filename_scroll_pos, MarlinUI::filename_scroll_max;
  115. #endif
  116. const char * MarlinUI::scrolled_filename(CardReader &theCard, const uint8_t maxlen, uint8_t hash, const bool doScroll) {
  117. const char *outstr = theCard.longest_filename();
  118. if (theCard.longFilename[0]) {
  119. #if ENABLED(SCROLL_LONG_FILENAMES)
  120. if (doScroll) {
  121. for (uint8_t l = FILENAME_LENGTH; l--;)
  122. hash = ((hash << 1) | (hash >> 7)) ^ theCard.filename[l]; // rotate, xor
  123. static uint8_t filename_scroll_hash;
  124. if (filename_scroll_hash != hash) { // If the hash changed...
  125. filename_scroll_hash = hash; // Save the new hash
  126. filename_scroll_max = MAX(0, utf8_strlen(theCard.longFilename) - maxlen); // Update the scroll limit
  127. filename_scroll_pos = 0; // Reset scroll to the start
  128. lcd_status_update_delay = 8; // Don't scroll right away
  129. }
  130. outstr += filename_scroll_pos;
  131. }
  132. #else
  133. theCard.longFilename[maxlen] = '\0'; // cutoff at screen edge
  134. #endif
  135. }
  136. return outstr;
  137. }
  138. #endif
  139. screenFunc_t MarlinUI::currentScreen; // Initialized in CTOR
  140. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  141. bool MarlinUI::encoderRateMultiplierEnabled;
  142. millis_t MarlinUI::lastEncoderMovementMillis = 0;
  143. void MarlinUI::enable_encoder_multiplier(const bool onoff) {
  144. encoderRateMultiplierEnabled = onoff;
  145. lastEncoderMovementMillis = 0;
  146. }
  147. #endif
  148. #if ENABLED(REVERSE_MENU_DIRECTION)
  149. int8_t MarlinUI::encoderDirection = 1;
  150. #endif
  151. bool MarlinUI::lcd_clicked;
  152. float move_menu_scale;
  153. bool MarlinUI::use_click() {
  154. const bool click = lcd_clicked;
  155. lcd_clicked = false;
  156. return click;
  157. }
  158. #if EITHER(AUTO_BED_LEVELING_UBL, G26_MESH_VALIDATION)
  159. bool MarlinUI::external_control; // = false
  160. void MarlinUI::wait_for_release() {
  161. while (button_pressed()) safe_delay(50);
  162. safe_delay(50);
  163. }
  164. #endif
  165. void _wrap_string(uint8_t &x, uint8_t &y, const char * const string, read_byte_cb_t cb_read_byte, bool wordwrap/*=false*/) {
  166. SETCURSOR(x, y);
  167. if (!string) return;
  168. auto _newline = [&x, &y]() {
  169. x = 0; y++; // move x to string len (plus space)
  170. SETCURSOR(0, y); // simulate carriage return
  171. };
  172. uint8_t *p = (uint8_t*)string;
  173. wchar_t ch;
  174. if (wordwrap) {
  175. uint8_t *wrd = nullptr, c = 0;
  176. // find the end of the part
  177. for (;;) {
  178. if (!wrd) wrd = p; // Get word start /before/ advancing
  179. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  180. const bool eol = !ch; // zero ends the string
  181. // End or a break between phrases?
  182. if (eol || ch == ' ' || ch == '-' || ch == '+' || ch == '.') {
  183. if (!c && ch == ' ') { if (wrd) wrd++; continue; } // collapse extra spaces
  184. // Past the right and the word is not too long?
  185. if (x + c > LCD_WIDTH && x >= (LCD_WIDTH) / 4) _newline(); // should it wrap?
  186. c += !eol; // +1 so the space will be printed
  187. x += c; // advance x to new position
  188. while (c) { // character countdown
  189. --c; // count down to zero
  190. wrd = get_utf8_value_cb(wrd, cb_read_byte, &ch); // get characters again
  191. lcd_put_wchar(ch); // character to the LCD
  192. }
  193. if (eol) break; // all done!
  194. wrd = nullptr; // set up for next word
  195. }
  196. else c++; // count word characters
  197. }
  198. }
  199. else {
  200. for (;;) {
  201. p = get_utf8_value_cb(p, cb_read_byte, &ch);
  202. if (!ch) break;
  203. lcd_put_wchar(ch);
  204. x++;
  205. if (x >= LCD_WIDTH) _newline();
  206. }
  207. }
  208. }
  209. void MarlinUI::draw_select_screen_prompt(PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) {
  210. const uint8_t plen = utf8_strlen_P(pref), slen = suff ? utf8_strlen_P(suff) : 0;
  211. uint8_t x = 0, y = 0;
  212. if (!string && plen + slen <= LCD_WIDTH) {
  213. x = (LCD_WIDTH - plen - slen) / 2;
  214. y = LCD_HEIGHT > 3 ? 1 : 0;
  215. }
  216. wrap_string_P(x, y, pref, true);
  217. if (string) {
  218. if (x) { x = 0; y++; } // Move to the start of the next line
  219. wrap_string(x, y, string);
  220. }
  221. if (suff) wrap_string_P(x, y, suff);
  222. }
  223. #endif // HAS_LCD_MENU
  224. void MarlinUI::init() {
  225. init_lcd();
  226. #if HAS_DIGITAL_BUTTONS
  227. #if BUTTON_EXISTS(EN1)
  228. SET_INPUT_PULLUP(BTN_EN1);
  229. #endif
  230. #if BUTTON_EXISTS(EN2)
  231. SET_INPUT_PULLUP(BTN_EN2);
  232. #endif
  233. #if BUTTON_EXISTS(ENC)
  234. SET_INPUT_PULLUP(BTN_ENC);
  235. #endif
  236. #if BUTTON_EXISTS(UP)
  237. SET_INPUT(BTN_UP);
  238. #endif
  239. #if BUTTON_EXISTS(DWN)
  240. SET_INPUT(BTN_DWN);
  241. #endif
  242. #if BUTTON_EXISTS(LFT)
  243. SET_INPUT(BTN_LFT);
  244. #endif
  245. #if BUTTON_EXISTS(RT)
  246. SET_INPUT(BTN_RT);
  247. #endif
  248. #endif // !HAS_DIGITAL_BUTTONS
  249. #if HAS_SHIFT_ENCODER
  250. #if ENABLED(SR_LCD_2W_NL) // Non latching 2 wire shift register
  251. SET_OUTPUT(SR_DATA_PIN);
  252. SET_OUTPUT(SR_CLK_PIN);
  253. #elif defined(SHIFT_CLK)
  254. SET_OUTPUT(SHIFT_CLK);
  255. OUT_WRITE(SHIFT_LD, HIGH);
  256. #if defined(SHIFT_EN) && SHIFT_EN >= 0
  257. OUT_WRITE(SHIFT_EN, LOW);
  258. #endif
  259. SET_INPUT_PULLUP(SHIFT_OUT);
  260. #endif
  261. #endif // HAS_SHIFT_ENCODER
  262. #if ENABLED(SDSUPPORT)
  263. #if PIN_EXISTS(SD_DETECT)
  264. SET_INPUT_PULLUP(SD_DETECT_PIN);
  265. #endif
  266. #if ENABLED(INIT_SDCARD_ON_BOOT)
  267. lcd_sd_status = 2; // UNKNOWN
  268. #endif
  269. #endif
  270. #if HAS_ENCODER_ACTION && HAS_SLOW_BUTTONS
  271. slow_buttons = 0;
  272. #endif
  273. update_buttons();
  274. #if HAS_ENCODER_ACTION
  275. encoderDiff = 0;
  276. #endif
  277. }
  278. bool MarlinUI::get_blink() {
  279. static uint8_t blink = 0;
  280. static millis_t next_blink_ms = 0;
  281. millis_t ms = millis();
  282. if (ELAPSED(ms, next_blink_ms)) {
  283. blink ^= 0xFF;
  284. next_blink_ms = ms + 1000 - (LCD_UPDATE_INTERVAL) / 2;
  285. }
  286. return blink != 0;
  287. }
  288. ////////////////////////////////////////////
  289. ///////////// Keypad Handling //////////////
  290. ////////////////////////////////////////////
  291. #if ENABLED(REPRAPWORLD_KEYPAD) && HAS_ENCODER_ACTION
  292. volatile uint8_t MarlinUI::keypad_buttons;
  293. #if HAS_LCD_MENU && !HAS_ADC_BUTTONS
  294. void lcd_move_x();
  295. void lcd_move_y();
  296. void lcd_move_z();
  297. void _reprapworld_keypad_move(const AxisEnum axis, const int16_t dir) {
  298. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  299. encoderPosition = dir;
  300. switch (axis) {
  301. case X_AXIS: lcd_move_x(); break;
  302. case Y_AXIS: lcd_move_y(); break;
  303. case Z_AXIS: lcd_move_z();
  304. default: break;
  305. }
  306. }
  307. #endif
  308. bool MarlinUI::handle_keypad() {
  309. #if HAS_ADC_BUTTONS
  310. #define ADC_MIN_KEY_DELAY 100
  311. if (keypad_buttons) {
  312. #if HAS_ENCODER_ACTION
  313. refresh(LCDVIEW_REDRAW_NOW);
  314. #if HAS_LCD_MENU
  315. if (encoderDirection == -1) { // ADC_KEYPAD forces REVERSE_MENU_DIRECTION, so this indicates menu navigation
  316. if (RRK(EN_KEYPAD_UP)) encoderPosition += ENCODER_STEPS_PER_MENU_ITEM;
  317. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition -= ENCODER_STEPS_PER_MENU_ITEM;
  318. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  319. else if (RRK(EN_KEYPAD_RIGHT)) { return_to_status(); quick_feedback(); }
  320. }
  321. else
  322. #endif
  323. {
  324. #if HAS_LCD_MENU
  325. if (RRK(EN_KEYPAD_UP)) encoderPosition -= ENCODER_PULSES_PER_STEP;
  326. else if (RRK(EN_KEYPAD_DOWN)) encoderPosition += ENCODER_PULSES_PER_STEP;
  327. else if (RRK(EN_KEYPAD_LEFT)) { MenuItem_back::action(); quick_feedback(); }
  328. else if (RRK(EN_KEYPAD_RIGHT)) encoderPosition = 0;
  329. #else
  330. if (RRK(EN_KEYPAD_UP) || RRK(EN_KEYPAD_LEFT)) encoderPosition -= ENCODER_PULSES_PER_STEP;
  331. else if (RRK(EN_KEYPAD_DOWN) || RRK(EN_KEYPAD_RIGHT)) encoderPosition += ENCODER_PULSES_PER_STEP;
  332. #endif
  333. }
  334. #endif
  335. next_button_update_ms = millis() + ADC_MIN_KEY_DELAY;
  336. return true;
  337. }
  338. #else // !HAS_ADC_BUTTONS
  339. static uint8_t keypad_debounce = 0;
  340. if (!RRK( EN_KEYPAD_F1 | EN_KEYPAD_F2
  341. | EN_KEYPAD_F3 | EN_KEYPAD_DOWN
  342. | EN_KEYPAD_RIGHT | EN_KEYPAD_MIDDLE
  343. | EN_KEYPAD_UP | EN_KEYPAD_LEFT )
  344. ) {
  345. if (keypad_debounce > 0) keypad_debounce--;
  346. }
  347. else if (!keypad_debounce) {
  348. keypad_debounce = 2;
  349. const bool homed = all_axes_homed();
  350. #if HAS_LCD_MENU
  351. if (RRK(EN_KEYPAD_MIDDLE)) goto_screen(menu_move);
  352. #if DISABLED(DELTA) && Z_HOME_DIR == -1
  353. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  354. #endif
  355. if (homed) {
  356. #if ENABLED(DELTA) || Z_HOME_DIR != -1
  357. if (RRK(EN_KEYPAD_F2)) _reprapworld_keypad_move(Z_AXIS, 1);
  358. #endif
  359. if (RRK(EN_KEYPAD_F3)) _reprapworld_keypad_move(Z_AXIS, -1);
  360. if (RRK(EN_KEYPAD_LEFT)) _reprapworld_keypad_move(X_AXIS, -1);
  361. if (RRK(EN_KEYPAD_RIGHT)) _reprapworld_keypad_move(X_AXIS, 1);
  362. if (RRK(EN_KEYPAD_DOWN)) _reprapworld_keypad_move(Y_AXIS, 1);
  363. if (RRK(EN_KEYPAD_UP)) _reprapworld_keypad_move(Y_AXIS, -1);
  364. }
  365. #endif // HAS_LCD_MENU
  366. if (!homed && RRK(EN_KEYPAD_F1)) queue.inject_P(PSTR("G28"));
  367. return true;
  368. }
  369. #endif // !ADC_KEYPAD
  370. return false;
  371. }
  372. #endif // REPRAPWORLD_KEYPAD
  373. /**
  374. * Status Screen
  375. *
  376. * This is very display-dependent, so the lcd implementation draws this.
  377. */
  378. #if ENABLED(LCD_PROGRESS_BAR)
  379. millis_t MarlinUI::progress_bar_ms; // = 0
  380. #if PROGRESS_MSG_EXPIRE > 0
  381. millis_t MarlinUI::expire_status_ms; // = 0
  382. #endif
  383. #endif
  384. void MarlinUI::status_screen() {
  385. #if HAS_LCD_MENU
  386. encoder_direction_normal();
  387. ENCODER_RATE_MULTIPLY(false);
  388. #endif
  389. #if ENABLED(LCD_PROGRESS_BAR)
  390. //
  391. // HD44780 implements the following message blinking and
  392. // message expiration because Status Line and Progress Bar
  393. // share the same line on the display.
  394. //
  395. #if DISABLED(PROGRESS_MSG_ONCE) || (PROGRESS_MSG_EXPIRE > 0)
  396. #define GOT_MS
  397. const millis_t ms = millis();
  398. #endif
  399. // If the message will blink rather than expire...
  400. #if DISABLED(PROGRESS_MSG_ONCE)
  401. if (ELAPSED(ms, progress_bar_ms + PROGRESS_BAR_MSG_TIME + PROGRESS_BAR_BAR_TIME))
  402. progress_bar_ms = ms;
  403. #endif
  404. #if PROGRESS_MSG_EXPIRE > 0
  405. // Handle message expire
  406. if (expire_status_ms > 0) {
  407. // Expire the message if a job is active and the bar has ticks
  408. if (get_progress() > 2 && !print_job_timer.isPaused()) {
  409. if (ELAPSED(ms, expire_status_ms)) {
  410. status_message[0] = '\0';
  411. expire_status_ms = 0;
  412. }
  413. }
  414. else {
  415. // Defer message expiration before bar appears
  416. // and during any pause (not just SD)
  417. expire_status_ms += LCD_UPDATE_INTERVAL;
  418. }
  419. }
  420. #endif // PROGRESS_MSG_EXPIRE
  421. #endif // LCD_PROGRESS_BAR
  422. #if HAS_LCD_MENU
  423. if (use_click()) {
  424. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  425. next_filament_display = millis() + 5000UL; // Show status message for 5s
  426. #endif
  427. goto_screen(menu_main);
  428. #if DISABLED(NO_LCD_REINIT)
  429. init_lcd(); // May revive the LCD if static electricity killed it
  430. #endif
  431. return;
  432. }
  433. #endif // HAS_LCD_MENU
  434. #if ENABLED(ULTIPANEL_FEEDMULTIPLY)
  435. const int16_t old_frm = feedrate_percentage;
  436. int16_t new_frm = old_frm + int16_t(encoderPosition);
  437. // Dead zone at 100% feedrate
  438. if (old_frm == 100) {
  439. if (int16_t(encoderPosition) > ENCODER_FEEDRATE_DEADZONE)
  440. new_frm -= ENCODER_FEEDRATE_DEADZONE;
  441. else if (int16_t(encoderPosition) < -(ENCODER_FEEDRATE_DEADZONE))
  442. new_frm += ENCODER_FEEDRATE_DEADZONE;
  443. else
  444. new_frm = old_frm;
  445. }
  446. else if ((old_frm < 100 && new_frm > 100) || (old_frm > 100 && new_frm < 100))
  447. new_frm = 100;
  448. new_frm = constrain(new_frm, 10, 999);
  449. if (old_frm != new_frm) {
  450. feedrate_percentage = new_frm;
  451. encoderPosition = 0;
  452. #if ENABLED(BEEP_ON_FEEDRATE_CHANGE)
  453. static millis_t next_beep;
  454. #ifndef GOT_MS
  455. const millis_t ms = millis();
  456. #endif
  457. if (ELAPSED(ms, next_beep)) {
  458. BUZZ(FEEDRATE_CHANGE_BEEP_DURATION, FEEDRATE_CHANGE_BEEP_FREQUENCY);
  459. next_beep = ms + 500UL;
  460. }
  461. #endif
  462. }
  463. #endif // ULTIPANEL_FEEDMULTIPLY
  464. draw_status_screen();
  465. }
  466. void MarlinUI::kill_screen(PGM_P lcd_msg) {
  467. init();
  468. set_alert_status_P(lcd_msg);
  469. // RED ALERT. RED ALERT.
  470. #ifdef LED_BACKLIGHT_TIMEOUT
  471. leds.set_color(LEDColorRed());
  472. #ifdef NEOPIXEL_BKGD_LED_INDEX
  473. pixels.setPixelColor(NEOPIXEL_BKGD_LED_INDEX, 255, 0, 0, 0);
  474. pixels.show();
  475. #endif
  476. #endif
  477. draw_kill_screen();
  478. }
  479. void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
  480. #if HAS_LCD_MENU
  481. refresh();
  482. #endif
  483. #if HAS_ENCODER_ACTION
  484. if (clear_buttons) buttons = 0;
  485. next_button_update_ms = millis() + 500;
  486. #else
  487. UNUSED(clear_buttons);
  488. #endif
  489. // Buzz and wait. The delay is needed for buttons to settle!
  490. buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  491. #if HAS_LCD_MENU
  492. #if ENABLED(LCD_USE_I2C_BUZZER)
  493. delay(10);
  494. #elif PIN_EXISTS(BEEPER)
  495. for (int8_t i = 5; i--;) { buzzer.tick(); delay(2); }
  496. #endif
  497. #endif
  498. }
  499. ////////////////////////////////////////////
  500. /////////////// Manual Move ////////////////
  501. ////////////////////////////////////////////
  502. #if HAS_LCD_MENU
  503. extern bool no_reentry; // Flag to prevent recursion into menu handlers
  504. int8_t manual_move_axis = (int8_t)NO_AXIS;
  505. millis_t manual_move_start_time = 0;
  506. #if IS_KINEMATIC
  507. bool MarlinUI::processing_manual_move = false;
  508. float manual_move_offset = 0;
  509. #endif
  510. #if E_MANUAL > 1
  511. int8_t MarlinUI::manual_move_e_index = 0;
  512. #endif
  513. /**
  514. * If the most recent manual move hasn't been fed to the planner yet,
  515. * and the planner can accept one, send a move immediately.
  516. */
  517. void MarlinUI::manage_manual_move() {
  518. if (processing_manual_move) return;
  519. if (manual_move_axis != (int8_t)NO_AXIS && ELAPSED(millis(), manual_move_start_time) && !planner.is_full()) {
  520. #if IS_KINEMATIC
  521. const float old_feedrate = feedrate_mm_s;
  522. feedrate_mm_s = MMM_TO_MMS(manual_feedrate_mm_m[manual_move_axis]);
  523. #if EXTRUDERS > 1
  524. const int8_t old_extruder = active_extruder;
  525. if (manual_move_axis == E_AXIS) active_extruder = manual_move_e_index;
  526. #endif
  527. // Set movement on a single axis
  528. set_destination_from_current();
  529. destination[manual_move_axis] += manual_move_offset;
  530. // Reset for the next move
  531. manual_move_offset = 0;
  532. manual_move_axis = (int8_t)NO_AXIS;
  533. // DELTA and SCARA machines use segmented moves, which could fill the planner during the call to
  534. // move_to_destination. This will cause idle() to be called, which can then call this function while the
  535. // previous invocation is being blocked. Modifications to manual_move_offset shouldn't be made while
  536. // processing_manual_move is true or the planner will get out of sync.
  537. processing_manual_move = true;
  538. prepare_move_to_destination(); // will call set_current_from_destination()
  539. processing_manual_move = false;
  540. feedrate_mm_s = old_feedrate;
  541. #if EXTRUDERS > 1
  542. active_extruder = old_extruder;
  543. #endif
  544. #else
  545. planner.buffer_line(current_position, MMM_TO_MMS(manual_feedrate_mm_m[manual_move_axis]), manual_move_axis == E_AXIS ? manual_move_e_index : active_extruder);
  546. manual_move_axis = (int8_t)NO_AXIS;
  547. #endif
  548. }
  549. }
  550. #endif // HAS_LCD_MENU
  551. /**
  552. * Update the LCD, read encoder buttons, etc.
  553. * - Read button states
  554. * - Check the SD Card slot state
  555. * - Act on RepRap World keypad input
  556. * - Update the encoder position
  557. * - Apply acceleration to the encoder position
  558. * - Do refresh(LCDVIEW_CALL_REDRAW_NOW) on controller events
  559. * - Reset the Info Screen timeout if there's any input
  560. * - Update status indicators, if any
  561. *
  562. * Run the current LCD menu handler callback function:
  563. * - Call the handler only if lcdDrawUpdate != LCDVIEW_NONE
  564. * - Before calling the handler, LCDVIEW_CALL_NO_REDRAW => LCDVIEW_NONE
  565. * - Call the menu handler. Menu handlers should do the following:
  566. * - If a value changes, set lcdDrawUpdate to LCDVIEW_REDRAW_NOW and draw the value
  567. * (Encoder events automatically set lcdDrawUpdate for you.)
  568. * - if (should_draw()) { redraw }
  569. * - Before exiting the handler set lcdDrawUpdate to:
  570. * - LCDVIEW_CLEAR_CALL_REDRAW to clear screen and set LCDVIEW_CALL_REDRAW_NEXT.
  571. * - LCDVIEW_REDRAW_NOW to draw now (including remaining stripes).
  572. * - LCDVIEW_CALL_REDRAW_NEXT to draw now and get LCDVIEW_REDRAW_NOW on the next loop.
  573. * - LCDVIEW_CALL_NO_REDRAW to draw now and get LCDVIEW_NONE on the next loop.
  574. * - NOTE: For graphical displays menu handlers may be called 2 or more times per loop,
  575. * so don't change lcdDrawUpdate without considering this.
  576. *
  577. * After the menu handler callback runs (or not):
  578. * - Clear the LCD if lcdDrawUpdate == LCDVIEW_CLEAR_CALL_REDRAW
  579. * - Update lcdDrawUpdate for the next loop (i.e., move one state down, usually)
  580. *
  581. * This function is only called from the main thread.
  582. */
  583. LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
  584. bool MarlinUI::detected() {
  585. return
  586. #if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && defined(DETECT_DEVICE)
  587. lcd.LcdDetected() == 1
  588. #else
  589. true
  590. #endif
  591. ;
  592. }
  593. void MarlinUI::update() {
  594. static uint16_t max_display_update_time = 0;
  595. static millis_t next_lcd_update_ms;
  596. millis_t ms = millis();
  597. #ifdef LED_BACKLIGHT_TIMEOUT
  598. leds.update_timeout(powersupply_on);
  599. #endif
  600. #if HAS_LCD_MENU
  601. #if LCD_TIMEOUT_TO_STATUS
  602. static millis_t return_to_status_ms = 0;
  603. #endif
  604. // Handle any queued Move Axis motion
  605. manage_manual_move();
  606. // Update button states for button_pressed(), etc.
  607. // If the state changes the next update may be delayed 300-500ms.
  608. update_buttons();
  609. // If the action button is pressed...
  610. static bool wait_for_unclick; // = 0
  611. if (!external_control && button_pressed()) {
  612. if (!wait_for_unclick) { // If not waiting for a debounce release:
  613. wait_for_unclick = true; // - Set debounce flag to ignore continous clicks
  614. lcd_clicked = !wait_for_user && !no_reentry; // - Keep the click if not waiting for a user-click
  615. wait_for_user = false; // - Any click clears wait for user
  616. quick_feedback(); // - Always make a click sound
  617. }
  618. }
  619. else wait_for_unclick = false;
  620. #if HAS_DIGITAL_BUTTONS && BUTTON_EXISTS(BACK)
  621. if (LCD_BACK_CLICKED()) {
  622. quick_feedback();
  623. goto_previous_screen();
  624. }
  625. #endif
  626. #endif // HAS_LCD_MENU
  627. #if ENABLED(INIT_SDCARD_ON_BOOT)
  628. const uint8_t sd_status = (uint8_t)IS_SD_INSERTED();
  629. if (sd_status != lcd_sd_status && detected()) {
  630. uint8_t old_sd_status = lcd_sd_status; // prevent re-entry to this block!
  631. lcd_sd_status = sd_status;
  632. if (sd_status) {
  633. safe_delay(500); // Some boards need a delay to get settled
  634. card.initsd();
  635. if (old_sd_status == 2)
  636. card.beginautostart(); // Initial boot
  637. else
  638. set_status_P(PSTR(MSG_SD_INSERTED));
  639. }
  640. #if PIN_EXISTS(SD_DETECT)
  641. else {
  642. card.release();
  643. if (old_sd_status != 2) {
  644. set_status_P(PSTR(MSG_SD_REMOVED));
  645. if (!on_status_screen()) return_to_status();
  646. }
  647. }
  648. #if DISABLED(NO_LCD_REINIT)
  649. init_lcd(); // May revive the LCD if static electricity killed it
  650. #endif
  651. #endif
  652. refresh();
  653. ms = millis();
  654. next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL; // delay LCD update until after SD activity completes
  655. #ifdef LED_BACKLIGHT_TIMEOUT
  656. leds.reset_timeout(ms);
  657. #endif
  658. }
  659. #endif // INIT_SDCARD_ON_BOOT
  660. if (ELAPSED(ms, next_lcd_update_ms)
  661. #if HAS_GRAPHICAL_LCD
  662. || drawing_screen
  663. #endif
  664. ) {
  665. next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL;
  666. #if ENABLED(LCD_HAS_STATUS_INDICATORS)
  667. update_indicators();
  668. #endif
  669. #if HAS_ENCODER_ACTION
  670. #if HAS_SLOW_BUTTONS
  671. slow_buttons = read_slow_buttons(); // Buttons that take too long to read in interrupt context
  672. #endif
  673. #if ENABLED(REPRAPWORLD_KEYPAD)
  674. if (handle_keypad()) {
  675. #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
  676. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  677. #endif
  678. }
  679. #endif
  680. const float abs_diff = ABS(encoderDiff);
  681. const bool encoderPastThreshold = (abs_diff >= (ENCODER_PULSES_PER_STEP));
  682. if (encoderPastThreshold || lcd_clicked) {
  683. if (encoderPastThreshold) {
  684. #if HAS_LCD_MENU && ENABLED(ENCODER_RATE_MULTIPLIER)
  685. int32_t encoderMultiplier = 1;
  686. if (encoderRateMultiplierEnabled) {
  687. const float encoderMovementSteps = abs_diff / (ENCODER_PULSES_PER_STEP);
  688. if (lastEncoderMovementMillis) {
  689. // Note that the rate is always calculated between two passes through the
  690. // loop and that the abs of the encoderDiff value is tracked.
  691. const float encoderStepRate = encoderMovementSteps / float(ms - lastEncoderMovementMillis) * 1000;
  692. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  693. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  694. #if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG)
  695. SERIAL_ECHO_START();
  696. SERIAL_ECHOPAIR("Enc Step Rate: ", encoderStepRate);
  697. SERIAL_ECHOPAIR(" Multiplier: ", encoderMultiplier);
  698. SERIAL_ECHOPAIR(" ENCODER_10X_STEPS_PER_SEC: ", ENCODER_10X_STEPS_PER_SEC);
  699. SERIAL_ECHOPAIR(" ENCODER_100X_STEPS_PER_SEC: ", ENCODER_100X_STEPS_PER_SEC);
  700. SERIAL_EOL();
  701. #endif
  702. }
  703. lastEncoderMovementMillis = ms;
  704. } // encoderRateMultiplierEnabled
  705. #else
  706. constexpr int32_t encoderMultiplier = 1;
  707. #endif // ENCODER_RATE_MULTIPLIER
  708. encoderPosition += (encoderDiff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
  709. encoderDiff = 0;
  710. }
  711. #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
  712. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  713. #endif
  714. refresh(LCDVIEW_REDRAW_NOW);
  715. #ifdef LED_BACKLIGHT_TIMEOUT
  716. leds.reset_timeout(ms);
  717. #endif
  718. }
  719. #endif
  720. // This runs every ~100ms when idling often enough.
  721. // Instead of tracking changes just redraw the Status Screen once per second.
  722. if (on_status_screen() && !lcd_status_update_delay--) {
  723. lcd_status_update_delay = 9
  724. #if HAS_GRAPHICAL_LCD
  725. + 3
  726. #endif
  727. ;
  728. max_display_update_time--;
  729. refresh(LCDVIEW_REDRAW_NOW);
  730. }
  731. #if HAS_LCD_MENU && ENABLED(SCROLL_LONG_FILENAMES)
  732. // If scrolling of long file names is enabled and we are in the sd card menu,
  733. // cause a refresh to occur until all the text has scrolled into view.
  734. if (currentScreen == menu_sdcard && !lcd_status_update_delay--) {
  735. lcd_status_update_delay = 4;
  736. if (++filename_scroll_pos > filename_scroll_max) {
  737. filename_scroll_pos = 0;
  738. lcd_status_update_delay = 12;
  739. }
  740. refresh(LCDVIEW_REDRAW_NOW);
  741. #if LCD_TIMEOUT_TO_STATUS
  742. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  743. #endif
  744. }
  745. #endif
  746. // then we want to use 1/2 of the time only.
  747. uint16_t bbr2 = planner.block_buffer_runtime() >> 1;
  748. if ((should_draw() || drawing_screen) && (!bbr2 || bbr2 > max_display_update_time)) {
  749. // Change state of drawing flag between screen updates
  750. if (!drawing_screen) switch (lcdDrawUpdate) {
  751. case LCDVIEW_CALL_NO_REDRAW:
  752. refresh(LCDVIEW_NONE);
  753. break;
  754. case LCDVIEW_CLEAR_CALL_REDRAW:
  755. case LCDVIEW_CALL_REDRAW_NEXT:
  756. refresh(LCDVIEW_REDRAW_NOW);
  757. case LCDVIEW_REDRAW_NOW: // set above, or by a handler through LCDVIEW_CALL_REDRAW_NEXT
  758. case LCDVIEW_NONE:
  759. break;
  760. } // switch
  761. #if HAS_ADC_BUTTONS
  762. keypad_buttons = 0;
  763. #endif
  764. #if HAS_GRAPHICAL_LCD
  765. #if ENABLED(LIGHTWEIGHT_UI)
  766. const bool in_status = on_status_screen(),
  767. do_u8g_loop = !in_status;
  768. lcd_in_status(in_status);
  769. if (in_status) status_screen();
  770. #else
  771. constexpr bool do_u8g_loop = true;
  772. #endif
  773. if (do_u8g_loop) {
  774. if (!drawing_screen) { // If not already drawing pages
  775. u8g.firstPage(); // Start the first page
  776. drawing_screen = first_page = true; // Flag as drawing pages
  777. }
  778. set_font(FONT_MENU); // Setup font for every page draw
  779. u8g.setColorIndex(1); // And reset the color
  780. run_current_screen(); // Draw and process the current screen
  781. first_page = false;
  782. // The screen handler can clear drawing_screen for an action that changes the screen.
  783. // If still drawing and there's another page, update max-time and return now.
  784. // The nextPage will already be set up on the next call.
  785. if (drawing_screen && (drawing_screen = u8g.nextPage())) {
  786. NOLESS(max_display_update_time, millis() - ms);
  787. return;
  788. }
  789. }
  790. #else
  791. run_current_screen();
  792. #endif
  793. #if HAS_LCD_MENU
  794. lcd_clicked = false;
  795. #endif
  796. // Keeping track of the longest time for an individual LCD update.
  797. // Used to do screen throttling when the planner starts to fill up.
  798. NOLESS(max_display_update_time, millis() - ms);
  799. }
  800. #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
  801. // Return to Status Screen after a timeout
  802. if (on_status_screen() || defer_return_to_status)
  803. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  804. else if (ELAPSED(ms, return_to_status_ms))
  805. return_to_status();
  806. #endif
  807. // Change state of drawing flag between screen updates
  808. if (!drawing_screen) switch (lcdDrawUpdate) {
  809. case LCDVIEW_CLEAR_CALL_REDRAW:
  810. clear_lcd(); break;
  811. case LCDVIEW_REDRAW_NOW:
  812. refresh(LCDVIEW_NONE);
  813. case LCDVIEW_NONE:
  814. case LCDVIEW_CALL_REDRAW_NEXT:
  815. case LCDVIEW_CALL_NO_REDRAW:
  816. default: break;
  817. } // switch
  818. } // ELAPSED(ms, next_lcd_update_ms)
  819. }
  820. #if HAS_ADC_BUTTONS
  821. typedef struct {
  822. uint16_t ADCKeyValueMin, ADCKeyValueMax;
  823. uint8_t ADCKeyNo;
  824. } _stADCKeypadTable_;
  825. #ifndef ADC_BUTTONS_VALUE_SCALE
  826. #define ADC_BUTTONS_VALUE_SCALE 1.0 // for the power voltage equal to the reference voltage
  827. #endif
  828. #ifndef ADC_BUTTONS_R_PULLUP
  829. #define ADC_BUTTONS_R_PULLUP 4.7 // common pull-up resistor in the voltage divider
  830. #endif
  831. #ifndef ADC_BUTTONS_LEFT_R_PULLDOWN
  832. #define ADC_BUTTONS_LEFT_R_PULLDOWN 0.47 // pull-down resistor for LEFT button voltage divider
  833. #endif
  834. #ifndef ADC_BUTTONS_RIGHT_R_PULLDOWN
  835. #define ADC_BUTTONS_RIGHT_R_PULLDOWN 4.7 // pull-down resistor for RIGHT button voltage divider
  836. #endif
  837. #ifndef ADC_BUTTONS_UP_R_PULLDOWN
  838. #define ADC_BUTTONS_UP_R_PULLDOWN 1.0 // pull-down resistor for UP button voltage divider
  839. #endif
  840. #ifndef ADC_BUTTONS_DOWN_R_PULLDOWN
  841. #define ADC_BUTTONS_DOWN_R_PULLDOWN 10.0 // pull-down resistor for DOWN button voltage divider
  842. #endif
  843. #ifndef ADC_BUTTONS_MIDDLE_R_PULLDOWN
  844. #define ADC_BUTTONS_MIDDLE_R_PULLDOWN 2.2 // pull-down resistor for MIDDLE button voltage divider
  845. #endif
  846. // Calculate the ADC value for the voltage divider with specified pull-down resistor value
  847. #define ADC_BUTTON_VALUE(r) (int(4096.0 * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP)))
  848. static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
  849. // VALUE_MIN, VALUE_MAX, KEY
  850. { 4000, 4096, 1 + BLEN_KEYPAD_F1 }, // F1
  851. { 4000, 4096, 1 + BLEN_KEYPAD_F2 }, // F2
  852. { 4000, 4096, 1 + BLEN_KEYPAD_F3 }, // F3
  853. { ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) - 100,
  854. ADC_BUTTON_VALUE(ADC_BUTTONS_LEFT_R_PULLDOWN) + 100, 1 + BLEN_KEYPAD_LEFT }, // LEFT ( 272 ... 472)
  855. { ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) - 100,
  856. ADC_BUTTON_VALUE(ADC_BUTTONS_RIGHT_R_PULLDOWN) + 100, 1 + BLEN_KEYPAD_RIGHT }, // RIGHT (1948 ... 2148)
  857. { ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) - 100,
  858. ADC_BUTTON_VALUE(ADC_BUTTONS_UP_R_PULLDOWN) + 100, 1 + BLEN_KEYPAD_UP }, // UP ( 618 ... 818)
  859. { ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) - 100,
  860. ADC_BUTTON_VALUE(ADC_BUTTONS_DOWN_R_PULLDOWN) + 100, 1 + BLEN_KEYPAD_DOWN }, // DOWN (2686 ... 2886)
  861. { ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) - 100,
  862. ADC_BUTTON_VALUE(ADC_BUTTONS_MIDDLE_R_PULLDOWN) + 100, 1 + BLEN_KEYPAD_MIDDLE }, // ENTER (1205 ... 1405)
  863. };
  864. uint8_t get_ADC_keyValue(void) {
  865. if (thermalManager.ADCKey_count >= 16) {
  866. const uint16_t currentkpADCValue = thermalManager.current_ADCKey_raw << 2;
  867. thermalManager.current_ADCKey_raw = 1024;
  868. thermalManager.ADCKey_count = 0;
  869. if (currentkpADCValue < 4000)
  870. for (uint8_t i = 0; i < ADC_KEY_NUM; i++) {
  871. const uint16_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
  872. hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
  873. if (WITHIN(currentkpADCValue, lo, hi)) return pgm_read_byte(&stADCKeyTable[i].ADCKeyNo);
  874. }
  875. }
  876. return 0;
  877. }
  878. #endif // HAS_ADC_BUTTONS
  879. #if HAS_ENCODER_ACTION
  880. #if DISABLED(ADC_KEYPAD) && (ENABLED(REPRAPWORLD_KEYPAD) || !HAS_DIGITAL_BUTTONS)
  881. /**
  882. * Setup Rotary Encoder Bit Values (for two pin encoders to indicate movement)
  883. * These values are independent of which pins are used for EN_A and EN_B indications
  884. * The rotary encoder part is also independent to the chipset used for the LCD
  885. */
  886. #define GET_SHIFT_BUTTON_STATES(DST) \
  887. uint8_t new_##DST = 0; \
  888. WRITE(SHIFT_LD, LOW); \
  889. WRITE(SHIFT_LD, HIGH); \
  890. for (int8_t i = 0; i < 8; i++) { \
  891. new_##DST >>= 1; \
  892. if (READ(SHIFT_OUT)) SBI(new_##DST, 7); \
  893. WRITE(SHIFT_CLK, HIGH); \
  894. WRITE(SHIFT_CLK, LOW); \
  895. } \
  896. DST = ~new_##DST; //invert it, because a pressed switch produces a logical 0
  897. #endif
  898. /**
  899. * Read encoder buttons from the hardware registers
  900. * Warning: This function is called from interrupt context!
  901. */
  902. void MarlinUI::update_buttons() {
  903. const millis_t now = millis();
  904. if (ELAPSED(now, next_button_update_ms)) {
  905. #if HAS_DIGITAL_BUTTONS
  906. #if ANY_BUTTON(EN1, EN2, ENC, BACK)
  907. uint8_t newbutton = 0;
  908. #if BUTTON_EXISTS(EN1)
  909. if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
  910. #endif
  911. #if BUTTON_EXISTS(EN2)
  912. if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
  913. #endif
  914. #if BUTTON_EXISTS(ENC)
  915. if (BUTTON_PRESSED(ENC)) newbutton |= EN_C;
  916. #endif
  917. #if BUTTON_EXISTS(BACK)
  918. if (BUTTON_PRESSED(BACK)) newbutton |= EN_D;
  919. #endif
  920. #else
  921. constexpr uint8_t newbutton = 0;
  922. #endif
  923. //
  924. // Directional buttons
  925. //
  926. #if ANY_BUTTON(UP, DWN, LFT, RT)
  927. const int8_t pulses = (ENCODER_PULSES_PER_STEP) * encoderDirection;
  928. if (false) {
  929. // for the else-ifs below
  930. }
  931. #if BUTTON_EXISTS(UP)
  932. else if (BUTTON_PRESSED(UP)) {
  933. encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * pulses;
  934. next_button_update_ms = now + 300;
  935. }
  936. #endif
  937. #if BUTTON_EXISTS(DWN)
  938. else if (BUTTON_PRESSED(DWN)) {
  939. encoderDiff = -(ENCODER_STEPS_PER_MENU_ITEM) * pulses;
  940. next_button_update_ms = now + 300;
  941. }
  942. #endif
  943. #if BUTTON_EXISTS(LFT)
  944. else if (BUTTON_PRESSED(LFT)) {
  945. encoderDiff = -pulses;
  946. next_button_update_ms = now + 300;
  947. }
  948. #endif
  949. #if BUTTON_EXISTS(RT)
  950. else if (BUTTON_PRESSED(RT)) {
  951. encoderDiff = pulses;
  952. next_button_update_ms = now + 300;
  953. }
  954. #endif
  955. #endif // UP || DWN || LFT || RT
  956. buttons = newbutton
  957. #if HAS_SLOW_BUTTONS
  958. | slow_buttons
  959. #endif
  960. ;
  961. #elif HAS_ADC_BUTTONS
  962. buttons = 0;
  963. #endif
  964. #if HAS_ADC_BUTTONS
  965. if (keypad_buttons == 0) {
  966. const uint8_t b = get_ADC_keyValue();
  967. if (WITHIN(b, 1, 8)) keypad_buttons = _BV(b - 1);
  968. }
  969. #endif
  970. #if HAS_SHIFT_ENCODER
  971. GET_SHIFT_BUTTON_STATES(
  972. #if ENABLED(REPRAPWORLD_KEYPAD)
  973. keypad_buttons
  974. #else
  975. buttons
  976. #endif
  977. );
  978. #endif
  979. } // next_button_update_ms
  980. #if HAS_ENCODER_WHEEL
  981. static uint8_t lastEncoderBits;
  982. #define encrot0 0
  983. #define encrot1 2
  984. #define encrot2 3
  985. #define encrot3 1
  986. // Manage encoder rotation
  987. #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: encoderDiff += encoderDirection; break; case _E2: encoderDiff -= encoderDirection; }
  988. uint8_t enc = 0;
  989. if (buttons & EN_A) enc |= B01;
  990. if (buttons & EN_B) enc |= B10;
  991. if (enc != lastEncoderBits) {
  992. switch (enc) {
  993. case encrot0: ENCODER_SPIN(encrot3, encrot1); break;
  994. case encrot1: ENCODER_SPIN(encrot0, encrot2); break;
  995. case encrot2: ENCODER_SPIN(encrot1, encrot3); break;
  996. case encrot3: ENCODER_SPIN(encrot2, encrot0); break;
  997. }
  998. if (external_control) {
  999. #if ENABLED(AUTO_BED_LEVELING_UBL)
  1000. ubl.encoder_diff = encoderDiff; // Make encoder rotation available to UBL G29 mesh editing.
  1001. #endif
  1002. encoderDiff = 0; // Hide the encoder event from the current screen handler.
  1003. }
  1004. lastEncoderBits = enc;
  1005. }
  1006. #endif // HAS_ENCODER_WHEEL
  1007. }
  1008. #if HAS_SLOW_BUTTONS
  1009. uint8_t MarlinUI::read_slow_buttons() {
  1010. #if ENABLED(LCD_I2C_TYPE_MCP23017)
  1011. // Reading these buttons this is likely to be too slow to call inside interrupt context
  1012. // so they are called during normal lcd_update
  1013. uint8_t slow_bits = lcd.readButtons() << B_I2C_BTN_OFFSET;
  1014. #if ENABLED(LCD_I2C_VIKI)
  1015. if ((slow_bits & (B_MI | B_RI)) && PENDING(millis(), next_button_update_ms)) // LCD clicked
  1016. slow_bits &= ~(B_MI | B_RI); // Disable LCD clicked buttons if screen is updated
  1017. #endif // LCD_I2C_VIKI
  1018. return slow_bits;
  1019. #endif // LCD_I2C_TYPE_MCP23017
  1020. }
  1021. #endif
  1022. #endif // HAS_ENCODER_ACTION
  1023. #endif // HAS_SPI_LCD
  1024. #if HAS_DISPLAY
  1025. #if ENABLED(EXTENSIBLE_UI)
  1026. #include "extensible_ui/ui_api.h"
  1027. #endif
  1028. ////////////////////////////////////////////
  1029. /////////////// Status Line ////////////////
  1030. ////////////////////////////////////////////
  1031. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  1032. void MarlinUI::advance_status_scroll() {
  1033. // Advance by one UTF8 code-word
  1034. if (status_scroll_offset < utf8_strlen(status_message))
  1035. while (!START_OF_UTF8_CHAR(status_message[++status_scroll_offset]));
  1036. else
  1037. status_scroll_offset = 0;
  1038. }
  1039. char* MarlinUI::status_and_len(uint8_t &len) {
  1040. char *out = status_message + status_scroll_offset;
  1041. len = utf8_strlen(out);
  1042. return out;
  1043. }
  1044. #endif
  1045. void MarlinUI::finish_status(const bool persist) {
  1046. #if !(ENABLED(LCD_PROGRESS_BAR) && (PROGRESS_MSG_EXPIRE > 0))
  1047. UNUSED(persist);
  1048. #endif
  1049. #if ENABLED(LCD_PROGRESS_BAR)
  1050. progress_bar_ms = millis();
  1051. #if PROGRESS_MSG_EXPIRE > 0
  1052. expire_status_ms = persist ? 0 : progress_bar_ms + PROGRESS_MSG_EXPIRE;
  1053. #endif
  1054. #endif
  1055. #if BOTH(FILAMENT_LCD_DISPLAY, SDSUPPORT)
  1056. next_filament_display = millis() + 5000UL; // Show status message for 5s
  1057. #endif
  1058. #if HAS_SPI_LCD && ENABLED(STATUS_MESSAGE_SCROLLING)
  1059. status_scroll_offset = 0;
  1060. #endif
  1061. #if ENABLED(EXTENSIBLE_UI)
  1062. ExtUI::onStatusChanged(status_message);
  1063. #endif
  1064. refresh();
  1065. }
  1066. bool MarlinUI::has_status() { return (status_message[0] != '\0'); }
  1067. void MarlinUI::set_status(const char * const message, const bool persist) {
  1068. if (status_message_level > 0) return;
  1069. // Here we have a problem. The message is encoded in UTF8, so
  1070. // arbitrarily cutting it will be a problem. We MUST be sure
  1071. // that there is no cutting in the middle of a multibyte character!
  1072. // Get a pointer to the null terminator
  1073. const char* pend = message + strlen(message);
  1074. // If length of supplied UTF8 string is greater than
  1075. // our buffer size, start cutting whole UTF8 chars
  1076. while ((pend - message) > MAX_MESSAGE_LENGTH) {
  1077. --pend;
  1078. while (!START_OF_UTF8_CHAR(*pend)) --pend;
  1079. };
  1080. // At this point, we have the proper cut point. Use it
  1081. uint8_t maxLen = pend - message;
  1082. strncpy(status_message, message, maxLen);
  1083. status_message[maxLen] = '\0';
  1084. finish_status(persist);
  1085. }
  1086. #include <stdarg.h>
  1087. void MarlinUI::status_printf_P(const uint8_t level, PGM_P const fmt, ...) {
  1088. if (level < status_message_level) return;
  1089. status_message_level = level;
  1090. va_list args;
  1091. va_start(args, fmt);
  1092. vsnprintf_P(status_message, MAX_MESSAGE_LENGTH, fmt, args);
  1093. va_end(args);
  1094. finish_status(level > 0);
  1095. }
  1096. void MarlinUI::set_status_P(PGM_P const message, int8_t level) {
  1097. if (level < 0) level = status_message_level = 0;
  1098. if (level < status_message_level) return;
  1099. status_message_level = level;
  1100. // Here we have a problem. The message is encoded in UTF8, so
  1101. // arbitrarily cutting it will be a problem. We MUST be sure
  1102. // that there is no cutting in the middle of a multibyte character!
  1103. // Get a pointer to the null terminator
  1104. PGM_P pend = message + strlen_P(message);
  1105. // If length of supplied UTF8 string is greater than
  1106. // our buffer size, start cutting whole UTF8 chars
  1107. while ((pend - message) > MAX_MESSAGE_LENGTH) {
  1108. --pend;
  1109. while (!START_OF_UTF8_CHAR(pgm_read_byte(pend))) --pend;
  1110. };
  1111. // At this point, we have the proper cut point. Use it
  1112. uint8_t maxLen = pend - message;
  1113. strncpy_P(status_message, message, maxLen);
  1114. status_message[maxLen] = '\0';
  1115. finish_status(level > 0);
  1116. }
  1117. void MarlinUI::set_alert_status_P(PGM_P const message) {
  1118. set_status_P(message, 1);
  1119. #if HAS_LCD_MENU
  1120. return_to_status();
  1121. #endif
  1122. }
  1123. #include "../module/printcounter.h"
  1124. /**
  1125. * Reset the status message
  1126. */
  1127. void MarlinUI::reset_status() {
  1128. static const char paused[] PROGMEM = MSG_PRINT_PAUSED;
  1129. static const char printing[] PROGMEM = MSG_PRINTING;
  1130. static const char welcome[] PROGMEM = WELCOME_MSG;
  1131. #if SERVICE_INTERVAL_1 > 0
  1132. static const char service1[] PROGMEM = { "> " SERVICE_NAME_1 "!" };
  1133. #endif
  1134. #if SERVICE_INTERVAL_2 > 0
  1135. static const char service2[] PROGMEM = { "> " SERVICE_NAME_2 "!" };
  1136. #endif
  1137. #if SERVICE_INTERVAL_3 > 0
  1138. static const char service3[] PROGMEM = { "> " SERVICE_NAME_3 "!" };
  1139. #endif
  1140. PGM_P msg;
  1141. if (!IS_SD_PRINTING() && print_job_timer.isPaused())
  1142. msg = paused;
  1143. #if ENABLED(SDSUPPORT)
  1144. else if (IS_SD_PRINTING())
  1145. return set_status(card.longest_filename(), true);
  1146. #endif
  1147. else if (print_job_timer.isRunning())
  1148. msg = printing;
  1149. #if SERVICE_INTERVAL_1 > 0
  1150. else if (print_job_timer.needsService(1)) msg = service1;
  1151. #endif
  1152. #if SERVICE_INTERVAL_2 > 0
  1153. else if (print_job_timer.needsService(2)) msg = service2;
  1154. #endif
  1155. #if SERVICE_INTERVAL_3 > 0
  1156. else if (print_job_timer.needsService(3)) msg = service3;
  1157. #endif
  1158. else
  1159. msg = welcome;
  1160. set_status_P(msg, -1);
  1161. }
  1162. #if ENABLED(SDSUPPORT)
  1163. extern bool wait_for_user, wait_for_heatup;
  1164. #endif
  1165. void MarlinUI::abort_print() {
  1166. #if ENABLED(SDSUPPORT)
  1167. wait_for_heatup = wait_for_user = false;
  1168. card.flag.abort_sd_printing = true;
  1169. #endif
  1170. #ifdef ACTION_ON_CANCEL
  1171. host_action_cancel();
  1172. #endif
  1173. #if ENABLED(HOST_PROMPT_SUPPORT)
  1174. host_prompt_open(PROMPT_INFO, PSTR("UI Abort"));
  1175. #endif
  1176. print_job_timer.stop();
  1177. set_status_P(PSTR(MSG_PRINT_ABORTED));
  1178. #if HAS_SPI_LCD
  1179. return_to_status();
  1180. #endif
  1181. }
  1182. #if ANY(PARK_HEAD_ON_PAUSE, SDSUPPORT)
  1183. #include "../gcode/queue.h"
  1184. #endif
  1185. void MarlinUI::pause_print() {
  1186. #if HAS_LCD_MENU
  1187. synchronize(PSTR(MSG_PAUSE_PRINT));
  1188. #endif
  1189. #if ENABLED(POWER_LOSS_RECOVERY)
  1190. if (recovery.enabled) recovery.save(true, false);
  1191. #endif
  1192. #if ENABLED(HOST_PROMPT_SUPPORT)
  1193. host_prompt_open(PROMPT_PAUSE_RESUME, PSTR("UI Pause"), PSTR("Resume"));
  1194. #endif
  1195. set_status_P(PSTR(MSG_PRINT_PAUSED));
  1196. #if ENABLED(PARK_HEAD_ON_PAUSE)
  1197. #if HAS_SPI_LCD
  1198. lcd_pause_show_message(PAUSE_MESSAGE_PAUSING, PAUSE_MODE_PAUSE_PRINT); // Show message immediately to let user know about pause in progress
  1199. #endif
  1200. queue.inject_P(PSTR("M25 P\nM24"));
  1201. #elif ENABLED(SDSUPPORT)
  1202. queue.inject_P(PSTR("M25"));
  1203. #elif defined(ACTION_ON_PAUSE)
  1204. host_action_pause();
  1205. #endif
  1206. }
  1207. void MarlinUI::resume_print() {
  1208. reset_status();
  1209. #if ENABLED(PARK_HEAD_ON_PAUSE)
  1210. wait_for_heatup = wait_for_user = false;
  1211. #endif
  1212. #if ENABLED(SDSUPPORT)
  1213. if (card.isPaused()) queue.inject_P(PSTR("M24"));
  1214. #endif
  1215. #ifdef ACTION_ON_RESUME
  1216. host_action_resume();
  1217. #endif
  1218. print_job_timer.start(); // Also called by M24
  1219. }
  1220. #if HAS_PRINT_PROGRESS
  1221. uint8_t MarlinUI::get_progress() {
  1222. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  1223. uint8_t &progress = progress_bar_percent;
  1224. #else
  1225. uint8_t progress = 0;
  1226. #endif
  1227. #if ENABLED(SDSUPPORT)
  1228. if (IS_SD_PRINTING()) progress = card.percentDone();
  1229. #endif
  1230. return progress;
  1231. }
  1232. #endif
  1233. #endif // HAS_DISPLAY