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

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