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

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