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

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