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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 WATCH_HOTENDS || WATCH_BED
  33. #include "../../module/temperature.h"
  34. #endif
  35. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  36. #include "../../module/probe.h"
  37. #endif
  38. #if EITHER(ENABLE_LEVELING_FADE_HEIGHT, AUTO_BED_LEVELING_UBL)
  39. #include "../../feature/bedlevel/bedlevel.h"
  40. #endif
  41. ////////////////////////////////////////////
  42. ///////////// Global Variables /////////////
  43. ////////////////////////////////////////////
  44. // Menu Navigation
  45. int8_t encoderTopLine, encoderLine, screen_items;
  46. typedef struct {
  47. screenFunc_t menu_function;
  48. uint32_t encoder_position;
  49. int8_t top_line, items;
  50. } menuPosition;
  51. menuPosition screen_history[6];
  52. uint8_t screen_history_depth = 0;
  53. int8_t MenuItemBase::itemIndex; // Index number for draw and action
  54. PGM_P MenuItemBase::itemString; // A PSTR for substitution
  55. chimera_t editable; // Value Editing
  56. // Menu Edit Items
  57. PGM_P MenuEditItemBase::editLabel;
  58. void* MenuEditItemBase::editValue;
  59. int32_t MenuEditItemBase::minEditValue,
  60. MenuEditItemBase::maxEditValue;
  61. screenFunc_t MenuEditItemBase::callbackFunc;
  62. bool MenuEditItemBase::liveEdit;
  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++] = { currentScreen, encoderPosition, encoderTopLine, screen_items };
  70. }
  71. void MarlinUI::_goto_previous_screen(TERN_(TURBO_BACK_MENU_ITEM, const bool is_back/*=false*/)) {
  72. TERN(TURBO_BACK_MENU_ITEM,,constexpr bool is_back = false);
  73. TERN_(TOUCH_BUTTONS, on_edit_screen = false);
  74. if (screen_history_depth > 0) {
  75. menuPosition &sh = screen_history[--screen_history_depth];
  76. goto_screen(sh.menu_function,
  77. is_back ? 0 : sh.encoder_position,
  78. is_back ? 0 : sh.top_line,
  79. sh.items
  80. );
  81. }
  82. else
  83. return_to_status();
  84. }
  85. ////////////////////////////////////////////
  86. /////////// Common Menu Actions ////////////
  87. ////////////////////////////////////////////
  88. void MenuItem_gcode::action(PGM_P const, PGM_P const pgcode) { queue.inject_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 classes needed to edit a numerical value.
  96. *
  97. * The prerequisite is that in the header the type was already declared:
  98. *
  99. * DEFINE_MENU_EDIT_ITEM_TYPE(int3, int16_t, i16tostr3rj, 1)
  100. *
  101. * For example, DEFINE_MENU_EDIT_ITEM(int3) expands into:
  102. *
  103. * template class TMenuEditItem<MenuEditItemInfo_int3>
  104. *
  105. * You can then use one of the menu macros to present the edit interface:
  106. * EDIT_ITEM(int3, MSG_SPEED, &feedrate_percentage, 10, 999)
  107. *
  108. * This expands into a more primitive menu item:
  109. * _MENU_ITEM_P(int3, false, GET_TEXT(MSG_SPEED), &feedrate_percentage, 10, 999)
  110. *
  111. * ...which calls:
  112. * MenuItem_int3::action(plabel, &feedrate_percentage, 10, 999)
  113. * MenuItem_int3::draw(encoderLine == _thisItemNr, _lcdLineNr, plabel, &feedrate_percentage, 10, 999)
  114. */
  115. void MenuEditItemBase::edit_screen(strfunc_t strfunc, loadfunc_t loadfunc) {
  116. TERN_(TOUCH_BUTTONS, ui.repeat_delay = BUTTON_DELAY_EDIT);
  117. if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
  118. if (int32_t(ui.encoderPosition) > maxEditValue) ui.encoderPosition = maxEditValue;
  119. if (ui.should_draw())
  120. draw_edit_screen(strfunc(ui.encoderPosition + minEditValue));
  121. if (ui.lcd_clicked || (liveEdit && ui.should_draw())) {
  122. if (editValue != nullptr) loadfunc(editValue, ui.encoderPosition + minEditValue);
  123. if (callbackFunc && (liveEdit || ui.lcd_clicked)) (*callbackFunc)();
  124. if (ui.use_click()) ui.goto_previous_screen();
  125. }
  126. }
  127. void MenuEditItemBase::goto_edit_screen(
  128. PGM_P const el, // Edit label
  129. void * const ev, // Edit value pointer
  130. const int32_t minv, // Encoder minimum
  131. const int32_t maxv, // Encoder maximum
  132. const uint16_t ep, // Initial encoder value
  133. const screenFunc_t cs, // MenuItem_type::draw_edit_screen => MenuEditItemBase::edit()
  134. const screenFunc_t cb, // Callback after edit
  135. const bool le // Flag to call cb() during editing
  136. ) {
  137. TERN_(TOUCH_BUTTONS, ui.on_edit_screen = true);
  138. ui.screen_changed = true;
  139. ui.save_previous_screen();
  140. ui.refresh();
  141. editLabel = el;
  142. editValue = ev;
  143. minEditValue = minv;
  144. maxEditValue = maxv;
  145. ui.encoderPosition = ep;
  146. ui.currentScreen = cs;
  147. callbackFunc = cb;
  148. liveEdit = le;
  149. }
  150. // TODO: Remove these but test build size with and without
  151. #define DEFINE_MENU_EDIT_ITEM(NAME) template class TMenuEditItem<MenuEditItemInfo_##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(float42_52); // _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(float31sign); // +12.3
  167. DEFINE_MENU_EDIT_ITEM(float41sign); // +123.4
  168. DEFINE_MENU_EDIT_ITEM(float51sign); // +1234.5
  169. DEFINE_MENU_EDIT_ITEM(float52sign); // +123.45
  170. DEFINE_MENU_EDIT_ITEM(long5); // 12345 right-justified
  171. DEFINE_MENU_EDIT_ITEM(long5_25); // 12345 right-justified (25 increment)
  172. void MenuItem_bool::action(PGM_P const, bool * const ptr, screenFunc_t callback) {
  173. *ptr ^= true; ui.refresh();
  174. if (callback) (*callback)();
  175. }
  176. ////////////////////////////////////////////
  177. ///////////////// Menu Tree ////////////////
  178. ////////////////////////////////////////////
  179. #include "../../MarlinCore.h"
  180. bool printer_busy() {
  181. return planner.movesplanned() || printingIsActive();
  182. }
  183. /**
  184. * General function to go directly to a screen
  185. */
  186. void MarlinUI::goto_screen(screenFunc_t screen, const uint16_t encoder/*=0*/, const uint8_t top/*=0*/, const uint8_t items/*=0*/) {
  187. if (currentScreen != screen) {
  188. TERN_(TOUCH_BUTTONS, repeat_delay = BUTTON_DELAY_MENU);
  189. TERN_(LCD_SET_PROGRESS_MANUALLY, progress_reset());
  190. #if BOTH(DOUBLECLICK_FOR_Z_BABYSTEPPING, BABYSTEPPING)
  191. static millis_t doubleclick_expire_ms = 0;
  192. // Going to menu_main from status screen? Remember first click time.
  193. // Going back to status screen within a very short time? Go to Z babystepping.
  194. if (screen == menu_main) {
  195. if (on_status_screen())
  196. doubleclick_expire_ms = millis() + DOUBLECLICK_MAX_INTERVAL;
  197. }
  198. else if (screen == status_screen && currentScreen == menu_main && PENDING(millis(), doubleclick_expire_ms)) {
  199. if ( (ENABLED(BABYSTEP_WITHOUT_HOMING) || all_axes_known())
  200. && (ENABLED(BABYSTEP_ALWAYS_AVAILABLE) || printer_busy()) )
  201. screen = TERN(BABYSTEP_ZPROBE_OFFSET, lcd_babystep_zoffset, lcd_babystep_z);
  202. else {
  203. #if ENABLED(MOVE_Z_WHEN_IDLE)
  204. ui.manual_move.menu_scale = MOVE_Z_IDLE_MULTIPLICATOR;
  205. screen = lcd_move_z;
  206. #endif
  207. }
  208. }
  209. #endif
  210. currentScreen = screen;
  211. encoderPosition = encoder;
  212. encoderTopLine = top;
  213. screen_items = items;
  214. if (on_status_screen()) {
  215. defer_status_screen(false);
  216. TERN_(AUTO_BED_LEVELING_UBL, ubl.lcd_map_control = false);
  217. screen_history_depth = 0;
  218. }
  219. clear_lcd();
  220. // Re-initialize custom characters that may be re-used
  221. #if HAS_CHARACTER_LCD
  222. if (TERN1(AUTO_BED_LEVELING_UBL, !ubl.lcd_map_control))
  223. set_custom_characters(on_status_screen() ? CHARSET_INFO : CHARSET_MENU);
  224. #endif
  225. refresh(LCDVIEW_CALL_REDRAW_NEXT);
  226. screen_changed = true;
  227. TERN_(HAS_GRAPHICAL_LCD, drawing_screen = false);
  228. TERN_(HAS_LCD_MENU, encoder_direction_normal());
  229. set_selection(false);
  230. }
  231. }
  232. ////////////////////////////////////////////
  233. ///////////// Manual Movement //////////////
  234. ////////////////////////////////////////////
  235. //
  236. // Display a "synchronize" screen with a custom message until
  237. // all moves are finished. Go back to calling screen when done.
  238. //
  239. void MarlinUI::synchronize(PGM_P const msg/*=nullptr*/) {
  240. static PGM_P sync_message = msg ?: GET_TEXT(MSG_MOVING);
  241. save_previous_screen();
  242. goto_screen([]{
  243. if (should_draw()) MenuItem_static::draw(LCD_HEIGHT >= 4, sync_message);
  244. });
  245. defer_status_screen();
  246. planner.synchronize(); // idle() is called until moves complete
  247. goto_previous_screen_no_defer();
  248. }
  249. /**
  250. * Scrolling for menus and other line-based screens
  251. *
  252. * encoderLine is the position based on the encoder
  253. * encoderTopLine is the top menu line to display
  254. * _lcdLineNr is the index of the LCD line (e.g., 0-3)
  255. * _menuLineNr is the menu item to draw and process
  256. * _thisItemNr is the index of each MENU_ITEM or STATIC_ITEM
  257. * screen_items is the total number of items in the menu (after one call)
  258. */
  259. void scroll_screen(const uint8_t limit, const bool is_menu) {
  260. ui.encoder_direction_menus();
  261. ENCODER_RATE_MULTIPLY(false);
  262. if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
  263. if (ui.first_page) {
  264. encoderLine = ui.encoderPosition / (ENCODER_STEPS_PER_MENU_ITEM);
  265. ui.screen_changed = false;
  266. }
  267. if (screen_items > 0 && encoderLine >= screen_items - limit) {
  268. encoderLine = _MAX(0, screen_items - limit);
  269. ui.encoderPosition = encoderLine * (ENCODER_STEPS_PER_MENU_ITEM);
  270. }
  271. if (is_menu) {
  272. NOMORE(encoderTopLine, encoderLine);
  273. if (encoderLine >= encoderTopLine + LCD_HEIGHT)
  274. encoderTopLine = encoderLine - LCD_HEIGHT + 1;
  275. }
  276. else
  277. encoderTopLine = encoderLine;
  278. }
  279. #if HAS_BUZZER
  280. void MarlinUI::completion_feedback(const bool good/*=true*/) {
  281. if (good) {
  282. BUZZ(100, 659);
  283. BUZZ(100, 698);
  284. }
  285. else BUZZ(20, 440);
  286. }
  287. #endif
  288. #if HAS_LINE_TO_Z
  289. void line_to_z(const float &z) {
  290. current_position.z = z;
  291. line_to_current_position(manual_feedrate_mm_s.z);
  292. }
  293. #endif
  294. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  295. #include "../../feature/babystep.h"
  296. void lcd_babystep_zoffset() {
  297. if (ui.use_click()) return ui.goto_previous_screen_no_defer();
  298. ui.defer_status_screen();
  299. const bool do_probe = DISABLED(BABYSTEP_HOTEND_Z_OFFSET) || active_extruder == 0;
  300. if (ui.encoderPosition) {
  301. const int16_t babystep_increment = int16_t(ui.encoderPosition) * (BABYSTEP_SIZE_Z);
  302. ui.encoderPosition = 0;
  303. const float diff = planner.steps_to_mm[Z_AXIS] * babystep_increment,
  304. new_probe_offset = probe.offset.z + diff,
  305. new_offs = TERN(BABYSTEP_HOTEND_Z_OFFSET
  306. , do_probe ? new_probe_offset : hotend_offset[active_extruder].z - diff
  307. , new_probe_offset
  308. );
  309. if (WITHIN(new_offs, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) {
  310. babystep.add_steps(Z_AXIS, babystep_increment);
  311. if (do_probe)
  312. probe.offset.z = new_offs;
  313. else
  314. TERN(BABYSTEP_HOTEND_Z_OFFSET, hotend_offset[active_extruder].z = new_offs, NOOP);
  315. ui.refresh(LCDVIEW_CALL_REDRAW_NEXT);
  316. }
  317. }
  318. if (ui.should_draw()) {
  319. if (do_probe) {
  320. MenuEditItemBase::draw_edit_screen(GET_TEXT(MSG_ZPROBE_ZOFFSET), BABYSTEP_TO_STR(probe.offset.z));
  321. TERN_(BABYSTEP_ZPROBE_GFX_OVERLAY, _lcd_zoffset_overlay_gfx(probe.offset.z));
  322. }
  323. else {
  324. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  325. MenuEditItemBase::draw_edit_screen(GET_TEXT(MSG_HOTEND_OFFSET_Z), ftostr54sign(hotend_offset[active_extruder].z));
  326. #endif
  327. }
  328. }
  329. }
  330. #endif // BABYSTEP_ZPROBE_OFFSET
  331. void _lcd_draw_homing() {
  332. constexpr uint8_t line = (LCD_HEIGHT - 1) / 2;
  333. if (ui.should_draw()) MenuItem_static::draw(line, GET_TEXT(MSG_LEVEL_BED_HOMING));
  334. }
  335. #if ENABLED(LCD_BED_LEVELING) || (HAS_LEVELING && DISABLED(SLIM_LCD_MENUS))
  336. #include "../../feature/bedlevel/bedlevel.h"
  337. void _lcd_toggle_bed_leveling() { set_bed_leveling_enabled(!planner.leveling_active); }
  338. #endif
  339. //
  340. // Selection screen presents a prompt and two options
  341. //
  342. bool MarlinUI::selection; // = false
  343. bool MarlinUI::update_selection() {
  344. encoder_direction_select();
  345. if (encoderPosition) {
  346. selection = int16_t(encoderPosition) > 0;
  347. encoderPosition = 0;
  348. }
  349. return selection;
  350. }
  351. void MenuItem_confirm::select_screen(
  352. PGM_P const yes, PGM_P const no,
  353. selectFunc_t yesFunc, selectFunc_t noFunc,
  354. PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/
  355. ) {
  356. const bool ui_selection = ui.update_selection(), got_click = ui.use_click();
  357. if (got_click || ui.should_draw()) {
  358. draw_select_screen(yes, no, ui_selection, pref, string, suff);
  359. if (got_click) { ui_selection ? yesFunc() : noFunc(); }
  360. ui.defer_status_screen();
  361. }
  362. }
  363. #endif // HAS_LCD_MENU