My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

menu.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 "../../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. bool screen_changed;
  57. // Value Editing
  58. chimera_t editable;
  59. PGM_P MenuEditItemBase::editLabel;
  60. void* MenuEditItemBase::editValue;
  61. int32_t MenuEditItemBase::minEditValue, MenuEditItemBase::maxEditValue;
  62. screenFunc_t MenuEditItemBase::callbackFunc;
  63. bool MenuEditItemBase::liveEdit;
  64. // Prevent recursion into screen handlers
  65. bool no_reentry = false;
  66. ////////////////////////////////////////////
  67. //////// Menu Navigation & History /////////
  68. ////////////////////////////////////////////
  69. void MarlinUI::return_to_status() { goto_screen(status_screen); }
  70. void MarlinUI::save_previous_screen() {
  71. if (screen_history_depth < COUNT(screen_history))
  72. screen_history[screen_history_depth++] = { currentScreen, encoderPosition, encoderTopLine, screen_items };
  73. }
  74. void MarlinUI::_goto_previous_screen(
  75. #if ENABLED(TURBO_BACK_MENU_ITEM)
  76. const bool is_back/*=false*/
  77. #endif
  78. ) {
  79. #if DISABLED(TURBO_BACK_MENU_ITEM)
  80. constexpr bool is_back = false;
  81. #endif
  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. }
  90. else
  91. return_to_status();
  92. }
  93. ////////////////////////////////////////////
  94. /////////// Common Menu Actions ////////////
  95. ////////////////////////////////////////////
  96. void MenuItem_gcode::action(PGM_P const, PGM_P const pgcode) { queue.inject_P(pgcode); }
  97. ////////////////////////////////////////////
  98. /////////// Menu Editing Actions ///////////
  99. ////////////////////////////////////////////
  100. /**
  101. * Functions for editing single values
  102. *
  103. * The "DEFINE_MENU_EDIT_ITEM" macro generates the functions needed to edit a numerical value.
  104. *
  105. * The prerequisite is that in the header the type was already declared:
  106. *
  107. * DECLARE_MENU_EDIT_TYPE(int16_t, int3, i16tostr3, 1)
  108. *
  109. * For example, DEFINE_MENU_EDIT_ITEM(int3) expands into these functions:
  110. *
  111. * bool MenuItem_int3::_edit();
  112. * void MenuItem_int3::edit(); // edit int16_t (interactively)
  113. * void MenuItem_int3::action(PGM_P const pstr, int16_t * const ptr, const int32_t minValue, const int32_t maxValue, const screenFunc_t callback = null, const bool live = false);
  114. *
  115. * You can then use one of the menu macros to present the edit interface:
  116. * EDIT_ITEM(int3, MSG_SPEED, &feedrate_percentage, 10, 999)
  117. *
  118. * This expands into a more primitive menu item:
  119. * _MENU_ITEM_P(int3, false, GET_TEXT(MSG_SPEED), &feedrate_percentage, 10, 999)
  120. *
  121. * ...which calls:
  122. * MenuItem_int3::action(plabel, &feedrate_percentage, 10, 999)
  123. * MenuItem_int3::draw(encoderLine == _thisItemNr, _lcdLineNr, plabel, &feedrate_percentage, 10, 999)
  124. */
  125. void MenuEditItemBase::edit(strfunc_t strfunc, loadfunc_t loadfunc) {
  126. #if ENABLED(TOUCH_BUTTONS)
  127. ui.repeat_delay = BUTTON_DELAY_EDIT;
  128. #endif
  129. if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
  130. if (int32_t(ui.encoderPosition) > maxEditValue) ui.encoderPosition = maxEditValue;
  131. if (ui.should_draw())
  132. 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 MenuEditItemBase::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 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(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(float41sign); // +123.4
  167. DEFINE_MENU_EDIT_ITEM(float51sign); // +1234.5
  168. DEFINE_MENU_EDIT_ITEM(float52sign); // +123.45
  169. DEFINE_MENU_EDIT_ITEM(long5); // 12345 right-justified
  170. DEFINE_MENU_EDIT_ITEM(long5_25); // 12345 right-justified (25 increment)
  171. void MenuItem_bool::action(PGM_P pstr, bool *ptr, screenFunc_t callback) {
  172. UNUSED(pstr); *ptr ^= true; ui.refresh();
  173. if (callback) (*callback)();
  174. }
  175. ////////////////////////////////////////////
  176. ///////////////// Menu Tree ////////////////
  177. ////////////////////////////////////////////
  178. #include "../../Marlin.h"
  179. bool printer_busy() {
  180. return planner.movesplanned() || printingIsActive();
  181. }
  182. /**
  183. * General function to go directly to a screen
  184. */
  185. void MarlinUI::goto_screen(screenFunc_t screen, const uint16_t encoder/*=0*/, const uint8_t top/*=0*/, const uint8_t items/*=0*/) {
  186. if (currentScreen != screen) {
  187. #if ENABLED(TOUCH_BUTTONS)
  188. repeat_delay = BUTTON_DELAY_MENU;
  189. #endif
  190. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  191. progress_reset();
  192. #endif
  193. #if BOTH(DOUBLECLICK_FOR_Z_BABYSTEPPING, BABYSTEPPING)
  194. static millis_t doubleclick_expire_ms = 0;
  195. // Going to menu_main from status screen? Remember first click time.
  196. // Going back to status screen within a very short time? Go to Z babystepping.
  197. if (screen == menu_main) {
  198. if (on_status_screen())
  199. doubleclick_expire_ms = millis() + DOUBLECLICK_MAX_INTERVAL;
  200. }
  201. else if (screen == status_screen && currentScreen == menu_main && PENDING(millis(), doubleclick_expire_ms)) {
  202. #if ENABLED(BABYSTEP_WITHOUT_HOMING)
  203. constexpr bool can_babystep = true;
  204. #else
  205. const bool can_babystep = all_axes_known();
  206. #endif
  207. #if ENABLED(BABYSTEP_ALWAYS_AVAILABLE)
  208. constexpr bool should_babystep = true;
  209. #else
  210. const bool should_babystep = printer_busy();
  211. #endif
  212. if (should_babystep && can_babystep) {
  213. screen =
  214. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  215. lcd_babystep_zoffset
  216. #else
  217. lcd_babystep_z
  218. #endif
  219. ;
  220. }
  221. #if ENABLED(MOVE_Z_WHEN_IDLE)
  222. else {
  223. move_menu_scale = MOVE_Z_IDLE_MULTIPLICATOR;
  224. screen = lcd_move_z;
  225. }
  226. #endif
  227. }
  228. #endif
  229. currentScreen = screen;
  230. encoderPosition = encoder;
  231. encoderTopLine = top;
  232. screen_items = items;
  233. if (screen == status_screen) {
  234. defer_status_screen(false);
  235. #if ENABLED(AUTO_BED_LEVELING_UBL)
  236. ubl.lcd_map_control = false;
  237. #endif
  238. screen_history_depth = 0;
  239. }
  240. clear_lcd();
  241. // Re-initialize custom characters that may be re-used
  242. #if HAS_CHARACTER_LCD
  243. if (true
  244. #if ENABLED(AUTO_BED_LEVELING_UBL)
  245. && !ubl.lcd_map_control
  246. #endif
  247. ) set_custom_characters(screen == status_screen ? CHARSET_INFO : CHARSET_MENU);
  248. #endif
  249. refresh(LCDVIEW_CALL_REDRAW_NEXT);
  250. screen_changed = true;
  251. #if HAS_GRAPHICAL_LCD
  252. drawing_screen = false;
  253. #endif
  254. #if HAS_LCD_MENU
  255. encoder_direction_normal();
  256. #endif
  257. set_selection(false);
  258. }
  259. }
  260. ////////////////////////////////////////////
  261. ///////////// Manual Movement //////////////
  262. ////////////////////////////////////////////
  263. //
  264. // Display the synchronize screen until moves are
  265. // finished, and don't return to the caller until
  266. // done. ** This blocks the command queue! **
  267. //
  268. static PGM_P sync_message;
  269. void MarlinUI::_synchronize() {
  270. if (should_draw()) MenuItem_static::draw(LCD_HEIGHT >= 4, sync_message);
  271. if (no_reentry) return;
  272. // Make this the current handler till all moves are done
  273. no_reentry = true;
  274. const screenFunc_t old_screen = currentScreen;
  275. goto_screen(_synchronize);
  276. planner.synchronize(); // idle() is called until moves complete
  277. no_reentry = false;
  278. goto_screen(old_screen);
  279. }
  280. // Display the synchronize screen with a custom message
  281. // ** This blocks the command queue! **
  282. void MarlinUI::synchronize(PGM_P const msg/*=nullptr*/) {
  283. sync_message = msg ?: GET_TEXT(MSG_MOVING);
  284. _synchronize();
  285. }
  286. /**
  287. * Scrolling for menus and other line-based screens
  288. *
  289. * encoderLine is the position based on the encoder
  290. * encoderTopLine is the top menu line to display
  291. * _lcdLineNr is the index of the LCD line (e.g., 0-3)
  292. * _menuLineNr is the menu item to draw and process
  293. * _thisItemNr is the index of each MENU_ITEM or STATIC_ITEM
  294. * screen_items is the total number of items in the menu (after one call)
  295. */
  296. void scroll_screen(const uint8_t limit, const bool is_menu) {
  297. ui.encoder_direction_menus();
  298. ENCODER_RATE_MULTIPLY(false);
  299. if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
  300. if (ui.first_page) {
  301. encoderLine = ui.encoderPosition / (ENCODER_STEPS_PER_MENU_ITEM);
  302. screen_changed = false;
  303. }
  304. if (screen_items > 0 && encoderLine >= screen_items - limit) {
  305. encoderLine = _MAX(0, screen_items - limit);
  306. ui.encoderPosition = encoderLine * (ENCODER_STEPS_PER_MENU_ITEM);
  307. }
  308. if (is_menu) {
  309. NOMORE(encoderTopLine, encoderLine);
  310. if (encoderLine >= encoderTopLine + LCD_HEIGHT)
  311. encoderTopLine = encoderLine - LCD_HEIGHT + 1;
  312. }
  313. else
  314. encoderTopLine = encoderLine;
  315. }
  316. #if HAS_BUZZER
  317. void MarlinUI::completion_feedback(const bool good/*=true*/) {
  318. if (good) {
  319. BUZZ(100, 659);
  320. BUZZ(100, 698);
  321. }
  322. else BUZZ(20, 440);
  323. }
  324. #endif
  325. #if HAS_LINE_TO_Z
  326. void line_to_z(const float &z) {
  327. current_position.z = z;
  328. line_to_current_position(MMM_TO_MMS(manual_feedrate_mm_m.z));
  329. }
  330. #endif
  331. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  332. #include "../../feature/babystep.h"
  333. void lcd_babystep_zoffset() {
  334. if (ui.use_click()) return ui.goto_previous_screen_no_defer();
  335. ui.defer_status_screen();
  336. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  337. const bool do_probe = (active_extruder == 0);
  338. #else
  339. constexpr bool do_probe = true;
  340. #endif
  341. if (ui.encoderPosition) {
  342. const int16_t babystep_increment = int16_t(ui.encoderPosition) * (BABYSTEP_MULTIPLICATOR_Z);
  343. ui.encoderPosition = 0;
  344. const float diff = planner.steps_to_mm[Z_AXIS] * babystep_increment,
  345. new_probe_offset = probe_offset.z + diff,
  346. new_offs =
  347. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  348. do_probe ? new_probe_offset : hotend_offset[active_extruder].z - diff
  349. #else
  350. new_probe_offset
  351. #endif
  352. ;
  353. if (WITHIN(new_offs, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) {
  354. babystep.add_steps(Z_AXIS, babystep_increment);
  355. if (do_probe) probe_offset.z = new_offs;
  356. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  357. else hotend_offset[active_extruder].z = new_offs;
  358. #endif
  359. ui.refresh(LCDVIEW_CALL_REDRAW_NEXT);
  360. }
  361. }
  362. if (ui.should_draw()) {
  363. #if ENABLED(BABYSTEP_HOTEND_Z_OFFSET)
  364. if (!do_probe)
  365. MenuEditItemBase::edit_screen(GET_TEXT(MSG_HOTEND_OFFSET_Z), ftostr43sign(hotend_offset[active_extruder].z));
  366. else
  367. #endif
  368. MenuEditItemBase::edit_screen(GET_TEXT(MSG_ZPROBE_ZOFFSET), ftostr43sign(probe_offset.z));
  369. #if ENABLED(BABYSTEP_ZPROBE_GFX_OVERLAY)
  370. if (do_probe) _lcd_zoffset_overlay_gfx(probe_offset.z);
  371. #endif
  372. }
  373. }
  374. #endif // BABYSTEP_ZPROBE_OFFSET
  375. #if ANY(AUTO_BED_LEVELING_UBL, PID_AUTOTUNE_MENU, ADVANCED_PAUSE_FEATURE)
  376. void lcd_enqueue_one_now(const char * const cmd) {
  377. no_reentry = true;
  378. queue.enqueue_one_now(cmd);
  379. no_reentry = false;
  380. }
  381. void lcd_enqueue_one_now_P(PGM_P const cmd) {
  382. no_reentry = true;
  383. queue.enqueue_now_P(cmd);
  384. no_reentry = false;
  385. }
  386. #endif
  387. #if ENABLED(EEPROM_SETTINGS)
  388. void lcd_store_settings() {
  389. const bool saved = settings.save();
  390. #if HAS_BUZZER
  391. ui.completion_feedback(saved);
  392. #endif
  393. UNUSED(saved);
  394. }
  395. void lcd_load_settings() {
  396. const bool loaded = settings.load();
  397. #if HAS_BUZZER
  398. ui.completion_feedback(loaded);
  399. #endif
  400. UNUSED(loaded);
  401. }
  402. #endif
  403. void _lcd_draw_homing() {
  404. constexpr uint8_t line = (LCD_HEIGHT - 1) / 2;
  405. if (ui.should_draw()) MenuItem_static::draw(line, GET_TEXT(MSG_LEVEL_BED_HOMING));
  406. }
  407. #if ENABLED(LCD_BED_LEVELING) || (HAS_LEVELING && DISABLED(SLIM_LCD_MENUS))
  408. #include "../../feature/bedlevel/bedlevel.h"
  409. void _lcd_toggle_bed_leveling() { set_bed_leveling_enabled(!planner.leveling_active); }
  410. #endif
  411. //
  412. // Selection screen presents a prompt and two options
  413. //
  414. bool MarlinUI::selection; // = false
  415. bool MarlinUI::update_selection() {
  416. encoder_direction_select();
  417. if (encoderPosition) {
  418. selection = int16_t(encoderPosition) > 0;
  419. encoderPosition = 0;
  420. }
  421. return selection;
  422. }
  423. 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*/) {
  424. const bool ui_selection = ui.update_selection(), got_click = ui.use_click();
  425. if (got_click || ui.should_draw()) {
  426. draw_select_screen(yes, no, ui_selection, pref, string, suff);
  427. if (got_click) { ui_selection ? yesFunc() : noFunc(); }
  428. }
  429. }
  430. #endif // HAS_LCD_MENU