Skip to content

Commit

Permalink
const_float_t is a reference on AVR
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Apr 1, 2021
1 parent 481092d commit 3e9c1ff
Show file tree
Hide file tree
Showing 80 changed files with 376 additions and 372 deletions.
2 changes: 1 addition & 1 deletion Marlin/src/HAL/AVR/fastio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ uint8_t extDigitalRead(const int8_t pin) {
*
* DC values -1.0 to 1.0. Negative duty cycle inverts the pulse.
*/
uint16_t set_pwm_frequency_hz(const float &hz, const float dca, const float dcb, const float dcc) {
uint16_t set_pwm_frequency_hz(const_float_t hz, const float dca, const float dcb, const float dcc) {
float count = 0;
if (hz > 0 && (dca || dcb || dcc)) {
count = float(F_CPU) / hz; // 1x prescaler, TOP for 16MHz base freq.
Expand Down
6 changes: 0 additions & 6 deletions Marlin/src/core/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,3 @@
#define MAP0(f, x, peek, ...) f(x) MAP_NEXT (peek, MAP1) (f, peek, __VA_ARGS__)
#define MAP1(f, x, peek, ...) f(x) MAP_NEXT (peek, MAP0) (f, peek, __VA_ARGS__)
#define MAP(f, ...) EVAL512 (MAP1 (f, __VA_ARGS__, (), 0))

#ifdef __AVR__
#define const_float_t const float &
#else
#define const_float_t const float
#endif
2 changes: 1 addition & 1 deletion Marlin/src/core/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void print_bin(uint16_t val) {
}
}

void print_xyz(const float &x, const float &y, const float &z, PGM_P const prefix/*=nullptr*/, PGM_P const suffix/*=nullptr*/) {
void print_xyz(const_float_t x, const_float_t y, const_float_t z, PGM_P const prefix/*=nullptr*/, PGM_P const suffix/*=nullptr*/) {
if (prefix) serialprintPGM(prefix);
SERIAL_ECHOPAIR_P(SP_X_STR, x, SP_Y_STR, y, SP_Z_STR, z);
if (suffix) serialprintPGM(suffix); else SERIAL_EOL();
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/core/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ void serialprint_truefalse(const bool tf);
void serial_spaces(uint8_t count);

void print_bin(const uint16_t val);
void print_xyz(const float &x, const float &y, const float &z, PGM_P const prefix=nullptr, PGM_P const suffix=nullptr);
void print_xyz(const_float_t x, const_float_t y, const_float_t z, PGM_P const prefix=nullptr, PGM_P const suffix=nullptr);

inline void print_xyz(const xyz_pos_t &xyz, PGM_P const prefix=nullptr, PGM_P const suffix=nullptr) {
print_xyz(xyz.x, xyz.y, xyz.z, prefix, suffix);
Expand Down
10 changes: 10 additions & 0 deletions Marlin/src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ typedef float feedRate_t;
//
typedef int16_t celsius_t;

//
// On AVR pointers are only 2 bytes so use 'const float &' for 'const float'
//
#ifdef __AVR__
typedef const float & const_float_t;
#else
typedef const float const_float_t;
#endif
typedef const_float_t const_feedRate_t;

// Conversion macros
#define MMM_TO_MMS(MM_M) feedRate_t(float(MM_M) / 60.0f)
#define MMS_TO_MMM(MM_S) (float(MM_S) * 60.0f)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/babystep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void Babystep::step_axis(const AxisEnum axis) {
}
}

void Babystep::add_mm(const AxisEnum axis, const float &mm) {
void Babystep::add_mm(const AxisEnum axis, const_float_t mm) {
add_steps(axis, mm * planner.settings.axis_steps_per_mm[axis]);
}

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/babystep.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Babystep {
#endif

static void add_steps(const AxisEnum axis, const int16_t distance);
static void add_mm(const AxisEnum axis, const float &mm);
static void add_mm(const AxisEnum axis, const_float_t mm);

static inline bool has_steps() {
return steps[BS_AXIS_IND(X_AXIS)] || steps[BS_AXIS_IND(Y_AXIS)] || steps[BS_AXIS_IND(Z_AXIS)];
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/backlash.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Backlash {
static float smoothing_mm;
#endif

static inline void set_correction(const float &v) { correction = _MAX(0, _MIN(1.0, v)) * all_on; }
static inline void set_correction(const_float_t v) { correction = _MAX(0, _MIN(1.0, v)) * all_on; }
static inline float get_correction() { return float(ui8_to_percent(correction)) / 100.0f; }
#else
static constexpr uint8_t correction = (BACKLASH_CORRECTION) * 0xFF;
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/feature/bedlevel/abl/abl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void print_bilinear_leveling_grid() {
) * 0.5f;
}

static float bed_level_virt_2cmr(const uint8_t x, const uint8_t y, const float &tx, const float &ty) {
static float bed_level_virt_2cmr(const uint8_t x, const uint8_t y, const_float_t tx, const_float_t ty) {
float row[4], column[4];
LOOP_L_N(i, 4) {
LOOP_L_N(j, 4) {
Expand Down Expand Up @@ -356,7 +356,7 @@ float bilinear_z_offset(const xy_pos_t &raw) {
* Prepare a bilinear-leveled linear move on Cartesian,
* splitting the move where it crosses grid borders.
*/
void bilinear_line_to_destination(const feedRate_t &scaled_fr_mm_s, uint16_t x_splits, uint16_t y_splits) {
void bilinear_line_to_destination(const_feedRate_t scaled_fr_mm_s, uint16_t x_splits, uint16_t y_splits) {
// Get current and destination cells for this line
xy_int_t c1 { CELL_INDEX(x, current_position.x), CELL_INDEX(y, current_position.y) },
c2 { CELL_INDEX(x, destination.x), CELL_INDEX(y, destination.y) };
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/bedlevel/abl/abl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void refresh_bed_level();
#endif

#if IS_CARTESIAN && DISABLED(SEGMENT_LEVELED_MOVES)
void bilinear_line_to_destination(const feedRate_t &scaled_fr_mm_s, uint16_t x_splits=0xFFFF, uint16_t y_splits=0xFFFF);
void bilinear_line_to_destination(const_feedRate_t scaled_fr_mm_s, uint16_t x_splits=0xFFFF, uint16_t y_splits=0xFFFF);
#endif

#define _GET_MESH_X(I) float(bilinear_start.x + (I) * bilinear_grid_spacing.x)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/bedlevel/bedlevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ TemporaryBedLevelingState::TemporaryBedLevelingState(const bool enable) : saved(

#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)

void set_z_fade_height(const float &zfh, const bool do_report/*=true*/) {
void set_z_fade_height(const_float_t zfh, const bool do_report/*=true*/) {

if (planner.z_fade_height == zfh) return;

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/bedlevel/bedlevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void set_bed_leveling_enabled(const bool enable=true);
void reset_bed_level();

#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
void set_z_fade_height(const float &zfh, const bool do_report=true);
void set_z_fade_height(const_float_t zfh, const bool do_report=true);
#endif

#if EITHER(MESH_BED_LEVELING, PROBE_MANUALLY)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
* Prepare a mesh-leveled linear move in a Cartesian setup,
* splitting the move where it crosses mesh borders.
*/
void mesh_bed_leveling::line_to_destination(const feedRate_t &scaled_fr_mm_s, uint8_t x_splits, uint8_t y_splits) {
void mesh_bed_leveling::line_to_destination(const_feedRate_t scaled_fr_mm_s, uint8_t x_splits, uint8_t y_splits) {
// Get current and destination cells for this line
xy_int8_t scel = cell_indexes(current_position), ecel = cell_indexes(destination);
NOMORE(scel.x, GRID_MAX_POINTS_X - 2);
Expand Down
22 changes: 11 additions & 11 deletions Marlin/src/feature/bedlevel/mbl/mesh_bed_leveling.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,55 +56,55 @@ class mesh_bed_leveling {
return false;
}

static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
static void set_z(const int8_t px, const int8_t py, const_float_t z) { z_values[px][py] = z; }

static inline void zigzag(const int8_t index, int8_t &px, int8_t &py) {
px = index % (GRID_MAX_POINTS_X);
py = index / (GRID_MAX_POINTS_X);
if (py & 1) px = (GRID_MAX_POINTS_X - 1) - px; // Zig zag
}

static void set_zigzag_z(const int8_t index, const float &z) {
static void set_zigzag_z(const int8_t index, const_float_t z) {
int8_t px, py;
zigzag(index, px, py);
set_z(px, py, z);
}

static int8_t cell_index_x(const float &x) {
static int8_t cell_index_x(const_float_t x) {
int8_t cx = (x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST);
return constrain(cx, 0, (GRID_MAX_POINTS_X) - 2);
}
static int8_t cell_index_y(const float &y) {
static int8_t cell_index_y(const_float_t y) {
int8_t cy = (y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST);
return constrain(cy, 0, (GRID_MAX_POINTS_Y) - 2);
}
static inline xy_int8_t cell_indexes(const float &x, const float &y) {
static inline xy_int8_t cell_indexes(const_float_t x, const_float_t y) {
return { cell_index_x(x), cell_index_y(y) };
}
static inline xy_int8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); }

static int8_t probe_index_x(const float &x) {
static int8_t probe_index_x(const_float_t x) {
int8_t px = (x - (MESH_MIN_X) + 0.5f * (MESH_X_DIST)) * RECIPROCAL(MESH_X_DIST);
return WITHIN(px, 0, GRID_MAX_POINTS_X - 1) ? px : -1;
}
static int8_t probe_index_y(const float &y) {
static int8_t probe_index_y(const_float_t y) {
int8_t py = (y - (MESH_MIN_Y) + 0.5f * (MESH_Y_DIST)) * RECIPROCAL(MESH_Y_DIST);
return WITHIN(py, 0, GRID_MAX_POINTS_Y - 1) ? py : -1;
}
static inline xy_int8_t probe_indexes(const float &x, const float &y) {
static inline xy_int8_t probe_indexes(const_float_t x, const_float_t y) {
return { probe_index_x(x), probe_index_y(y) };
}
static inline xy_int8_t probe_indexes(const xy_pos_t &xy) { return probe_indexes(xy.x, xy.y); }

static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
static float calc_z0(const_float_t a0, const_float_t a1, const_float_t z1, const_float_t a2, const_float_t z2) {
const float delta_z = (z2 - z1) / (a2 - a1),
delta_a = a0 - a1;
return z1 + delta_a * delta_z;
}

static float get_z(const xy_pos_t &pos
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
, const float &factor=1.0f
, const_float_t factor=1.0f
#endif
) {
#if DISABLED(ENABLE_LEVELING_FADE_HEIGHT)
Expand All @@ -120,7 +120,7 @@ class mesh_bed_leveling {
}

#if IS_CARTESIAN && DISABLED(SEGMENT_LEVELED_MOVES)
static void line_to_destination(const feedRate_t &scaled_fr_mm_s, uint8_t x_splits=0xFF, uint8_t y_splits=0xFF);
static void line_to_destination(const_feedRate_t scaled_fr_mm_s, uint8_t x_splits=0xFF, uint8_t y_splits=0xFF);
#endif
};

Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/feature/bedlevel/ubl/ubl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void unified_bed_leveling::invalidate() {
set_all_mesh_points_to_value(NAN);
}

void unified_bed_leveling::set_all_mesh_points_to_value(const float value) {
void unified_bed_leveling::set_all_mesh_points_to_value(const_float_t value) {
GRID_LOOP(x, y) {
z_values[x][y] = value;
TERN_(EXTENSIBLE_UI, ExtUI::onMeshUpdate(x, y, value));
Expand All @@ -115,7 +115,7 @@ void unified_bed_leveling::set_all_mesh_points_to_value(const float value) {
constexpr int16_t Z_STEPS_NAN = INT16_MAX;

void unified_bed_leveling::set_store_from_mesh(const bed_mesh_t &in_values, mesh_store_t &stored_values) {
auto z_to_store = [](const float &z) {
auto z_to_store = [](const_float_t z) {
if (isnan(z)) return Z_STEPS_NAN;
const int32_t z_scaled = TRUNC(z * mesh_store_scaling);
if (z_scaled == Z_STEPS_NAN || !WITHIN(z_scaled, INT16_MIN, INT16_MAX))
Expand Down
44 changes: 22 additions & 22 deletions Marlin/src/feature/bedlevel/ubl/ubl.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ class unified_bed_leveling {
static G29_parameters_t param;

#if IS_NEWPANEL
static void move_z_with_encoder(const float &multiplier);
static void move_z_with_encoder(const_float_t multiplier);
static float measure_point_with_encoder();
static float measure_business_card_thickness();
static void manually_probe_remaining_mesh(const xy_pos_t&, const float&, const float&, const bool) _O0;
static void manually_probe_remaining_mesh(const xy_pos_t&, const_float_t , const_float_t , const bool) _O0;
static void fine_tune_mesh(const xy_pos_t &pos, const bool do_ubl_mesh_map) _O0;
#endif

static bool G29_parse_parameters() _O0;
static void shift_mesh_height();
static void probe_entire_mesh(const xy_pos_t &near, const bool do_ubl_mesh_map, const bool stow_probe, const bool do_furthest) _O0;
static void tilt_mesh_based_on_3pts(const float &z1, const float &z2, const float &z3);
static void tilt_mesh_based_on_3pts(const_float_t z1, const_float_t z2, const_float_t z3);
static void tilt_mesh_based_on_probed_grid(const bool do_ubl_mesh_map);
static bool smart_fill_one(const uint8_t x, const uint8_t y, const int8_t xdir, const int8_t ydir);
static inline bool smart_fill_one(const xy_uint8_t &pos, const xy_uint8_t &dir) {
Expand All @@ -103,12 +103,12 @@ class unified_bed_leveling {
static mesh_index_pair find_furthest_invalid_mesh_point() _O0;
static void reset();
static void invalidate();
static void set_all_mesh_points_to_value(const float value);
static void adjust_mesh_to_mean(const bool cflag, const float value);
static void set_all_mesh_points_to_value(const_float_t value);
static void adjust_mesh_to_mean(const bool cflag, const_float_t value);
static bool sanity_check();

static void G29() _O0; // O0 for no optimization
static void smart_fill_wlsf(const float &) _O2; // O2 gives smaller code than Os on A2560
static void smart_fill_wlsf(const_float_t ) _O2; // O2 gives smaller code than Os on A2560

static int8_t storage_slot;

Expand All @@ -131,42 +131,42 @@ class unified_bed_leveling {

unified_bed_leveling();

FORCE_INLINE static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
FORCE_INLINE static void set_z(const int8_t px, const int8_t py, const_float_t z) { z_values[px][py] = z; }

static int8_t cell_index_x_raw(const float &x) {
static int8_t cell_index_x_raw(const_float_t x) {
return FLOOR((x - (MESH_MIN_X)) * RECIPROCAL(MESH_X_DIST));
}

static int8_t cell_index_y_raw(const float &y) {
static int8_t cell_index_y_raw(const_float_t y) {
return FLOOR((y - (MESH_MIN_Y)) * RECIPROCAL(MESH_Y_DIST));
}

static int8_t cell_index_x_valid(const float &x) {
static int8_t cell_index_x_valid(const_float_t x) {
return WITHIN(cell_index_x_raw(x), 0, (GRID_MAX_POINTS_X - 2));
}

static int8_t cell_index_y_valid(const float &y) {
static int8_t cell_index_y_valid(const_float_t y) {
return WITHIN(cell_index_y_raw(y), 0, (GRID_MAX_POINTS_Y - 2));
}

static int8_t cell_index_x(const float &x) {
static int8_t cell_index_x(const_float_t x) {
return constrain(cell_index_x_raw(x), 0, (GRID_MAX_POINTS_X) - 2);
}

static int8_t cell_index_y(const float &y) {
static int8_t cell_index_y(const_float_t y) {
return constrain(cell_index_y_raw(y), 0, (GRID_MAX_POINTS_Y) - 2);
}

static inline xy_int8_t cell_indexes(const float &x, const float &y) {
static inline xy_int8_t cell_indexes(const_float_t x, const_float_t y) {
return { cell_index_x(x), cell_index_y(y) };
}
static inline xy_int8_t cell_indexes(const xy_pos_t &xy) { return cell_indexes(xy.x, xy.y); }

static int8_t closest_x_index(const float &x) {
static int8_t closest_x_index(const_float_t x) {
const int8_t px = (x - (MESH_MIN_X) + (MESH_X_DIST) * 0.5) * RECIPROCAL(MESH_X_DIST);
return WITHIN(px, 0, GRID_MAX_POINTS_X - 1) ? px : -1;
}
static int8_t closest_y_index(const float &y) {
static int8_t closest_y_index(const_float_t y) {
const int8_t py = (y - (MESH_MIN_Y) + (MESH_Y_DIST) * 0.5) * RECIPROCAL(MESH_Y_DIST);
return WITHIN(py, 0, GRID_MAX_POINTS_Y - 1) ? py : -1;
}
Expand All @@ -189,7 +189,7 @@ class unified_bed_leveling {
* It is fairly expensive with its 4 floating point additions and 2 floating point
* multiplications.
*/
FORCE_INLINE static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
FORCE_INLINE static float calc_z0(const_float_t a0, const_float_t a1, const_float_t z1, const_float_t a2, const_float_t z2) {
return z1 + (z2 - z1) * (a0 - a1) / (a2 - a1);
}

Expand All @@ -203,7 +203,7 @@ class unified_bed_leveling {
* z_correction_for_x_on_horizontal_mesh_line is an optimization for
* the case where the printer is making a vertical line that only crosses horizontal mesh lines.
*/
static inline float z_correction_for_x_on_horizontal_mesh_line(const float &rx0, const int x1_i, const int yi) {
static inline float z_correction_for_x_on_horizontal_mesh_line(const_float_t rx0, const int x1_i, const int yi) {
if (!WITHIN(x1_i, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(yi, 0, GRID_MAX_POINTS_Y - 1)) {

if (DEBUGGING(LEVELING)) {
Expand All @@ -226,7 +226,7 @@ class unified_bed_leveling {
//
// See comments above for z_correction_for_x_on_horizontal_mesh_line
//
static inline float z_correction_for_y_on_vertical_mesh_line(const float &ry0, const int xi, const int y1_i) {
static inline float z_correction_for_y_on_vertical_mesh_line(const_float_t ry0, const int xi, const int y1_i) {
if (!WITHIN(xi, 0, GRID_MAX_POINTS_X - 1) || !WITHIN(y1_i, 0, GRID_MAX_POINTS_Y - 1)) {

if (DEBUGGING(LEVELING)) {
Expand All @@ -252,7 +252,7 @@ class unified_bed_leveling {
* Z-Height at both ends. Then it does a linear interpolation of these heights based
* on the Y position within the cell.
*/
static float get_z_correction(const float &rx0, const float &ry0) {
static float get_z_correction(const_float_t rx0, const_float_t ry0) {
const int8_t cx = cell_index_x(rx0), cy = cell_index_y(ry0); // return values are clamped

/**
Expand Down Expand Up @@ -309,9 +309,9 @@ class unified_bed_leveling {
}

#if UBL_SEGMENTED
static bool line_to_destination_segmented(const feedRate_t &scaled_fr_mm_s);
static bool line_to_destination_segmented(const_feedRate_t scaled_fr_mm_s);
#else
static void line_to_destination_cartesian(const feedRate_t &scaled_fr_mm_s, const uint8_t e);
static void line_to_destination_cartesian(const_feedRate_t scaled_fr_mm_s, const uint8_t e);
#endif

static inline bool mesh_is_valid() {
Expand Down
Loading

0 comments on commit 3e9c1ff

Please sign in to comment.