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 49KB

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