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

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