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_ubl.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. //
  23. // Unified Bed Leveling Menus
  24. //
  25. #include "../../inc/MarlinConfigPre.h"
  26. #if BOTH(HAS_MARLINUI_MENU, AUTO_BED_LEVELING_UBL)
  27. #include "menu_item.h"
  28. #include "../../gcode/gcode.h"
  29. #include "../../gcode/queue.h"
  30. #include "../../module/motion.h"
  31. #include "../../module/planner.h"
  32. #include "../../module/settings.h"
  33. #include "../../feature/bedlevel/bedlevel.h"
  34. static int16_t ubl_storage_slot = 0,
  35. custom_hotend_temp = 150,
  36. side_points = 3,
  37. ubl_fillin_amount = 5,
  38. ubl_height_amount = 1;
  39. static uint8_t n_edit_pts = 1;
  40. static int8_t x_plot = 0, y_plot = 0; // May be negative during move
  41. #if HAS_HEATED_BED
  42. static int16_t custom_bed_temp = 50;
  43. #endif
  44. float mesh_edit_accumulator; // Rounded to 2.5 decimal places on use
  45. inline float rounded_mesh_value() {
  46. const int32_t rounded = int32_t(mesh_edit_accumulator * 1000);
  47. return float(rounded - (rounded % 5L)) / 1000;
  48. }
  49. /**
  50. * This screen displays the temporary mesh value and updates it based on encoder
  51. * movement. While this screen is active ubl.fine_tune_mesh sits in a loop getting
  52. * the current value via ubl_mesh_value, moves the Z axis, and updates the mesh
  53. * value until the encoder button is pressed.
  54. *
  55. * - Update the 'mesh_edit_accumulator' from encoder rotation
  56. * - Draw the mesh value (with draw_edit_screen)
  57. * - Draw the graphical overlay, if enabled.
  58. * - Update the 'refresh' state according to the display type
  59. */
  60. void _lcd_mesh_fine_tune(PGM_P const msg) {
  61. constexpr float mesh_edit_step = 1.0f / 200.0f;
  62. ui.defer_status_screen();
  63. if (ubl.encoder_diff) {
  64. mesh_edit_accumulator += TERN(IS_TFTGLCD_PANEL,
  65. ubl.encoder_diff * mesh_edit_step / ENCODER_PULSES_PER_STEP,
  66. ubl.encoder_diff > 0 ? mesh_edit_step : -mesh_edit_step
  67. );
  68. ubl.encoder_diff = 0;
  69. IF_DISABLED(IS_TFTGLCD_PANEL, ui.refresh(LCDVIEW_CALL_REDRAW_NEXT));
  70. }
  71. TERN_(IS_TFTGLCD_PANEL, ui.refresh(LCDVIEW_CALL_REDRAW_NEXT));
  72. if (ui.should_draw()) {
  73. const float rounded_f = rounded_mesh_value();
  74. MenuEditItemBase::draw_edit_screen(msg, ftostr43sign(rounded_f));
  75. TERN_(MESH_EDIT_GFX_OVERLAY, ui.zoffset_overlay(rounded_f));
  76. TERN_(HAS_GRAPHICAL_TFT, ui.refresh(LCDVIEW_NONE));
  77. }
  78. }
  79. //
  80. // Init mesh editing and go to the fine tuning screen (ubl.fine_tune_mesh)
  81. // To capture encoder events UBL will also call ui.capture and ui.release.
  82. //
  83. void MarlinUI::ubl_mesh_edit_start(const_float_t initial) {
  84. TERN_(HAS_GRAPHICAL_TFT, clear_lcd());
  85. mesh_edit_accumulator = initial;
  86. goto_screen([]{ _lcd_mesh_fine_tune(GET_TEXT(MSG_MESH_EDIT_Z)); });
  87. }
  88. //
  89. // Get the mesh value within a Z adjustment loop (ubl.fine_tune_mesh)
  90. //
  91. float MarlinUI::ubl_mesh_value() { return rounded_mesh_value(); }
  92. /**
  93. * UBL Build Custom Mesh Command
  94. */
  95. void _lcd_ubl_build_custom_mesh() {
  96. char ubl_lcd_gcode[64];
  97. #if HAS_HEATED_BED
  98. sprintf_P(ubl_lcd_gcode, PSTR("G28\nM190 S%i\nM109 S%i\nG29 P1"), custom_bed_temp, custom_hotend_temp);
  99. #else
  100. sprintf_P(ubl_lcd_gcode, PSTR("G28\nM109 S%i\nG29 P1"), custom_hotend_temp);
  101. #endif
  102. queue.inject(ubl_lcd_gcode);
  103. }
  104. /**
  105. * UBL Custom Mesh submenu
  106. *
  107. * << Build Mesh
  108. * Hotend Temp: ---
  109. * Bed Temp: ---
  110. * Build Custom Mesh
  111. */
  112. void _lcd_ubl_custom_mesh() {
  113. START_MENU();
  114. BACK_ITEM(MSG_UBL_BUILD_MESH_MENU);
  115. #if HAS_HOTEND
  116. EDIT_ITEM(int3, MSG_UBL_HOTEND_TEMP_CUSTOM, &custom_hotend_temp, EXTRUDE_MINTEMP, thermalManager.hotend_max_target(0));
  117. #endif
  118. #if HAS_HEATED_BED
  119. EDIT_ITEM(int3, MSG_UBL_BED_TEMP_CUSTOM, &custom_bed_temp, BED_MINTEMP, BED_MAX_TARGET);
  120. #endif
  121. ACTION_ITEM(MSG_UBL_BUILD_CUSTOM_MESH, _lcd_ubl_build_custom_mesh);
  122. END_MENU();
  123. }
  124. /**
  125. * UBL Adjust Mesh Height Command
  126. */
  127. void _lcd_ubl_adjust_height_cmd() {
  128. char ubl_lcd_gcode[13];
  129. const int ind = ubl_height_amount > 0 ? 6 : 7;
  130. strcpy_P(ubl_lcd_gcode, PSTR("G29P6C-"));
  131. sprintf_P(&ubl_lcd_gcode[ind], PSTR(".%i"), ABS(ubl_height_amount));
  132. queue.inject(ubl_lcd_gcode);
  133. }
  134. /**
  135. * UBL Adjust Mesh Height submenu
  136. *
  137. * << Edit Mesh
  138. * Height Amount: ---
  139. * Adjust Mesh Height
  140. * << Info Screen
  141. */
  142. void _menu_ubl_height_adjust() {
  143. START_MENU();
  144. BACK_ITEM(MSG_EDIT_MESH);
  145. EDIT_ITEM(int3, MSG_UBL_MESH_HEIGHT_AMOUNT, &ubl_height_amount, -9, 9, _lcd_ubl_adjust_height_cmd);
  146. ACTION_ITEM(MSG_INFO_SCREEN, ui.return_to_status);
  147. END_MENU();
  148. }
  149. /**
  150. * UBL Edit Mesh submenu
  151. *
  152. * << UBL Tools
  153. * Fine Tune All
  154. * Fine Tune Closest
  155. * - Adjust Mesh Height >>
  156. * << Info Screen
  157. */
  158. void _lcd_ubl_edit_mesh() {
  159. START_MENU();
  160. BACK_ITEM(MSG_UBL_TOOLS);
  161. GCODES_ITEM(MSG_UBL_FINE_TUNE_ALL, PSTR("G29P4RT"));
  162. GCODES_ITEM(MSG_UBL_FINE_TUNE_CLOSEST, PSTR("G29P4T"));
  163. SUBMENU(MSG_UBL_MESH_HEIGHT_ADJUST, _menu_ubl_height_adjust);
  164. ACTION_ITEM(MSG_INFO_SCREEN, ui.return_to_status);
  165. END_MENU();
  166. }
  167. #if ENABLED(G26_MESH_VALIDATION)
  168. /**
  169. * UBL Validate Custom Mesh Command
  170. */
  171. void _lcd_ubl_validate_custom_mesh() {
  172. char ubl_lcd_gcode[20];
  173. sprintf_P(ubl_lcd_gcode, PSTR("G28\nG26CPH%" PRIi16 TERN_(HAS_HEATED_BED, "B%" PRIi16))
  174. , custom_hotend_temp
  175. OPTARG(HAS_HEATED_BED, custom_bed_temp)
  176. );
  177. queue.inject(ubl_lcd_gcode);
  178. }
  179. /**
  180. * UBL Validate Mesh submenu
  181. *
  182. * << UBL Tools
  183. * Mesh Validation with Material 1 up to 5
  184. * Validate Custom Mesh
  185. * << Info Screen
  186. */
  187. void _lcd_ubl_validate_mesh() {
  188. START_MENU();
  189. BACK_ITEM(MSG_UBL_TOOLS);
  190. #if HAS_PREHEAT
  191. #if HAS_HEATED_BED
  192. #define VALIDATE_MESH_GCODE_ITEM(M) \
  193. GCODES_ITEM_N_S(M, ui.get_preheat_label(M), MSG_UBL_VALIDATE_MESH_M, PSTR("G28\nG26CPI" STRINGIFY(M)))
  194. #else
  195. #define VALIDATE_MESH_GCODE_ITEM(M) \
  196. GCODES_ITEM_N_S(M, ui.get_preheat_label(M), MSG_UBL_VALIDATE_MESH_M, PSTR("G28\nG26CPB0I" STRINGIFY(M)))
  197. #endif
  198. VALIDATE_MESH_GCODE_ITEM(0);
  199. #if PREHEAT_COUNT > 1
  200. VALIDATE_MESH_GCODE_ITEM(1);
  201. #if PREHEAT_COUNT > 2
  202. VALIDATE_MESH_GCODE_ITEM(2);
  203. #if PREHEAT_COUNT > 3
  204. VALIDATE_MESH_GCODE_ITEM(3);
  205. #if PREHEAT_COUNT > 4
  206. VALIDATE_MESH_GCODE_ITEM(4);
  207. #endif
  208. #endif
  209. #endif
  210. #endif
  211. #endif // HAS_PREHEAT
  212. ACTION_ITEM(MSG_UBL_VALIDATE_CUSTOM_MESH, _lcd_ubl_validate_custom_mesh);
  213. ACTION_ITEM(MSG_INFO_SCREEN, ui.return_to_status);
  214. END_MENU();
  215. }
  216. #endif
  217. /**
  218. * UBL Grid Leveling submenu
  219. *
  220. * << UBL Tools
  221. * Side points: ---
  222. * Level Mesh
  223. */
  224. void _lcd_ubl_grid_level() {
  225. START_MENU();
  226. BACK_ITEM(MSG_UBL_TOOLS);
  227. EDIT_ITEM(int3, MSG_UBL_SIDE_POINTS, &side_points, 2, 6);
  228. ACTION_ITEM(MSG_UBL_MESH_LEVEL, []{
  229. char ubl_lcd_gcode[12];
  230. sprintf_P(ubl_lcd_gcode, PSTR("G29J%i"), side_points);
  231. queue.inject(ubl_lcd_gcode);
  232. });
  233. END_MENU();
  234. }
  235. /**
  236. * UBL Mesh Leveling submenu
  237. *
  238. * << UBL Tools
  239. * 3-Point Mesh Leveling
  240. * - Grid Mesh Leveling >>
  241. * << Info Screen
  242. */
  243. void _lcd_ubl_mesh_leveling() {
  244. START_MENU();
  245. BACK_ITEM(MSG_UBL_TOOLS);
  246. GCODES_ITEM(MSG_UBL_3POINT_MESH_LEVELING, PSTR("G29J0"));
  247. SUBMENU(MSG_UBL_GRID_MESH_LEVELING, _lcd_ubl_grid_level);
  248. ACTION_ITEM(MSG_INFO_SCREEN, ui.return_to_status);
  249. END_MENU();
  250. }
  251. /**
  252. * UBL Fill-in Amount Mesh Command
  253. */
  254. void _lcd_ubl_fillin_amount_cmd() {
  255. char ubl_lcd_gcode[18];
  256. sprintf_P(ubl_lcd_gcode, PSTR("G29P3RC.%i"), ubl_fillin_amount);
  257. gcode.process_subcommands_now(ubl_lcd_gcode);
  258. }
  259. /**
  260. * UBL Fill-in Mesh submenu
  261. *
  262. * << Build Mesh
  263. * Fill-in Amount: ---
  264. * Fill-in Mesh
  265. * Smart Fill-in
  266. * Manual Fill-in
  267. * << Info Screen
  268. */
  269. void _menu_ubl_fillin() {
  270. START_MENU();
  271. BACK_ITEM(MSG_UBL_BUILD_MESH_MENU);
  272. EDIT_ITEM(int3, MSG_UBL_FILLIN_AMOUNT, &ubl_fillin_amount, 0, 9, _lcd_ubl_fillin_amount_cmd);
  273. GCODES_ITEM(MSG_UBL_SMART_FILLIN, PSTR("G29P3T0"));
  274. GCODES_ITEM(MSG_UBL_MANUAL_FILLIN, PSTR("G29P2BT0"));
  275. ACTION_ITEM(MSG_INFO_SCREEN, ui.return_to_status);
  276. END_MENU();
  277. }
  278. void _lcd_ubl_invalidate() {
  279. ubl.invalidate();
  280. SERIAL_ECHOLNPGM("Mesh invalidated.");
  281. }
  282. /**
  283. * UBL Build Mesh submenu
  284. *
  285. * << UBL Tools
  286. * Build Mesh with Material 1 up to 5
  287. * - Build Custom Mesh >>
  288. * Build Cold Mesh
  289. * - Fill-in Mesh >>
  290. * Continue Bed Mesh
  291. * Invalidate All
  292. * Invalidate Closest
  293. * << Info Screen
  294. */
  295. void _lcd_ubl_build_mesh() {
  296. START_MENU();
  297. BACK_ITEM(MSG_UBL_TOOLS);
  298. #if HAS_PREHEAT
  299. #if HAS_HEATED_BED
  300. #define PREHEAT_BED_GCODE(M) "M190I" STRINGIFY(M) "\n"
  301. #else
  302. #define PREHEAT_BED_GCODE(M) ""
  303. #endif
  304. #define BUILD_MESH_GCODE_ITEM(M) GCODES_ITEM_S(ui.get_preheat_label(M), MSG_UBL_BUILD_MESH_M, \
  305. PSTR( \
  306. "G28\n" \
  307. PREHEAT_BED_GCODE(M) \
  308. "M109I" STRINGIFY(M) "\n" \
  309. "G29P1\n" \
  310. "M104S0\n" \
  311. "M140S0" \
  312. ) )
  313. BUILD_MESH_GCODE_ITEM(0);
  314. #if PREHEAT_COUNT > 1
  315. BUILD_MESH_GCODE_ITEM(1);
  316. #if PREHEAT_COUNT > 2
  317. BUILD_MESH_GCODE_ITEM(2);
  318. #if PREHEAT_COUNT > 3
  319. BUILD_MESH_GCODE_ITEM(3);
  320. #if PREHEAT_COUNT > 4
  321. BUILD_MESH_GCODE_ITEM(4);
  322. #endif
  323. #endif
  324. #endif
  325. #endif
  326. #endif // HAS_PREHEAT
  327. SUBMENU(MSG_UBL_BUILD_CUSTOM_MESH, _lcd_ubl_custom_mesh);
  328. GCODES_ITEM(MSG_UBL_BUILD_COLD_MESH, PSTR("G29NP1"));
  329. SUBMENU(MSG_UBL_FILLIN_MESH, _menu_ubl_fillin);
  330. GCODES_ITEM(MSG_UBL_CONTINUE_MESH, PSTR("G29P1C"));
  331. ACTION_ITEM(MSG_UBL_INVALIDATE_ALL, _lcd_ubl_invalidate);
  332. GCODES_ITEM(MSG_UBL_INVALIDATE_CLOSEST, PSTR("G29I"));
  333. ACTION_ITEM(MSG_INFO_SCREEN, ui.return_to_status);
  334. END_MENU();
  335. }
  336. /**
  337. * UBL Load / Save Mesh Commands
  338. */
  339. inline void _lcd_ubl_load_save_cmd(const char loadsave, PGM_P const msg) {
  340. char ubl_lcd_gcode[40];
  341. sprintf_P(ubl_lcd_gcode, PSTR("G29%c%i\nM117 "), loadsave, ubl_storage_slot);
  342. sprintf_P(&ubl_lcd_gcode[strlen(ubl_lcd_gcode)], msg, ubl_storage_slot);
  343. gcode.process_subcommands_now(ubl_lcd_gcode);
  344. }
  345. void _lcd_ubl_load_mesh_cmd() { _lcd_ubl_load_save_cmd('L', GET_TEXT(MSG_MESH_LOADED)); }
  346. void _lcd_ubl_save_mesh_cmd() { _lcd_ubl_load_save_cmd('S', GET_TEXT(MSG_MESH_SAVED)); }
  347. /**
  348. * UBL Mesh Storage submenu
  349. *
  350. * << Unified Bed Leveling
  351. * Memory Slot: ---
  352. * Load Bed Mesh
  353. * Save Bed Mesh
  354. */
  355. void _lcd_ubl_storage_mesh() {
  356. int16_t a = settings.calc_num_meshes();
  357. START_MENU();
  358. BACK_ITEM(MSG_UBL_LEVEL_BED);
  359. if (!WITHIN(ubl_storage_slot, 0, a - 1))
  360. STATIC_ITEM(MSG_UBL_NO_STORAGE);
  361. else {
  362. EDIT_ITEM(int3, MSG_UBL_STORAGE_SLOT, &ubl_storage_slot, 0, a - 1);
  363. ACTION_ITEM(MSG_UBL_LOAD_MESH, _lcd_ubl_load_mesh_cmd);
  364. ACTION_ITEM(MSG_UBL_SAVE_MESH, _lcd_ubl_save_mesh_cmd);
  365. }
  366. END_MENU();
  367. }
  368. /**
  369. * UBL LCD "radar" map point editing
  370. */
  371. void _lcd_ubl_map_edit_cmd() {
  372. char ubl_lcd_gcode[50], str[10], str2[10];
  373. dtostrf(ubl.mesh_index_to_xpos(x_plot), 0, 2, str);
  374. dtostrf(ubl.mesh_index_to_ypos(y_plot), 0, 2, str2);
  375. snprintf_P(ubl_lcd_gcode, sizeof(ubl_lcd_gcode), PSTR("G29P4X%sY%sR%i"), str, str2, int(n_edit_pts));
  376. queue.inject(ubl_lcd_gcode);
  377. }
  378. /**
  379. * UBL LCD Map Movement
  380. */
  381. void ubl_map_move_to_xy() {
  382. const xy_pos_t xy = { ubl.mesh_index_to_xpos(x_plot), ubl.mesh_index_to_ypos(y_plot) };
  383. // Some printers have unreachable areas in the mesh. Skip the move if unreachable.
  384. if (!position_is_reachable(xy)) return;
  385. #if ENABLED(DELTA)
  386. if (current_position.z > delta_clip_start_height) { // Make sure the delta has fully free motion
  387. destination = current_position;
  388. destination.z = delta_clip_start_height;
  389. prepare_internal_fast_move_to_destination(homing_feedrate(Z_AXIS)); // Set current_position from destination
  390. }
  391. #endif
  392. // Use the built-in manual move handler to move to the mesh point.
  393. ui.manual_move.set_destination(xy);
  394. ui.manual_move.soon(ALL_AXES_ENUM);
  395. }
  396. inline int32_t grid_index(const uint8_t x, const uint8_t y) {
  397. return (GRID_MAX_POINTS_X) * y + x;
  398. }
  399. /**
  400. * UBL LCD "radar" map
  401. */
  402. void ubl_map_screen() {
  403. // static millis_t next_move = 0;
  404. // const millis_t ms = millis();
  405. uint8_t x, y;
  406. if (ui.first_page) {
  407. // On click send "G29 P4 ..." to edit the Z value
  408. if (ui.use_click()) {
  409. _lcd_ubl_map_edit_cmd();
  410. return;
  411. }
  412. ui.defer_status_screen();
  413. #if IS_KINEMATIC
  414. // Index of the mesh point upon entry
  415. const int32_t old_pos_index = grid_index(x_plot, y_plot);
  416. // Direction from new (unconstrained) encoder value
  417. const int8_t step_dir = int32_t(ui.encoderPosition) < old_pos_index ? -1 : 1;
  418. #endif
  419. do {
  420. // Now, keep the encoder position within range
  421. if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = GRID_MAX_POINTS + TERN(TOUCH_SCREEN, ui.encoderPosition, -1);
  422. if (int32_t(ui.encoderPosition) > GRID_MAX_POINTS - 1) ui.encoderPosition = TERN(TOUCH_SCREEN, ui.encoderPosition - GRID_MAX_POINTS, 0);
  423. // Draw the grid point based on the encoder
  424. x = ui.encoderPosition % (GRID_MAX_POINTS_X);
  425. y = ui.encoderPosition / (GRID_MAX_POINTS_X);
  426. // Validate if needed
  427. #if IS_KINEMATIC
  428. const xy_pos_t xy = { ubl.mesh_index_to_xpos(x), ubl.mesh_index_to_ypos(y) };
  429. if (position_is_reachable(xy)) break; // Found a valid point
  430. ui.encoderPosition += step_dir; // Test the next point
  431. #endif
  432. } while (ENABLED(IS_KINEMATIC));
  433. // Determine number of points to edit
  434. #if IS_KINEMATIC
  435. n_edit_pts = 9; // TODO: Delta accessible edit points
  436. #else
  437. const bool xc = WITHIN(x, 1, (GRID_MAX_POINTS_X) - 2),
  438. yc = WITHIN(y, 1, (GRID_MAX_POINTS_Y) - 2);
  439. n_edit_pts = yc ? (xc ? 9 : 6) : (xc ? 6 : 4); // Corners
  440. #endif
  441. // Refresh is also set by encoder movement
  442. //if (int32_t(ui.encoderPosition) != grid_index(x, y))
  443. // ui.refresh(LCDVIEW_CALL_REDRAW_NEXT);
  444. }
  445. // Draw the grid point based on the encoder
  446. x = ui.encoderPosition % (GRID_MAX_POINTS_X);
  447. y = ui.encoderPosition / (GRID_MAX_POINTS_X);
  448. if (ui.should_draw()) ui.ubl_plot(x, y);
  449. // Add a move if needed to match the grid point
  450. if (x != x_plot || y != y_plot) {
  451. x_plot = x; y_plot = y; // The move is always posted, so update the grid point now
  452. ubl_map_move_to_xy(); // Sets up a "manual move"
  453. ui.refresh(LCDVIEW_CALL_REDRAW_NEXT); // Clean up a half drawn box
  454. }
  455. }
  456. /**
  457. * UBL LCD "radar" map homing
  458. */
  459. void _ubl_map_screen_homing() {
  460. ui.defer_status_screen();
  461. _lcd_draw_homing();
  462. if (all_axes_homed()) {
  463. ubl.lcd_map_control = true; // Return to the map screen after editing Z
  464. ui.goto_screen(ubl_map_screen, grid_index(x_plot, y_plot)); // Pre-set the encoder value
  465. ui.manual_move.menu_scale = 0; // Immediate move
  466. ubl_map_move_to_xy(); // Move to current mesh point
  467. ui.manual_move.menu_scale = 1; // Delayed moves
  468. }
  469. }
  470. /**
  471. * UBL Homing before LCD map
  472. */
  473. void _ubl_goto_map_screen() {
  474. if (planner.movesplanned()) return; // The ACTION_ITEM will do nothing
  475. if (!all_axes_trusted()) {
  476. set_all_unhomed();
  477. queue.inject_P(G28_STR);
  478. }
  479. ui.goto_screen(_ubl_map_screen_homing); // Go to the "Homing" screen
  480. }
  481. /**
  482. * UBL Output map submenu
  483. *
  484. * << Unified Bed Leveling
  485. * Output for Host
  486. * Output for CSV
  487. * Off Printer Backup
  488. */
  489. void _lcd_ubl_output_map() {
  490. START_MENU();
  491. BACK_ITEM(MSG_UBL_LEVEL_BED);
  492. GCODES_ITEM(MSG_UBL_OUTPUT_MAP_HOST, PSTR("G29T0"));
  493. GCODES_ITEM(MSG_UBL_OUTPUT_MAP_CSV, PSTR("G29T1"));
  494. GCODES_ITEM(MSG_UBL_OUTPUT_MAP_BACKUP, PSTR("G29S-1"));
  495. END_MENU();
  496. }
  497. /**
  498. * UBL Tools submenu
  499. *
  500. * << Unified Bed Leveling
  501. * - Build Mesh >>
  502. * - Validate Mesh >>
  503. * - Edit Mesh >>
  504. * - Mesh Leveling >>
  505. */
  506. void _menu_ubl_tools() {
  507. START_MENU();
  508. BACK_ITEM(MSG_UBL_LEVEL_BED);
  509. SUBMENU(MSG_UBL_BUILD_MESH_MENU, _lcd_ubl_build_mesh);
  510. GCODES_ITEM(MSG_UBL_MANUAL_MESH, PSTR("G29I999\nG29P2BT0"));
  511. #if ENABLED(G26_MESH_VALIDATION)
  512. SUBMENU(MSG_UBL_VALIDATE_MESH_MENU, _lcd_ubl_validate_mesh);
  513. #endif
  514. SUBMENU(MSG_EDIT_MESH, _lcd_ubl_edit_mesh);
  515. SUBMENU(MSG_UBL_MESH_LEVELING, _lcd_ubl_mesh_leveling);
  516. END_MENU();
  517. }
  518. #if ENABLED(G26_MESH_VALIDATION)
  519. /**
  520. * UBL Step-By-Step submenu
  521. *
  522. * << Unified Bed Leveling
  523. * 1 Build Cold Mesh
  524. * 2 Smart Fill-in
  525. * - 3 Validate Mesh >>
  526. * 4 Fine Tune All
  527. * - 5 Validate Mesh >>
  528. * 6 Fine Tune All
  529. * 7 Save Bed Mesh
  530. */
  531. void _lcd_ubl_step_by_step() {
  532. START_MENU();
  533. BACK_ITEM(MSG_UBL_LEVEL_BED);
  534. GCODES_ITEM(MSG_UBL_1_BUILD_COLD_MESH, PSTR("G29NP1"));
  535. GCODES_ITEM(MSG_UBL_2_SMART_FILLIN, PSTR("G29P3T0"));
  536. SUBMENU(MSG_UBL_3_VALIDATE_MESH_MENU, _lcd_ubl_validate_mesh);
  537. GCODES_ITEM(MSG_UBL_4_FINE_TUNE_ALL, PSTR("G29P4RT"));
  538. SUBMENU(MSG_UBL_5_VALIDATE_MESH_MENU, _lcd_ubl_validate_mesh);
  539. GCODES_ITEM(MSG_UBL_6_FINE_TUNE_ALL, PSTR("G29P4RT"));
  540. ACTION_ITEM(MSG_UBL_7_SAVE_MESH, _lcd_ubl_save_mesh_cmd);
  541. END_MENU();
  542. }
  543. #endif
  544. #if ENABLED(UBL_MESH_WIZARD)
  545. /**
  546. * UBL Mesh Wizard - One-click mesh creation with or without a probe
  547. */
  548. void _lcd_ubl_mesh_wizard() {
  549. char ubl_lcd_gcode[30];
  550. #if HAS_HEATED_BED && HAS_HOTEND
  551. sprintf_P(ubl_lcd_gcode, PSTR("M1004B%iH%iS%i"), custom_bed_temp, custom_hotend_temp, ubl_storage_slot);
  552. #elif HAS_HOTEND
  553. sprintf_P(ubl_lcd_gcode, PSTR("M1004H%iS%i"), custom_hotend_temp, ubl_storage_slot);
  554. #else
  555. sprintf_P(ubl_lcd_gcode, PSTR("M1004S%i"), ubl_storage_slot);
  556. #endif
  557. queue.inject(ubl_lcd_gcode);
  558. ui.return_to_status();
  559. }
  560. void _menu_ubl_mesh_wizard() {
  561. const int16_t total_slots = settings.calc_num_meshes();
  562. START_MENU();
  563. BACK_ITEM(MSG_UBL_LEVEL_BED);
  564. #if HAS_HOTEND
  565. EDIT_ITEM(int3, MSG_UBL_HOTEND_TEMP_CUSTOM, &custom_hotend_temp, HEATER_0_MINTEMP + 20, thermalManager.hotend_max_target(0));
  566. #endif
  567. #if HAS_HEATED_BED
  568. EDIT_ITEM(int3, MSG_UBL_BED_TEMP_CUSTOM, &custom_bed_temp, BED_MINTEMP + 20, BED_MAX_TARGET);
  569. #endif
  570. EDIT_ITEM(int3, MSG_UBL_STORAGE_SLOT, &ubl_storage_slot, 0, total_slots);
  571. ACTION_ITEM(MSG_UBL_MESH_WIZARD, _lcd_ubl_mesh_wizard);
  572. #if ENABLED(G26_MESH_VALIDATION)
  573. SUBMENU(MSG_UBL_VALIDATE_MESH_MENU, _lcd_ubl_validate_mesh);
  574. #endif
  575. ACTION_ITEM(MSG_INFO_SCREEN, ui.return_to_status);
  576. END_MENU();
  577. }
  578. #endif
  579. /**
  580. * UBL System submenu
  581. *
  582. * << Motion
  583. * - Manually Build Mesh >>
  584. * - Activate UBL >>
  585. * - Deactivate UBL >>
  586. * - Step-By-Step UBL >>
  587. * - Mesh Storage >>
  588. * - Output Map >>
  589. * - UBL Tools >>
  590. * - Output UBL Info >>
  591. */
  592. void _lcd_ubl_level_bed() {
  593. START_MENU();
  594. BACK_ITEM(MSG_MOTION);
  595. if (planner.leveling_active)
  596. GCODES_ITEM(MSG_UBL_DEACTIVATE_MESH, PSTR("G29D"));
  597. else
  598. GCODES_ITEM(MSG_UBL_ACTIVATE_MESH, PSTR("G29A"));
  599. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  600. editable.decimal = planner.z_fade_height;
  601. EDIT_ITEM_FAST(float3, MSG_Z_FADE_HEIGHT, &editable.decimal, 0, 100, []{ set_z_fade_height(editable.decimal); });
  602. #endif
  603. #if ENABLED(G26_MESH_VALIDATION)
  604. SUBMENU(MSG_UBL_STEP_BY_STEP_MENU, _lcd_ubl_step_by_step);
  605. #endif
  606. #if ENABLED(UBL_MESH_WIZARD)
  607. SUBMENU(MSG_UBL_MESH_WIZARD, _menu_ubl_mesh_wizard);
  608. #endif
  609. ACTION_ITEM(MSG_UBL_MESH_EDIT, _ubl_goto_map_screen);
  610. SUBMENU(MSG_UBL_STORAGE_MESH_MENU, _lcd_ubl_storage_mesh);
  611. SUBMENU(MSG_UBL_OUTPUT_MAP, _lcd_ubl_output_map);
  612. SUBMENU(MSG_UBL_TOOLS, _menu_ubl_tools);
  613. GCODES_ITEM(MSG_UBL_INFO_UBL, PSTR("G29W"));
  614. END_MENU();
  615. }
  616. #endif // HAS_MARLINUI_MENU && AUTO_BED_LEVELING_UBL