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

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