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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 <https://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(BABYSTEP_ZPROBE_OFFSET)
  33. #include "../../module/probe.h"
  34. #endif
  35. #if EITHER(ENABLE_LEVELING_FADE_HEIGHT, AUTO_BED_LEVELING_UBL)
  36. #include "../../feature/bedlevel/bedlevel.h"
  37. #endif
  38. ////////////////////////////////////////////
  39. ///////////// Global Variables /////////////
  40. ////////////////////////////////////////////
  41. // Menu Navigation
  42. int8_t encoderTopLine, encoderLine, screen_items;
  43. typedef struct {
  44. screenFunc_t menu_function; // The screen's function
  45. uint32_t encoder_position; // The position of the encoder
  46. int8_t top_line, items; // The amount of scroll, and the number of items
  47. #if SCREENS_CAN_TIME_OUT
  48. bool sticky; // The screen is sticky
  49. #endif
  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::push_current_screen() {
  68. if (screen_history_depth < COUNT(screen_history))
  69. screen_history[screen_history_depth++] = { currentScreen, encoderPosition, encoderTopLine, screen_items OPTARG(SCREENS_CAN_TIME_OUT, screen_is_sticky()) };
  70. }
  71. void MarlinUI::_goto_previous_screen(TERN_(TURBO_BACK_MENU_ITEM, const bool is_back/*=false*/)) {
  72. IF_DISABLED(TURBO_BACK_MENU_ITEM, constexpr bool is_back = false);
  73. TERN_(HAS_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. defer_status_screen(TERN_(SCREENS_CAN_TIME_OUT, sh.sticky));
  82. }
  83. else
  84. return_to_status();
  85. }
  86. ////////////////////////////////////////////
  87. /////////// Menu Editing Actions ///////////
  88. ////////////////////////////////////////////
  89. // All Edit Screens run the same way, but `draw_edit_screen` is implementation-specific
  90. void MenuEditItemBase::edit_screen(strfunc_t strfunc, loadfunc_t loadfunc) {
  91. // Reset repeat_delay for Touch Buttons
  92. TERN_(HAS_TOUCH_BUTTONS, ui.repeat_delay = BUTTON_DELAY_EDIT);
  93. // Constrain ui.encoderPosition to 0 ... maxEditValue (calculated in encoder steps)
  94. if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
  95. if (int32_t(ui.encoderPosition) > maxEditValue) ui.encoderPosition = maxEditValue;
  96. // If drawing is flagged then redraw the (whole) edit screen
  97. if (ui.should_draw())
  98. draw_edit_screen(strfunc(ui.encoderPosition + minEditValue));
  99. // If there was a click or "live editing" and encoder moved...
  100. if (ui.lcd_clicked || (liveEdit && ui.should_draw())) {
  101. // Pass the editValue pointer to the loadfunc along with the encoder plus min
  102. if (editValue) loadfunc(editValue, ui.encoderPosition + minEditValue);
  103. // If a callbackFunc was set, call it for click or always for "live editing"
  104. if (callbackFunc && (liveEdit || ui.lcd_clicked)) (*callbackFunc)();
  105. // Use up the click to finish editing and go to the previous screen
  106. if (ui.use_click()) ui.goto_previous_screen();
  107. }
  108. }
  109. // Going to an edit screen sets up some persistent values first
  110. void MenuEditItemBase::goto_edit_screen(
  111. PGM_P const el, // Edit label
  112. void * const ev, // Edit value pointer
  113. const int32_t minv, // Encoder minimum
  114. const int32_t maxv, // Encoder maximum
  115. const uint16_t ep, // Initial encoder value
  116. const screenFunc_t cs, // MenuItem_type::draw_edit_screen => MenuEditItemBase::edit()
  117. const screenFunc_t cb, // Callback after edit
  118. const bool le // Flag to call cb() during editing
  119. ) {
  120. TERN_(HAS_TOUCH_BUTTONS, ui.on_edit_screen = true);
  121. ui.screen_changed = true;
  122. ui.push_current_screen();
  123. ui.refresh();
  124. editLabel = el;
  125. editValue = ev;
  126. minEditValue = minv;
  127. maxEditValue = maxv;
  128. ui.encoderPosition = ep;
  129. ui.currentScreen = cs;
  130. callbackFunc = cb;
  131. liveEdit = le;
  132. }
  133. ////////////////////////////////////////////
  134. ///////////////// Menu Tree ////////////////
  135. ////////////////////////////////////////////
  136. #include "../../MarlinCore.h"
  137. bool printer_busy() {
  138. return planner.movesplanned() || printingIsActive();
  139. }
  140. /**
  141. * General function to go directly to a screen
  142. */
  143. void MarlinUI::goto_screen(screenFunc_t screen, const uint16_t encoder/*=0*/, const uint8_t top/*=0*/, const uint8_t items/*=0*/) {
  144. if (currentScreen != screen) {
  145. TERN_(IS_DWIN_MARLINUI, did_first_redraw = false);
  146. TERN_(HAS_TOUCH_BUTTONS, repeat_delay = BUTTON_DELAY_MENU);
  147. TERN_(LCD_SET_PROGRESS_MANUALLY, progress_reset());
  148. #if BOTH(DOUBLECLICK_FOR_Z_BABYSTEPPING, BABYSTEPPING)
  149. static millis_t doubleclick_expire_ms = 0;
  150. // Going to menu_main from status screen? Remember first click time.
  151. // Going back to status screen within a very short time? Go to Z babystepping.
  152. if (screen == menu_main) {
  153. if (on_status_screen())
  154. doubleclick_expire_ms = millis() + DOUBLECLICK_MAX_INTERVAL;
  155. }
  156. else if (screen == status_screen && currentScreen == menu_main && PENDING(millis(), doubleclick_expire_ms)) {
  157. if (BABYSTEP_ALLOWED())
  158. screen = TERN(BABYSTEP_ZPROBE_OFFSET, lcd_babystep_zoffset, lcd_babystep_z);
  159. else {
  160. #if ENABLED(MOVE_Z_WHEN_IDLE)
  161. ui.manual_move.menu_scale = MOVE_Z_IDLE_MULTIPLICATOR;
  162. screen = lcd_move_z;
  163. #endif
  164. }
  165. }
  166. #endif
  167. currentScreen = screen;
  168. encoderPosition = encoder;
  169. encoderTopLine = top;
  170. screen_items = items;
  171. if (on_status_screen()) {
  172. defer_status_screen(false);
  173. clear_menu_history();
  174. TERN_(AUTO_BED_LEVELING_UBL, ubl.lcd_map_control = false);
  175. }
  176. clear_lcd();
  177. // Re-initialize custom characters that may be re-used
  178. #if HAS_MARLINUI_HD44780
  179. if (TERN1(AUTO_BED_LEVELING_UBL, !ubl.lcd_map_control))
  180. set_custom_characters(on_status_screen() ? CHARSET_INFO : CHARSET_MENU);
  181. #endif
  182. refresh(LCDVIEW_CALL_REDRAW_NEXT);
  183. screen_changed = true;
  184. TERN_(HAS_MARLINUI_U8GLIB, drawing_screen = false);
  185. TERN_(HAS_LCD_MENU, encoder_direction_normal());
  186. set_selection(false);
  187. }
  188. }
  189. ////////////////////////////////////////////
  190. ///////////// Manual Movement //////////////
  191. ////////////////////////////////////////////
  192. //
  193. // Display a "synchronize" screen with a custom message until
  194. // all moves are finished. Go back to calling screen when done.
  195. //
  196. void MarlinUI::synchronize(PGM_P const msg/*=nullptr*/) {
  197. static PGM_P sync_message = msg ?: GET_TEXT(MSG_MOVING);
  198. push_current_screen();
  199. goto_screen([]{
  200. if (should_draw()) MenuItem_static::draw(LCD_HEIGHT >= 4, sync_message);
  201. });
  202. defer_status_screen();
  203. planner.synchronize(); // idle() is called until moves complete
  204. goto_previous_screen_no_defer();
  205. }
  206. /**
  207. * Scrolling for menus and other line-based screens
  208. *
  209. * encoderLine is the position based on the encoder
  210. * encoderTopLine is the top menu line to display
  211. * screen_items is the total number of items in the menu (after one call)
  212. */
  213. void scroll_screen(const uint8_t limit, const bool is_menu) {
  214. ui.encoder_direction_menus();
  215. ENCODER_RATE_MULTIPLY(false);
  216. if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
  217. if (ui.first_page) {
  218. encoderLine = ui.encoderPosition / (ENCODER_STEPS_PER_MENU_ITEM);
  219. ui.screen_changed = false;
  220. }
  221. if (screen_items > 0 && encoderLine >= screen_items - limit) {
  222. encoderLine = _MAX(0, screen_items - limit);
  223. ui.encoderPosition = encoderLine * (ENCODER_STEPS_PER_MENU_ITEM);
  224. }
  225. if (is_menu) {
  226. NOMORE(encoderTopLine, encoderLine);
  227. if (encoderLine >= encoderTopLine + LCD_HEIGHT)
  228. encoderTopLine = encoderLine - LCD_HEIGHT + 1;
  229. }
  230. else
  231. encoderTopLine = encoderLine;
  232. }
  233. #if HAS_BUZZER
  234. void MarlinUI::completion_feedback(const bool good/*=true*/) {
  235. TERN_(HAS_TOUCH_SLEEP, wakeup_screen()); // Wake up on rotary encoder click...
  236. if (good) {
  237. BUZZ(100, 659);
  238. BUZZ(100, 698);
  239. }
  240. else BUZZ(20, 440);
  241. }
  242. #endif
  243. #if HAS_LINE_TO_Z
  244. void line_to_z(const_float_t z) {
  245. current_position.z = z;
  246. line_to_current_position(manual_feedrate_mm_s.z);
  247. }
  248. #endif
  249. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  250. #include "../../feature/babystep.h"
  251. void lcd_babystep_zoffset() {
  252. if (ui.use_click()) return ui.goto_previous_screen_no_defer();
  253. ui.defer_status_screen();
  254. const bool do_probe = DISABLED(BABYSTEP_HOTEND_Z_OFFSET) || active_extruder == 0;
  255. if (ui.encoderPosition) {
  256. const int16_t babystep_increment = int16_t(ui.encoderPosition) * (BABYSTEP_SIZE_Z);
  257. ui.encoderPosition = 0;
  258. const float diff = planner.mm_per_step[Z_AXIS] * babystep_increment,
  259. new_probe_offset = probe.offset.z + diff,
  260. new_offs = TERN(BABYSTEP_HOTEND_Z_OFFSET
  261. , do_probe ? new_probe_offset : hotend_offset[active_extruder].z - diff
  262. , new_probe_offset
  263. );
  264. if (WITHIN(new_offs, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) {
  265. babystep.add_steps(Z_AXIS, babystep_increment);
  266. if (do_probe)
  267. probe.offset.z = new_offs;
  268. else
  269. TERN(BABYSTEP_HOTEND_Z_OFFSET, hotend_offset[active_extruder].z = new_offs, NOOP);
  270. ui.refresh(LCDVIEW_CALL_REDRAW_NEXT);
  271. }
  272. }
  273. if (ui.should_draw()) {
  274. if (do_probe) {
  275. MenuEditItemBase::draw_edit_screen(GET_TEXT(MSG_ZPROBE_ZOFFSET), BABYSTEP_TO_STR(probe.offset.z));
  276. TERN_(BABYSTEP_ZPROBE_GFX_OVERLAY, ui.zoffset_overlay(probe.offset.z));
  277. }
  278. else {
  279. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  280. MenuEditItemBase::draw_edit_screen(GET_TEXT(MSG_HOTEND_OFFSET_Z), ftostr54sign(hotend_offset[active_extruder].z));
  281. #endif
  282. }
  283. }
  284. }
  285. #endif // BABYSTEP_ZPROBE_OFFSET
  286. void _lcd_draw_homing() {
  287. if (ui.should_draw()) {
  288. constexpr uint8_t line = (LCD_HEIGHT - 1) / 2;
  289. MenuItem_static::draw(line, GET_TEXT(MSG_LEVEL_BED_HOMING));
  290. }
  291. }
  292. #if ENABLED(LCD_BED_LEVELING) || (HAS_LEVELING && DISABLED(SLIM_LCD_MENUS))
  293. #include "../../feature/bedlevel/bedlevel.h"
  294. void _lcd_toggle_bed_leveling() { set_bed_leveling_enabled(!planner.leveling_active); }
  295. #endif
  296. //
  297. // Selection screen presents a prompt and two options
  298. //
  299. bool MarlinUI::selection; // = false
  300. bool MarlinUI::update_selection() {
  301. encoder_direction_select();
  302. if (encoderPosition) {
  303. selection = int16_t(encoderPosition) > 0;
  304. encoderPosition = 0;
  305. }
  306. return selection;
  307. }
  308. void MenuItem_confirm::select_screen(
  309. PGM_P const yes, PGM_P const no,
  310. selectFunc_t yesFunc, selectFunc_t noFunc,
  311. PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/
  312. ) {
  313. ui.defer_status_screen();
  314. const bool ui_selection = ui.update_selection(), got_click = ui.use_click();
  315. if (got_click || ui.should_draw()) {
  316. draw_select_screen(yes, no, ui_selection, pref, string, suff);
  317. if (got_click) {
  318. selectFunc_t callFunc = ui_selection ? yesFunc : noFunc;
  319. if (callFunc) callFunc(); else ui.goto_previous_screen();
  320. }
  321. }
  322. }
  323. #endif // HAS_LCD_MENU