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.

menu.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #if HAS_LCD_MENU
  24. #include "menu.h"
  25. #include "../ultralcd.h"
  26. #include "../../module/planner.h"
  27. #include "../../module/motion.h"
  28. #include "../../gcode/queue.h"
  29. #include "../../sd/cardreader.h"
  30. #if ENABLED(EEPROM_SETTINGS)
  31. #include "../../module/configuration_store.h"
  32. #endif
  33. #if WATCH_HOTENDS || WATCH_THE_BED
  34. #include "../../module/temperature.h"
  35. #endif
  36. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  37. #include "../../module/probe.h"
  38. #endif
  39. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) || ENABLED(AUTO_BED_LEVELING_UBL)
  40. #include "../../feature/bedlevel/bedlevel.h"
  41. #endif
  42. ////////////////////////////////////////////
  43. ///////////// Global Variables /////////////
  44. ////////////////////////////////////////////
  45. // Menu Navigation
  46. int8_t encoderTopLine;
  47. typedef struct {
  48. screenFunc_t menu_function;
  49. uint32_t encoder_position;
  50. } menuPosition;
  51. menuPosition screen_history[6];
  52. uint8_t screen_history_depth = 0;
  53. bool screen_changed, defer_return_to_status;
  54. // Value Editing
  55. PGM_P editLabel;
  56. void *editValue;
  57. int32_t minEditValue, maxEditValue;
  58. screenFunc_t callbackFunc;
  59. bool liveEdit;
  60. // Prevent recursion into screen handlers
  61. bool no_reentry = false;
  62. ////////////////////////////////////////////
  63. //////// Menu Navigation & History /////////
  64. ////////////////////////////////////////////
  65. void lcd_return_to_status() { lcd_goto_screen(lcd_status_screen); }
  66. void lcd_save_previous_screen() {
  67. if (screen_history_depth < COUNT(screen_history)) {
  68. screen_history[screen_history_depth].menu_function = currentScreen;
  69. screen_history[screen_history_depth].encoder_position = encoderPosition;
  70. ++screen_history_depth;
  71. }
  72. }
  73. void lcd_goto_previous_menu() {
  74. if (screen_history_depth > 0) {
  75. --screen_history_depth;
  76. lcd_goto_screen(
  77. screen_history[screen_history_depth].menu_function,
  78. screen_history[screen_history_depth].encoder_position
  79. );
  80. }
  81. else
  82. lcd_return_to_status();
  83. }
  84. void lcd_goto_previous_menu_no_defer() {
  85. defer_return_to_status = false;
  86. lcd_goto_previous_menu();
  87. }
  88. ////////////////////////////////////////////
  89. /////////// Common Menu Actions ////////////
  90. ////////////////////////////////////////////
  91. void menu_item_gcode::action(PGM_P pgcode) { enqueue_and_echo_commands_P(pgcode); }
  92. ////////////////////////////////////////////
  93. /////////// Menu Editing Actions ///////////
  94. ////////////////////////////////////////////
  95. /**
  96. * Functions for editing single values
  97. *
  98. * The "DEFINE_MENU_EDIT_ITEM" macro generates the functions needed to edit a numerical value.
  99. *
  100. * The prerequisite is that in the header the type was already declared:
  101. *
  102. * DECLARE_MENU_EDIT_TYPE(int16_t, int3, itostr3, 1)
  103. *
  104. * For example, DEFINE_MENU_EDIT_ITEM(int3) expands into these functions:
  105. *
  106. * bool menu_item_int3::_edit();
  107. * void menu_item_int3::edit(); // edit int16_t (interactively)
  108. * void menu_item_int3::action_setting_edit(PGM_P const pstr, int16_t * const ptr, const int16_t minValue, const int16_t maxValue, const screenFunc_t callback = null, const bool live = false);
  109. *
  110. * You can then use one of the menu macros to present the edit interface:
  111. * MENU_ITEM_EDIT(int3, MSG_SPEED, &feedrate_percentage, 10, 999)
  112. *
  113. * This expands into a more primitive menu item:
  114. * MENU_ITEM_VARIANT(int3, _setting_edit, MSG_SPEED, PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
  115. *
  116. * ...which calls:
  117. * menu_item_int3::action_setting_edit(PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
  118. */
  119. void menu_item_invariants::edit(strfunc_t strfunc, loadfunc_t loadfunc) {
  120. ENCODER_DIRECTION_NORMAL();
  121. if ((int32_t)encoderPosition < 0) encoderPosition = 0;
  122. if ((int32_t)encoderPosition > maxEditValue) encoderPosition = maxEditValue;
  123. if (lcdDrawUpdate)
  124. lcd_implementation_drawedit(editLabel, strfunc(encoderPosition + minEditValue));
  125. if (lcd_clicked || (liveEdit && lcdDrawUpdate)) {
  126. if (editValue != NULL) loadfunc(editValue, encoderPosition + minEditValue);
  127. if (callbackFunc && (liveEdit || lcd_clicked)) (*callbackFunc)();
  128. if (lcd_clicked) lcd_goto_previous_menu();
  129. lcd_clicked = false;
  130. }
  131. }
  132. void menu_item_invariants::init(PGM_P const el, void * const ev, const int32_t minv, const int32_t maxv, const uint32_t ep, const screenFunc_t cs, const screenFunc_t cb, const bool le) {
  133. lcd_save_previous_screen();
  134. lcd_refresh();
  135. editLabel = el;
  136. editValue = ev;
  137. minEditValue = minv;
  138. maxEditValue = maxv;
  139. encoderPosition = ep;
  140. currentScreen = cs;
  141. callbackFunc = cb;
  142. liveEdit = le;
  143. }
  144. #define DEFINE_MENU_EDIT_ITEM(NAME) template class menu_item_template<NAME ## _item_info>;
  145. DEFINE_MENU_EDIT_ITEM(int3);
  146. DEFINE_MENU_EDIT_ITEM(int4);
  147. DEFINE_MENU_EDIT_ITEM(int8);
  148. DEFINE_MENU_EDIT_ITEM(float3);
  149. DEFINE_MENU_EDIT_ITEM(float52);
  150. DEFINE_MENU_EDIT_ITEM(float43);
  151. DEFINE_MENU_EDIT_ITEM(float5);
  152. DEFINE_MENU_EDIT_ITEM(float51);
  153. DEFINE_MENU_EDIT_ITEM(float52sign);
  154. DEFINE_MENU_EDIT_ITEM(float62);
  155. DEFINE_MENU_EDIT_ITEM(long5);
  156. void menu_item_bool::action_setting_edit(PGM_P pstr, bool *ptr, screenFunc_t callback) {
  157. UNUSED(pstr); *ptr ^= true; lcd_refresh();
  158. if (callback) (*callback)();
  159. }
  160. ////////////////////////////////////////////
  161. ///////////////// Menu Tree ////////////////
  162. ////////////////////////////////////////////
  163. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  164. float lcd_z_fade_height;
  165. void _lcd_set_z_fade_height() { set_z_fade_height(lcd_z_fade_height); }
  166. #endif
  167. bool printer_busy() { return planner.movesplanned() || IS_SD_PRINTING(); }
  168. /**
  169. * General function to go directly to a screen
  170. */
  171. void lcd_goto_screen(screenFunc_t screen, const uint32_t encoder/*=0*/) {
  172. if (currentScreen != screen) {
  173. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  174. // Shadow for editing the fade height
  175. lcd_z_fade_height = planner.z_fade_height;
  176. #endif
  177. #if ENABLED(DOUBLECLICK_FOR_Z_BABYSTEPPING) && ENABLED(BABYSTEPPING)
  178. static millis_t doubleclick_expire_ms = 0;
  179. // Going to menu_main from status screen? Remember first click time.
  180. // Going back to status screen within a very short time? Go to Z babystepping.
  181. if (screen == menu_main) {
  182. if (currentScreen == lcd_status_screen)
  183. doubleclick_expire_ms = millis() + DOUBLECLICK_MAX_INTERVAL;
  184. }
  185. else if (screen == lcd_status_screen && currentScreen == menu_main && PENDING(millis(), doubleclick_expire_ms)) {
  186. if (printer_busy()) {
  187. screen =
  188. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  189. lcd_babystep_zoffset
  190. #else
  191. lcd_babystep_z
  192. #endif
  193. ;
  194. }
  195. #if ENABLED(MOVE_Z_WHEN_IDLE)
  196. else {
  197. move_menu_scale = MOVE_Z_IDLE_MULTIPLICATOR;
  198. screen = lcd_move_z;
  199. }
  200. #endif
  201. }
  202. #endif
  203. currentScreen = screen;
  204. encoderPosition = encoder;
  205. if (screen == lcd_status_screen) {
  206. defer_return_to_status = false;
  207. #if ENABLED(AUTO_BED_LEVELING_UBL)
  208. ubl.lcd_map_control = false;
  209. #endif
  210. screen_history_depth = 0;
  211. }
  212. lcd_implementation_clear();
  213. // Re-initialize custom characters that may be re-used
  214. #if HAS_CHARACTER_LCD
  215. #if ENABLED(AUTO_BED_LEVELING_UBL)
  216. if (!ubl.lcd_map_control)
  217. #endif
  218. LCD_SET_CHARSET(screen == lcd_status_screen ? CHARSET_INFO : CHARSET_MENU);
  219. #endif
  220. lcdDrawUpdate = LCDVIEW_CALL_REDRAW_NEXT;
  221. screen_changed = true;
  222. #if HAS_GRAPHICAL_LCD
  223. drawing_screen = false;
  224. #endif
  225. }
  226. }
  227. ////////////////////////////////////////////
  228. ///////////// Manual Movement //////////////
  229. ////////////////////////////////////////////
  230. //
  231. // Display the synchronize screen until moves are
  232. // finished, and don't return to the caller until
  233. // done. ** This blocks the command queue! **
  234. //
  235. static PGM_P sync_message;
  236. void _lcd_synchronize() {
  237. if (lcdDrawUpdate) lcd_implementation_drawmenu_static(LCD_HEIGHT >= 4 ? 1 : 0, sync_message);
  238. if (no_reentry) return;
  239. // Make this the current handler till all moves are done
  240. no_reentry = true;
  241. const screenFunc_t old_screen = currentScreen;
  242. lcd_goto_screen(_lcd_synchronize);
  243. planner.synchronize(); // idle() is called until moves complete
  244. no_reentry = false;
  245. lcd_goto_screen(old_screen);
  246. }
  247. // Display the synchronize screen with a custom message
  248. // ** This blocks the command queue! **
  249. void lcd_synchronize(PGM_P const msg/*=NULL*/) {
  250. static const char moving[] PROGMEM = MSG_MOVING;
  251. sync_message = msg ? msg : moving;
  252. _lcd_synchronize();
  253. }
  254. /**
  255. * Scrolling for menus and other line-based screens
  256. *
  257. * encoderLine is the position based on the encoder
  258. * encoderTopLine is the top menu line to display
  259. * _lcdLineNr is the index of the LCD line (e.g., 0-3)
  260. * _menuLineNr is the menu item to draw and process
  261. * _thisItemNr is the index of each MENU_ITEM or STATIC_ITEM
  262. * screen_items is the total number of items in the menu (after one call)
  263. */
  264. int8_t encoderLine, screen_items;
  265. void scroll_screen(const uint8_t limit, const bool is_menu) {
  266. ENCODER_DIRECTION_MENUS();
  267. ENCODER_RATE_MULTIPLY(false);
  268. if (encoderPosition > 0x8000) encoderPosition = 0;
  269. if (first_page) {
  270. encoderLine = encoderPosition / (ENCODER_STEPS_PER_MENU_ITEM);
  271. screen_changed = false;
  272. }
  273. if (screen_items > 0 && encoderLine >= screen_items - limit) {
  274. encoderLine = MAX(0, screen_items - limit);
  275. encoderPosition = encoderLine * (ENCODER_STEPS_PER_MENU_ITEM);
  276. }
  277. if (is_menu) {
  278. NOMORE(encoderTopLine, encoderLine);
  279. if (encoderLine >= encoderTopLine + LCD_HEIGHT)
  280. encoderTopLine = encoderLine - LCD_HEIGHT + 1;
  281. }
  282. else
  283. encoderTopLine = encoderLine;
  284. }
  285. void lcd_completion_feedback(const bool good/*=true*/) {
  286. if (good) {
  287. lcd_buzz(100, 659);
  288. lcd_buzz(100, 698);
  289. }
  290. else lcd_buzz(20, 440);
  291. }
  292. #if HAS_LINE_TO_Z
  293. void line_to_z(const float &z) {
  294. current_position[Z_AXIS] = z;
  295. planner.buffer_line(current_position, MMM_TO_MMS(manual_feedrate_mm_m[Z_AXIS]), active_extruder);
  296. }
  297. #endif
  298. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  299. void lcd_babystep_zoffset() {
  300. if (use_click()) { return lcd_goto_previous_menu_no_defer(); }
  301. defer_return_to_status = true;
  302. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  303. const bool do_probe = (active_extruder == 0);
  304. #else
  305. constexpr bool do_probe = true;
  306. #endif
  307. ENCODER_DIRECTION_NORMAL();
  308. if (encoderPosition) {
  309. const int16_t babystep_increment = (int32_t)encoderPosition * (BABYSTEP_MULTIPLICATOR);
  310. encoderPosition = 0;
  311. const float diff = planner.steps_to_mm[Z_AXIS] * babystep_increment,
  312. new_probe_offset = zprobe_zoffset + diff,
  313. new_offs =
  314. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  315. do_probe ? new_probe_offset : hotend_offset[Z_AXIS][active_extruder] - diff
  316. #else
  317. new_probe_offset
  318. #endif
  319. ;
  320. if (WITHIN(new_offs, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) {
  321. thermalManager.babystep_axis(Z_AXIS, babystep_increment);
  322. if (do_probe) zprobe_zoffset = new_offs;
  323. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  324. else hotend_offset[Z_AXIS][active_extruder] = new_offs;
  325. #endif
  326. lcdDrawUpdate = LCDVIEW_CALL_REDRAW_NEXT;
  327. }
  328. }
  329. if (lcdDrawUpdate) {
  330. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  331. if (!do_probe)
  332. lcd_implementation_drawedit(PSTR(MSG_IDEX_Z_OFFSET), ftostr43sign(hotend_offset[Z_AXIS][active_extruder]));
  333. else
  334. #endif
  335. lcd_implementation_drawedit(PSTR(MSG_ZPROBE_ZOFFSET), ftostr43sign(zprobe_zoffset));
  336. #if ENABLED(BABYSTEP_ZPROBE_GFX_OVERLAY)
  337. if (do_probe) _lcd_zoffset_overlay_gfx(zprobe_zoffset);
  338. #endif
  339. }
  340. }
  341. #endif // BABYSTEP_ZPROBE_OFFSET
  342. /**
  343. * Watch temperature callbacks
  344. */
  345. #if HAS_TEMP_HOTEND
  346. #if WATCH_HOTENDS
  347. #define _WATCH_FUNC(N) thermalManager.start_watching_heater(N)
  348. #else
  349. #define _WATCH_FUNC(N) NOOP
  350. #endif
  351. void watch_temp_callback_E0() { _WATCH_FUNC(0); }
  352. #if HOTENDS > 1
  353. void watch_temp_callback_E1() { _WATCH_FUNC(1); }
  354. #if HOTENDS > 2
  355. void watch_temp_callback_E2() { _WATCH_FUNC(2); }
  356. #if HOTENDS > 3
  357. void watch_temp_callback_E3() { _WATCH_FUNC(3); }
  358. #if HOTENDS > 4
  359. void watch_temp_callback_E4() { _WATCH_FUNC(4); }
  360. #if HOTENDS > 5
  361. void watch_temp_callback_E5() { _WATCH_FUNC(5); }
  362. #endif // HOTENDS > 5
  363. #endif // HOTENDS > 4
  364. #endif // HOTENDS > 3
  365. #endif // HOTENDS > 2
  366. #endif // HOTENDS > 1
  367. #endif // HAS_TEMP_HOTEND
  368. void watch_temp_callback_bed() {
  369. #if WATCH_THE_BED
  370. thermalManager.start_watching_bed();
  371. #endif
  372. }
  373. #if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(PID_AUTOTUNE_MENU) || ENABLED(ADVANCED_PAUSE_FEATURE)
  374. void lcd_enqueue_command(const char * const cmd) {
  375. no_reentry = true;
  376. enqueue_and_echo_command_now(cmd);
  377. no_reentry = false;
  378. }
  379. void lcd_enqueue_commands_P(PGM_P const cmd) {
  380. no_reentry = true;
  381. enqueue_and_echo_commands_now_P(cmd);
  382. no_reentry = false;
  383. }
  384. #endif
  385. #if ENABLED(EEPROM_SETTINGS)
  386. void lcd_store_settings() { lcd_completion_feedback(settings.save()); }
  387. void lcd_load_settings() { lcd_completion_feedback(settings.load()); }
  388. #endif
  389. void _lcd_draw_homing() {
  390. constexpr uint8_t line = (LCD_HEIGHT - 1) / 2;
  391. if (lcdDrawUpdate) lcd_implementation_drawmenu_static(line, PSTR(MSG_LEVEL_BED_HOMING));
  392. lcdDrawUpdate = LCDVIEW_CALL_NO_REDRAW;
  393. }
  394. #if ENABLED(LCD_BED_LEVELING) || (HAS_LEVELING && DISABLED(SLIM_LCD_MENUS))
  395. #include "../../feature/bedlevel/bedlevel.h"
  396. void _lcd_toggle_bed_leveling() { set_bed_leveling_enabled(!planner.leveling_active); }
  397. #endif
  398. #endif // HAS_LCD_MENU