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

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