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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfigPre.h"
  23. #if HAS_LCD_MENU
  24. #include "menu.h"
  25. #include "../ultralcd.h"
  26. #include "../../module/planner.h"
  27. #include "../../module/motion.h"
  28. #include "../../module/printcounter.h"
  29. #include "../../gcode/queue.h"
  30. #include "../../sd/cardreader.h"
  31. #if HAS_BUZZER
  32. #include "../../libs/buzzer.h"
  33. #endif
  34. #if ENABLED(EEPROM_SETTINGS)
  35. #include "../../module/configuration_store.h"
  36. #endif
  37. #if WATCH_HOTENDS || WATCH_BED
  38. #include "../../module/temperature.h"
  39. #endif
  40. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  41. #include "../../module/probe.h"
  42. #endif
  43. #if EITHER(ENABLE_LEVELING_FADE_HEIGHT, AUTO_BED_LEVELING_UBL)
  44. #include "../../feature/bedlevel/bedlevel.h"
  45. #endif
  46. ////////////////////////////////////////////
  47. ///////////// Global Variables /////////////
  48. ////////////////////////////////////////////
  49. // Menu Navigation
  50. int8_t encoderTopLine, encoderLine, screen_items;
  51. typedef struct {
  52. screenFunc_t menu_function;
  53. uint32_t encoder_position;
  54. int8_t top_line, items;
  55. } menuPosition;
  56. menuPosition screen_history[6];
  57. uint8_t screen_history_depth = 0;
  58. bool screen_changed;
  59. // Value Editing
  60. PGM_P MenuItemBase::editLabel;
  61. void* MenuItemBase::editValue;
  62. int32_t MenuItemBase::minEditValue, MenuItemBase::maxEditValue;
  63. screenFunc_t MenuItemBase::callbackFunc;
  64. bool MenuItemBase::liveEdit;
  65. // Prevent recursion into screen handlers
  66. bool no_reentry = false;
  67. ////////////////////////////////////////////
  68. //////// Menu Navigation & History /////////
  69. ////////////////////////////////////////////
  70. void MarlinUI::return_to_status() { goto_screen(status_screen); }
  71. void MarlinUI::save_previous_screen() {
  72. if (screen_history_depth < COUNT(screen_history))
  73. screen_history[screen_history_depth++] = { currentScreen, encoderPosition, encoderTopLine, screen_items };
  74. }
  75. void MarlinUI::goto_previous_screen(
  76. #if ENABLED(TURBO_BACK_MENU_ITEM)
  77. const bool is_back/*=false*/
  78. #endif
  79. ) {
  80. #if DISABLED(TURBO_BACK_MENU_ITEM)
  81. constexpr bool is_back = false;
  82. #endif
  83. if (screen_history_depth > 0) {
  84. menuPosition &sh = screen_history[--screen_history_depth];
  85. goto_screen(sh.menu_function,
  86. is_back ? 0 : sh.encoder_position,
  87. is_back ? 0 : sh.top_line,
  88. sh.items
  89. );
  90. }
  91. else
  92. return_to_status();
  93. }
  94. ////////////////////////////////////////////
  95. /////////// Common Menu Actions ////////////
  96. ////////////////////////////////////////////
  97. void MenuItem_gcode::action(PGM_P const pgcode) { queue.inject_P(pgcode); }
  98. ////////////////////////////////////////////
  99. /////////// Menu Editing Actions ///////////
  100. ////////////////////////////////////////////
  101. /**
  102. * Functions for editing single values
  103. *
  104. * The "DEFINE_MENU_EDIT_ITEM" macro generates the functions needed to edit a numerical value.
  105. *
  106. * The prerequisite is that in the header the type was already declared:
  107. *
  108. * DECLARE_MENU_EDIT_TYPE(int16_t, int3, i16tostr3, 1)
  109. *
  110. * For example, DEFINE_MENU_EDIT_ITEM(int3) expands into these functions:
  111. *
  112. * bool MenuItem_int3::_edit();
  113. * void MenuItem_int3::edit(); // edit int16_t (interactively)
  114. * 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);
  115. *
  116. * You can then use one of the menu macros to present the edit interface:
  117. * MENU_ITEM_EDIT(int3, MSG_SPEED, &feedrate_percentage, 10, 999)
  118. *
  119. * This expands into a more primitive menu item:
  120. * MENU_ITEM_VARIANT(int3, _edit, MSG_SPEED, PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
  121. *
  122. * ...which calls:
  123. * MenuItem_int3::action_edit(PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
  124. */
  125. void MenuItemBase::edit(strfunc_t strfunc, loadfunc_t loadfunc) {
  126. #if ENABLED(TOUCH_BUTTONS)
  127. ui.repeat_delay = BUTTON_DELAY_EDIT;
  128. #endif
  129. if (int16_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
  130. if (int16_t(ui.encoderPosition) > maxEditValue) ui.encoderPosition = maxEditValue;
  131. if (ui.should_draw())
  132. draw_edit_screen(editLabel, strfunc(ui.encoderPosition + minEditValue));
  133. if (ui.lcd_clicked || (liveEdit && ui.should_draw())) {
  134. if (editValue != nullptr) loadfunc(editValue, ui.encoderPosition + minEditValue);
  135. if (callbackFunc && (liveEdit || ui.lcd_clicked)) (*callbackFunc)();
  136. if (ui.use_click()) ui.goto_previous_screen();
  137. }
  138. }
  139. void MenuItemBase::init(PGM_P const el, void * const ev, const int32_t minv, const int32_t maxv, const uint16_t ep, const screenFunc_t cs, const screenFunc_t cb, const bool le) {
  140. ui.save_previous_screen();
  141. ui.refresh();
  142. editLabel = el;
  143. editValue = ev;
  144. minEditValue = minv;
  145. maxEditValue = maxv;
  146. ui.encoderPosition = ep;
  147. ui.currentScreen = cs;
  148. callbackFunc = cb;
  149. liveEdit = le;
  150. }
  151. #define DEFINE_MENU_EDIT_ITEM(NAME) template class TMenuItem<MenuItemInfo_##NAME>
  152. DEFINE_MENU_EDIT_ITEM(percent); // 100% right-justified
  153. DEFINE_MENU_EDIT_ITEM(int3); // 123, -12 right-justified
  154. DEFINE_MENU_EDIT_ITEM(int4); // 1234, -123 right-justified
  155. DEFINE_MENU_EDIT_ITEM(int8); // 123, -12 right-justified
  156. DEFINE_MENU_EDIT_ITEM(uint8); // 123 right-justified
  157. DEFINE_MENU_EDIT_ITEM(uint16_3); // 123 right-justified
  158. DEFINE_MENU_EDIT_ITEM(uint16_4); // 1234 right-justified
  159. DEFINE_MENU_EDIT_ITEM(uint16_5); // 12345 right-justified
  160. DEFINE_MENU_EDIT_ITEM(float3); // 123 right-justified
  161. DEFINE_MENU_EDIT_ITEM(float52); // _2.34, 12.34, -2.34 or 123.45, -23.45
  162. DEFINE_MENU_EDIT_ITEM(float43); // 1.234
  163. DEFINE_MENU_EDIT_ITEM(float5); // 12345 right-justified
  164. DEFINE_MENU_EDIT_ITEM(float5_25); // 12345 right-justified (25 increment)
  165. DEFINE_MENU_EDIT_ITEM(float51); // 1234.5 right-justified
  166. DEFINE_MENU_EDIT_ITEM(float51sign); // +1234.5
  167. DEFINE_MENU_EDIT_ITEM(float52sign); // +123.45
  168. DEFINE_MENU_EDIT_ITEM(long5); // 12345 right-justified
  169. DEFINE_MENU_EDIT_ITEM(long5_25); // 12345 right-justified (25 increment)
  170. void MenuItem_bool::action_edit(PGM_P pstr, bool *ptr, screenFunc_t callback) {
  171. UNUSED(pstr); *ptr ^= true; ui.refresh();
  172. if (callback) (*callback)();
  173. }
  174. ////////////////////////////////////////////
  175. ///////////////// Menu Tree ////////////////
  176. ////////////////////////////////////////////
  177. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  178. float lcd_z_fade_height;
  179. void _lcd_set_z_fade_height() { set_z_fade_height(lcd_z_fade_height); }
  180. #endif
  181. bool printer_busy() {
  182. return planner.movesplanned() || IS_SD_PRINTING() || print_job_timer.isRunning();
  183. }
  184. /**
  185. * General function to go directly to a screen
  186. */
  187. void MarlinUI::goto_screen(screenFunc_t screen, const uint16_t encoder/*=0*/, const uint8_t top/*=0*/, const uint8_t items/*=0*/) {
  188. if (currentScreen != screen) {
  189. #if ENABLED(TOUCH_BUTTONS)
  190. repeat_delay = BUTTON_DELAY_MENU;
  191. #endif
  192. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  193. // Shadow for editing the fade height
  194. lcd_z_fade_height = planner.z_fade_height;
  195. #endif
  196. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  197. progress_reset();
  198. #endif
  199. #if BOTH(DOUBLECLICK_FOR_Z_BABYSTEPPING, BABYSTEPPING)
  200. static millis_t doubleclick_expire_ms = 0;
  201. // Going to menu_main from status screen? Remember first click time.
  202. // Going back to status screen within a very short time? Go to Z babystepping.
  203. if (screen == menu_main) {
  204. if (on_status_screen())
  205. doubleclick_expire_ms = millis() + DOUBLECLICK_MAX_INTERVAL;
  206. }
  207. else if (screen == status_screen && currentScreen == menu_main && PENDING(millis(), doubleclick_expire_ms)) {
  208. #if ENABLED(BABYSTEP_WITHOUT_HOMING)
  209. constexpr bool can_babystep = true;
  210. #else
  211. const bool can_babystep = all_axes_known();
  212. #endif
  213. #if ENABLED(BABYSTEP_ALWAYS_AVAILABLE)
  214. constexpr bool should_babystep = true;
  215. #else
  216. const bool should_babystep = printer_busy();
  217. #endif
  218. if (should_babystep && can_babystep) {
  219. screen =
  220. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  221. lcd_babystep_zoffset
  222. #else
  223. lcd_babystep_z
  224. #endif
  225. ;
  226. }
  227. #if ENABLED(MOVE_Z_WHEN_IDLE)
  228. else {
  229. move_menu_scale = MOVE_Z_IDLE_MULTIPLICATOR;
  230. screen = lcd_move_z;
  231. }
  232. #endif
  233. }
  234. #endif
  235. currentScreen = screen;
  236. encoderPosition = encoder;
  237. encoderTopLine = top;
  238. screen_items = items;
  239. if (screen == status_screen) {
  240. defer_status_screen(false);
  241. #if ENABLED(AUTO_BED_LEVELING_UBL)
  242. ubl.lcd_map_control = false;
  243. #endif
  244. screen_history_depth = 0;
  245. }
  246. clear_lcd();
  247. // Re-initialize custom characters that may be re-used
  248. #if HAS_CHARACTER_LCD
  249. if (true
  250. #if ENABLED(AUTO_BED_LEVELING_UBL)
  251. && !ubl.lcd_map_control
  252. #endif
  253. ) set_custom_characters(screen == status_screen ? CHARSET_INFO : CHARSET_MENU);
  254. #endif
  255. refresh(LCDVIEW_CALL_REDRAW_NEXT);
  256. screen_changed = true;
  257. #if HAS_GRAPHICAL_LCD
  258. drawing_screen = false;
  259. #endif
  260. #if HAS_LCD_MENU
  261. encoder_direction_normal();
  262. #endif
  263. set_selection(false);
  264. }
  265. }
  266. ////////////////////////////////////////////
  267. ///////////// Manual Movement //////////////
  268. ////////////////////////////////////////////
  269. //
  270. // Display the synchronize screen until moves are
  271. // finished, and don't return to the caller until
  272. // done. ** This blocks the command queue! **
  273. //
  274. static PGM_P sync_message;
  275. void MarlinUI::_synchronize() {
  276. if (should_draw()) draw_menu_item_static(LCD_HEIGHT >= 4 ? 1 : 0, sync_message);
  277. if (no_reentry) return;
  278. // Make this the current handler till all moves are done
  279. no_reentry = true;
  280. const screenFunc_t old_screen = currentScreen;
  281. goto_screen(_synchronize);
  282. planner.synchronize(); // idle() is called until moves complete
  283. no_reentry = false;
  284. goto_screen(old_screen);
  285. }
  286. // Display the synchronize screen with a custom message
  287. // ** This blocks the command queue! **
  288. void MarlinUI::synchronize(PGM_P const msg/*=nullptr*/) {
  289. static const char moving[] PROGMEM = MSG_MOVING;
  290. sync_message = msg ? msg : moving;
  291. _synchronize();
  292. }
  293. /**
  294. * Scrolling for menus and other line-based screens
  295. *
  296. * encoderLine is the position based on the encoder
  297. * encoderTopLine is the top menu line to display
  298. * _lcdLineNr is the index of the LCD line (e.g., 0-3)
  299. * _menuLineNr is the menu item to draw and process
  300. * _thisItemNr is the index of each MENU_ITEM or STATIC_ITEM
  301. * screen_items is the total number of items in the menu (after one call)
  302. */
  303. void scroll_screen(const uint8_t limit, const bool is_menu) {
  304. ui.encoder_direction_menus();
  305. ENCODER_RATE_MULTIPLY(false);
  306. if (ui.encoderPosition > 0x8000) ui.encoderPosition = 0;
  307. if (ui.first_page) {
  308. encoderLine = ui.encoderPosition / (ENCODER_STEPS_PER_MENU_ITEM);
  309. screen_changed = false;
  310. }
  311. if (screen_items > 0 && encoderLine >= screen_items - limit) {
  312. encoderLine = _MAX(0, screen_items - limit);
  313. ui.encoderPosition = encoderLine * (ENCODER_STEPS_PER_MENU_ITEM);
  314. }
  315. if (is_menu) {
  316. NOMORE(encoderTopLine, encoderLine);
  317. if (encoderLine >= encoderTopLine + LCD_HEIGHT)
  318. encoderTopLine = encoderLine - LCD_HEIGHT + 1;
  319. }
  320. else
  321. encoderTopLine = encoderLine;
  322. }
  323. #if HAS_BUZZER
  324. void MarlinUI::completion_feedback(const bool good/*=true*/) {
  325. if (good) {
  326. BUZZ(100, 659);
  327. BUZZ(100, 698);
  328. }
  329. else BUZZ(20, 440);
  330. }
  331. #endif
  332. #if HAS_LINE_TO_Z
  333. void line_to_z(const float &z) {
  334. current_position[Z_AXIS] = z;
  335. planner.buffer_line(current_position, MMM_TO_MMS(manual_feedrate_mm_m[Z_AXIS]), active_extruder);
  336. }
  337. #endif
  338. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  339. #include "../../feature/babystep.h"
  340. void lcd_babystep_zoffset() {
  341. if (ui.use_click()) return ui.goto_previous_screen_no_defer();
  342. ui.defer_status_screen();
  343. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  344. const bool do_probe = (active_extruder == 0);
  345. #else
  346. constexpr bool do_probe = true;
  347. #endif
  348. if (ui.encoderPosition) {
  349. const int16_t babystep_increment = int16_t(ui.encoderPosition) * (BABYSTEP_MULTIPLICATOR);
  350. ui.encoderPosition = 0;
  351. const float diff = planner.steps_to_mm[Z_AXIS] * babystep_increment,
  352. new_probe_offset = zprobe_offset[Z_AXIS] + diff,
  353. new_offs =
  354. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  355. do_probe ? new_probe_offset : hotend_offset[Z_AXIS][active_extruder] - diff
  356. #else
  357. new_probe_offset
  358. #endif
  359. ;
  360. if (WITHIN(new_offs, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) {
  361. babystep.add_steps(Z_AXIS, babystep_increment);
  362. if (do_probe) zprobe_offset[Z_AXIS] = new_offs;
  363. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  364. else hotend_offset[Z_AXIS][active_extruder] = new_offs;
  365. #endif
  366. ui.refresh(LCDVIEW_CALL_REDRAW_NEXT);
  367. }
  368. }
  369. if (ui.should_draw()) {
  370. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  371. if (!do_probe)
  372. draw_edit_screen(PSTR(MSG_Z_OFFSET), ftostr43sign(hotend_offset[Z_AXIS][active_extruder]));
  373. else
  374. #endif
  375. draw_edit_screen(PSTR(MSG_ZPROBE_ZOFFSET), ftostr43sign(zprobe_offset[Z_AXIS]));
  376. #if ENABLED(BABYSTEP_ZPROBE_GFX_OVERLAY)
  377. if (do_probe) _lcd_zoffset_overlay_gfx(zprobe_offset[Z_AXIS]);
  378. #endif
  379. }
  380. }
  381. #endif // BABYSTEP_ZPROBE_OFFSET
  382. #if ANY(AUTO_BED_LEVELING_UBL, PID_AUTOTUNE_MENU, ADVANCED_PAUSE_FEATURE)
  383. void lcd_enqueue_one_now(const char * const cmd) {
  384. no_reentry = true;
  385. queue.enqueue_one_now(cmd);
  386. no_reentry = false;
  387. }
  388. void lcd_enqueue_one_now_P(PGM_P const cmd) {
  389. no_reentry = true;
  390. queue.enqueue_now_P(cmd);
  391. no_reentry = false;
  392. }
  393. #endif
  394. #if ENABLED(EEPROM_SETTINGS)
  395. void lcd_store_settings() {
  396. const bool saved = settings.save();
  397. #if HAS_BUZZER
  398. ui.completion_feedback(saved);
  399. #endif
  400. UNUSED(saved);
  401. }
  402. void lcd_load_settings() {
  403. const bool loaded = settings.load();
  404. #if HAS_BUZZER
  405. ui.completion_feedback(loaded);
  406. #endif
  407. UNUSED(loaded);
  408. }
  409. #endif
  410. void _lcd_draw_homing() {
  411. constexpr uint8_t line = (LCD_HEIGHT - 1) / 2;
  412. if (ui.should_draw()) draw_menu_item_static(line, PSTR(MSG_LEVEL_BED_HOMING));
  413. ui.refresh(LCDVIEW_CALL_NO_REDRAW);
  414. }
  415. #if ENABLED(LCD_BED_LEVELING) || (HAS_LEVELING && DISABLED(SLIM_LCD_MENUS))
  416. #include "../../feature/bedlevel/bedlevel.h"
  417. void _lcd_toggle_bed_leveling() { set_bed_leveling_enabled(!planner.leveling_active); }
  418. #endif
  419. //
  420. // Selection screen presents a prompt and two options
  421. //
  422. bool MarlinUI::selection; // = false
  423. bool MarlinUI::update_selection() {
  424. encoder_direction_select();
  425. if (encoderPosition) {
  426. selection = int16_t(encoderPosition) > 0;
  427. encoderPosition = 0;
  428. }
  429. return selection;
  430. }
  431. void do_select_screen(PGM_P const yes, PGM_P const no, selectFunc_t yesFunc, selectFunc_t noFunc, PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/) {
  432. const bool ui_selection = ui.update_selection(), got_click = ui.use_click();
  433. if (got_click || ui.should_draw()) {
  434. draw_select_screen(yes, no, ui_selection, pref, string, suff);
  435. if (got_click) { ui_selection ? yesFunc() : noFunc(); }
  436. }
  437. }
  438. #endif // HAS_LCD_MENU