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) 2020 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 "../../module/planner.h"
  26. #include "../../module/motion.h"
  27. #include "../../module/printcounter.h"
  28. #include "../../gcode/queue.h"
  29. #if HAS_BUZZER
  30. #include "../../libs/buzzer.h"
  31. #endif
  32. #if ENABLED(EEPROM_SETTINGS)
  33. #include "../../module/configuration_store.h"
  34. #endif
  35. #if WATCH_HOTENDS || WATCH_BED
  36. #include "../../module/temperature.h"
  37. #endif
  38. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  39. #include "../../module/probe.h"
  40. #endif
  41. #if EITHER(ENABLE_LEVELING_FADE_HEIGHT, AUTO_BED_LEVELING_UBL)
  42. #include "../../feature/bedlevel/bedlevel.h"
  43. #endif
  44. ////////////////////////////////////////////
  45. ///////////// Global Variables /////////////
  46. ////////////////////////////////////////////
  47. // Menu Navigation
  48. int8_t encoderTopLine, encoderLine, screen_items;
  49. typedef struct {
  50. screenFunc_t menu_function;
  51. uint32_t encoder_position;
  52. int8_t top_line, items;
  53. } menuPosition;
  54. menuPosition screen_history[6];
  55. uint8_t screen_history_depth = 0;
  56. uint8_t MenuItemBase::itemIndex; // Index number for draw and action
  57. chimera_t editable; // Value Editing
  58. // Menu Edit Items
  59. PGM_P MenuEditItemBase::editLabel;
  60. void* MenuEditItemBase::editValue;
  61. int32_t MenuEditItemBase::minEditValue,
  62. MenuEditItemBase::maxEditValue;
  63. screenFunc_t MenuEditItemBase::callbackFunc;
  64. bool MenuEditItemBase::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, 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 classes needed to edit a numerical value.
  105. *
  106. * The prerequisite is that in the header the type was already declared:
  107. *
  108. * DEFINE_MENU_EDIT_ITEM_TYPE(int3, int16_t, i16tostr3rj, 1)
  109. *
  110. * For example, DEFINE_MENU_EDIT_ITEM(int3) expands into:
  111. *
  112. * template class TMenuEditItem<MenuEditItemInfo_int3>
  113. *
  114. * You can then use one of the menu macros to present the edit interface:
  115. * EDIT_ITEM(int3, MSG_SPEED, &feedrate_percentage, 10, 999)
  116. *
  117. * This expands into a more primitive menu item:
  118. * _MENU_ITEM_P(int3, false, GET_TEXT(MSG_SPEED), &feedrate_percentage, 10, 999)
  119. *
  120. * ...which calls:
  121. * MenuItem_int3::action(plabel, &feedrate_percentage, 10, 999)
  122. * MenuItem_int3::draw(encoderLine == _thisItemNr, _lcdLineNr, plabel, &feedrate_percentage, 10, 999)
  123. */
  124. void MenuEditItemBase::edit_screen(strfunc_t strfunc, loadfunc_t loadfunc) {
  125. #if ENABLED(TOUCH_BUTTONS)
  126. ui.repeat_delay = BUTTON_DELAY_EDIT;
  127. #endif
  128. if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
  129. if (int32_t(ui.encoderPosition) > maxEditValue) ui.encoderPosition = maxEditValue;
  130. if (ui.should_draw())
  131. draw_edit_screen(strfunc(ui.encoderPosition + minEditValue));
  132. if (ui.lcd_clicked || (liveEdit && ui.should_draw())) {
  133. if (editValue != nullptr) loadfunc(editValue, ui.encoderPosition + minEditValue);
  134. if (callbackFunc && (liveEdit || ui.lcd_clicked)) (*callbackFunc)();
  135. if (ui.use_click()) ui.goto_previous_screen();
  136. }
  137. }
  138. void MenuEditItemBase::goto_edit_screen(
  139. PGM_P const el, // Edit label
  140. void * const ev, // Edit value pointer
  141. const int32_t minv, // Encoder minimum
  142. const int32_t maxv, // Encoder maximum
  143. const uint16_t ep, // Initial encoder value
  144. const screenFunc_t cs, // MenuItem_type::draw_edit_screen => MenuEditItemBase::edit()
  145. const screenFunc_t cb, // Callback after edit
  146. const bool le // Flag to call cb() during editing
  147. ) {
  148. ui.screen_changed = true;
  149. ui.save_previous_screen();
  150. ui.refresh();
  151. editLabel = el;
  152. editValue = ev;
  153. minEditValue = minv;
  154. maxEditValue = maxv;
  155. ui.encoderPosition = ep;
  156. ui.currentScreen = cs;
  157. callbackFunc = cb;
  158. liveEdit = le;
  159. }
  160. // TODO: Remove these but test build size with and without
  161. #define DEFINE_MENU_EDIT_ITEM(NAME) template class TMenuEditItem<MenuEditItemInfo_##NAME>
  162. DEFINE_MENU_EDIT_ITEM(percent); // 100% right-justified
  163. DEFINE_MENU_EDIT_ITEM(int3); // 123, -12 right-justified
  164. DEFINE_MENU_EDIT_ITEM(int4); // 1234, -123 right-justified
  165. DEFINE_MENU_EDIT_ITEM(int8); // 123, -12 right-justified
  166. DEFINE_MENU_EDIT_ITEM(uint8); // 123 right-justified
  167. DEFINE_MENU_EDIT_ITEM(uint16_3); // 123 right-justified
  168. DEFINE_MENU_EDIT_ITEM(uint16_4); // 1234 right-justified
  169. DEFINE_MENU_EDIT_ITEM(uint16_5); // 12345 right-justified
  170. DEFINE_MENU_EDIT_ITEM(float3); // 123 right-justified
  171. DEFINE_MENU_EDIT_ITEM(float52); // _2.34, 12.34, -2.34 or 123.45, -23.45
  172. DEFINE_MENU_EDIT_ITEM(float43); // 1.234
  173. DEFINE_MENU_EDIT_ITEM(float5); // 12345 right-justified
  174. DEFINE_MENU_EDIT_ITEM(float5_25); // 12345 right-justified (25 increment)
  175. DEFINE_MENU_EDIT_ITEM(float51); // 1234.5 right-justified
  176. DEFINE_MENU_EDIT_ITEM(float41sign); // +123.4
  177. DEFINE_MENU_EDIT_ITEM(float51sign); // +1234.5
  178. DEFINE_MENU_EDIT_ITEM(float52sign); // +123.45
  179. DEFINE_MENU_EDIT_ITEM(long5); // 12345 right-justified
  180. DEFINE_MENU_EDIT_ITEM(long5_25); // 12345 right-justified (25 increment)
  181. void MenuItem_bool::action(PGM_P const, bool * const ptr, screenFunc_t callback) {
  182. *ptr ^= true; ui.refresh();
  183. if (callback) (*callback)();
  184. }
  185. ////////////////////////////////////////////
  186. ///////////////// Menu Tree ////////////////
  187. ////////////////////////////////////////////
  188. #include "../../MarlinCore.h"
  189. bool printer_busy() {
  190. return planner.movesplanned() || printingIsActive();
  191. }
  192. /**
  193. * General function to go directly to a screen
  194. */
  195. void MarlinUI::goto_screen(screenFunc_t screen, const uint16_t encoder/*=0*/, const uint8_t top/*=0*/, const uint8_t items/*=0*/) {
  196. if (currentScreen != screen) {
  197. #if ENABLED(TOUCH_BUTTONS)
  198. repeat_delay = BUTTON_DELAY_MENU;
  199. #endif
  200. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  201. progress_reset();
  202. #endif
  203. #if BOTH(DOUBLECLICK_FOR_Z_BABYSTEPPING, BABYSTEPPING)
  204. static millis_t doubleclick_expire_ms = 0;
  205. // Going to menu_main from status screen? Remember first click time.
  206. // Going back to status screen within a very short time? Go to Z babystepping.
  207. if (screen == menu_main) {
  208. if (on_status_screen())
  209. doubleclick_expire_ms = millis() + DOUBLECLICK_MAX_INTERVAL;
  210. }
  211. else if (screen == status_screen && currentScreen == menu_main && PENDING(millis(), doubleclick_expire_ms)) {
  212. #if ENABLED(BABYSTEP_WITHOUT_HOMING)
  213. constexpr bool can_babystep = true;
  214. #else
  215. const bool can_babystep = all_axes_known();
  216. #endif
  217. #if ENABLED(BABYSTEP_ALWAYS_AVAILABLE)
  218. constexpr bool should_babystep = true;
  219. #else
  220. const bool should_babystep = printer_busy();
  221. #endif
  222. if (should_babystep && can_babystep) {
  223. screen =
  224. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  225. lcd_babystep_zoffset
  226. #else
  227. lcd_babystep_z
  228. #endif
  229. ;
  230. }
  231. #if ENABLED(MOVE_Z_WHEN_IDLE)
  232. else {
  233. move_menu_scale = MOVE_Z_IDLE_MULTIPLICATOR;
  234. screen = lcd_move_z;
  235. }
  236. #endif
  237. }
  238. #endif
  239. currentScreen = screen;
  240. encoderPosition = encoder;
  241. encoderTopLine = top;
  242. screen_items = items;
  243. if (screen == status_screen) {
  244. defer_status_screen(false);
  245. #if ENABLED(AUTO_BED_LEVELING_UBL)
  246. ubl.lcd_map_control = false;
  247. #endif
  248. screen_history_depth = 0;
  249. }
  250. clear_lcd();
  251. // Re-initialize custom characters that may be re-used
  252. #if HAS_CHARACTER_LCD
  253. if (true
  254. #if ENABLED(AUTO_BED_LEVELING_UBL)
  255. && !ubl.lcd_map_control
  256. #endif
  257. ) set_custom_characters(screen == status_screen ? CHARSET_INFO : CHARSET_MENU);
  258. #endif
  259. refresh(LCDVIEW_CALL_REDRAW_NEXT);
  260. screen_changed = true;
  261. #if HAS_GRAPHICAL_LCD
  262. drawing_screen = false;
  263. #endif
  264. #if HAS_LCD_MENU
  265. encoder_direction_normal();
  266. #endif
  267. set_selection(false);
  268. }
  269. }
  270. ////////////////////////////////////////////
  271. ///////////// Manual Movement //////////////
  272. ////////////////////////////////////////////
  273. //
  274. // Display the synchronize screen until moves are
  275. // finished, and don't return to the caller until
  276. // done. ** This blocks the command queue! **
  277. //
  278. static PGM_P sync_message;
  279. void MarlinUI::_synchronize() {
  280. if (should_draw()) MenuItem_static::draw(LCD_HEIGHT >= 4, sync_message);
  281. if (no_reentry) return;
  282. // Make this the current handler till all moves are done
  283. const screenFunc_t old_screen = currentScreen;
  284. goto_screen(_synchronize);
  285. no_reentry = true;
  286. planner.synchronize(); // idle() is called until moves complete
  287. no_reentry = false;
  288. goto_screen(old_screen);
  289. }
  290. // Display the synchronize screen with a custom message
  291. // ** This blocks the command queue! **
  292. void MarlinUI::synchronize(PGM_P const msg/*=nullptr*/) {
  293. sync_message = msg ?: GET_TEXT(MSG_MOVING);
  294. _synchronize();
  295. }
  296. /**
  297. * Scrolling for menus and other line-based screens
  298. *
  299. * encoderLine is the position based on the encoder
  300. * encoderTopLine is the top menu line to display
  301. * _lcdLineNr is the index of the LCD line (e.g., 0-3)
  302. * _menuLineNr is the menu item to draw and process
  303. * _thisItemNr is the index of each MENU_ITEM or STATIC_ITEM
  304. * screen_items is the total number of items in the menu (after one call)
  305. */
  306. void scroll_screen(const uint8_t limit, const bool is_menu) {
  307. ui.encoder_direction_menus();
  308. ENCODER_RATE_MULTIPLY(false);
  309. if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
  310. if (ui.first_page) {
  311. encoderLine = ui.encoderPosition / (ENCODER_STEPS_PER_MENU_ITEM);
  312. ui.screen_changed = false;
  313. }
  314. if (screen_items > 0 && encoderLine >= screen_items - limit) {
  315. encoderLine = _MAX(0, screen_items - limit);
  316. ui.encoderPosition = encoderLine * (ENCODER_STEPS_PER_MENU_ITEM);
  317. }
  318. if (is_menu) {
  319. NOMORE(encoderTopLine, encoderLine);
  320. if (encoderLine >= encoderTopLine + LCD_HEIGHT)
  321. encoderTopLine = encoderLine - LCD_HEIGHT + 1;
  322. }
  323. else
  324. encoderTopLine = encoderLine;
  325. }
  326. #if HAS_BUZZER
  327. void MarlinUI::completion_feedback(const bool good/*=true*/) {
  328. if (good) {
  329. BUZZ(100, 659);
  330. BUZZ(100, 698);
  331. }
  332. else BUZZ(20, 440);
  333. }
  334. #endif
  335. #if HAS_LINE_TO_Z
  336. void line_to_z(const float &z) {
  337. current_position.z = z;
  338. line_to_current_position(manual_feedrate_mm_s.z);
  339. }
  340. #endif
  341. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  342. #include "../../feature/babystep.h"
  343. void lcd_babystep_zoffset() {
  344. if (ui.use_click()) return ui.goto_previous_screen_no_defer();
  345. ui.defer_status_screen();
  346. const bool do_probe = DISABLED(BABYSTEP_HOTEND_Z_OFFSET) || active_extruder == 0;
  347. if (ui.encoderPosition) {
  348. const int16_t babystep_increment = int16_t(ui.encoderPosition) * (BABYSTEP_MULTIPLICATOR_Z);
  349. ui.encoderPosition = 0;
  350. const float diff = planner.steps_to_mm[Z_AXIS] * babystep_increment,
  351. new_probe_offset = probe.offset.z + diff,
  352. new_offs =
  353. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  354. do_probe ? new_probe_offset : hotend_offset[active_extruder].z - diff
  355. #else
  356. new_probe_offset
  357. #endif
  358. ;
  359. if (WITHIN(new_offs, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) {
  360. babystep.add_steps(Z_AXIS, babystep_increment);
  361. if (do_probe) probe.offset.z = new_offs;
  362. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  363. else hotend_offset[active_extruder].z = new_offs;
  364. #endif
  365. ui.refresh(LCDVIEW_CALL_REDRAW_NEXT);
  366. }
  367. }
  368. if (ui.should_draw()) {
  369. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  370. if (!do_probe)
  371. MenuEditItemBase::draw_edit_screen(GET_TEXT(MSG_HOTEND_OFFSET_Z), ftostr54sign(hotend_offset[active_extruder].z));
  372. #endif
  373. if (do_probe) {
  374. MenuEditItemBase::draw_edit_screen(GET_TEXT(MSG_ZPROBE_ZOFFSET), ftostr52sign(probe.offset.z));
  375. #if ENABLED(BABYSTEP_ZPROBE_GFX_OVERLAY)
  376. _lcd_zoffset_overlay_gfx(probe.offset.z);
  377. #endif
  378. }
  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()) MenuItem_static::draw(line, GET_TEXT(MSG_LEVEL_BED_HOMING));
  413. }
  414. #if ENABLED(LCD_BED_LEVELING) || (HAS_LEVELING && DISABLED(SLIM_LCD_MENUS))
  415. #include "../../feature/bedlevel/bedlevel.h"
  416. void _lcd_toggle_bed_leveling() { set_bed_leveling_enabled(!planner.leveling_active); }
  417. #endif
  418. //
  419. // Selection screen presents a prompt and two options
  420. //
  421. bool MarlinUI::selection; // = false
  422. bool MarlinUI::update_selection() {
  423. encoder_direction_select();
  424. if (encoderPosition) {
  425. selection = int16_t(encoderPosition) > 0;
  426. encoderPosition = 0;
  427. }
  428. return selection;
  429. }
  430. void MenuItem_confirm::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*/) {
  431. const bool ui_selection = ui.update_selection(), got_click = ui.use_click();
  432. if (got_click || ui.should_draw()) {
  433. draw_select_screen(yes, no, ui_selection, pref, string, suff);
  434. if (got_click) { ui_selection ? yesFunc() : noFunc(); }
  435. }
  436. }
  437. #endif // HAS_LCD_MENU