My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ultralcd.cpp 46KB

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