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.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. #pragma once
  23. #include "../inc/MarlinConfig.h"
  24. #if HAS_BUZZER
  25. #include "../libs/buzzer.h"
  26. #endif
  27. #define HAS_ENCODER_ACTION (HAS_LCD_MENU || ENABLED(ULTIPANEL_FEEDMULTIPLY))
  28. #if HAS_SPI_LCD
  29. #include "../Marlin.h"
  30. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  31. #include "../feature/pause.h"
  32. #include "../module/motion.h" // for active_extruder
  33. #endif
  34. enum LCDViewAction : uint8_t {
  35. LCDVIEW_NONE,
  36. LCDVIEW_REDRAW_NOW,
  37. LCDVIEW_CALL_REDRAW_NEXT,
  38. LCDVIEW_CLEAR_CALL_REDRAW,
  39. LCDVIEW_CALL_NO_REDRAW
  40. };
  41. #if ENABLED(ADC_KEYPAD)
  42. uint8_t get_ADC_keyValue();
  43. #endif
  44. #if HAS_GRAPHICAL_LCD
  45. #define SETCURSOR(col, row) lcd_moveto(col * (MENU_FONT_WIDTH), (row + 1) * (MENU_FONT_HEIGHT))
  46. #define SETCURSOR_RJ(len, row) lcd_moveto(LCD_PIXEL_WIDTH - len * (MENU_FONT_WIDTH), (row + 1) * (MENU_FONT_HEIGHT))
  47. #else
  48. #define SETCURSOR(col, row) lcd_moveto(col, row)
  49. #define SETCURSOR_RJ(len, row) lcd_moveto(LCD_WIDTH - len, row)
  50. #endif
  51. #define LCD_UPDATE_INTERVAL 100
  52. #if HAS_LCD_MENU
  53. #if ENABLED(SDSUPPORT)
  54. #include "../sd/cardreader.h"
  55. #endif
  56. typedef void (*screenFunc_t)();
  57. typedef void (*menuAction_t)();
  58. // Manual Movement
  59. constexpr float manual_feedrate_mm_m[XYZE] = MANUAL_FEEDRATE;
  60. extern float move_menu_scale;
  61. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  62. void lcd_advanced_pause_show_message(const AdvancedPauseMessage message,
  63. const AdvancedPauseMode mode=ADVANCED_PAUSE_MODE_SAME,
  64. const uint8_t extruder=active_extruder);
  65. #endif
  66. #if ENABLED(AUTO_BED_LEVELING_UBL)
  67. void lcd_mesh_edit_setup(const float &initial);
  68. float lcd_mesh_edit();
  69. #endif
  70. #endif // HAS_LCD_MENU
  71. #endif
  72. #if HAS_DIGITAL_BUTTONS
  73. // Wheel spin pins where BA is 00, 10, 11, 01 (1 bit always changes)
  74. #define BLEN_A 0
  75. #define BLEN_B 1
  76. #define EN_A _BV(BLEN_A)
  77. #define EN_B _BV(BLEN_B)
  78. #define BUTTON_EXISTS(BN) (defined(BTN_## BN) && BTN_## BN >= 0)
  79. #define BUTTON_PRESSED(BN) !READ(BTN_## BN)
  80. #if BUTTON_EXISTS(ENC)
  81. #define BLEN_C 2
  82. #define EN_C _BV(BLEN_C)
  83. #endif
  84. #if BUTTON_EXISTS(BACK)
  85. #define BLEN_D 3
  86. #define EN_D _BV(BLEN_D)
  87. #define LCD_BACK_CLICKED() (buttons & EN_D)
  88. #endif
  89. #if ENABLED(REPRAPWORLD_KEYPAD)
  90. #define REPRAPWORLD_BTN_OFFSET 0 // Bit offset into buttons for shift register values
  91. #define BLEN_REPRAPWORLD_KEYPAD_F3 0
  92. #define BLEN_REPRAPWORLD_KEYPAD_F2 1
  93. #define BLEN_REPRAPWORLD_KEYPAD_F1 2
  94. #define BLEN_REPRAPWORLD_KEYPAD_DOWN 3
  95. #define BLEN_REPRAPWORLD_KEYPAD_RIGHT 4
  96. #define BLEN_REPRAPWORLD_KEYPAD_MIDDLE 5
  97. #define BLEN_REPRAPWORLD_KEYPAD_UP 6
  98. #define BLEN_REPRAPWORLD_KEYPAD_LEFT 7
  99. #define EN_REPRAPWORLD_KEYPAD_F1 (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_F1))
  100. #define EN_REPRAPWORLD_KEYPAD_F2 (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_F2))
  101. #define EN_REPRAPWORLD_KEYPAD_F3 (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_F3))
  102. #define EN_REPRAPWORLD_KEYPAD_DOWN (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_DOWN))
  103. #define EN_REPRAPWORLD_KEYPAD_RIGHT (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_RIGHT))
  104. #define EN_REPRAPWORLD_KEYPAD_MIDDLE (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_MIDDLE))
  105. #define EN_REPRAPWORLD_KEYPAD_UP (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_UP))
  106. #define EN_REPRAPWORLD_KEYPAD_LEFT (_BV(REPRAPWORLD_BTN_OFFSET + BLEN_REPRAPWORLD_KEYPAD_LEFT))
  107. #define RRK(B) (buttons_reprapworld_keypad & (B))
  108. #ifdef EN_C
  109. #define BUTTON_CLICK() ((buttons & EN_C) || RRK(EN_REPRAPWORLD_KEYPAD_MIDDLE))
  110. #else
  111. #define BUTTON_CLICK() RRK(EN_REPRAPWORLD_KEYPAD_MIDDLE)
  112. #endif
  113. #elif ENABLED(LCD_I2C_VIKI)
  114. #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
  115. // button and encoder bit positions within 'buttons'
  116. #define B_LE (BUTTON_LEFT << B_I2C_BTN_OFFSET) // The remaining normalized buttons are all read via I2C
  117. #define B_UP (BUTTON_UP << B_I2C_BTN_OFFSET)
  118. #define B_MI (BUTTON_SELECT << B_I2C_BTN_OFFSET)
  119. #define B_DW (BUTTON_DOWN << B_I2C_BTN_OFFSET)
  120. #define B_RI (BUTTON_RIGHT << B_I2C_BTN_OFFSET)
  121. #if BUTTON_EXISTS(ENC) // The pause/stop/restart button is connected to BTN_ENC when used
  122. #define B_ST (EN_C) // Map the pause/stop/resume button into its normalized functional name
  123. #define BUTTON_CLICK() (buttons & (B_MI|B_RI|B_ST)) // Pause/stop also acts as click until a proper pause/stop is implemented.
  124. #else
  125. #define BUTTON_CLICK() (buttons & (B_MI|B_RI))
  126. #endif
  127. // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
  128. #define LCD_HAS_SLOW_BUTTONS
  129. #elif ENABLED(LCD_I2C_PANELOLU2)
  130. #if !BUTTON_EXISTS(ENC) // Use I2C if not directly connected to a pin
  131. #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C)
  132. #define B_MI (PANELOLU2_ENCODER_C << B_I2C_BTN_OFFSET) // requires LiquidTWI2 library v1.2.3 or later
  133. #define BUTTON_CLICK() (buttons & B_MI)
  134. // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update
  135. #define LCD_HAS_SLOW_BUTTONS
  136. #endif
  137. #endif
  138. #else
  139. // Shift register bits correspond to buttons:
  140. #define BL_LE 7 // Left
  141. #define BL_UP 6 // Up
  142. #define BL_MI 5 // Middle
  143. #define BL_DW 4 // Down
  144. #define BL_RI 3 // Right
  145. #define BL_ST 2 // Red Button
  146. #define B_LE (_BV(BL_LE))
  147. #define B_UP (_BV(BL_UP))
  148. #define B_MI (_BV(BL_MI))
  149. #define B_DW (_BV(BL_DW))
  150. #define B_RI (_BV(BL_RI))
  151. #define B_ST (_BV(BL_ST))
  152. #define BUTTON_CLICK() (buttons & (B_MI|B_ST))
  153. #endif
  154. #ifndef BUTTON_CLICK
  155. #ifdef EN_C
  156. #define BUTTON_CLICK() (buttons & EN_C)
  157. #else
  158. #define BUTTON_CLICK() false
  159. #endif
  160. #endif
  161. #if HAS_GRAPHICAL_LCD
  162. enum MarlinFont : uint8_t {
  163. FONT_STATUSMENU = 1,
  164. FONT_EDIT,
  165. FONT_MENU
  166. };
  167. #endif
  168. ////////////////////////////////////////////
  169. //////////// MarlinUI Singleton ////////////
  170. ////////////////////////////////////////////
  171. class MarlinUI {
  172. public:
  173. MarlinUI() {
  174. #if HAS_LCD_MENU
  175. currentScreen = status_screen;
  176. #endif
  177. }
  178. static inline void buzz(const long duration, const uint16_t freq) {
  179. #if ENABLED(LCD_USE_I2C_BUZZER)
  180. lcd.buzz(duration, freq);
  181. #elif PIN_EXISTS(BEEPER)
  182. buzzer.tone(duration, freq);
  183. #else
  184. UNUSED(duration); UNUSED(freq);
  185. #endif
  186. }
  187. // LCD implementations
  188. static void clear_lcd();
  189. static void init_lcd();
  190. #if HAS_SPI_LCD || ENABLED(MALYAN_LCD) || ENABLED(EXTENSIBLE_UI)
  191. static void init();
  192. static void update();
  193. static void set_alert_status_P(PGM_P message);
  194. #else // NO LCD
  195. static inline void init() {}
  196. static inline void update() {}
  197. static inline void set_alert_status_P(PGM_P message) { UNUSED(message); }
  198. #endif
  199. #if HAS_SPI_LCD || ENABLED(EXTENSIBLE_UI)
  200. static char status_message[];
  201. static bool has_status();
  202. static uint8_t status_message_level; // Higher levels block lower levels
  203. static inline void reset_alert_level() { status_message_level = 0; }
  204. #if HAS_SPI_LCD
  205. static bool detected();
  206. static LCDViewAction lcdDrawUpdate;
  207. static inline bool should_draw() { return bool(lcdDrawUpdate); }
  208. static inline void refresh(const LCDViewAction type) { lcdDrawUpdate = type; }
  209. static inline void refresh() { refresh(LCDVIEW_CLEAR_CALL_REDRAW); }
  210. #if ENABLED(SHOW_BOOTSCREEN)
  211. static void show_bootscreen();
  212. #endif
  213. #if HAS_GRAPHICAL_LCD
  214. static bool drawing_screen, first_page;
  215. static void set_font(const MarlinFont font_nr);
  216. #else
  217. static constexpr bool drawing_screen = false, first_page = true;
  218. enum HD44780CharSet : uint8_t { CHARSET_MENU, CHARSET_INFO, CHARSET_BOOT };
  219. static void set_custom_characters(
  220. #if ENABLED(LCD_PROGRESS_BAR) || ENABLED(SHOW_BOOTSCREEN)
  221. const HD44780CharSet screen_charset=CHARSET_INFO
  222. #endif
  223. );
  224. #if ENABLED(LCD_PROGRESS_BAR)
  225. static millis_t progress_bar_ms; // Start time for the current progress bar cycle
  226. #if PROGRESS_MSG_EXPIRE > 0
  227. static millis_t MarlinUI::expire_status_ms; // = 0
  228. static inline void reset_progress_bar_timeout() { expire_status_ms = 0; }
  229. #endif
  230. #define LCD_SET_CHARSET(C) set_custom_characters(C)
  231. #else
  232. #define LCD_SET_CHARSET(C) set_custom_characters()
  233. #endif
  234. #endif
  235. #if ENABLED(STATUS_MESSAGE_SCROLLING)
  236. static uint8_t status_scroll_offset;
  237. #endif
  238. static uint8_t lcd_status_update_delay;
  239. #if HAS_PRINT_PROGRESS
  240. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  241. static uint8_t progress_bar_percent;
  242. static void set_progress(const uint8_t progress) { progress_bar_percent = MIN(progress, 100); }
  243. #endif
  244. static uint8_t get_progress();
  245. #else
  246. static constexpr uint8_t get_progress() { return 0; }
  247. #endif
  248. #if HAS_LCD_CONTRAST
  249. static int16_t contrast;
  250. static void set_contrast(const int16_t value);
  251. static inline void refresh_contrast() { set_contrast(contrast); }
  252. #endif
  253. #if ENABLED(FILAMENT_LCD_DISPLAY) && ENABLED(SDSUPPORT)
  254. static millis_t next_filament_display;
  255. #endif
  256. static void quick_feedback(const bool clear_buttons=true);
  257. static void completion_feedback(const bool good=true);
  258. #if DISABLED(LIGHTWEIGHT_UI)
  259. static void draw_status_message(const bool blink);
  260. #endif
  261. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  262. static void draw_hotend_status(const uint8_t row, const uint8_t extruder);
  263. #endif
  264. static void status_screen();
  265. #else
  266. static void refresh() {}
  267. #endif
  268. static bool get_blink();
  269. static void kill_screen(PGM_P const lcd_msg);
  270. static void draw_kill_screen();
  271. static void set_status(const char* const message, const bool persist=false);
  272. static void set_status_P(PGM_P const message, const int8_t level=0);
  273. static void status_printf_P(const uint8_t level, PGM_P const fmt, ...);
  274. static void reset_status();
  275. #else // MALYAN_LCD or NO LCD
  276. static inline void refresh() {}
  277. static inline void set_status(const char* const message, const bool persist=false) { UNUSED(message); UNUSED(persist); }
  278. static inline void set_status_P(PGM_P const message, const int8_t level=0) { UNUSED(message); UNUSED(level); }
  279. static inline void status_printf_P(const uint8_t level, PGM_P const fmt, ...) { UNUSED(level); UNUSED(fmt); }
  280. static inline void reset_status() {}
  281. static inline void reset_alert_level() {}
  282. static constexpr bool has_status() { return false; }
  283. #endif
  284. #if HAS_LCD_MENU
  285. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  286. static bool encoderRateMultiplierEnabled;
  287. static millis_t lastEncoderMovementMillis;
  288. static void enable_encoder_multiplier(const bool onoff);
  289. #endif
  290. #if ENABLED(SDSUPPORT)
  291. #if ENABLED(SCROLL_LONG_FILENAMES)
  292. static uint8_t filename_scroll_pos, filename_scroll_max;
  293. #endif
  294. static const char * const scrolled_filename(CardReader &theCard, const uint8_t maxlen, uint8_t hash, const bool doScroll);
  295. #endif
  296. #if IS_KINEMATIC
  297. static bool processing_manual_move;
  298. #else
  299. static constexpr bool processing_manual_move = false;
  300. #endif
  301. #if E_MANUAL > 1
  302. static int8_t manual_move_e_index;
  303. #else
  304. static constexpr int8_t manual_move_e_index = 0;
  305. #endif
  306. static int16_t preheat_hotend_temp[2], preheat_bed_temp[2];
  307. static uint8_t preheat_fan_speed[2];
  308. static void manage_manual_move();
  309. static bool lcd_clicked;
  310. static bool use_click();
  311. static void synchronize(PGM_P const msg=NULL);
  312. static screenFunc_t currentScreen;
  313. static void goto_screen(const screenFunc_t screen, const uint32_t encoder=0);
  314. static void save_previous_screen();
  315. static void goto_previous_screen();
  316. static void return_to_status();
  317. static inline bool on_status_screen() { return currentScreen == status_screen; }
  318. static inline void run_current_screen() { (*currentScreen)(); }
  319. #if ENABLED(LIGHTWEIGHT_UI)
  320. static void lcd_in_status(const bool inStatus);
  321. #endif
  322. static inline void defer_status_screen(const bool defer) {
  323. #if LCD_TIMEOUT_TO_STATUS
  324. defer_return_to_status = defer;
  325. #else
  326. UNUSED(defer);
  327. #endif
  328. }
  329. static inline void goto_previous_screen_no_defer() {
  330. defer_status_screen(false);
  331. goto_previous_screen();
  332. }
  333. #if ENABLED(SD_REPRINT_LAST_SELECTED_FILE)
  334. static void reselect_last_file();
  335. #endif
  336. #if ENABLED(G26_MESH_VALIDATION)
  337. static inline void chirp() { buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ); }
  338. #endif
  339. #if ENABLED(AUTO_BED_LEVELING_UBL)
  340. static void ubl_plot(const uint8_t x, const uint8_t inverted_y);
  341. #endif
  342. #elif HAS_SPI_LCD
  343. static constexpr bool lcd_clicked = false;
  344. static constexpr bool on_status_screen() { return true; }
  345. static inline void run_current_screen() { status_screen(); }
  346. #endif
  347. #if ENABLED(LCD_BED_LEVELING) && (ENABLED(PROBE_MANUALLY) || ENABLED(MESH_BED_LEVELING))
  348. static bool wait_for_bl_move;
  349. #else
  350. static constexpr bool wait_for_bl_move = false;
  351. #endif
  352. #if HAS_LCD_MENU && (ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(G26_MESH_VALIDATION))
  353. static bool external_control;
  354. FORCE_INLINE static void capture() { external_control = true; }
  355. FORCE_INLINE static void release() { external_control = false; }
  356. #else
  357. static constexpr bool external_control = false;
  358. #endif
  359. #if HAS_ENCODER_ACTION
  360. static volatile uint8_t buttons;
  361. #if ENABLED(REPRAPWORLD_KEYPAD)
  362. static volatile uint8_t buttons_reprapworld_keypad;
  363. static bool handle_keypad();
  364. #endif
  365. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  366. static volatile uint8_t slow_buttons;
  367. static uint8_t read_slow_buttons();
  368. #endif
  369. static void update_buttons();
  370. static inline bool button_pressed() { return BUTTON_CLICK(); }
  371. #if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(G26_MESH_VALIDATION)
  372. static void wait_for_release();
  373. #endif
  374. static uint32_t encoderPosition;
  375. #if ENABLED(REVERSE_ENCODER_DIRECTION)
  376. #define ENCODERBASE -1
  377. #else
  378. #define ENCODERBASE +1
  379. #endif
  380. #if ENABLED(REVERSE_MENU_DIRECTION)
  381. static int8_t encoderDirection;
  382. static inline void encoder_direction_normal() { encoderDirection = +(ENCODERBASE); }
  383. static inline void encoder_direction_menus() { encoderDirection = -(ENCODERBASE); }
  384. #else
  385. static constexpr int8_t encoderDirection = ENCODERBASE;
  386. static inline void encoder_direction_normal() {}
  387. static inline void encoder_direction_menus() {}
  388. #endif
  389. #else
  390. static inline void update_buttons() {}
  391. #endif
  392. private:
  393. static void _synchronize();
  394. #if HAS_SPI_LCD || ENABLED(EXTENSIBLE_UI)
  395. static void finishstatus(const bool persist);
  396. #endif
  397. #if HAS_SPI_LCD
  398. #if HAS_LCD_MENU
  399. #if LCD_TIMEOUT_TO_STATUS
  400. static bool defer_return_to_status;
  401. #else
  402. static constexpr bool defer_return_to_status = false;
  403. #endif
  404. #endif
  405. static void draw_status_screen();
  406. #endif
  407. };
  408. extern MarlinUI ui;
  409. #define LCD_MESSAGEPGM(x) ui.set_status_P(PSTR(x))
  410. #define LCD_ALERTMESSAGEPGM(x) ui.set_alert_status_P(PSTR(x))