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

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