diff --git a/Marlin/src/feature/bedlevel/abl/bbl.cpp b/Marlin/src/feature/bedlevel/abl/bbl.cpp index b0a84c5e356d..28f8dd09093c 100644 --- a/Marlin/src/feature/bedlevel/abl/bbl.cpp +++ b/Marlin/src/feature/bedlevel/abl/bbl.cpp @@ -125,20 +125,20 @@ void LevelingBilinear::set_grid(const xy_pos_t &_grid_spacing, const xy_pos_t &_ */ void LevelingBilinear::extrapolate_unprobed_bed_level() { #ifdef HALF_IN_X - constexpr uint8_t ctrx2 = 0, xend = nr_grid_points.x - 1; + constexpr uint8_t ctrx2 = 0, xend = TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) - 1; #else IF_DISABLED(VARIABLE_GRID_POINTS, constexpr) uint8_t - ctrx1 = (nr_grid_points.x - 1) / 2, // left-of-center - ctrx2 = nr_grid_points.x / 2, // right-of-center + ctrx1 = (TERN(VARIABLE_GRID_POINTS, (nr_grid_points.x - 1), GRID_MAX_CELLS_X)) / 2, // left-of-center + ctrx2 = TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) / 2, // right-of-center xend = ctrx1; #endif #ifdef HALF_IN_Y - constexpr uint8_t ctry2 = 0, yend = nr_grid_points.y - 1; + constexpr uint8_t ctry2 = 0, yend = TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y) - 1; #else IF_DISABLED(VARIABLE_GRID_POINTS, constexpr) uint8_t - ctry1 = (nr_grid_points.y - 1) / 2, // top-of-center - ctry2 = nr_grid_points.y / 2, // bottom-of-center + ctry1 = (TERN(VARIABLE_GRID_POINTS, (nr_grid_points.y - 1), GRID_MAX_CELLS_Y)) / 2, // top-of-center + ctry2 = TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y) / 2, // bottom-of-center yend = ctry1; #endif @@ -165,26 +165,43 @@ void LevelingBilinear::extrapolate_unprobed_bed_level() { void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values/*=nullptr*/ OPTARG(VARIABLE_GRID_POINTS, const xy_uint8_t *_grid_points/*=nullptr*/)) { // Print the passed mesh grid(s) or the current mesh SERIAL_ECHOLNPGM("Bilinear Leveling Grid:"); - PRINT_2D_ARRAY( - GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 3, _z_values ? *_z_values[0] : z_values[0], - _grid_points ? _grid_points->x : nr_grid_points.x, - _grid_points ? _grid_points->y : nr_grid_points.y - ); + #if DISABLED(VARIABLE_GRID_POINTS) + print_2d_array(GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 3, _z_values ? *_z_values[0] : z_values[0]); + #endif + #if ENABLED(VARIABLE_GRID_POINTS) + PRINT_2D_ARRAY( + GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y, 3, _z_values ? *_z_values[0] : z_values[0], + _grid_points ? _grid_points->x : nr_grid_points.x, + _grid_points ? _grid_points->y : nr_grid_points.y + ); + #endif #if ENABLED(ABL_BILINEAR_SUBDIVISION) if (!_z_values) { SERIAL_ECHOLNPGM("Subdivided with CATMULL ROM Leveling Grid:"); - PRINT_2D_ARRAY( - ABL_MAX_POINTS_VIRT_X, ABL_MAX_POINTS_VIRT_Y, 5, z_values_virt[0], - nr_grid_points_virt.x, nr_grid_points_virt.y - ); + #if DISABLED(VARIABLE_GRID_POINTS) + print_2d_array(ABL_GRID_POINTS_VIRT_X, ABL_GRID_POINTS_VIRT_Y, 5, z_values_virt[0]); + #endif + #if ENABLED(VARIABLE_GRID_POINTS) + PRINT_2D_ARRAY( + ABL_MAX_POINTS_VIRT_X, ABL_MAX_POINTS_VIRT_Y, 5, z_values_virt[0], + nr_grid_points_virt.x, nr_grid_points_virt.y + ); + #endif } #endif } #if ENABLED(ABL_BILINEAR_SUBDIVISION) - - float LevelingBilinear::z_values_virt[ABL_MAX_POINTS_VIRT_X][ABL_MAX_POINTS_VIRT_Y]; + + #if DISABLED(VARIABLE_GRID_POINTS) + #define ABL_TEMP_POINTS_X (GRID_MAX_POINTS_X + 2) + #define ABL_TEMP_POINTS_Y (GRID_MAX_POINTS_Y + 2) + float LevelingBilinear::z_values_virt[ABL_GRID_POINTS_VIRT_X][ABL_GRID_POINTS_VIRT_Y]; + #endif + #if ENABLED(VARIABLE_GRID_POINTS) + float LevelingBilinear::z_values_virt[ABL_MAX_POINTS_VIRT_X][ABL_MAX_POINTS_VIRT_Y]; + #endif xy_pos_t LevelingBilinear::grid_spacing_virt; xy_float_t LevelingBilinear::grid_factor_virt; #if ENABLED(VARIABLE_GRID_POINTS) @@ -194,7 +211,7 @@ void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values/*=nullptr #define LINEAR_EXTRAPOLATION(E, I) ((E) * 2 - (I)) float LevelingBilinear::virt_coord(const uint8_t x, const uint8_t y) { uint8_t ep = 0, ip = 1; - if (x > nr_grid_points.x + 1 || y > nr_grid_points.y + 1) { + if (x > (TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) + 1) || y > (TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y) + 1)) { // The requested point requires extrapolating two points beyond the mesh. // These values are only requested for the edges of the mesh, which are always an actual mesh point, // and do not require interpolation. When interpolation is not needed, this "Mesh + 2" point is @@ -202,12 +219,12 @@ void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values/*=nullptr // making this function more complex by extrapolating two points. return 0.0; } - if (!x || x == (nr_grid_points.x + 2) - 1) { + if (!x || x == TERN(VARIABLE_GRID_POINTS, (nr_grid_points.x + 2), ABL_TEMP_POINTS_X) - 1) { if (x) { - ep = nr_grid_points.x - 1; - ip = (nr_grid_points.x - 1) - 1; + ep = TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) - 1; + ip = (TERN(VARIABLE_GRID_POINTS, (nr_grid_points.x - 1), GRID_MAX_CELLS_X)) - 1; } - if (WITHIN(y, 1, (nr_grid_points.y + 2) - 2)) + if (WITHIN(y, 1, TERN(VARIABLE_GRID_POINTS, (nr_grid_points.y + 2), ABL_TEMP_POINTS_Y) - 2)) return LINEAR_EXTRAPOLATION( z_values[ep][y - 1], z_values[ip][y - 1] @@ -218,12 +235,12 @@ void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values/*=nullptr virt_coord(ip + 1, y) ); } - if (!y || y == (nr_grid_points.y + 2) - 1) { + if (!y || y == TERN(VARIABLE_GRID_POINTS, (nr_grid_points.y + 2), ABL_TEMP_POINTS_Y) - 1) { if (y) { - ep = nr_grid_points.y - 1; - ip = (nr_grid_points.y - 1) - 1; + ep = TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y) - 1; + ip = (TERN(VARIABLE_GRID_POINTS, (nr_grid_points.y - 1), GRID_MAX_CELLS_Y)) - 1; } - if (WITHIN(x, 1, (nr_grid_points.x + 2) - 2)) + if (WITHIN(x, 1, TERN(VARIABLE_GRID_POINTS, (nr_grid_points.x + 2), ABL_TEMP_POINTS_X) - 2)) return LINEAR_EXTRAPOLATION( z_values[x - 1][ep], z_values[x - 1][ip] @@ -263,11 +280,11 @@ void LevelingBilinear::print_leveling_grid(const bed_mesh_t* _z_values/*=nullptr #if ENABLED(VARIABLE_GRID_POINTS) nr_grid_points_virt.set((nr_grid_points.x - 1) * (uint16_t)(BILINEAR_SUBDIVISIONS) + (uint16_t)1, (nr_grid_points.y - 1) * (uint16_t)(BILINEAR_SUBDIVISIONS) + (uint16_t)1); #endif - for (uint8_t y = 0; y < nr_grid_points.y; ++y) - for (uint8_t x = 0; x < nr_grid_points.x; ++x) + for (uint8_t y = 0; y < TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y); ++y) + for (uint8_t x = 0; x < TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X); ++x) for (uint8_t ty = 0; ty < BILINEAR_SUBDIVISIONS; ++ty) for (uint8_t tx = 0; tx < BILINEAR_SUBDIVISIONS; ++tx) { - if ((ty && y == nr_grid_points.y - 1) || (tx && x == nr_grid_points.x - 1)) + if ((ty && y == (TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, (GRID_MAX_POINTS_Y))) - 1) || (tx && x == (TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, (GRID_MAX_POINTS_X))) - 1)) continue; z_values_virt[x * (BILINEAR_SUBDIVISIONS) + tx][y * (BILINEAR_SUBDIVISIONS) + ty] = virt_2cmr(x + 1, y + 1, (float)tx / (BILINEAR_SUBDIVISIONS), (float)ty / (BILINEAR_SUBDIVISIONS)); @@ -290,14 +307,14 @@ void LevelingBilinear::refresh_bed_level() { #if ENABLED(ABL_BILINEAR_SUBDIVISION) #define ABL_BG_SPACING(A) grid_spacing_virt.A #define ABL_BG_FACTOR(A) grid_factor_virt.A - #define ABL_BG_POINTS_X nr_grid_points_virt.x - #define ABL_BG_POINTS_Y nr_grid_points_virt.y + #define ABL_BG_POINTS_X TERN(VARIABLE_GRID_POINTS, nr_grid_points_virt.x, ABL_GRID_POINTS_VIRT_X) + #define ABL_BG_POINTS_Y TERN(VARIABLE_GRID_POINTS, nr_grid_points_virt.y, ABL_GRID_POINTS_VIRT_Y) #define ABL_BG_GRID(X,Y) z_values_virt[X][Y] #else #define ABL_BG_SPACING(A) grid_spacing.A #define ABL_BG_FACTOR(A) grid_factor.A - #define ABL_BG_POINTS_X nr_grid_points.x - #define ABL_BG_POINTS_Y nr_grid_points.y + #define ABL_BG_POINTS_X TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) + #define ABL_BG_POINTS_Y TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y) #define ABL_BG_GRID(X,Y) z_values[X][Y] #endif diff --git a/Marlin/src/feature/bedlevel/abl/bbl.h b/Marlin/src/feature/bedlevel/abl/bbl.h index 0d1ffd6e9eea..ddca13c9e191 100644 --- a/Marlin/src/feature/bedlevel/abl/bbl.h +++ b/Marlin/src/feature/bedlevel/abl/bbl.h @@ -46,10 +46,17 @@ class LevelingBilinear { static void extrapolate_one_point(const uint8_t x, const uint8_t y, const int8_t xdir, const int8_t ydir); #if ENABLED(ABL_BILINEAR_SUBDIVISION) - #define ABL_MAX_POINTS_VIRT_X (GRID_MAX_CELLS_X * (BILINEAR_SUBDIVISIONS) + 1) - #define ABL_MAX_POINTS_VIRT_Y (GRID_MAX_CELLS_Y * (BILINEAR_SUBDIVISIONS) + 1) + #if DISABLED(VARIABLE_GRID_POINTS) + #define ABL_GRID_POINTS_VIRT_X (GRID_MAX_CELLS_X * (BILINEAR_SUBDIVISIONS) + 1) + #define ABL_GRID_POINTS_VIRT_Y (GRID_MAX_CELLS_Y * (BILINEAR_SUBDIVISIONS) + 1) + static float z_values_virt[ABL_GRID_POINTS_VIRT_X][ABL_GRID_POINTS_VIRT_Y]; + #endif + #if ENABLED(VARIABLE_GRID_POINTS) + #define ABL_MAX_POINTS_VIRT_X (GRID_MAX_CELLS_X * (BILINEAR_SUBDIVISIONS) + 1) + #define ABL_MAX_POINTS_VIRT_Y (GRID_MAX_CELLS_Y * (BILINEAR_SUBDIVISIONS) + 1) + static float z_values_virt[ABL_MAX_POINTS_VIRT_X][ABL_MAX_POINTS_VIRT_Y]; + #endif - static float z_values_virt[ABL_MAX_POINTS_VIRT_X][ABL_MAX_POINTS_VIRT_Y]; static xy_pos_t grid_spacing_virt; static xy_float_t grid_factor_virt; #if ENABLED(VARIABLE_GRID_POINTS) diff --git a/Marlin/src/feature/bedlevel/bedlevel.cpp b/Marlin/src/feature/bedlevel/bedlevel.cpp index 661d28c9d5f4..f41fd8a29efe 100644 --- a/Marlin/src/feature/bedlevel/bedlevel.cpp +++ b/Marlin/src/feature/bedlevel/bedlevel.cpp @@ -135,21 +135,10 @@ void reset_bed_level() { /** * Print calibration results for plotting or manual frame adjustment. */ - void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const float *values - #if ENABLED(VARIABLE_GRID_POINTS) - , uint8_t print_x/*=0*/, uint8_t print_y/*=0*/ - #endif - ) { - - #if ENABLED(VARIABLE_GRID_POINTS) - if (!print_x) print_x = sx; - if (!print_y) print_y = sy; - #else - const uint8_t print_x = sx, print_y = sy; - #endif + void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const float *values OPTARG(VARIABLE_GRID_POINTS, uint8_t print_x/*=0*/, uint8_t print_y/*=0*/)) { #ifndef SCAD_MESH_OUTPUT - for (uint8_t x = 0; x < print_x; ++x) { + for (uint8_t x = 0; x < TERN(VARIABLE_GRID_POINTS, print_x, sx); ++x) { SERIAL_ECHO_SP(precision + (x < 10 ? 3 : 2)); SERIAL_ECHO(x); } @@ -158,14 +147,14 @@ void reset_bed_level() { #ifdef SCAD_MESH_OUTPUT SERIAL_ECHOLNPGM("measured_z = ["); // open 2D array #endif - for (uint8_t y = 0; y < print_y; ++y) { + for (uint8_t y = 0; y < TERN(VARIABLE_GRID_POINTS, print_y, sy); ++y) { #ifdef SCAD_MESH_OUTPUT SERIAL_ECHOPGM(" ["); // open sub-array #else if (y < 10) SERIAL_CHAR(' '); SERIAL_ECHO(y); #endif - for (uint8_t x = 0; x < print_x; ++x) { + for (uint8_t x = 0; x < TERN(VARIABLE_GRID_POINTS, print_x, sx); ++x) { SERIAL_CHAR(' '); const float offset = values[x * sy + y]; if (!isnan(offset)) { @@ -183,12 +172,12 @@ void reset_bed_level() { #endif } #ifdef SCAD_MESH_OUTPUT - if (x < print_x - 1) SERIAL_CHAR(','); + if (x < TERN(VARIABLE_GRID_POINTS, print_x, sx) - 1) SERIAL_CHAR(','); #endif } #ifdef SCAD_MESH_OUTPUT SERIAL_ECHOPGM(" ]"); // close sub-array - if (y < print_y - 1) SERIAL_CHAR(','); + if (y < TERN(VARIABLE_GRID_POINTS, print_y, sy) - 1) SERIAL_CHAR(','); #endif SERIAL_EOL(); } diff --git a/Marlin/src/feature/bedlevel/bedlevel.h b/Marlin/src/feature/bedlevel/bedlevel.h index ff847ff1145e..5ad9b0944014 100644 --- a/Marlin/src/feature/bedlevel/bedlevel.h +++ b/Marlin/src/feature/bedlevel/bedlevel.h @@ -78,11 +78,7 @@ class TemporaryBedLevelingState { /** * Print calibration results for plotting or manual frame adjustment. */ - void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const float *values - #if ENABLED(VARIABLE_GRID_POINTS) - , uint8_t print_x=0, uint8_t print_y=0 - #endif - ); + void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const float *values OPTARG(VARIABLE_GRID_POINTS, uint8_t print_x=0, uint8_t print_y=0)); #if ENABLED(VARIABLE_GRID_POINTS) #define PRINT_2D_ARRAY(X, Y, P, V, M...) print_2d_array(X, Y, P, V, M) diff --git a/Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.h b/Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.h index cb4f36cd59f2..78a3a237fe53 100644 --- a/Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.h +++ b/Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.h @@ -49,7 +49,7 @@ class mesh_bed_leveling { static void reset(); FORCE_INLINE static bool has_mesh() { - GRID_LOOP(x, y) if (z_values[x][y]) return true; + GRID_LOOP_COND(x, y) if (z_values[x][y]) return true; return false; } diff --git a/Marlin/src/feature/bedlevel/ubl/ubl.cpp b/Marlin/src/feature/bedlevel/ubl/ubl.cpp index ec5feda54563..ed91e8c3d091 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl.cpp @@ -84,7 +84,7 @@ float unified_bed_leveling::z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y]; #endif #if ENABLED(VARIABLE_GRID_POINTS) - xy_uint8_t unified_bed_leveling::grid_points; + xy_uint8_t unified_bed_leveling::nr_grid_points; xy_float_t unified_bed_leveling::mesh_dist, // Initialized by settings.load unified_bed_leveling::mesh_dist_recip; @@ -114,7 +114,9 @@ void unified_bed_leveling::reset() { set_bed_leveling_enabled(false); storage_slot = -1; ZERO(z_values); - set_grid_points(xy_uint8_t({ GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y })); + #if ENABLED(VARIABLE_GRID_POINTS) + set_nr_grid_points(xy_uint8_t({ GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y })); + #endif #if ENABLED(EXTENSIBLE_UI) GRID_LOOP(x, y) ExtUI::onMeshUpdate(x, y, 0); #endif @@ -170,7 +172,7 @@ static void serial_echo_xy(const uint8_t sp, const int16_t x, const int16_t y) { static void serial_echo_column_labels(const uint8_t sp) { SERIAL_ECHO_SP(7); - for (uint8_t i = 0; i < GRID_USED_POINTS_X; ++i) { + for (uint8_t i = 0; i < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X); ++i) { if (i < 10) SERIAL_CHAR(' '); SERIAL_ECHO(i); SERIAL_ECHO_SP(sp); diff --git a/Marlin/src/feature/bedlevel/ubl/ubl.h b/Marlin/src/feature/bedlevel/ubl/ubl.h index 0c7ff06e324a..0c500718a5e4 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl.h +++ b/Marlin/src/feature/bedlevel/ubl/ubl.h @@ -62,8 +62,9 @@ typedef struct { #endif } G29_parameters_t; -class unified_bed_leveling; -extern unified_bed_leveling bedlevel; +#if ENABLED(VARIABLE_GRID_POINTS) + class unified_bed_leveling; +#endif class unified_bed_leveling { private: @@ -117,10 +118,10 @@ class unified_bed_leveling { static bed_mesh_t z_values; #if ENABLED(VARIABLE_GRID_POINTS) - static xy_uint8_t grid_points; + static xy_uint8_t nr_grid_points; static xy_float_t mesh_dist, mesh_dist_recip; static void refresh_mesh_dist(); - static void set_grid_points(const xy_uint8_t &gp) { grid_points = gp; refresh_mesh_dist(); } + static void set_nr_grid_points(const xy_uint8_t &gp) { nr_grid_points = gp; refresh_mesh_dist(); } #endif #if ENABLED(OPTIMIZED_MESH_STORAGE) @@ -128,6 +129,11 @@ class unified_bed_leveling { static void set_mesh_from_store(const mesh_store_t &stored_values, bed_mesh_t &out_values); #endif + #if DISABLED(VARIABLE_GRID_POINTS) + static const float _mesh_index_to_xpos[GRID_MAX_POINTS_X], + _mesh_index_to_ypos[GRID_MAX_POINTS_Y]; + #endif + #if HAS_MARLINUI_MENU static bool lcd_map_control; static void steppers_were_disabled(); @@ -170,16 +176,16 @@ class unified_bed_leveling { } static xy_uint8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); } - static int8_t closest_x_index(const_float_t x OPTARG(VARIABLE_GRID_POINTS, const xy_uint8_t &_grid_points)) { + static int8_t closest_x_index(const_float_t x OPTARG(VARIABLE_GRID_POINTS, const xy_uint8_t &_nr_grid_points)) { const int8_t px = (x - (MESH_MIN_X) + (TERN(VARIABLE_GRID_POINTS, mesh_dist.x, MESH_X_DIST)) * 0.5f) * TERN(VARIABLE_GRID_POINTS, mesh_dist_recip.x, RECIPROCAL(MESH_X_DIST)); return WITHIN(px, 0, TERN(VARIABLE_GRID_POINTS, GRID_USED_CELLS_X, GRID_MAX_POINTS_X)) ? px : -1; } - static int8_t closest_y_index(const_float_t y OPTARG(VARIABLE_GRID_POINTS, const xy_uint8_t &_grid_points)) { + static int8_t closest_y_index(const_float_t y OPTARG(VARIABLE_GRID_POINTS, const xy_uint8_t &_nr_grid_points)) { const int8_t py = (y - (MESH_MIN_Y) + (TERN(VARIABLE_GRID_POINTS, mesh_dist.y, MESH_Y_DIST)) * 0.5f) * TERN(VARIABLE_GRID_POINTS, mesh_dist_recip.y, RECIPROCAL(MESH_Y_DIST)); return WITHIN(py, 0, TERN(VARIABLE_GRID_POINTS, GRID_USED_CELLS_Y, GRID_MAX_POINTS_Y)) ? py : -1; } static xy_int8_t closest_indexes(const xy_pos_t &xy) { - return { closest_x_index(xy.x OPTARG(VARIABLE_GRID_POINTS, grid_points)), closest_y_index(xy.y OPTARG(VARIABLE_GRID_POINTS, grid_points)) }; + return { closest_x_index(xy.x OPTARG(VARIABLE_GRID_POINTS, nr_grid_points)), closest_y_index(xy.y OPTARG(VARIABLE_GRID_POINTS, nr_grid_points)) }; } /** @@ -212,10 +218,10 @@ class unified_bed_leveling { * the case where the printer is making a vertical line that only crosses horizontal mesh lines. */ static float z_correction_for_x_on_horizontal_mesh_line(const_float_t rx0, const uint8_t x1_i, const int yi) { - if (!WITHIN(x1_i, 0, (TERN(VARIABLE_GRID_POINTS, grid_points.x, GRID_MAX_POINTS_X)) - 1) || !WITHIN(yi, 0, (TERN(VARIABLE_GRID_POINTS, grid_points.y, GRID_MAX_POINTS_Y)) - 1)) { + if (!WITHIN(x1_i, 0, (TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X)) - 1) || !WITHIN(yi, 0, (TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y)) - 1)) { if (DEBUGGING(LEVELING)) { - if (WITHIN(x1_i, 0, TERN(VARIABLE_GRID_POINTS, grid_points.x, GRID_MAX_POINTS_X) - 1)) DEBUG_ECHOPGM("yi"); else DEBUG_ECHOPGM("x1_i"); + if (WITHIN(x1_i, 0, TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) - 1)) DEBUG_ECHOPGM("yi"); else DEBUG_ECHOPGM("x1_i"); DEBUG_ECHOLNPGM(" out of bounds in z_correction_for_x_on_horizontal_mesh_line(rx0=", rx0, ",x1_i=", x1_i, ",yi=", yi, ")"); } @@ -226,7 +232,7 @@ class unified_bed_leveling { const float xratio = (rx0 - get_mesh_x(x1_i)) * TERN(VARIABLE_GRID_POINTS, mesh_dist_recip.x, RECIPROCAL(MESH_X_DIST)); const float z1 = z_values[x1_i][yi]; - return z1 + xratio * (z_values[_MIN(x1_i, TERN(VARIABLE_GRID_POINTS, grid_points.x, GRID_MAX_POINTS) - 2) + 1][yi] - z1); // Don't allow x1_i+1 to be past the end of the array + return z1 + xratio * (z_values[_MIN(x1_i, TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS) - 2) + 1][yi] - z1); // Don't allow x1_i+1 to be past the end of the array // If it is, it is clamped to the last element of the // z_values[][] array and no correction is applied. } @@ -235,10 +241,10 @@ class unified_bed_leveling { // See comments above for z_correction_for_x_on_horizontal_mesh_line // static float z_correction_for_y_on_vertical_mesh_line(const_float_t ry0, const int xi, const int y1_i) { - if (!WITHIN(xi, 0, (TERN(VARIABLE_GRID_POINTS, grid_points.x, GRID_MAX_POINTS_X)) - 1) || !WITHIN(y1_i, 0, (TERN(VARIABLE_GRID_POINTS, grid_points.y, GRID_MAX_POINTS_Y)) - 1)) { + if (!WITHIN(xi, 0, (TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X)) - 1) || !WITHIN(y1_i, 0, (TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y)) - 1)) { if (DEBUGGING(LEVELING)) { - if (WITHIN(xi, 0, (TERN(VARIABLE_GRID_POINTS, grid_points.x, GRID_MAX_POINTS_X)) - 1)) DEBUG_ECHOPGM("y1_i"); else DEBUG_ECHOPGM("xi"); + if (WITHIN(xi, 0, (TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X)) - 1)) DEBUG_ECHOPGM("y1_i"); else DEBUG_ECHOPGM("xi"); DEBUG_ECHOLNPGM(" out of bounds in z_correction_for_y_on_vertical_mesh_line(ry0=", ry0, ", xi=", xi, ", y1_i=", y1_i, ")"); } @@ -249,7 +255,7 @@ class unified_bed_leveling { const float yratio = (ry0 - get_mesh_y(y1_i)) * TERN(VARIABLE_GRID_POINTS, mesh_dist_recip.y, RECIPROCAL(MESH_Y_DIST)); const float z1 = z_values[xi][y1_i]; - return z1 + yratio * (z_values[xi][_MIN(y1_i, TERN(VARIABLE_GRID_POINTS, grid_points.y, GRID_MAX_POINTS) - 2) + 1] - z1); // Don't allow y1_i+1 to be past the end of the array + return z1 + yratio * (z_values[xi][_MIN(y1_i, TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS) - 2) + 1] - z1); // Don't allow y1_i+1 to be past the end of the array // If it is, it is clamped to the last element of the // z_values[][] array and no correction is applied. } @@ -272,7 +278,7 @@ class unified_bed_leveling { return UBL_Z_RAISE_WHEN_OFF_MESH; #endif - IF_DISABLED(VARIABLE_GRID_POINTS, const) uint8_t mx = _MIN(cx, TERN(VARIABLE_GRID_POINTS, grid_points.x, GRID_MAX_POINTS_X) - 2) + 1, my = _MIN(cy, TERN(VARIABLE_GRID_POINTS, grid_points.y, GRID_MAX_POINTS_Y) - 2) + 1; + IF_DISABLED(VARIABLE_GRID_POINTS, const) uint8_t mx = _MIN(cx, TERN(VARIABLE_GRID_POINTS, nr_grid_points.x, GRID_MAX_POINTS_X) - 2) + 1, my = _MIN(cy, TERN(VARIABLE_GRID_POINTS, nr_grid_points.y, GRID_MAX_POINTS_Y) - 2) + 1; IF_DISABLED(VARIABLE_GRID_POINTS, const) float x0 = get_mesh_x(cx), x1 = get_mesh_x(cx + 1), z1 = calc_z0(rx0, x0, z_values[cx][cy], x1, z_values[mx][cy]), z2 = calc_z0(rx0, x0, z_values[cx][my], x1, z_values[mx][my]); @@ -295,6 +301,15 @@ class unified_bed_leveling { static float get_z_correction(const xy_pos_t &pos) { return get_z_correction(pos.x, pos.y); } static constexpr float get_z_offset() { return 0.0f; } + + #if DISABLED(VARIABLE_GRID_POINTS) + static float get_mesh_x(const uint8_t i) { + return i < (GRID_MAX_POINTS_X) ? pgm_read_float(&_mesh_index_to_xpos[i]) : MESH_MIN_X + i * (MESH_X_DIST); + } + static float get_mesh_y(const uint8_t i) { + return i < (GRID_MAX_POINTS_Y) ? pgm_read_float(&_mesh_index_to_ypos[i]) : MESH_MIN_Y + i * (MESH_Y_DIST); + } + #endif #if ENABLED(VARIABLE_GRID_POINTS) static float get_mesh_x(const uint8_t i); static float get_mesh_y(const uint8_t i); @@ -313,6 +328,8 @@ class unified_bed_leveling { }; // class unified_bed_leveling +extern unified_bed_leveling bedlevel; + // Serial with delay shorthand #define UBL_SERIAL_ECHO(D, V...) do{ SERIAL_ECHO(V); serial_delay(D); }while(0) #define UBL_SERIAL_ECHOLN(D, V...) do{ SERIAL_ECHOLN(V); serial_delay(D); }while(0) diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp index 09ea4625327d..4ec2d45317ed 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp @@ -752,7 +752,7 @@ void unified_bed_leveling::adjust_mesh_to_mean(const bool cflag, const_float_t o * G29 P6 C : Shift Mesh Height by a uniform constant. */ void unified_bed_leveling::shift_mesh_height() { - GRID_LOOP(x, y) + GRID_LOOP_COND(x, y) if (!isnan(z_values[x][y])) { z_values[x][y] += param.C_constant; TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, z_values[x][y])); @@ -1329,7 +1329,7 @@ mesh_index_pair unified_bed_leveling::find_furthest_invalid_mesh_point() { farthest.pos = nearby; // Found an invalid location farther from the defined mesh point farthest.distance = d2; } - } // GRID_LOOP + } // GRID_LOOP_COND if (!found_a_real && found_a_NAN) { // if the mesh is totally unpopulated, start the probing farthest.pos.set(TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) / 2, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) / 2); @@ -1419,7 +1419,7 @@ mesh_index_pair unified_bed_leveling::find_closest_mesh_point_of_type(const Mesh closest.distance = best_so_far; } } - } // GRID_LOOP + } // GRID_LOOP_COND return closest; diff --git a/Marlin/src/gcode/bedlevel/G42.cpp b/Marlin/src/gcode/bedlevel/G42.cpp index 44f5ceada884..d1fc64380cc7 100644 --- a/Marlin/src/gcode/bedlevel/G42.cpp +++ b/Marlin/src/gcode/bedlevel/G42.cpp @@ -50,7 +50,7 @@ void GcodeSuite::G42() { const bool hasJ = parser.seenval('J'); const int8_t iy = hasJ ? parser.value_int() : 0; - if ((hasI && !WITHIN(ix, 0, GRID_MAX_POINTS_X - 1)) || (hasJ && !WITHIN(iy, 0, GRID_MAX_POINTS_Y - 1))) { + if ((hasI && !WITHIN(ix, 0, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1)) || (hasJ && !WITHIN(iy, 0, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1))) { SERIAL_ECHOLNPGM(STR_ERR_MESH_XY); return; } diff --git a/Marlin/src/gcode/bedlevel/M420.cpp b/Marlin/src/gcode/bedlevel/M420.cpp index 345926ce039d..f404cd359979 100644 --- a/Marlin/src/gcode/bedlevel/M420.cpp +++ b/Marlin/src/gcode/bedlevel/M420.cpp @@ -74,14 +74,14 @@ void GcodeSuite::M420() { start.set(x_min, y_min); spacing.set((x_max - x_min) / (GRID_MAX_CELLS_X), (y_max - y_min) / (GRID_MAX_CELLS_Y)); - bedlevel.set_grid(spacing, start); + bedlevel.set_grid(spacing, start OPTARG(VARIABLE_GRID_POINTS, bedlevel.nr_grid_points)); #endif - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { bedlevel.z_values[x][y] = 0.001 * random(-200, 200); TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, bedlevel.z_values[x][y])); } TERN_(AUTO_BED_LEVELING_BILINEAR, bedlevel.refresh_bed_level()); - SERIAL_ECHOPGM("Simulated " STRINGIFY(GRID_MAX_POINTS_X) "x" STRINGIFY(GRID_MAX_POINTS_Y) " mesh "); + SERIAL_ECHOPGM("Simulated " STRINGIFY(TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) "x" STRINGIFY(TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) " mesh "); SERIAL_ECHOPGM(" (", x_min); SERIAL_CHAR(','); SERIAL_ECHO(y_min); SERIAL_ECHOPGM(")-(", x_max); @@ -159,7 +159,7 @@ void GcodeSuite::M420() { // Get the sum and average of all mesh values float mesh_sum = 0; GRID_LOOP_COND(x, y) mesh_sum += bedlevel.z_values[x][y]; - const float zmean = mesh_sum / float(GRID_USED_POINTS); + const float zmean = mesh_sum / TERN(VARIABLE_GRID_POINTS, float(GRID_USED_POINTS), float(GRID_MAX_POINTS)); #else // midrange @@ -204,7 +204,12 @@ void GcodeSuite::M420() { #else if (leveling_is_valid()) { #if ENABLED(AUTO_BED_LEVELING_BILINEAR) - bedlevel.print_leveling_grid(); + #if ENABLED(VARIABLE_GRID_POINTS) + bedlevel.set_grid(bedlevel.grid_spacing, bedlevel.grid_start, bedlevel.nr_grid_points); + bedlevel.print_leveling_grid(nullptr, &bedlevel.nr_grid_points); + #else + bedlevel.print_leveling_grid(); + #endif #elif ENABLED(MESH_BED_LEVELING) SERIAL_ECHOLNPGM("Mesh Bed Level data:"); bedlevel.report_mesh(); diff --git a/Marlin/src/gcode/bedlevel/abl/G29.cpp b/Marlin/src/gcode/bedlevel/abl/G29.cpp index 23faec234acb..d1107899bff5 100644 --- a/Marlin/src/gcode/bedlevel/abl/G29.cpp +++ b/Marlin/src/gcode/bedlevel/abl/G29.cpp @@ -332,13 +332,13 @@ G29_TYPE GcodeSuite::G29() { // Get nearest i / j from rx / ry i = (rx - bedlevel.grid_start.x) / bedlevel.grid_spacing.x + 0.5f; j = (ry - bedlevel.grid_start.y) / bedlevel.grid_spacing.y + 0.5f; - LIMIT(i, 0, bedlevel.nr_grid_points.x - 1); - LIMIT(j, 0, bedlevel.nr_grid_points.y - 1); + LIMIT(i, 0, (TERN(VARIABLE_GRID_POINTS, bedlevel.nr_grid_points.x, (GRID_MAX_POINTS_X))) - 1); + LIMIT(j, 0, (TERN(VARIABLE_GRID_POINTS, bedlevel.nr_grid_points.y, (GRID_MAX_POINTS_Y))) - 1); } #pragma GCC diagnostic pop - if (WITHIN(i, 0, bedlevel.nr_grid_points.x - 1) && WITHIN(j, 0, bedlevel.nr_grid_points.y - 1)) { + if (WITHIN(i, 0, (TERN(VARIABLE_GRID_POINTS, bedlevel.nr_grid_points.x, (GRID_MAX_POINTS_X))) - 1) && WITHIN(j, 0, (TERN(VARIABLE_GRID_POINTS, bedlevel.nr_grid_points.y, (GRID_MAX_POINTS_Y))) - 1)) { set_bed_leveling_enabled(false); bedlevel.z_values[i][j] = rz; bedlevel.refresh_bed_level(); diff --git a/Marlin/src/gcode/bedlevel/abl/M421.cpp b/Marlin/src/gcode/bedlevel/abl/M421.cpp index b4afe224b4ae..03e5040af4e3 100644 --- a/Marlin/src/gcode/bedlevel/abl/M421.cpp +++ b/Marlin/src/gcode/bedlevel/abl/M421.cpp @@ -52,10 +52,10 @@ void GcodeSuite::M421() { hasQ = !hasZ && parser.seenval('Q'); if (hasZ || hasQ) { - if (WITHIN(ix, -1, GRID_USED_POINTS_X - 1) && WITHIN(iy, -1, GRID_USED_POINTS_Y - 1)) { + if (WITHIN(ix, -1, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) && WITHIN(iy, -1, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1)) { const float zval = parser.value_linear_units(); - uint8_t sx = ix >= 0 ? ix : 0, ex = ix >= 0 ? ix : GRID_USED_POINTS_X - 1, - sy = iy >= 0 ? iy : 0, ey = iy >= 0 ? iy : GRID_USED_POINTS_Y - 1; + uint8_t sx = ix >= 0 ? ix : 0, ex = ix >= 0 ? ix : TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1; + uint8_t sy = iy >= 0 ? iy : 0, ey = iy >= 0 ? iy : TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1; for (uint8_t x = sx; x <= ex; ++x) { for (uint8_t y = sy; y <= ey; ++y) { bedlevel.z_values[x][y] = zval + (hasQ ? bedlevel.z_values[x][y] : 0); diff --git a/Marlin/src/gcode/bedlevel/ubl/M421.cpp b/Marlin/src/gcode/bedlevel/ubl/M421.cpp index 99ba3ce19b46..79b3c3b038a1 100644 --- a/Marlin/src/gcode/bedlevel/ubl/M421.cpp +++ b/Marlin/src/gcode/bedlevel/ubl/M421.cpp @@ -61,7 +61,7 @@ void GcodeSuite::M421() { SERIAL_ERROR_MSG(STR_ERR_M421_PARAMETERS); // Test for I J out of range - else if (!WITHIN(ij.x, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(ij.y, 0, GRID_MAX_POINTS_Y - 1)) + else if (!WITHIN(ij.x, 0, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) || !WITHIN(ij.y, 0, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1)) SERIAL_ERROR_MSG(STR_ERR_MESH_XY); else { float &zval = bedlevel.z_values[ij.x][ij.y]; // Altering this Mesh Point diff --git a/Marlin/src/inc/Conditionals-3-etc.h b/Marlin/src/inc/Conditionals-3-etc.h index 448f8ee1d8b7..80a8f7d1ea73 100644 --- a/Marlin/src/inc/Conditionals-3-etc.h +++ b/Marlin/src/inc/Conditionals-3-etc.h @@ -622,8 +622,8 @@ #ifdef GRID_MAX_POINTS_X #if ALL(AUTO_BED_LEVELING_UBL, VARIABLE_GRID_POINTS) - #define GRID_USED_POINTS_X bedlevel.grid_points.x - #define GRID_USED_POINTS_Y bedlevel.grid_points.y + #define GRID_USED_POINTS_X unified_bed_leveling::nr_grid_points.x + #define GRID_USED_POINTS_Y unified_bed_leveling::nr_grid_points.y #elif ALL(AUTO_BED_LEVELING_BILINEAR, VARIABLE_GRID_POINTS) #define GRID_USED_POINTS_X bedlevel.nr_grid_points.x #define GRID_USED_POINTS_Y bedlevel.nr_grid_points.y diff --git a/Marlin/src/lcd/HD44780/marlinui_HD44780.cpp b/Marlin/src/lcd/HD44780/marlinui_HD44780.cpp index a7507bd1e83f..7ccb3ee7cc36 100644 --- a/Marlin/src/lcd/HD44780/marlinui_HD44780.cpp +++ b/Marlin/src/lcd/HD44780/marlinui_HD44780.cpp @@ -1537,7 +1537,7 @@ void MarlinUI::draw_status_screen() { pixels_per_x_mesh_pnt, pixels_per_y_mesh_pnt, suppress_x_offset = 0, suppress_y_offset = 0; - const uint8_t y_plot_inv = (GRID_MAX_POINTS_Y) - 1 - y_plot; + const uint8_t y_plot_inv = (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) - 1 - y_plot; upper_left.column = 0; upper_left.row = 0; @@ -1549,8 +1549,8 @@ void MarlinUI::draw_status_screen() { x_map_pixels = (HD44780_CHAR_WIDTH) * (MESH_MAP_COLS) - 2; // Minus 2 because we are drawing a box around the map y_map_pixels = (HD44780_CHAR_HEIGHT) * (MESH_MAP_ROWS) - 2; - pixels_per_x_mesh_pnt = x_map_pixels / (GRID_MAX_POINTS_X); - pixels_per_y_mesh_pnt = y_map_pixels / (GRID_MAX_POINTS_Y); + pixels_per_x_mesh_pnt = x_map_pixels / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + pixels_per_y_mesh_pnt = y_map_pixels / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)); if (pixels_per_x_mesh_pnt >= HD44780_CHAR_WIDTH) { // There are only 2 custom characters available, so the X pixels_per_x_mesh_pnt = HD44780_CHAR_WIDTH; // Size of the mesh point needs to fit within them independent @@ -1562,11 +1562,11 @@ void MarlinUI::draw_status_screen() { suppress_y_offset = 1; // Of where the starting pixel is located. } - x_map_pixels = pixels_per_x_mesh_pnt * (GRID_MAX_POINTS_X); // Now we have the right number of pixels to make both - y_map_pixels = pixels_per_y_mesh_pnt * (GRID_MAX_POINTS_Y); // Directions fit nicely + x_map_pixels = pixels_per_x_mesh_pnt * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); // Now we have the right number of pixels to make both + y_map_pixels = pixels_per_y_mesh_pnt * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)); // Directions fit nicely - right_edge = pixels_per_x_mesh_pnt * (GRID_MAX_POINTS_X) + 1; // Find location of right edge within the character cell - bottom_line = pixels_per_y_mesh_pnt * (GRID_MAX_POINTS_Y) + 1; // Find location of bottom line within the character cell + right_edge = pixels_per_x_mesh_pnt * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) + 1; // Find location of right edge within the character cell + bottom_line = pixels_per_y_mesh_pnt * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) + 1; // Find location of bottom line within the character cell n_rows = bottom_line / (HD44780_CHAR_HEIGHT) + 1; n_cols = right_edge / (HD44780_CHAR_WIDTH) + 1; @@ -1585,7 +1585,7 @@ void MarlinUI::draw_status_screen() { * If the entire 4th row is not in use, do not put vertical bars all the way down to the bottom of the display */ - k = pixels_per_y_mesh_pnt * (GRID_MAX_POINTS_Y) + 2; + k = pixels_per_y_mesh_pnt * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) + 2; l = (HD44780_CHAR_HEIGHT) * n_rows; if (l > k && l - k >= (HD44780_CHAR_HEIGHT) / 2) { lcd_put_lchar(0, n_rows - 1, ' '); // Box Left edge @@ -1597,7 +1597,7 @@ void MarlinUI::draw_status_screen() { lcd.createChar(CHAR_LINE_TOP, (uint8_t*)&new_char); clear_custom_char(&new_char); - k = (GRID_MAX_POINTS_Y) * pixels_per_y_mesh_pnt + 1; // Row of pixels for the bottom box line + k = (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) * pixels_per_y_mesh_pnt + 1; // Row of pixels for the bottom box line l = k % (HD44780_CHAR_HEIGHT); // Row within relevant character cell new_char.custom_char_bits[l] = 0b11111U; // Char #1 is used for the box bottom line lcd.createChar(CHAR_LINE_BOT, (uint8_t*)&new_char); @@ -1608,7 +1608,7 @@ void MarlinUI::draw_status_screen() { lcd.createChar(CHAR_EDGE_L, (uint8_t*)&new_char); clear_custom_char(&new_char); - m = (GRID_MAX_POINTS_X) * pixels_per_x_mesh_pnt + 1; // Column of pixels for the right box line + m = (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) * pixels_per_x_mesh_pnt + 1; // Column of pixels for the right box line n = m % (HD44780_CHAR_WIDTH); // Column within relevant character cell i = HD44780_CHAR_WIDTH - 1 - n; // Column within relevant character cell (0 on the right) for (j = 0; j < HD44780_CHAR_HEIGHT; j++) diff --git a/Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp b/Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp index d772447551e7..2b53ff079c51 100644 --- a/Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp +++ b/Marlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp @@ -1117,7 +1117,7 @@ void MarlinUI::draw_status_screen() { //print only top left corner. All frame with grid points will be printed by panel lcd_moveto(0, 0); *fb++ = TLC; //top left corner - marker for plot parameters - *fb = (GRID_MAX_POINTS_X << 4) + GRID_MAX_POINTS_Y; //set mesh size + *fb = (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) << 4) + TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y); //set mesh size // Print plot position lcd_moveto(_LCD_W_POS, 0); diff --git a/Marlin/src/lcd/dogm/marlinui_DOGM.cpp b/Marlin/src/lcd/dogm/marlinui_DOGM.cpp index 721389cb2c56..93c620848e83 100644 --- a/Marlin/src/lcd/dogm/marlinui_DOGM.cpp +++ b/Marlin/src/lcd/dogm/marlinui_DOGM.cpp @@ -638,14 +638,14 @@ void MarlinUI::clear_for_drawing() { void MarlinUI::ubl_plot(const uint8_t x_plot, const uint8_t y_plot) { // Scale the box pixels appropriately - u8g_uint_t x_map_pixels = ((MAP_MAX_PIXELS_X - 4) / (GRID_MAX_POINTS_X)) * (GRID_MAX_POINTS_X), - y_map_pixels = ((MAP_MAX_PIXELS_Y - 4) / (GRID_MAX_POINTS_Y)) * (GRID_MAX_POINTS_Y), + u8g_uint_t x_map_pixels = ((MAP_MAX_PIXELS_X - 4) / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X))) * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + u8g_uint_t y_map_pixels = ((MAP_MAX_PIXELS_Y - 4) / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y))) * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)); - pixels_per_x_mesh_pnt = x_map_pixels / (GRID_MAX_POINTS_X), - pixels_per_y_mesh_pnt = y_map_pixels / (GRID_MAX_POINTS_Y), + u8g_uint_t pixels_per_x_mesh_pnt = x_map_pixels / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + u8g_uint_t pixels_per_y_mesh_pnt = y_map_pixels / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y))); - x_offset = MAP_UPPER_LEFT_CORNER_X + 1 + (MAP_MAX_PIXELS_X - x_map_pixels - 2) / 2, - y_offset = MAP_UPPER_LEFT_CORNER_Y + 1 + (MAP_MAX_PIXELS_Y - y_map_pixels - 2) / 2; + u8g_uint_t x_offset = MAP_UPPER_LEFT_CORNER_X + 1 + (MAP_MAX_PIXELS_X - x_map_pixels - 2) / 2, + y_offset = MAP_UPPER_LEFT_CORNER_Y + 1 + (MAP_MAX_PIXELS_Y - y_map_pixels - 2) / 2; // Clear the Mesh Map @@ -663,14 +663,14 @@ void MarlinUI::clear_for_drawing() { u8g.setColorIndex(1); const u8g_uint_t sx = x_offset + pixels_per_x_mesh_pnt / 2; u8g_uint_t y = y_offset + pixels_per_y_mesh_pnt / 2; - for (uint8_t j = 0; j < (GRID_MAX_POINTS_Y); j++, y += pixels_per_y_mesh_pnt) + for (uint8_t j = 0; j < (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)); j++, y += pixels_per_y_mesh_pnt) if (PAGE_CONTAINS(y, y)) - for (uint8_t i = 0, x = sx; i < (GRID_MAX_POINTS_X); i++, x += pixels_per_x_mesh_pnt) + for (uint8_t i = 0, x = sx; i < (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); i++, x += pixels_per_x_mesh_pnt) u8g.drawBox(x, y, 1, 1); // Fill in the Specified Mesh Point - const uint8_t y_plot_inv = (GRID_MAX_POINTS_Y) - 1 - y_plot; // The origin is typically in the lower right corner. We need to + const uint8_t y_plot_inv = (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) - 1 - y_plot; // The origin is typically in the lower right corner. We need to // invert the Y to get it to plot in the right location. const u8g_uint_t by = y_offset + y_plot_inv * pixels_per_y_mesh_pnt; diff --git a/Marlin/src/lcd/e3v2/jyersui/dwin.cpp b/Marlin/src/lcd/e3v2/jyersui/dwin.cpp index f2c54da74522..e0f6d73744df 100644 --- a/Marlin/src/lcd/e3v2/jyersui/dwin.cpp +++ b/Marlin/src/lcd/e3v2/jyersui/dwin.cpp @@ -275,7 +275,7 @@ class TextScroller { bool createPlaneFromMesh() { struct linear_fit_data lsf_results; incremental_LSF_reset(&lsf_results); - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { if (!isnan(bedlevel.z_values[x][y])) { xy_pos_t rpos = { bedlevel.get_mesh_x(x), bedlevel.get_mesh_y(y) }; incremental_LSF(&lsf_results, rpos, bedlevel.z_values[x][y]); @@ -290,7 +290,7 @@ class TextScroller { bedlevel.set_all_mesh_points_to_value(0); matrix_3x3 rotation = matrix_3x3::create_look_at(vector_3(lsf_results.A, lsf_results.B, 1)); - GRID_LOOP(i, j) { + GRID_LOOP_COND(i, j) { float mx = bedlevel.get_mesh_x(i), my = bedlevel.get_mesh_y(j), mz = bedlevel.z_values[i][j]; if (DEBUGGING(LEVELING)) { @@ -342,38 +342,38 @@ class TextScroller { float getMaxValue() { float max = -(__FLT_MAX__); - GRID_LOOP(x, y) { const float z = bedlevel.z_values[x][y]; if (!isnan(z)) NOLESS(max, z); } + GRID_LOOP_COND(x, y) { const float z = bedlevel.z_values[x][y]; if (!isnan(z)) NOLESS(max, z); } return max; } float getMinValue() { float min = __FLT_MAX__; - GRID_LOOP(x, y) { const float z = bedlevel.z_values[x][y]; if (!isnan(z)) NOMORE(min, z); } + GRID_LOOP_COND(x, y) { const float z = bedlevel.z_values[x][y]; if (!isnan(z)) NOMORE(min, z); } return min; } void drawBedMesh(const int16_t selected=-1, const uint8_t gridline_width=1, const uint16_t padding_x=8, const uint16_t padding_y_top=40 + 53 - 7) { drawing_mesh = true; const uint16_t total_width_px = DWIN_WIDTH - padding_x - padding_x, - cell_width_px = total_width_px / (GRID_MAX_POINTS_X), - cell_height_px = total_width_px / (GRID_MAX_POINTS_Y); + cell_width_px = total_width_px / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X))); + const uint16_t cell_height_px = total_width_px / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)); const float v_max = abs(getMaxValue()), v_min = abs(getMinValue()), rmax = _MAX(v_min, v_max); // Clear background from previous selection and select new square dwinDrawRectangle(1, COLOR_BG_BLACK, _MAX(0, padding_x - gridline_width), _MAX(0, padding_y_top - gridline_width), padding_x + total_width_px, padding_y_top + total_width_px); if (selected >= 0) { - const auto selected_y = selected / (GRID_MAX_POINTS_X); - const auto selected_x = selected - (GRID_MAX_POINTS_X) * selected_y; + const auto selected_y = selected / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + const auto selected_x = selected - (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) * selected_y; const auto start_y_px = padding_y_top + selected_y * cell_height_px; const auto start_x_px = padding_x + selected_x * cell_width_px; dwinDrawRectangle(1, COLOR_WHITE, _MAX(0, start_x_px - gridline_width), _MAX(0, start_y_px - gridline_width), start_x_px + cell_width_px, start_y_px + cell_height_px); } // Draw value square grid - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { const auto start_x_px = padding_x + x * cell_width_px; const auto end_x_px = start_x_px + cell_width_px - 1 - gridline_width; - const auto start_y_px = padding_y_top + (GRID_MAX_POINTS_Y - y - 1) * cell_height_px; + const auto start_y_px = padding_y_top + (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - y - 1) * cell_height_px; const auto end_y_px = start_y_px + cell_height_px - 1 - gridline_width; dwinDrawRectangle(1, // RGB565 colors: http://www.barth-dev.de/online/rgb565-color-picker/ isnan(bedlevel.z_values[x][y]) ? COLOR_GREY : ( // gray if undefined @@ -395,12 +395,12 @@ class TextScroller { } else { // has value MString<12> msg; - if (GRID_MAX_POINTS_X < 10) + if (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) < 10) msg.set(p_float_t(abs(bedlevel.z_values[x][y]), 2)); else msg.setf(F("%02i"), uint16_t(abs(bedlevel.z_values[x][y] - int16_t(bedlevel.z_values[x][y])) * 100)); const int8_t offset_x = cell_width_px / 2 - 3 * msg.length() - 2; - if (GRID_MAX_POINTS_X >= 10) + if (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) >= 10) dwinDrawString(false, font6x12, COLOR_WHITE, COLOR_BG_BLUE, start_x_px - 2 + offset_x, start_y_px + offset_y /*+ square / 2 - 6*/, F(".")); dwinDrawString(false, font6x12, COLOR_WHITE, COLOR_BG_BLUE, start_x_px + 1 + offset_x, start_y_px + offset_y /*+ square / 2 - 6*/, msg); } @@ -3535,7 +3535,7 @@ void JyersDWIN::menuItemHandler(const uint8_t menu, const uint8_t item, bool dra drawFloat(mesh_conf.mesh_x, row, 0, 1); } else - modifyValue(mesh_conf.mesh_x, 0, GRID_MAX_POINTS_X - 1, 1); + modifyValue(mesh_conf.mesh_x, 0, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1, 1); break; case LEVELING_M_Y: if (draw) { @@ -3543,14 +3543,14 @@ void JyersDWIN::menuItemHandler(const uint8_t menu, const uint8_t item, bool dra drawFloat(mesh_conf.mesh_y, row, 0, 1); } else - modifyValue(mesh_conf.mesh_y, 0, GRID_MAX_POINTS_Y - 1, 1); + modifyValue(mesh_conf.mesh_y, 0, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1, 1); break; case LEVELING_M_NEXT: if (draw) drawMenuItem(row, ICON_More, GET_TEXT_F(MSG_LEVEL_BED_NEXT_POINT)); else { - if (mesh_conf.mesh_x != (GRID_MAX_POINTS_X - 1) || mesh_conf.mesh_y != (GRID_MAX_POINTS_Y - 1)) { - if ((mesh_conf.mesh_x == (GRID_MAX_POINTS_X - 1) && mesh_conf.mesh_y % 2 == 0) || (mesh_conf.mesh_x == 0 && mesh_conf.mesh_y % 2 == 1)) + if (mesh_conf.mesh_x != (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) || mesh_conf.mesh_y != (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1)) { + if ((mesh_conf.mesh_x == (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) && mesh_conf.mesh_y % 2 == 0) || (mesh_conf.mesh_x == 0 && mesh_conf.mesh_y % 2 == 1)) mesh_conf.mesh_y++; else if (mesh_conf.mesh_y % 2 == 0) mesh_conf.mesh_x++; @@ -3643,14 +3643,14 @@ void JyersDWIN::menuItemHandler(const uint8_t menu, const uint8_t item, bool dra break; case UBL_M_NEXT: if (draw) { - if (mesh_conf.mesh_x != (GRID_MAX_POINTS_X - 1) || mesh_conf.mesh_y != (GRID_MAX_POINTS_Y - 1)) + if (mesh_conf.mesh_x != (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) || mesh_conf.mesh_y != (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1)) drawMenuItem(row, ICON_More, GET_TEXT_F(MSG_LEVEL_BED_NEXT_POINT)); else drawMenuItem(row, ICON_More, GET_TEXT_F(MSG_UBL_SAVE_MESH)); } else { - if (mesh_conf.mesh_x != (GRID_MAX_POINTS_X - 1) || mesh_conf.mesh_y != (GRID_MAX_POINTS_Y - 1)) { - if ((mesh_conf.mesh_x == (GRID_MAX_POINTS_X - 1) && mesh_conf.mesh_y % 2 == 0) || (mesh_conf.mesh_x == 0 && mesh_conf.mesh_y % 2 == 1)) + if (mesh_conf.mesh_x != (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) || mesh_conf.mesh_y != (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1)) { + if ((mesh_conf.mesh_x == (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) && mesh_conf.mesh_y % 2 == 0) || (mesh_conf.mesh_x == 0 && mesh_conf.mesh_y % 2 == 1)) mesh_conf.mesh_y++; else if (mesh_conf.mesh_y % 2 == 0) mesh_conf.mesh_x++; @@ -3671,7 +3671,7 @@ void JyersDWIN::menuItemHandler(const uint8_t menu, const uint8_t item, bool dra drawMenuItem(row, ICON_More, F("Previous Point")); else { if (mesh_conf.mesh_x != 0 || mesh_conf.mesh_y != 0) { - if ((mesh_conf.mesh_x == (GRID_MAX_POINTS_X - 1) && mesh_conf.mesh_y % 2 == 1) || (mesh_conf.mesh_x == 0 && mesh_conf.mesh_y % 2 == 0)) + if ((mesh_conf.mesh_x == (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) && mesh_conf.mesh_y % 2 == 1) || (mesh_conf.mesh_x == 0 && mesh_conf.mesh_y % 2 == 0)) mesh_conf.mesh_y--; else if (mesh_conf.mesh_y % 2 == 0) mesh_conf.mesh_x--; @@ -3744,12 +3744,12 @@ void JyersDWIN::menuItemHandler(const uint8_t menu, const uint8_t item, bool dra break; case MMESH_NEXT: if (draw) { - if (gridpoint < GRID_MAX_POINTS) + if (gridpoint < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, GRID_MAX_POINTS))) drawMenuItem(row, ICON_More, GET_TEXT_F(MSG_LEVEL_BED_NEXT_POINT)); else drawMenuItem(row, ICON_More, GET_TEXT_F(MSG_UBL_SAVE_MESH)); } - else if (gridpoint < GRID_MAX_POINTS) { + else if (gridpoint < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, GRID_MAX_POINTS)) { popupHandler(Popup_MoveWait); gcode.process_subcommands_now(F("G29")); planner.synchronize(); @@ -3797,11 +3797,11 @@ void JyersDWIN::menuItemHandler(const uint8_t menu, const uint8_t item, bool dra case MMESH_OLD: uint8_t mesh_x, mesh_y; // 0,0 -> 1,0 -> 2,0 -> 2,1 -> 1,1 -> 0,1 -> 0,2 -> 1,2 -> 2,2 - mesh_y = (gridpoint - 1) / (GRID_MAX_POINTS_Y); - mesh_x = (gridpoint - 1) % (GRID_MAX_POINTS_X); + mesh_y = (gridpoint - 1) / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)); + mesh_x = (gridpoint - 1) % (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); if (mesh_y % 2 == 1) - mesh_x = (GRID_MAX_POINTS_X) - mesh_x - 1; + mesh_x = (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) - mesh_x - 1; const float currval = bedlevel.z_values[mesh_x][mesh_y]; diff --git a/Marlin/src/lcd/e3v2/marlinui/ui_common.cpp b/Marlin/src/lcd/e3v2/marlinui/ui_common.cpp index 5f7ff1063a6d..abf642ba79fd 100644 --- a/Marlin/src/lcd/e3v2/marlinui/ui_common.cpp +++ b/Marlin/src/lcd/e3v2/marlinui/ui_common.cpp @@ -510,11 +510,11 @@ void MarlinUI::draw_status_message(const bool blink) { void MarlinUI::ubl_plot(const uint8_t x_plot, const uint8_t y_plot) { // Scale the box pixels appropriately - dwin_coord_t x_map_pixels = ((MAP_MAX_PIXELS_X - 4) / (GRID_MAX_POINTS_X)) * (GRID_MAX_POINTS_X), - y_map_pixels = ((MAP_MAX_PIXELS_Y - 4) / (GRID_MAX_POINTS_Y)) * (GRID_MAX_POINTS_Y), + dwin_coord_t x_map_pixels = ((MAP_MAX_PIXELS_X - 4) / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X))) * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + dwin_coord_t y_map_pixels = ((MAP_MAX_PIXELS_Y - 4) / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)))) * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)), - pixels_per_x_mesh_pnt = x_map_pixels / (GRID_MAX_POINTS_X), - pixels_per_y_mesh_pnt = y_map_pixels / (GRID_MAX_POINTS_Y), + pixels_per_x_mesh_pnt = x_map_pixels / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)), + pixels_per_y_mesh_pnt = y_map_pixels / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)), x_offset = MAP_UPPER_LEFT_CORNER_X + 1 + (MAP_MAX_PIXELS_X - x_map_pixels - 2) / 2, y_offset = MAP_UPPER_LEFT_CORNER_Y + 1 + (MAP_MAX_PIXELS_Y - y_map_pixels - 2) / 2; @@ -528,7 +528,7 @@ void MarlinUI::draw_status_message(const bool blink) { // Fill in the Specified Mesh Point - const uint8_t y_plot_inv = (GRID_MAX_POINTS_Y - 1) - y_plot; // The origin is typically in the lower right corner. We need to + const uint8_t y_plot_inv = (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1) - y_plot; // The origin is typically in the lower right corner. We need to // invert the Y to get it to plot in the right location. const dwin_coord_t by = y_offset + y_plot_inv * pixels_per_y_mesh_pnt; @@ -540,8 +540,8 @@ void MarlinUI::draw_status_message(const bool blink) { // Display Mesh Point Locations const dwin_coord_t sx = x_offset + pixels_per_x_mesh_pnt / 2; dwin_coord_t y = y_offset + pixels_per_y_mesh_pnt / 2; - for (uint8_t j = 0; j < (GRID_MAX_POINTS_Y); j++, y += pixels_per_y_mesh_pnt) - for (uint8_t i = 0, x = sx; i < (GRID_MAX_POINTS_X); i++, x += pixels_per_x_mesh_pnt) + for (uint8_t j = 0; j < (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)); j++, y += pixels_per_y_mesh_pnt) + for (uint8_t i = 0, x = sx; i < (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); i++, x += pixels_per_x_mesh_pnt) dwinDrawPoint(COLOR_WHITE, 1, 1, x, y); // Put Relevant Text on Display diff --git a/Marlin/src/lcd/e3v2/proui/bedlevel_tools.cpp b/Marlin/src/lcd/e3v2/proui/bedlevel_tools.cpp index b85caaf6cc8b..4cb908e7ba85 100644 --- a/Marlin/src/lcd/e3v2/proui/bedlevel_tools.cpp +++ b/Marlin/src/lcd/e3v2/proui/bedlevel_tools.cpp @@ -86,7 +86,7 @@ bool drawing_mesh = false; bool BedLevelTools::createPlaneFromMesh() { struct linear_fit_data lsf_results; incremental_LSF_reset(&lsf_results); - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { const float z = bedlevel.z_values[x][y]; if (!isnan(z)) { xy_pos_t rpos = { bedlevel.get_mesh_x(x), bedlevel.get_mesh_y(y) }; @@ -102,7 +102,7 @@ bool drawing_mesh = false; bedlevel.set_all_mesh_points_to_value(0); matrix_3x3 rotation = matrix_3x3::create_look_at(vector_3(lsf_results.A, lsf_results.B, 1)); - GRID_LOOP(i, j) { + GRID_LOOP_COND(i, j) { float mx = bedlevel.get_mesh_x(i), my = bedlevel.get_mesh_y(j), mz = bedlevel.z_values[i][j]; if (DEBUGGING(LEVELING)) { @@ -179,19 +179,19 @@ void BedLevelTools::meshReset() { // Accessors float BedLevelTools::getMaxValue() { float max = -(__FLT_MAX__); - GRID_LOOP(x, y) { const float z = bedlevel.z_values[x][y]; if (!isnan(z)) NOLESS(max, z); } + GRID_LOOP_COND(x, y) { const float z = bedlevel.z_values[x][y]; if (!isnan(z)) NOLESS(max, z); } return max; } float BedLevelTools::getMinValue() { float min = __FLT_MAX__; - GRID_LOOP(x, y) { const float z = bedlevel.z_values[x][y]; if (!isnan(z)) NOMORE(min, z); } + GRID_LOOP_COND(x, y) { const float z = bedlevel.z_values[x][y]; if (!isnan(z)) NOMORE(min, z); } return min; } // Return 'true' if mesh is good and within LCD limits bool BedLevelTools::meshValidate() { - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { const float z = bedlevel.z_values[x][y]; if (isnan(z) || !WITHIN(z, Z_OFFSET_MIN, Z_OFFSET_MAX)) return false; } @@ -204,26 +204,26 @@ bool BedLevelTools::meshValidate() { void BedLevelTools::drawBedMesh(int16_t selected/*=-1*/, uint8_t gridline_width/*=1*/, uint16_t padding_x/*=8*/, uint16_t padding_y_top/*=(40 + 53 - 7)*/) { drawing_mesh = true; - const uint16_t total_width_px = DWIN_WIDTH - padding_x - padding_x, - cell_width_px = total_width_px / (GRID_MAX_POINTS_X), - cell_height_px = total_width_px / (GRID_MAX_POINTS_Y); + const uint16_t total_width_px = DWIN_WIDTH - padding_x - padding_x; + const uint16_t cell_width_px = total_width_px / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + const uint16_t cell_height_px = total_width_px / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)); const float v_max = abs(getMaxValue()), v_min = abs(getMinValue()), rmax = _MAX(v_min, v_max); // Clear background from previous selection and select new square dwinDrawRectangle(1, COLOR_BG_BLACK, _MAX(0, padding_x - gridline_width), _MAX(0, padding_y_top - gridline_width), padding_x + total_width_px, padding_y_top + total_width_px); if (selected >= 0) { - const auto selected_y = selected / (GRID_MAX_POINTS_X); - const auto selected_x = selected - (GRID_MAX_POINTS_X) * selected_y; + const auto selected_y = selected / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + const auto selected_x = selected - (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) * selected_y; const auto start_y_px = padding_y_top + selected_y * cell_height_px; const auto start_x_px = padding_x + selected_x * cell_width_px; dwinDrawRectangle(1, COLOR_WHITE, _MAX(0, start_x_px - gridline_width), _MAX(0, start_y_px - gridline_width), start_x_px + cell_width_px, start_y_px + cell_height_px); } // Draw value square grid - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { const auto start_x_px = padding_x + x * cell_width_px; const auto end_x_px = start_x_px + cell_width_px - 1 - gridline_width; - const auto start_y_px = padding_y_top + ((GRID_MAX_POINTS_Y) - y - 1) * cell_height_px; + const auto start_y_px = padding_y_top + ((TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) - y - 1) * cell_height_px; const auto end_y_px = start_y_px + cell_height_px - 1 - gridline_width; const float z = bedlevel.z_values[x][y]; const uint16_t color = isnan(z) ? COLOR_GREY : ( // Gray if undefined @@ -247,7 +247,7 @@ bool BedLevelTools::meshValidate() { } else { // has value MString<12> msg; - constexpr bool is_wide = (GRID_MAX_POINTS_X) >= TERN(TJC_DISPLAY, 8, 10); + constexpr bool is_wide = (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) >= TERN(TJC_DISPLAY, 8, 10); if (is_wide) msg.setf(F("%02i"), uint16_t(z * 100) % 100); else @@ -261,7 +261,7 @@ bool BedLevelTools::meshValidate() { safe_delay(10); LCD_SERIAL.flushTX(); - } // GRID_LOOP + } // GRID_LOOP_COND } void BedLevelTools::setMeshViewerStatus() { // TODO: draw gradient with values as a legend instead diff --git a/Marlin/src/lcd/e3v2/proui/dwin.cpp b/Marlin/src/lcd/e3v2/proui/dwin.cpp index 9299bdc68657..770382764fe9 100644 --- a/Marlin/src/lcd/e3v2/proui/dwin.cpp +++ b/Marlin/src/lcd/e3v2/proui/dwin.cpp @@ -4300,8 +4300,8 @@ void drawMaxAccelMenu() { void applyEditMeshX() { bedLevelTools.mesh_x = menuData.value; } void applyEditMeshY() { bedLevelTools.mesh_y = menuData.value; } void resetMesh() { bedLevelTools.meshReset(); LCD_MESSAGE(MSG_MESH_RESET); } - void setEditMeshX() { hmiValue.select = 0; setIntOnClick(0, GRID_MAX_POINTS_X - 1, bedLevelTools.mesh_x, applyEditMeshX, liveEditMesh); } - void setEditMeshY() { hmiValue.select = 1; setIntOnClick(0, GRID_MAX_POINTS_Y - 1, bedLevelTools.mesh_y, applyEditMeshY, liveEditMesh); } + void setEditMeshX() { hmiValue.select = 0; setIntOnClick(0, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1, bedLevelTools.mesh_x, applyEditMeshX, liveEditMesh); } + void setEditMeshY() { hmiValue.select = 1; setIntOnClick(0, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1, bedLevelTools.mesh_y, applyEditMeshY, liveEditMesh); } void setEditZValue() { setPFloatOnClick(Z_OFFSET_MIN, Z_OFFSET_MAX, 3); } #endif @@ -4329,7 +4329,7 @@ void drawMaxAccelMenu() { } void ublSmartFillMesh() { - for (uint8_t x = 0; x < GRID_MAX_POINTS_Y; ++x) bedlevel.smart_fill_mesh(); + for (uint8_t x = 0; x < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y); ++x) bedlevel.smart_fill_mesh(); LCD_MESSAGE(MSG_UBL_MESH_FILLED); } diff --git a/Marlin/src/lcd/e3v2/proui/meshviewer.cpp b/Marlin/src/lcd/e3v2/proui/meshviewer.cpp index e167af56655c..8d6954ecdf80 100644 --- a/Marlin/src/lcd/e3v2/proui/meshviewer.cpp +++ b/Marlin/src/lcd/e3v2/proui/meshviewer.cpp @@ -122,7 +122,7 @@ void MeshViewer::draw(const bool withsave/*=false*/, const bool redraw/*=true*/) #endif } else { - if (redraw) drawMesh(bedlevel.z_values, GRID_MAX_POINTS_X, GRID_MAX_POINTS_Y); + if (redraw) drawMesh(bedlevel.z_values, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X), TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)); else DWINUI::drawBox(1, hmiData.colorBackground, { 89, 305, 99, 38 }); } diff --git a/Marlin/src/lcd/e3v2/proui/proui_extui.cpp b/Marlin/src/lcd/e3v2/proui/proui_extui.cpp index 2adca7cbe6cc..5c384dc42ad6 100644 --- a/Marlin/src/lcd/e3v2/proui/proui_extui.cpp +++ b/Marlin/src/lcd/e3v2/proui/proui_extui.cpp @@ -164,8 +164,8 @@ namespace ExtUI { #if HAS_MESH void onMeshUpdate(const int8_t xpos, const int8_t ypos, const_float_t zval) { - const int16_t idx = ypos * (GRID_MAX_POINTS_X) + xpos; - dwinMeshUpdate(_MIN(idx, GRID_MAX_POINTS), int(GRID_MAX_POINTS), zval); + const int16_t idx = ypos * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) + xpos; + dwinMeshUpdate(_MIN(idx, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, GRID_MAX_POINTS)), int(TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, GRID_MAX_POINTS)), zval); dwinRedrawScreen(); } diff --git a/Marlin/src/lcd/extui/anycubic_chiron/chiron_tft.cpp b/Marlin/src/lcd/extui/anycubic_chiron/chiron_tft.cpp index b4e197828358..d6f492bcf134 100644 --- a/Marlin/src/lcd/extui/anycubic_chiron/chiron_tft.cpp +++ b/Marlin/src/lcd/extui/anycubic_chiron/chiron_tft.cpp @@ -870,7 +870,7 @@ void ChironTFT::panelProcess(uint8_t req) { TFTSer.println(live_Zoffset); } else { - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { const xy_uint8_t pos { x, y }; const float currval = getMeshPoint(pos); setMeshPoint(pos, constrain(currval + Zshift, AC_LOWEST_MESHPOINT_VAL, 2)); diff --git a/Marlin/src/lcd/extui/anycubic_vyper/dgus_tft.cpp b/Marlin/src/lcd/extui/anycubic_vyper/dgus_tft.cpp index 696cfad68405..38437435ef02 100644 --- a/Marlin/src/lcd/extui/anycubic_vyper/dgus_tft.cpp +++ b/Marlin/src/lcd/extui/anycubic_vyper/dgus_tft.cpp @@ -562,7 +562,7 @@ namespace Anycubic { // If probing completes ok save the mesh and park // Ignore the custom machine name if (strcmp_P(msg + strlen(MACHINE_NAME), MARLIN_msg_ready) == 0) { - if (probe_cnt == GRID_MAX_POINTS) { + if (probe_cnt == TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, GRID_MAX_POINTS)) { probe_cnt = 0; injectCommands(F("M500")); // G27 park nozzle //changePageOfTFT(PAGE_PreLEVEL); @@ -1514,7 +1514,7 @@ namespace Anycubic { babystepAxis_steps(steps, Z); #endif - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { const xy_uint8_t pos { x, y }; const float currval = getMeshPoint(pos); #if ACDEBUG(AC_MARLIN) @@ -1555,7 +1555,7 @@ namespace Anycubic { babystepAxis_steps(steps, Z); #endif - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { const xy_uint8_t pos { x, y }; const float currval = getMeshPoint(pos); //SERIAL_ECHOLNPGM("x: ", x, " y: ", y, " z: ", currval); @@ -2003,7 +2003,7 @@ namespace Anycubic { DEBUG_ECHOLNPGM("z off: ", ftostr52sprj(getZOffset_mm())); #endif #if HAS_LEVELING - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { const xy_uint8_t pos { x, y }; const float currval = getMeshPoint(pos); setMeshPoint(pos, constrain(currval + getZOffset_mm(), AC_LOWEST_MESHPOINT_VAL, 5)); diff --git a/Marlin/src/lcd/extui/dgus/mks/DGUSScreenHandler.cpp b/Marlin/src/lcd/extui/dgus/mks/DGUSScreenHandler.cpp index 2a9ba87d68fb..b447a46d7cfb 100644 --- a/Marlin/src/lcd/extui/dgus/mks/DGUSScreenHandler.cpp +++ b/Marlin/src/lcd/extui/dgus/mks/DGUSScreenHandler.cpp @@ -501,7 +501,7 @@ void DGUSScreenHandlerMKS::meshLevel(DGUS_VP_Variable &var, void *val_ptr) { break; case 2: - if (mesh_point_count == GRID_MAX_POINTS) { // The first point + if (mesh_point_count == TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, GRID_MAX_POINTS)) { // The first point queue.enqueue_now(F("G28\nG29S1")); mesh_point_count--; diff --git a/Marlin/src/lcd/extui/dgus_reloaded/DGUSScreenHandler.cpp b/Marlin/src/lcd/extui/dgus_reloaded/DGUSScreenHandler.cpp index bb31a1d71ad7..f7d05932df4e 100644 --- a/Marlin/src/lcd/extui/dgus_reloaded/DGUSScreenHandler.cpp +++ b/Marlin/src/lcd/extui/dgus_reloaded/DGUSScreenHandler.cpp @@ -240,10 +240,10 @@ void DGUSScreenHandler::meshUpdate(const int8_t xpos, const int8_t ypos) { return; } - uint8_t point = ypos * GRID_MAX_POINTS_X + xpos; + uint8_t point = ypos * TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) + xpos; probing_icons[point < 16 ? 0 : 1] |= (1U << (point % 16)); - if (xpos >= GRID_MAX_POINTS_X - 1 && ypos >= GRID_MAX_POINTS_Y - 1 && !ExtUI::getLevelingIsValid()) + if (xpos >= TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1 && ypos >= TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) - 1 && !ExtUI::getLevelingIsValid()) probing_icons[0] = probing_icons[1] = 0; triggerFullUpdate(); diff --git a/Marlin/src/lcd/extui/dgus_reloaded/DGUSTxHandler.cpp b/Marlin/src/lcd/extui/dgus_reloaded/DGUSTxHandler.cpp index 1212f715c008..093b2c2aaf16 100644 --- a/Marlin/src/lcd/extui/dgus_reloaded/DGUSTxHandler.cpp +++ b/Marlin/src/lcd/extui/dgus_reloaded/DGUSTxHandler.cpp @@ -344,8 +344,8 @@ void DGUSTxHandler::ablGrid(DGUS_VP &vp) { int16_t fixed; for (int16_t i = 0; i < DGUS_LEVEL_GRID_SIZE; i++) { - point.x = i % (GRID_MAX_POINTS_X); - point.y = i / (GRID_MAX_POINTS_X); + point.x = i % (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + point.y = i / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); fixed = dgus.toFixedPoint(ExtUI::getMeshPoint(point)); data[i] = Swap16(fixed); } diff --git a/Marlin/src/lcd/extui/dgus_reloaded/config/DGUS_Addr.h b/Marlin/src/lcd/extui/dgus_reloaded/config/DGUS_Addr.h index ec4962e6b27f..a1fecc4849f7 100644 --- a/Marlin/src/lcd/extui/dgus_reloaded/config/DGUS_Addr.h +++ b/Marlin/src/lcd/extui/dgus_reloaded/config/DGUS_Addr.h @@ -26,7 +26,7 @@ #define DGUS_FILE_COUNT 5 #define DGUS_FILENAME_LEN 32 #define DGUS_ELAPSED_LEN 15 -#define DGUS_LEVEL_GRID_SIZE 25 +#define DGUS_LEVEL_GRID_SIZE TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, 25) #define DGUS_MACHINE_LEN 24 #define DGUS_BUILDVOLUME_LEN 24 #define DGUS_VERSION_LEN 16 diff --git a/Marlin/src/lcd/extui/dgus_reloaded/config/DGUS_Constants.h b/Marlin/src/lcd/extui/dgus_reloaded/config/DGUS_Constants.h index 0562b892395c..f99436a5b4c5 100644 --- a/Marlin/src/lcd/extui/dgus_reloaded/config/DGUS_Constants.h +++ b/Marlin/src/lcd/extui/dgus_reloaded/config/DGUS_Constants.h @@ -25,7 +25,7 @@ #include "DGUS_Addr.h" -#if DGUS_LEVEL_GRID_SIZE != GRID_MAX_POINTS +#if DGUS_LEVEL_GRID_SIZE != TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, GRID_MAX_POINTS) #error "DGUS_LEVEL_GRID_SIZE is incompatible with current mesh." #endif diff --git a/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_base.cpp b/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_base.cpp index 14f219645337..9480e6b1d02d 100644 --- a/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_base.cpp +++ b/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_base.cpp @@ -27,8 +27,8 @@ using namespace FTDI; void BedMeshBase::_drawMesh(CommandProcessor &cmd, int16_t x, int16_t y, int16_t w, int16_t h, uint8_t opts, float autoscale_max, uint8_t highlightedTag, mesh_getter_ptr func, void *data) { - constexpr uint8_t rows = GRID_MAX_POINTS_Y; - constexpr uint8_t cols = GRID_MAX_POINTS_X; + IF_DISABLED(VARIABLE_GRID_POINTS, constexpr) uint8_t rows = TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y); + IF_DISABLED(VARIABLE_GRID_POINTS, constexpr) uint8_t cols = TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X); #define VALUE(X,Y) (func ? func(X,Y,data) : 0) #define ISVAL(X,Y) (func ? !isnan(VALUE(X,Y)) : true) @@ -194,13 +194,13 @@ void BedMeshBase::_drawMesh(CommandProcessor &cmd, int16_t x, int16_t y, int16_t } uint8_t BedMeshBase::pointToTag(uint8_t x, uint8_t y) { - return x >= 0 && x < GRID_MAX_POINTS_X && y >= 0 && y < GRID_MAX_POINTS_Y ? y * (GRID_MAX_POINTS_X) + x + 10 : 0; + return x >= 0 && x < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) && y >= 0 && y < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y) ? y * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) + x + 10 : 0; } bool BedMeshBase::tagToPoint(uint8_t tag, xy_uint8_t &pt) { if (tag < 10) return false; - pt.x = (tag - 10) % (GRID_MAX_POINTS_X); - pt.y = (tag - 10) / (GRID_MAX_POINTS_X); + pt.x = (tag - 10) % (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + pt.y = (tag - 10) / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); return true; } diff --git a/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_edit_screen.cpp b/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_edit_screen.cpp index c894d017ebcd..1bfc787b2acf 100644 --- a/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_edit_screen.cpp +++ b/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_edit_screen.cpp @@ -75,7 +75,7 @@ void BedMeshEditScreen::onEntry() { void BedMeshEditScreen::makeMeshValid() { bed_mesh_t &mesh = ExtUI::getMeshArray(); - GRID_LOOP(x, y) { + GRID_LOOP_COND(x, y) { if (isnan(mesh[x][y])) mesh[x][y] = 0; } } diff --git a/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_view_screen.cpp b/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_view_screen.cpp index 6030fd02a0e7..8258f99e6672 100644 --- a/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_view_screen.cpp +++ b/Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/bed_mesh_view_screen.cpp @@ -93,7 +93,7 @@ void BedMeshViewScreen::onRedraw(draw_mode_t what) { } if (what & FOREGROUND) { - const float progress = sq(float(mydata.count) / GRID_MAX_POINTS); + const float progress = sq(float(mydata.count) / TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, GRID_MAX_POINTS)); if (progress >= 1.0) drawHighlightedPointValue(); drawMeshForeground(cmd, INSET_POS(MESH_POS), meshGetter, nullptr, pointToTag(mydata.highlight.x, mydata.highlight.y), progress); diff --git a/Marlin/src/lcd/extui/ia_creality/ia_creality_extui.cpp b/Marlin/src/lcd/extui/ia_creality/ia_creality_extui.cpp index 86a19f60890c..afea9d17504d 100644 --- a/Marlin/src/lcd/extui/ia_creality/ia_creality_extui.cpp +++ b/Marlin/src/lcd/extui/ia_creality/ia_creality_extui.cpp @@ -314,10 +314,10 @@ void onSettingsLoaded(const bool success) { #if HAS_MESH if (ExtUI::getLevelingIsValid()) { uint8_t abl_probe_index = 0; - for (uint8_t outer = 0; outer < GRID_MAX_POINTS_Y; outer++) - for (uint8_t inner = 0; inner < GRID_MAX_POINTS_X; inner++) { + for (uint8_t outer = 0; outer < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y); outer++) + for (uint8_t inner = 0; inner < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X); inner++) { const bool zig = outer & 1; - const xy_uint8_t point = { uint8_t(zig ? (GRID_MAX_POINTS_X - 1) - inner : inner), outer }; + const xy_uint8_t point = { uint8_t(zig ? (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) - inner : inner), outer }; rts.sendData(ExtUI::getMeshPoint(point) * 1000, AutolevelVal + (abl_probe_index * 2)); ++abl_probe_index; } @@ -344,10 +344,10 @@ void onPostprocessSettings() {} #if HAS_MESH if (ExtUI::getLevelingIsValid()) { uint8_t abl_probe_index = 0; - for (uint8_t outer = 0; outer < GRID_MAX_POINTS_Y; outer++) - for (uint8_t inner = 0; inner < GRID_MAX_POINTS_X; inner++) { + for (uint8_t outer = 0; outer < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y); outer++) + for (uint8_t inner = 0; inner < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X); inner++) { const bool zig = outer & 1; - const xy_uint8_t point = { uint8_t(zig ? (GRID_MAX_POINTS_X - 1) - inner : inner), outer }; + const xy_uint8_t point = { uint8_t(zig ? (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) - inner : inner), outer }; rts.sendData(ExtUI::getMeshPoint(point) * 1000, AutolevelVal + abl_probe_index * 2); ++abl_probe_index; } @@ -372,10 +372,10 @@ void onPostprocessSettings() {} rts.sendData(ExchangePageBase + 64, ExchangepageAddr); #if HAS_MESH uint8_t abl_probe_index = 0; - for (uint8_t outer = 0; outer < GRID_MAX_POINTS_Y; outer++) - for (uint8_t inner = 0; inner < GRID_MAX_POINTS_X; inner++) { + for (uint8_t outer = 0; outer < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y); outer++) + for (uint8_t inner = 0; inner < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X); inner++) { const bool zig = outer & 1; // != ((PR_OUTER_END) & 1); - const xy_uint8_t point = { uint8_t(zig ? (GRID_MAX_POINTS_X - 1) - inner : inner), outer }; + const xy_uint8_t point = { uint8_t(zig ? (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) - inner : inner), outer }; if (point.x == xpos && outer == ypos) rts.sendData(ExtUI::getMeshPoint(point) * 1000, AutolevelVal + (abl_probe_index * 2)); ++abl_probe_index; diff --git a/Marlin/src/lcd/extui/ia_creality/ia_creality_rts.cpp b/Marlin/src/lcd/extui/ia_creality/ia_creality_rts.cpp index 8fb672931d59..ff64819b0d98 100644 --- a/Marlin/src/lcd/extui/ia_creality/ia_creality_rts.cpp +++ b/Marlin/src/lcd/extui/ia_creality/ia_creality_rts.cpp @@ -1060,10 +1060,10 @@ void RTS::handleData() { if (ExtUI::getLevelingIsValid()) { uint8_t abl_probe_index = 0; - for (uint8_t outer = 0; outer < GRID_MAX_POINTS_Y; outer++) - for (uint8_t inner = 0; inner < GRID_MAX_POINTS_X; inner++) { + for (uint8_t outer = 0; outer < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y); outer++) + for (uint8_t inner = 0; inner < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X); inner++) { const bool zig = outer & 1; - const xy_uint8_t point = { uint8_t(zig ? (GRID_MAX_POINTS_X - 1) - inner : inner), outer }; + const xy_uint8_t point = { uint8_t(zig ? (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1) - inner : inner), outer }; sendData(ExtUI::getMeshPoint(point) * 1000, AutolevelVal + abl_probe_index * 2); ++abl_probe_index; } @@ -1621,9 +1621,9 @@ void RTS::handleData() { case AutolevelVal: { uint8_t meshPoint = (recdat.addr - AutolevelVal) / 2, - yPnt = meshPoint / (GRID_MAX_POINTS_X), - xPnt = meshPoint - yPnt * (GRID_MAX_POINTS_X); - if (yPnt % 2 != 0) xPnt = (GRID_MAX_POINTS_X) - 1 - xPnt; // zag row + yPnt = meshPoint / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)), + xPnt = meshPoint - yPnt * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + if (yPnt % 2 != 0) xPnt = (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) - 1 - xPnt; // zag row float meshVal = float(recdat.data[0] - (recdat.data[0] >= 32768 ? 65536 : 0)) / 1000; diff --git a/Marlin/src/lcd/extui/ui_api.cpp b/Marlin/src/lcd/extui/ui_api.cpp index 9677a988a930..17e913a9dec5 100644 --- a/Marlin/src/lcd/extui/ui_api.cpp +++ b/Marlin/src/lcd/extui/ui_api.cpp @@ -874,7 +874,7 @@ namespace ExtUI { bed_mesh_t& getMeshArray() { return bedlevel.z_values; } float getMeshPoint(const xy_uint8_t &pos) { return bedlevel.z_values[pos.x][pos.y]; } void setMeshPoint(const xy_uint8_t &pos, const_float_t zoff) { - if (WITHIN(pos.x, 0, (GRID_MAX_POINTS_X) - 1) && WITHIN(pos.y, 0, (GRID_MAX_POINTS_Y) - 1)) { + if (WITHIN(pos.x, 0, (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) - 1) && WITHIN(pos.y, 0, (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) - 1)) { bedlevel.z_values[pos.x][pos.y] = zoff; TERN_(ABL_BILINEAR_SUBDIVISION, bedlevel.refresh_bed_level()); } diff --git a/Marlin/src/lcd/menu/menu_probe_level.cpp b/Marlin/src/lcd/menu/menu_probe_level.cpp index 18c735489937..2877d27bc224 100644 --- a/Marlin/src/lcd/menu/menu_probe_level.cpp +++ b/Marlin/src/lcd/menu/menu_probe_level.cpp @@ -59,7 +59,7 @@ // // LCD probed points are from defaults - constexpr grid_count_t total_probe_points = TERN(AUTO_BED_LEVELING_3POINT, 3, GRID_MAX_POINTS); + IF_DISABLED(VARIABLE_GRID_POINTS, constexpr) grid_count_t total_probe_points = TERN(AUTO_BED_LEVELING_3POINT, 3, TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, GRID_MAX_POINTS)); // // Bed leveling is done. Wait for G29 to complete. diff --git a/Marlin/src/lcd/menu/menu_ubl.cpp b/Marlin/src/lcd/menu/menu_ubl.cpp index 63ad34a70b16..ebc3b6cdf2cc 100644 --- a/Marlin/src/lcd/menu/menu_ubl.cpp +++ b/Marlin/src/lcd/menu/menu_ubl.cpp @@ -414,7 +414,7 @@ void ubl_map_move_to_xy() { } inline int32_t grid_index(const uint8_t x, const uint8_t y) { - return (GRID_MAX_POINTS_X) * y + x; + return (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) * y + x; } /** @@ -446,11 +446,11 @@ void ubl_map_screen() { do { // Now, keep the encoder position within range if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = GRID_MAX_POINTS + TERN(TOUCH_SCREEN, ui.encoderPosition, -1); - if (int32_t(ui.encoderPosition) > GRID_MAX_POINTS - 1) ui.encoderPosition = TERN0(TOUCH_SCREEN, ui.encoderPosition - GRID_MAX_POINTS); + if (int32_t(ui.encoderPosition) > GRID_MAX_POINTS - 1) ui.encoderPosition = TERN0(TOUCH_SCREEN, ui.encoderPosition - TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS, GRID_MAX_POINTS)); // Draw the grid point based on the encoder - x = ui.encoderPosition % (GRID_MAX_POINTS_X); - y = ui.encoderPosition / (GRID_MAX_POINTS_X); + x = ui.encoderPosition % (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + y = ui.encoderPosition / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); // Validate if needed #if IS_KINEMATIC @@ -464,8 +464,8 @@ void ubl_map_screen() { #if IS_KINEMATIC n_edit_pts = 9; // TODO: Delta accessible edit points #else - const bool xc = WITHIN(x, 1, (GRID_MAX_POINTS_X) - 2), - yc = WITHIN(y, 1, (GRID_MAX_POINTS_Y) - 2); + const bool xc = WITHIN(x, 1, (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) - 2); + const bool yc = WITHIN(y, 1, (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) - 2); n_edit_pts = yc ? (xc ? 9 : 6) : (xc ? 6 : 4); // Corners #endif @@ -475,8 +475,8 @@ void ubl_map_screen() { } // Draw the grid point based on the encoder - x = ui.encoderPosition % (GRID_MAX_POINTS_X); - y = ui.encoderPosition / (GRID_MAX_POINTS_X); + x = ui.encoderPosition % (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); + y = ui.encoderPosition / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); if (ui.should_draw()) ui.ubl_plot(x, y); diff --git a/Marlin/src/lcd/sovol_rts/sovol_rts.cpp b/Marlin/src/lcd/sovol_rts/sovol_rts.cpp index 3c098386845d..c913f4cce907 100644 --- a/Marlin/src/lcd/sovol_rts/sovol_rts.cpp +++ b/Marlin/src/lcd/sovol_rts/sovol_rts.cpp @@ -245,16 +245,16 @@ void RTS::init() { bool zig = false; int8_t inStart, inStop, inInc, showcount; showcount = 0; - for (int8_t y = 0; y < GRID_MAX_POINTS_Y; y++) { + for (int8_t y = 0; y < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y); y++) { // Away from origin if (zig) { inStart = 0; - inStop = GRID_MAX_POINTS_X; + inStop = TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X); inInc = 1; } else { // Towards origin - inStart = GRID_MAX_POINTS_X - 1; + inStart = TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1; inStop = -1; inInc = -1; } @@ -1193,16 +1193,16 @@ void RTS::handleData() { bool zig = true; int8_t inStart, inStop, inInc, showcount; showcount = 0; - for (int8_t y = 0; y < GRID_MAX_POINTS_Y; y++) { + for (int8_t y = 0; y < TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y); y++) { // Away from origin if (zig) { inStart = 0; - inStop = GRID_MAX_POINTS_X; + inStop = TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X); inInc = 1; } else { // Towards origin - inStart = GRID_MAX_POINTS_X - 1; + inStart = TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X) - 1; inStop = -1; inInc = -1; } diff --git a/Marlin/src/lcd/tft/ui_color_ui.cpp b/Marlin/src/lcd/tft/ui_color_ui.cpp index 6c90cb6c3627..c7c83bbe4343 100644 --- a/Marlin/src/lcd/tft/ui_color_ui.cpp +++ b/Marlin/src/lcd/tft/ui_color_ui.cpp @@ -522,12 +522,12 @@ void MenuItem_confirm::draw_select_screen(FSTR_P const yes, FSTR_P const no, con tft.set_background(COLOR_BACKGROUND); tft.add_rectangle(0, 0, UBL_GRID_W, UBL_GRID_H, COLOR_WHITE); - for (uint16_t x = 0; x < (GRID_MAX_POINTS_X); x++) - for (uint16_t y = 0; y < (GRID_MAX_POINTS_Y); y++) + for (uint16_t x = 0; x < (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)); x++) + for (uint16_t y = 0; y < (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)); y++) if (position_is_reachable({ bedlevel.get_mesh_x(x), bedlevel.get_mesh_y(y) })) - tft.add_bar(1 + (x * 2 + 1) * (UBL_GRID_W - 4) / (GRID_MAX_POINTS_X) / 2, UBL_GRID_H - 3 - ((y * 2 + 1) * (UBL_GRID_H - 4) / (GRID_MAX_POINTS_Y) / 2), 2, 2, COLOR_UBL); + tft.add_bar(1 + (x * 2 + 1) * (UBL_GRID_W - 4) / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) / 2, UBL_GRID_H - 3 - ((y * 2 + 1) * (UBL_GRID_H - 4) / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) / 2), 2, 2, COLOR_UBL); - tft.add_rectangle((x_plot * 2 + 1) * (UBL_GRID_W - 4) / (GRID_MAX_POINTS_X) / 2 - 1, UBL_GRID_H - 5 - ((y_plot * 2 + 1) * (UBL_GRID_H - 4) / (GRID_MAX_POINTS_Y) / 2), 6, 6, COLOR_UBL); + tft.add_rectangle((x_plot * 2 + 1) * (UBL_GRID_W - 4) / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)) / 2 - 1, UBL_GRID_H - 5 - ((y_plot * 2 + 1) * (UBL_GRID_H - 4) / (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_Y, GRID_MAX_POINTS_Y)) / 2), 6, 6, COLOR_UBL); const xy_pos_t pos = { bedlevel.get_mesh_x(x_plot), bedlevel.get_mesh_y(y_plot) }, lpos = pos.asLogical(); @@ -573,8 +573,8 @@ void MenuItem_confirm::draw_select_screen(FSTR_P const yes, FSTR_P const no, con #if ENABLED(TOUCH_SCREEN) touch.clear(); draw_menu_navigation = false; - add_control(UBL_GRID_X + UBL_GRID_W + UBL_CONTROL_OFFSET, UBL_GRID_Y + UBL_CONTROL_OFFSET, UBL, (ENCODER_STEPS_PER_MENU_ITEM) * (GRID_MAX_POINTS_X), imgUp); - add_control(UBL_GRID_X + UBL_GRID_W + UBL_CONTROL_OFFSET, UBL_GRID_Y + UBL_GRID_H - UBL_CONTROL_OFFSET - 32, UBL, -(ENCODER_STEPS_PER_MENU_ITEM) * (GRID_MAX_POINTS_X), imgDown); + add_control(UBL_GRID_X + UBL_GRID_W + UBL_CONTROL_OFFSET, UBL_GRID_Y + UBL_CONTROL_OFFSET, UBL, (ENCODER_STEPS_PER_MENU_ITEM) * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)), imgUp); + add_control(UBL_GRID_X + UBL_GRID_W + UBL_CONTROL_OFFSET, UBL_GRID_Y + UBL_GRID_H - UBL_CONTROL_OFFSET - 32, UBL, -(ENCODER_STEPS_PER_MENU_ITEM) * (TERN(VARIABLE_GRID_POINTS, GRID_USED_POINTS_X, GRID_MAX_POINTS_X)), imgDown); add_control(UBL_GRID_X + UBL_CONTROL_OFFSET, UBL_GRID_Y + UBL_GRID_H + UBL_CONTROL_OFFSET, UBL, -(ENCODER_STEPS_PER_MENU_ITEM), imgLeft); add_control(UBL_GRID_X + UBL_GRID_W - UBL_CONTROL_OFFSET - 32, UBL_GRID_Y + UBL_GRID_H + UBL_CONTROL_OFFSET, UBL, ENCODER_STEPS_PER_MENU_ITEM, imgRight); diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index d4b7b0f44884..6ce586f8e3c1 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -3180,7 +3180,7 @@ void MarlinSettings::postprocess() { // Write crc to MAT along with other data, or just tack on to the beginning or end persistentStore.access_start(); - const bool err = persistentStore.write_data(pos, (uint8_t *)&bedlevel.grid_points, sizeof(bedlevel.grid_points), &crc) + const bool err = persistentStore.write_data(pos, (uint8_t *)&bedlevel.nr_grid_points, sizeof(bedlevel.nr_grid_points), &crc) || persistentStore.write_data(pos, src, MESH_DATA_SIZE, &crc); persistentStore.access_finish(); @@ -3215,8 +3215,8 @@ void MarlinSettings::postprocess() { #endif persistentStore.access_start(); - xy_uint8_t grid_points; - bool err = persistentStore.read_data(pos, (uint8_t *)&grid_points, sizeof(grid_points), &crc) + xy_uint8_t nr_grid_points; + bool err = persistentStore.read_data(pos, (uint8_t *)&nr_grid_points, sizeof(nr_grid_points), &crc) || persistentStore.read_data(pos, dest, MESH_DATA_SIZE, &crc); persistentStore.access_finish(); @@ -3243,7 +3243,7 @@ void MarlinSettings::postprocess() { if (err) SERIAL_ECHOLNPGM("?Unable to load mesh data."); else { - bedlevel.set_grid_points(grid_points); + bedlevel.set_nr_grid_points(nr_grid_points); DEBUG_ECHOLNPGM("Mesh loaded from slot ", slot); }