From bf0fcebfe634bf58a2137acfe758c789da908170 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 13 May 2018 03:25:31 -0500 Subject: [PATCH 1/2] Apply _AXIS macro --- Marlin/Marlin_main.cpp | 14 +++++++------- Marlin/configuration_store.cpp | 4 ++-- Marlin/endstops.cpp | 2 +- Marlin/macros.h | 2 +- Marlin/planner.cpp | 4 ++-- Marlin/stepper.cpp | 34 +++++++++++++++++----------------- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index c30756033..8e8d24eb5 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -3027,7 +3027,7 @@ static void do_homing_move(const AxisEnum axis, const float distance, const floa * before updating the current position. */ -#define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS) +#define HOMEAXIS(A) homeaxis(_AXIS(A)) static void homeaxis(const AxisEnum axis) { @@ -3036,7 +3036,7 @@ static void homeaxis(const AxisEnum axis) { if (axis != Z_AXIS) { BUZZ(100, 880); return; } #else #define CAN_HOME(A) \ - (axis == A##_AXIS && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0))) + (axis == _AXIS(A) && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0))) if (!CAN_HOME(X) && !CAN_HOME(Y) && !CAN_HOME(Z)) return; #endif @@ -11048,8 +11048,8 @@ inline void gcode_M502() { */ #if ENABLED(HYBRID_THRESHOLD) inline void gcode_M913() { - #define TMC_SAY_PWMTHRS(P,Q) tmc_get_pwmthrs(stepper##Q, TMC_##Q, planner.axis_steps_per_mm[P##_AXIS]) - #define TMC_SET_PWMTHRS(P,Q) tmc_set_pwmthrs(stepper##Q, value, planner.axis_steps_per_mm[P##_AXIS]) + #define TMC_SAY_PWMTHRS(A,Q) tmc_get_pwmthrs(stepper##Q, TMC_##Q, planner.axis_steps_per_mm[_AXIS(A)]) + #define TMC_SET_PWMTHRS(A,Q) tmc_set_pwmthrs(stepper##Q, value, planner.axis_steps_per_mm[_AXIS(A)]) #define TMC_SAY_PWMTHRS_E(E) do{ const uint8_t extruder = E; tmc_get_pwmthrs(stepperE##E, TMC_E##E, planner.axis_steps_per_mm[E_AXIS_N]); }while(0) #define TMC_SET_PWMTHRS_E(E) do{ const uint8_t extruder = E; tmc_set_pwmthrs(stepperE##E, value, planner.axis_steps_per_mm[E_AXIS_N]); }while(0) @@ -13039,7 +13039,7 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) { return; } - #define MBL_SEGMENT_END(A) (current_position[A ##_AXIS] + (destination[A ##_AXIS] - current_position[A ##_AXIS]) * normalized_dist) + #define MBL_SEGMENT_END(A) (current_position[_AXIS(A)] + (destination[_AXIS(A)] - current_position[_AXIS(A)]) * normalized_dist) float normalized_dist, end[XYZE]; const int8_t gcx = max(cx1, cx2), gcy = max(cy1, cy2); @@ -13083,7 +13083,7 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) { #elif ENABLED(AUTO_BED_LEVELING_BILINEAR) - #define CELL_INDEX(A,V) ((V - bilinear_start[A##_AXIS]) * ABL_BG_FACTOR(A##_AXIS)) + #define CELL_INDEX(A,V) ((V - bilinear_start[_AXIS(A)]) * ABL_BG_FACTOR(_AXIS(A))) /** * Prepare a bilinear-leveled linear move on Cartesian, @@ -13107,7 +13107,7 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) { return; } - #define LINE_SEGMENT_END(A) (current_position[A ##_AXIS] + (destination[A ##_AXIS] - current_position[A ##_AXIS]) * normalized_dist) + #define LINE_SEGMENT_END(A) (current_position[_AXIS(A)] + (destination[_AXIS(A)] - current_position[_AXIS(A)]) * normalized_dist) float normalized_dist, end[XYZE]; const int8_t gcx = max(cx1, cx2), gcy = max(cy1, cy2); diff --git a/Marlin/configuration_store.cpp b/Marlin/configuration_store.cpp index 09d753966..707513e10 100644 --- a/Marlin/configuration_store.cpp +++ b/Marlin/configuration_store.cpp @@ -62,7 +62,7 @@ #if HAS_TRINAMIC #include "stepper_indirection.h" #include "tmc_util.h" - #define TMC_GET_PWMTHRS(P,Q) _tmc_thrs(stepper##Q.microsteps(), stepper##Q.TPWMTHRS(), planner.axis_steps_per_mm[P##_AXIS]) + #define TMC_GET_PWMTHRS(A,Q) _tmc_thrs(stepper##Q.microsteps(), stepper##Q.TPWMTHRS(), planner.axis_steps_per_mm[_AXIS(A)]) #endif #if ENABLED(AUTO_BED_LEVELING_UBL) @@ -1343,7 +1343,7 @@ void MarlinSettings::postprocess() { #endif #if ENABLED(HYBRID_THRESHOLD) - #define TMC_SET_PWMTHRS(P,Q) tmc_set_pwmthrs(stepper##Q, tmc_hybrid_threshold[TMC_##Q], planner.axis_steps_per_mm[P##_AXIS]) + #define TMC_SET_PWMTHRS(A,Q) tmc_set_pwmthrs(stepper##Q, tmc_hybrid_threshold[TMC_##Q], planner.axis_steps_per_mm[_AXIS(A)]) uint32_t tmc_hybrid_threshold[TMC_AXES]; EEPROM_READ(tmc_hybrid_threshold); if (!validating) { diff --git a/Marlin/endstops.cpp b/Marlin/endstops.cpp index f51a11b8a..93fbd9a5a 100644 --- a/Marlin/endstops.cpp +++ b/Marlin/endstops.cpp @@ -181,7 +181,7 @@ void Endstops::report_state() { #endif #define _ENDSTOP_HIT_ECHO(A,C) do{ \ - SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", stepper.triggered_position_mm(A ##_AXIS)); \ + SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", stepper.triggered_position_mm(_AXIS(A))); \ _SET_STOP_CHAR(A,C); }while(0) #define _ENDSTOP_HIT_TEST(A,C) \ diff --git a/Marlin/macros.h b/Marlin/macros.h index ec9e55c0b..bb006fe48 100644 --- a/Marlin/macros.h +++ b/Marlin/macros.h @@ -30,7 +30,7 @@ #define XYZ 3 // For use in macros that take a single axis letter -#define _AXIS(AXIS) AXIS ##_AXIS +#define _AXIS(A) (A##_AXIS) #define _XMIN_ 100 #define _YMIN_ 200 diff --git a/Marlin/planner.cpp b/Marlin/planner.cpp index fbc66e2e0..0e9f4fccf 100644 --- a/Marlin/planner.cpp +++ b/Marlin/planner.cpp @@ -2188,11 +2188,11 @@ void Planner::buffer_segment(const float &a, const float &b, const float &c, con // Always split the first move into two (if not homing or probing) if (!has_blocks_queued()) { - #define _BETWEEN(A) (position[A##_AXIS] + target[A##_AXIS]) >> 1 + #define _BETWEEN(A) (position[_AXIS(A)] + target[_AXIS(A)]) >> 1 const int32_t between[ABCE] = { _BETWEEN(A), _BETWEEN(B), _BETWEEN(C), _BETWEEN(E) }; #if HAS_POSITION_FLOAT - #define _BETWEEN_F(A) (position_float[A##_AXIS] + target_float[A##_AXIS]) * 0.5 + #define _BETWEEN_F(A) (position_float[_AXIS(A)] + target_float[_AXIS(A)]) * 0.5 const float between_float[ABCE] = { _BETWEEN_F(A), _BETWEEN_F(B), _BETWEEN_F(C), _BETWEEN_F(E) }; #endif diff --git a/Marlin/stepper.cpp b/Marlin/stepper.cpp index 03cf80ee5..57bef81a8 100644 --- a/Marlin/stepper.cpp +++ b/Marlin/stepper.cpp @@ -167,20 +167,20 @@ volatile int32_t Stepper::endstops_trigsteps[XYZ]; #define LOCKED_X2_MOTOR locked_x2_motor #define LOCKED_Y2_MOTOR locked_y2_motor #define LOCKED_Z2_MOTOR locked_z2_motor - #define DUAL_ENDSTOP_APPLY_STEP(AXIS,v) \ - if (performing_homing) { \ - if (AXIS##_HOME_DIR < 0) { \ - if (!(TEST(endstops.old_endstop_bits, AXIS##_MIN) && count_direction[AXIS##_AXIS] < 0) && !LOCKED_##AXIS##_MOTOR) AXIS##_STEP_WRITE(v); \ - if (!(TEST(endstops.old_endstop_bits, AXIS##2_MIN) && count_direction[AXIS##_AXIS] < 0) && !LOCKED_##AXIS##2_MOTOR) AXIS##2_STEP_WRITE(v); \ - } \ - else { \ - if (!(TEST(endstops.old_endstop_bits, AXIS##_MAX) && count_direction[AXIS##_AXIS] > 0) && !LOCKED_##AXIS##_MOTOR) AXIS##_STEP_WRITE(v); \ - if (!(TEST(endstops.old_endstop_bits, AXIS##2_MAX) && count_direction[AXIS##_AXIS] > 0) && !LOCKED_##AXIS##2_MOTOR) AXIS##2_STEP_WRITE(v); \ - } \ - } \ - else { \ - AXIS##_STEP_WRITE(v); \ - AXIS##2_STEP_WRITE(v); \ + #define DUAL_ENDSTOP_APPLY_STEP(A,V) \ + if (performing_homing) { \ + if (A##_HOME_DIR < 0) { \ + if (!(TEST(endstops.old_endstop_bits, A##_MIN) && count_direction[_AXIS(A)] < 0) && !LOCKED_##A##_MOTOR) A##_STEP_WRITE(V); \ + if (!(TEST(endstops.old_endstop_bits, A##2_MIN) && count_direction[_AXIS(A)] < 0) && !LOCKED_##A##2_MOTOR) A##2_STEP_WRITE(V); \ + } \ + else { \ + if (!(TEST(endstops.old_endstop_bits, A##_MAX) && count_direction[_AXIS(A)] > 0) && !LOCKED_##A##_MOTOR) A##_STEP_WRITE(V); \ + if (!(TEST(endstops.old_endstop_bits, A##2_MAX) && count_direction[_AXIS(A)] > 0) && !LOCKED_##A##2_MOTOR) A##2_STEP_WRITE(V); \ + } \ + } \ + else { \ + A##_STEP_WRITE(V); \ + A##2_STEP_WRITE(V); \ } #endif @@ -331,13 +331,13 @@ void Stepper::wake_up() { void Stepper::set_directions() { #define SET_STEP_DIR(AXIS) \ - if (motor_direction(AXIS ##_AXIS)) { \ + if (motor_direction(_AXIS(AXIS))) { \ AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \ - count_direction[AXIS ##_AXIS] = -1; \ + count_direction[_AXIS(AXIS)] = -1; \ } \ else { \ AXIS ##_APPLY_DIR(!INVERT_## AXIS ##_DIR, false); \ - count_direction[AXIS ##_AXIS] = 1; \ + count_direction[_AXIS(AXIS)] = 1; \ } #if HAS_X_DIR From 3505d018db6be82e80b5e67629a4db2e2bf23cda Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 13 May 2018 03:17:25 -0500 Subject: [PATCH 2/2] Smarter MIN, MAX, ABS macros Use macros that explicitly avoid double-evaluation and can be used for any datatype, replacing `min`, `max`, `abs`, `fabs`, `labs`, and `FABS`. Co-Authored-By: ejtagle --- Marlin/G26_Mesh_Validation_Tool.cpp | 6 +- Marlin/I2CPositionEncoder.cpp | 20 +++---- Marlin/Marlin.h | 2 +- Marlin/Marlin_main.cpp | 86 ++++++++++++++--------------- Marlin/Max7219_Debug_LEDs.cpp | 4 +- Marlin/digipot_mcp4018.cpp | 2 +- Marlin/digipot_mcp4451.cpp | 2 +- Marlin/least_squares_fit.cpp | 2 +- Marlin/least_squares_fit.h | 8 +-- Marlin/macros.h | 51 ++++++++++++++--- Marlin/malyanlcd.cpp | 4 +- Marlin/nozzle.cpp | 6 +- Marlin/planner.cpp | 60 ++++++++++---------- Marlin/planner.h | 2 +- Marlin/planner_bezier.cpp | 2 +- Marlin/servo.cpp | 2 +- Marlin/temperature.cpp | 8 +-- Marlin/temperature.h | 6 +- Marlin/ubl.h | 10 ++-- Marlin/ubl_G29.cpp | 16 +++--- Marlin/ubl_motion.cpp | 6 +- Marlin/ultralcd.cpp | 18 +++--- Marlin/ultralcd_impl_DOGM.h | 2 +- Marlin/ultralcd_impl_HD44780.h | 10 ++-- 24 files changed, 185 insertions(+), 150 deletions(-) diff --git a/Marlin/G26_Mesh_Validation_Tool.cpp b/Marlin/G26_Mesh_Validation_Tool.cpp index a1769b303..beb795ab4 100644 --- a/Marlin/G26_Mesh_Validation_Tool.cpp +++ b/Marlin/G26_Mesh_Validation_Tool.cpp @@ -363,7 +363,7 @@ // If the end point of the line is closer to the nozzle, flip the direction, // moving from the end to the start. On very small lines the optimization isn't worth it. - if (dist_end < dist_start && (INTERSECTION_CIRCLE_RADIUS) < FABS(line_length)) + if (dist_end < dist_start && (INTERSECTION_CIRCLE_RADIUS) < ABS(line_length)) return print_line_from_here_to_there(ex, ey, ez, sx, sy, sz); // Decide whether to retract & bump @@ -485,7 +485,7 @@ #endif #endif thermalManager.setTargetBed(g26_bed_temp); - while (abs(thermalManager.degBed() - g26_bed_temp) > 3) { + while (ABS(thermalManager.degBed() - g26_bed_temp) > 3) { #if ENABLED(NEWPANEL) if (is_lcd_clicked()) return exit_from_g26(); @@ -508,7 +508,7 @@ // Start heating the nozzle and wait for it to reach temperature. thermalManager.setTargetHotend(g26_hotend_temp, 0); - while (abs(thermalManager.degHotend(0) - g26_hotend_temp) > 3) { + while (ABS(thermalManager.degHotend(0) - g26_hotend_temp) > 3) { #if ENABLED(NEWPANEL) if (is_lcd_clicked()) return exit_from_g26(); diff --git a/Marlin/I2CPositionEncoder.cpp b/Marlin/I2CPositionEncoder.cpp index dc8119f96..ce7029237 100644 --- a/Marlin/I2CPositionEncoder.cpp +++ b/Marlin/I2CPositionEncoder.cpp @@ -134,7 +134,7 @@ #ifdef I2CPE_EC_THRESH_PROPORTIONAL const millis_t deltaTime = positionTime - lastPositionTime; - const uint32_t distance = abs(position - lastPosition), + const uint32_t distance = ABS(position - lastPosition), speed = distance / deltaTime; const float threshold = constrain((speed / 50), 1, 50) * ecThreshold; #else @@ -150,7 +150,7 @@ LOOP_L_N(i, I2CPE_ERR_ARRAY_SIZE) { sum += err[i]; - if (i) diffSum += abs(err[i-1] - err[i]); + if (i) diffSum += ABS(err[i-1] - err[i]); } const int32_t error = int32_t(sum / (I2CPE_ERR_ARRAY_SIZE + 1)); //calculate average for error @@ -163,7 +163,7 @@ //SERIAL_ECHOLN(error); #ifdef I2CPE_ERR_THRESH_ABORT - if (labs(error) > I2CPE_ERR_THRESH_ABORT * planner.axis_steps_per_mm[encoderAxis]) { + if (ABS(error) > I2CPE_ERR_THRESH_ABORT * planner.axis_steps_per_mm[encoderAxis]) { //kill("Significant Error"); SERIAL_ECHOPGM("Axis error greater than set threshold, aborting!"); SERIAL_ECHOLN(error); @@ -175,8 +175,8 @@ if (errIdx == 0) { // In order to correct for "error" but avoid correcting for noise and non-skips // it must be > threshold and have a difference average of < 10 and be < 2000 steps - if (labs(error) > threshold * planner.axis_steps_per_mm[encoderAxis] && - diffSum < 10 * (I2CPE_ERR_ARRAY_SIZE - 1) && labs(error) < 2000) { // Check for persistent error (skip) + if (ABS(error) > threshold * planner.axis_steps_per_mm[encoderAxis] && + diffSum < 10 * (I2CPE_ERR_ARRAY_SIZE - 1) && ABS(error) < 2000) { // Check for persistent error (skip) errPrst[errPrstIdx++] = error; // Error must persist for I2CPE_ERR_PRST_ARRAY_SIZE error cycles. This also serves to improve the average accuracy if (errPrstIdx >= I2CPE_ERR_PRST_ARRAY_SIZE) { float sumP = 0; @@ -193,14 +193,14 @@ errPrstIdx = 0; } #else - if (labs(error) > threshold * planner.axis_steps_per_mm[encoderAxis]) { + if (ABS(error) > threshold * planner.axis_steps_per_mm[encoderAxis]) { //SERIAL_ECHOLN(error); //SERIAL_ECHOLN(position); thermalManager.babystepsTodo[encoderAxis] = -LROUND(error / 2); } #endif - if (labs(error) > I2CPE_ERR_CNT_THRESH * planner.axis_steps_per_mm[encoderAxis]) { + if (ABS(error) > I2CPE_ERR_CNT_THRESH * planner.axis_steps_per_mm[encoderAxis]) { const millis_t ms = millis(); if (ELAPSED(ms, nextErrorCountTime)) { SERIAL_ECHOPAIR("Large error on ", axis_codes[encoderAxis]); @@ -258,7 +258,7 @@ actual = mm_from_count(position); error = actual - target; - if (labs(error) > 10000) error = 0; // ? + if (ABS(error) > 10000) error = 0; // ? if (report) { SERIAL_ECHO(axis_codes[encoderAxis]); @@ -293,7 +293,7 @@ error = (encoderCountInStepperTicksScaled - target); //suppress discontinuities (might be caused by bad I2C readings...?) - bool suppressOutput = (labs(error - errorPrev) > 100); + const bool suppressOutput = (ABS(error - errorPrev) > 100); if (report) { SERIAL_ECHO(axis_codes[encoderAxis]); @@ -435,7 +435,7 @@ delay(250); stopCount = get_position(); - travelledDistance = mm_from_count(abs(stopCount - startCount)); + travelledDistance = mm_from_count(ABS(stopCount - startCount)); SERIAL_ECHOPAIR("Attempted to travel: ", travelDistance); SERIAL_ECHOLNPGM("mm."); diff --git a/Marlin/Marlin.h b/Marlin/Marlin.h index f05e23211..efb73d172 100644 --- a/Marlin/Marlin.h +++ b/Marlin/Marlin.h @@ -522,7 +522,7 @@ void do_blocking_move_to_xy(const float &rx, const float &ry, const float &fr_mm // Note: This won't work on SCARA since the probe offset rotates with the arm. inline bool position_is_reachable_by_probe(const float &rx, const float &ry) { return position_is_reachable(rx - (X_PROBE_OFFSET_FROM_EXTRUDER), ry - (Y_PROBE_OFFSET_FROM_EXTRUDER)) - && position_is_reachable(rx, ry, FABS(MIN_PROBE_EDGE)); + && position_is_reachable(rx, ry, ABS(MIN_PROBE_EDGE)); } #endif diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 8e8d24eb5..0ddfad5d2 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -1338,7 +1338,7 @@ bool get_target_extruder_from_command(const uint16_t code) { if (axis == X_AXIS) { // In Dual X mode hotend_offset[X] is T1's home position - float dual_max_x = max(hotend_offset[X_AXIS][1], X2_MAX_POS); + float dual_max_x = MAX(hotend_offset[X_AXIS][1], X2_MAX_POS); if (active_extruder != 0) { // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger) @@ -1349,7 +1349,7 @@ bool get_target_extruder_from_command(const uint16_t code) { // In Duplication Mode, T0 can move as far left as X_MIN_POS // but not so far to the right that T1 would move past the end soft_endstop_min[X_AXIS] = base_min_pos(X_AXIS); - soft_endstop_max[X_AXIS] = min(base_max_pos(X_AXIS), dual_max_x - duplicate_extruder_x_offset); + soft_endstop_max[X_AXIS] = MIN(base_max_pos(X_AXIS), dual_max_x - duplicate_extruder_x_offset); } else { // In other modes, T0 can move from X_MIN_POS to X_MAX_POS @@ -1385,7 +1385,7 @@ bool get_target_extruder_from_command(const uint16_t code) { case X_AXIS: case Y_AXIS: // Get a minimum radius for clamping - soft_endstop_radius = MIN3(FABS(max(soft_endstop_min[X_AXIS], soft_endstop_min[Y_AXIS])), soft_endstop_max[X_AXIS], soft_endstop_max[Y_AXIS]); + soft_endstop_radius = MIN3(ABS(MAX(soft_endstop_min[X_AXIS], soft_endstop_min[Y_AXIS])), soft_endstop_max[X_AXIS], soft_endstop_max[Y_AXIS]); soft_endstop_radius_2 = sq(soft_endstop_radius); break; #endif @@ -2124,7 +2124,7 @@ void clean_up_after_endstop_or_probe_move() { #endif if (deploy_stow_condition && unknown_condition) - do_probe_raise(max(Z_CLEARANCE_BETWEEN_PROBES, Z_CLEARANCE_DEPLOY_PROBE)); + do_probe_raise(MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_CLEARANCE_DEPLOY_PROBE)); #if ENABLED(Z_PROBE_SLED) || ENABLED(Z_PROBE_ALLEN_KEY) #if ENABLED(Z_PROBE_SLED) @@ -2389,7 +2389,7 @@ void clean_up_after_endstop_or_probe_move() { const float nz = #if ENABLED(DELTA) // Move below clip height or xy move will be aborted by do_blocking_move_to - min(current_position[Z_AXIS], delta_clip_start_height) + MIN(current_position[Z_AXIS], delta_clip_start_height) #else current_position[Z_AXIS] #endif @@ -3079,7 +3079,7 @@ static void homeaxis(const AxisEnum axis) { // When homing Z with probe respect probe clearance const float bump = axis_home_dir * ( #if HOMING_Z_WITH_PROBE - (axis == Z_AXIS && (Z_HOME_BUMP_MM)) ? max(Z_CLEARANCE_BETWEEN_PROBES, Z_HOME_BUMP_MM) : + (axis == Z_AXIS && (Z_HOME_BUMP_MM)) ? MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_HOME_BUMP_MM) : #endif home_bump_mm(axis) ); @@ -3111,7 +3111,7 @@ static void homeaxis(const AxisEnum axis) { #if ENABLED(X_DUAL_ENDSTOPS) if (axis == X_AXIS) { const bool lock_x1 = pos_dir ? (endstops.x_endstop_adj > 0) : (endstops.x_endstop_adj < 0); - const float adj = FABS(endstops.x_endstop_adj); + const float adj = ABS(endstops.x_endstop_adj); if (lock_x1) stepper.set_x_lock(true); else stepper.set_x2_lock(true); do_homing_move(axis, pos_dir ? -adj : adj); if (lock_x1) stepper.set_x_lock(false); else stepper.set_x2_lock(false); @@ -3121,7 +3121,7 @@ static void homeaxis(const AxisEnum axis) { #if ENABLED(Y_DUAL_ENDSTOPS) if (axis == Y_AXIS) { const bool lock_y1 = pos_dir ? (endstops.y_endstop_adj > 0) : (endstops.y_endstop_adj < 0); - const float adj = FABS(endstops.y_endstop_adj); + const float adj = ABS(endstops.y_endstop_adj); if (lock_y1) stepper.set_y_lock(true); else stepper.set_y2_lock(true); do_homing_move(axis, pos_dir ? -adj : adj); if (lock_y1) stepper.set_y_lock(false); else stepper.set_y2_lock(false); @@ -3131,7 +3131,7 @@ static void homeaxis(const AxisEnum axis) { #if ENABLED(Z_DUAL_ENDSTOPS) if (axis == Z_AXIS) { const bool lock_z1 = pos_dir ? (endstops.z_endstop_adj > 0) : (endstops.z_endstop_adj < 0); - const float adj = FABS(endstops.z_endstop_adj); + const float adj = ABS(endstops.z_endstop_adj); if (lock_z1) stepper.set_z_lock(true); else stepper.set_z2_lock(true); do_homing_move(axis, pos_dir ? -adj : adj); if (lock_z1) stepper.set_z_lock(false); else stepper.set_z2_lock(false); @@ -3333,7 +3333,7 @@ inline void gcode_G0_G1( if (fwretract.autoretract_enabled && parser.seen('E') && !(parser.seen('X') || parser.seen('Y') || parser.seen('Z'))) { const float echange = destination[E_AXIS] - current_position[E_AXIS]; // Is this a retract or prime move? - if (WITHIN(FABS(echange), MIN_AUTORETRACT, MAX_AUTORETRACT) && fwretract.retracted[active_extruder] == (echange > 0.0)) { + if (WITHIN(ABS(echange), MIN_AUTORETRACT, MAX_AUTORETRACT) && fwretract.retracted[active_extruder] == (echange > 0.0)) { current_position[E_AXIS] = destination[E_AXIS]; // Hide a G1-based retract/prime from calculations sync_plan_position_e(); // AND from the planner return fwretract.retract(echange < 0.0); // Firmware-based retract/prime (double-retract ignored) @@ -3699,7 +3699,7 @@ inline void gcode_G4() { const float mlx = max_length(X_AXIS), mly = max_length(Y_AXIS), mlratio = mlx > mly ? mly / mlx : mlx / mly, - fr_mm_s = min(homing_feedrate(X_AXIS), homing_feedrate(Y_AXIS)) * SQRT(sq(mlratio) + 1.0); + fr_mm_s = MIN(homing_feedrate(X_AXIS), homing_feedrate(Y_AXIS)) * SQRT(sq(mlratio) + 1.0); #if ENABLED(SENSORLESS_HOMING) sensorless_homing_per_axis(X_AXIS); @@ -4423,7 +4423,7 @@ void home_all_axes() { gcode_G28(true); } } // switch(state) if (state == MeshNext) { - SERIAL_PROTOCOLPAIR("MBL G29 point ", min(mbl_probe_index, GRID_MAX_POINTS)); + SERIAL_PROTOCOLPAIR("MBL G29 point ", MIN(mbl_probe_index, GRID_MAX_POINTS)); SERIAL_PROTOCOLLNPAIR(" of ", int(GRID_MAX_POINTS)); } @@ -4855,7 +4855,7 @@ void home_all_axes() { gcode_G28(true); } if (verbose_level || seenQ) { SERIAL_PROTOCOLPGM("Manual G29 "); if (g29_in_progress) { - SERIAL_PROTOCOLPAIR("point ", min(abl_probe_index + 1, abl_points)); + SERIAL_PROTOCOLPAIR("point ", MIN(abl_probe_index + 1, abl_points)); SERIAL_PROTOCOLLNPAIR(" of ", abl_points); } else @@ -6126,7 +6126,7 @@ void home_all_axes() { gcode_G28(true); } float retract_mm[XYZ]; LOOP_XYZ(i) { float dist = destination[i] - current_position[i]; - retract_mm[i] = FABS(dist) < G38_MINIMUM_MOVE ? 0 : home_bump_mm((AxisEnum)i) * (dist > 0 ? -1 : 1); + retract_mm[i] = ABS(dist) < G38_MINIMUM_MOVE ? 0 : home_bump_mm((AxisEnum)i) * (dist > 0 ? -1 : 1); } #endif @@ -6190,7 +6190,7 @@ void home_all_axes() { gcode_G28(true); } // If any axis has enough movement, do the move LOOP_XYZ(i) - if (FABS(destination[i] - current_position[i]) >= G38_MINIMUM_MOVE) { + if (ABS(destination[i] - current_position[i]) >= G38_MINIMUM_MOVE) { if (!parser.seenval('F')) feedrate_mm_s = homing_feedrate((AxisEnum)i); // If G38.2 fails throw an error if (!G38_run_probe() && is_38_2) { @@ -7632,7 +7632,7 @@ inline void gcode_M42() { 0.1250000000 * (DELTA_PRINTABLE_RADIUS), 0.3333333333 * (DELTA_PRINTABLE_RADIUS) #else - 5.0, 0.125 * min(X_BED_SIZE, Y_BED_SIZE) + 5.0, 0.125 * MIN(X_BED_SIZE, Y_BED_SIZE) #endif ); @@ -7946,14 +7946,14 @@ inline void gcode_M105() { fanSpeeds[p] = new_fanSpeeds[p]; break; default: - new_fanSpeeds[p] = min(t, 255); + new_fanSpeeds[p] = MIN(t, 255); break; } return; } #endif // EXTRA_FAN_SPEED const uint16_t s = parser.ushortval('S', 255); - fanSpeeds[p] = min(s, 255); + fanSpeeds[p] = MIN(s, 255); } } @@ -8122,7 +8122,7 @@ inline void gcode_M109() { #if TEMP_RESIDENCY_TIME > 0 - const float temp_diff = FABS(target_temp - temp); + const float temp_diff = ABS(target_temp - temp); if (!residency_start_ms) { // Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time. @@ -8268,7 +8268,7 @@ inline void gcode_M109() { #if TEMP_BED_RESIDENCY_TIME > 0 - const float temp_diff = FABS(target_temp - temp); + const float temp_diff = ABS(target_temp - temp); if (!residency_start_ms) { // Start the TEMP_BED_RESIDENCY_TIME timer when we reach target temp for the first time. @@ -8910,7 +8910,7 @@ inline void gcode_M121() { endstops.enable_globally(false); } inline void gcode_M125() { // Initial retract before move to filament change position - const float retract = -FABS(parser.seen('L') ? parser.value_axis_units(E_AXIS) : 0 + const float retract = -ABS(parser.seen('L') ? parser.value_axis_units(E_AXIS) : 0 #ifdef PAUSE_PARK_RETRACT_LENGTH + (PAUSE_PARK_RETRACT_LENGTH) #endif @@ -10500,7 +10500,7 @@ inline void gcode_M502() { #endif // Initial retract before move to filament change position - const float retract = -FABS(parser.seen('E') ? parser.value_axis_units(E_AXIS) : 0 + const float retract = -ABS(parser.seen('E') ? parser.value_axis_units(E_AXIS) : 0 #ifdef PAUSE_PARK_RETRACT_LENGTH + (PAUSE_PARK_RETRACT_LENGTH) #endif @@ -10519,14 +10519,14 @@ inline void gcode_M502() { #endif // Unload filament - const float unload_length = -FABS(parser.seen('U') ? parser.value_axis_units(E_AXIS) : + const float unload_length = -ABS(parser.seen('U') ? parser.value_axis_units(E_AXIS) : filament_change_unload_length[active_extruder]); // Slow load filament constexpr float slow_load_length = FILAMENT_CHANGE_SLOW_LOAD_LENGTH; // Fast load filament - const float fast_load_length = FABS(parser.seen('L') ? parser.value_axis_units(E_AXIS) : + const float fast_load_length = ABS(parser.seen('L') ? parser.value_axis_units(E_AXIS) : filament_change_load_length[active_extruder]); const int beep_count = parser.intval('B', @@ -10568,7 +10568,7 @@ inline void gcode_M502() { // Unload length if (parser.seen('U')) { - filament_change_unload_length[target_extruder] = FABS(parser.value_axis_units(E_AXIS)); + filament_change_unload_length[target_extruder] = ABS(parser.value_axis_units(E_AXIS)); #if ENABLED(PREVENT_LENGTHY_EXTRUDE) NOMORE(filament_change_unload_length[target_extruder], EXTRUDE_MAXLENGTH); #endif @@ -10576,7 +10576,7 @@ inline void gcode_M502() { // Load length if (parser.seen('L')) { - filament_change_load_length[target_extruder] = FABS(parser.value_axis_units(E_AXIS)); + filament_change_load_length[target_extruder] = ABS(parser.value_axis_units(E_AXIS)); #if ENABLED(PREVENT_LENGTHY_EXTRUDE) NOMORE(filament_change_load_length[target_extruder], EXTRUDE_MAXLENGTH); #endif @@ -10620,7 +10620,7 @@ inline void gcode_M502() { case DXC_AUTO_PARK_MODE: break; case DXC_DUPLICATION_MODE: - if (parser.seen('X')) duplicate_extruder_x_offset = max(parser.value_linear_units(), X2_MIN_POS - x_home_pos(0)); + if (parser.seen('X')) duplicate_extruder_x_offset = MAX(parser.value_linear_units(), X2_MIN_POS - x_home_pos(0)); if (parser.seen('R')) duplicate_extruder_temp_offset = parser.value_celsius_diff(); SERIAL_ECHO_START(); SERIAL_ECHOPGM(MSG_HOTEND_OFFSET); @@ -10691,16 +10691,16 @@ inline void gcode_M502() { // Lift Z axis if (park_point.z > 0) - do_blocking_move_to_z(min(current_position[Z_AXIS] + park_point.z, Z_MAX_POS), NOZZLE_PARK_Z_FEEDRATE); + do_blocking_move_to_z(MIN(current_position[Z_AXIS] + park_point.z, Z_MAX_POS), NOZZLE_PARK_Z_FEEDRATE); constexpr float slow_load_length = FILAMENT_CHANGE_SLOW_LOAD_LENGTH; - const float fast_load_length = FABS(parser.seen('L') ? parser.value_axis_units(E_AXIS) : filament_change_load_length[active_extruder]); + const float fast_load_length = ABS(parser.seen('L') ? parser.value_axis_units(E_AXIS) : filament_change_load_length[active_extruder]); load_filament(slow_load_length, fast_load_length, ADVANCED_PAUSE_PURGE_LENGTH, FILAMENT_CHANGE_ALERT_BEEPS, true, thermalManager.wait_for_heating(target_extruder), ADVANCED_PAUSE_MODE_LOAD_FILAMENT); // Restore Z axis if (park_point.z > 0) - do_blocking_move_to_z(max(current_position[Z_AXIS] - park_point.z, 0), NOZZLE_PARK_Z_FEEDRATE); + do_blocking_move_to_z(MAX(current_position[Z_AXIS] - park_point.z, 0), NOZZLE_PARK_Z_FEEDRATE); #if EXTRUDERS > 1 // Restore toolhead if it was changed @@ -10751,7 +10751,7 @@ inline void gcode_M502() { // Lift Z axis if (park_point.z > 0) - do_blocking_move_to_z(min(current_position[Z_AXIS] + park_point.z, Z_MAX_POS), NOZZLE_PARK_Z_FEEDRATE); + do_blocking_move_to_z(MIN(current_position[Z_AXIS] + park_point.z, Z_MAX_POS), NOZZLE_PARK_Z_FEEDRATE); // Unload filament #if EXTRUDERS > 1 && ENABLED(FILAMENT_UNLOAD_ALL_EXTRUDERS) @@ -10765,7 +10765,7 @@ inline void gcode_M502() { #endif { // Unload length - const float unload_length = -FABS(parser.seen('U') ? parser.value_axis_units(E_AXIS) : + const float unload_length = -ABS(parser.seen('U') ? parser.value_axis_units(E_AXIS) : filament_change_unload_length[target_extruder]); unload_filament(unload_length, true, ADVANCED_PAUSE_MODE_UNLOAD_FILAMENT); @@ -10773,7 +10773,7 @@ inline void gcode_M502() { // Restore Z axis if (park_point.z > 0) - do_blocking_move_to_z(max(current_position[Z_AXIS] - park_point.z, 0), NOZZLE_PARK_Z_FEEDRATE); + do_blocking_move_to_z(MAX(current_position[Z_AXIS] - park_point.z, 0), NOZZLE_PARK_Z_FEEDRATE); #if EXTRUDERS > 1 // Restore toolhead if it was changed @@ -12635,7 +12635,7 @@ void ok_to_send() { #endif gridx = gx; - nextx = min(gridx + 1, ABL_BG_POINTS_X - 1); + nextx = MIN(gridx + 1, ABL_BG_POINTS_X - 1); } if (last_y != ry || last_gridx != gridx) { @@ -12652,7 +12652,7 @@ void ok_to_send() { #endif gridy = gy; - nexty = min(gridy + 1, ABL_BG_POINTS_Y - 1); + nexty = MIN(gridy + 1, ABL_BG_POINTS_Y - 1); } if (last_gridx != gridx || last_gridy != gridy) { @@ -12676,7 +12676,7 @@ void ok_to_send() { /* static float last_offset = 0; - if (FABS(last_offset - offset) > 0.2) { + if (ABS(last_offset - offset) > 0.2) { SERIAL_ECHOPGM("Sudden Shift at "); SERIAL_ECHOPAIR("x=", rx); SERIAL_ECHOPAIR(" / ", bilinear_grid_spacing[X_AXIS]); @@ -12799,7 +12799,7 @@ void ok_to_send() { const float centered_extent = delta[A_AXIS]; cartesian[Y_AXIS] = DELTA_PRINTABLE_RADIUS; inverse_kinematics(cartesian); - return FABS(centered_extent - delta[A_AXIS]); + return ABS(centered_extent - delta[A_AXIS]); } /** @@ -12972,7 +12972,7 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) { // If the move is very short, check the E move distance // No E move either? Game over. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff)); - if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = FABS(ediff); + if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(ediff); if (UNEAR_ZERO(cartesian_mm)) return; // The length divided by the segment size @@ -13042,7 +13042,7 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) { #define MBL_SEGMENT_END(A) (current_position[_AXIS(A)] + (destination[_AXIS(A)] - current_position[_AXIS(A)]) * normalized_dist) float normalized_dist, end[XYZE]; - const int8_t gcx = max(cx1, cx2), gcy = max(cy1, cy2); + const int8_t gcx = MAX(cx1, cx2), gcy = MAX(cy1, cy2); // Crosses on the X and not already split on this X? // The x_splits flags are insurance against rounding errors. @@ -13110,7 +13110,7 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) { #define LINE_SEGMENT_END(A) (current_position[_AXIS(A)] + (destination[_AXIS(A)] - current_position[_AXIS(A)]) * normalized_dist) float normalized_dist, end[XYZE]; - const int8_t gcx = max(cx1, cx2), gcy = max(cy1, cy2); + const int8_t gcx = MAX(cx1, cx2), gcy = MAX(cy1, cy2); // Crosses on the X and not already split on this X? // The x_splits flags are insurance against rounding errors. @@ -13203,7 +13203,7 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) { // If the move is very short, check the E move distance // No E move either? Game over. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff)); - if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = FABS(ediff); + if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(ediff); if (UNEAR_ZERO(cartesian_mm)) return true; // Minimum number of seconds to move the given distance @@ -13476,7 +13476,7 @@ void prepare_move_to_destination() { } #endif // PREVENT_COLD_EXTRUSION #if ENABLED(PREVENT_LENGTHY_EXTRUDE) - if (FABS(destination[E_AXIS] - current_position[E_AXIS]) * planner.e_factor[active_extruder] > (EXTRUDE_MAXLENGTH)) { + if (ABS(destination[E_AXIS] - current_position[E_AXIS]) * planner.e_factor[active_extruder] > (EXTRUDE_MAXLENGTH)) { current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part SERIAL_ECHO_START(); SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP); @@ -13558,7 +13558,7 @@ void prepare_move_to_destination() { angular_travel = RADIANS(360); const float flat_mm = radius * angular_travel, - mm_of_travel = linear_travel ? HYPOT(flat_mm, linear_travel) : FABS(flat_mm); + mm_of_travel = linear_travel ? HYPOT(flat_mm, linear_travel) : ABS(flat_mm); if (mm_of_travel < 0.001) return; uint16_t segments = FLOOR(mm_of_travel / (MM_PER_ARC_SEGMENT)); diff --git a/Marlin/Max7219_Debug_LEDs.cpp b/Marlin/Max7219_Debug_LEDs.cpp index e47796bb5..ad2cf3eff 100644 --- a/Marlin/Max7219_Debug_LEDs.cpp +++ b/Marlin/Max7219_Debug_LEDs.cpp @@ -342,8 +342,8 @@ void Max7219_idle_tasks() { NOMORE(current_depth, 16); // if the BLOCK_BUFFER_SIZE is greater than 16, two lines // of LEDs is enough to see if the buffer is draining - const uint8_t st = min(current_depth, last_depth), - en = max(current_depth, last_depth); + const uint8_t st = MIN(current_depth, last_depth), + en = MAX(current_depth, last_depth); if (current_depth < last_depth) for (uint8_t i = st; i <= en; i++) // clear the highest order LEDs Max7219_LED_Off(MAX7219_DEBUG_STEPPER_QUEUE + (i & 1), i / 2); diff --git a/Marlin/digipot_mcp4018.cpp b/Marlin/digipot_mcp4018.cpp index 06622d057..e29e929e9 100644 --- a/Marlin/digipot_mcp4018.cpp +++ b/Marlin/digipot_mcp4018.cpp @@ -89,7 +89,7 @@ static void i2c_send(const uint8_t channel, const byte v) { // This is for the MCP4018 I2C based digipot void digipot_i2c_set_current(uint8_t channel, float current) { - i2c_send(channel, current_to_wiper(min(max(current, 0.0f), float(DIGIPOT_A4988_MAX_CURRENT)))); + i2c_send(channel, current_to_wiper(MIN(MAX(current, 0.0f), float(DIGIPOT_A4988_MAX_CURRENT)))); } void digipot_i2c_init() { diff --git a/Marlin/digipot_mcp4451.cpp b/Marlin/digipot_mcp4451.cpp index d79915cc9..8e372220c 100644 --- a/Marlin/digipot_mcp4451.cpp +++ b/Marlin/digipot_mcp4451.cpp @@ -50,7 +50,7 @@ static void i2c_send(const byte addr, const byte a, const byte b) { // This is for the MCP4451 I2C based digipot void digipot_i2c_set_current(uint8_t channel, float current) { - current = min((float) max(current, 0.0f), DIGIPOT_I2C_MAX_CURRENT); + current = MIN((float) MAX(current, 0.0f), DIGIPOT_I2C_MAX_CURRENT); // these addresses are specific to Azteeg X3 Pro, can be set to others, // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1 byte addr = 0x2C; // channel 0-3 diff --git a/Marlin/least_squares_fit.cpp b/Marlin/least_squares_fit.cpp index 66821ce58..9e59804f0 100644 --- a/Marlin/least_squares_fit.cpp +++ b/Marlin/least_squares_fit.cpp @@ -59,7 +59,7 @@ int finish_incremental_LSF(struct linear_fit_data *lsf) { lsf->xzbar = lsf->xzbar / N - lsf->xbar * lsf->zbar; const float DD = lsf->x2bar * lsf->y2bar - sq(lsf->xybar); - if (FABS(DD) <= 1e-10 * (lsf->max_absx + lsf->max_absy)) + if (ABS(DD) <= 1e-10 * (lsf->max_absx + lsf->max_absy)) return 1; lsf->A = (lsf->yzbar * lsf->xybar - lsf->xzbar * lsf->y2bar) / DD; diff --git a/Marlin/least_squares_fit.h b/Marlin/least_squares_fit.h index 9ed923ab4..68aa62b9c 100644 --- a/Marlin/least_squares_fit.h +++ b/Marlin/least_squares_fit.h @@ -65,8 +65,8 @@ void inline incremental_WLSF(struct linear_fit_data *lsf, const float &x, const lsf->xzbar += w * x * z; lsf->yzbar += w * y * z; lsf->N += w; - lsf->max_absx = max(FABS(w * x), lsf->max_absx); - lsf->max_absy = max(FABS(w * y), lsf->max_absy); + lsf->max_absx = MAX(ABS(w * x), lsf->max_absx); + lsf->max_absy = MAX(ABS(w * y), lsf->max_absy); } void inline incremental_LSF(struct linear_fit_data *lsf, const float &x, const float &y, const float &z) { @@ -79,8 +79,8 @@ void inline incremental_LSF(struct linear_fit_data *lsf, const float &x, const f lsf->xybar += x * y; lsf->xzbar += x * z; lsf->yzbar += y * z; - lsf->max_absx = max(FABS(x), lsf->max_absx); - lsf->max_absy = max(FABS(y), lsf->max_absy); + lsf->max_absx = MAX(ABS(x), lsf->max_absx); + lsf->max_absy = MAX(ABS(y), lsf->max_absy); lsf->N += 1.0; } diff --git a/Marlin/macros.h b/Marlin/macros.h index bb006fe48..6b8e87812 100644 --- a/Marlin/macros.h +++ b/Marlin/macros.h @@ -123,7 +123,7 @@ #define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+') #define COUNT(a) (sizeof(a)/sizeof(*a)) #define ZERO(a) memset(a,0,sizeof(a)) -#define COPY(a,b) memcpy(a,b,min(sizeof(a),sizeof(b))) +#define COPY(a,b) memcpy(a,b,MIN(sizeof(a),sizeof(b))) // Macros for initializing arrays #define ARRAY_6(v1, v2, v3, v4, v5, v6, ...) { v1, v2, v3, v4, v5, v6 } @@ -174,12 +174,48 @@ #define CEILING(x,y) (((x) + (y) - 1) / (y)) -#define MIN3(a, b, c) min(min(a, b), c) -#define MIN4(a, b, c, d) min(MIN3(a, b, c), d) -#define MIN5(a, b, c, d, e) min(MIN4(a, b, c, d), e) -#define MAX3(a, b, c) max(max(a, b), c) -#define MAX4(a, b, c, d) max(MAX3(a, b, c), d) -#define MAX5(a, b, c, d, e) max(MAX4(a, b, c, d), e) +// Avoid double evaluation of arguments on MIN/MAX/ABS +#undef MIN +#undef MAX +#undef ABS +#ifdef __cplusplus + + // C++11 solution that is standards compliant. Return type is deduced automatically + template static inline constexpr auto MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) { + return lhs < rhs ? lhs : rhs; + } + template static inline constexpr auto MAX(const L lhs, const R rhs) -> decltype(lhs + rhs){ + return lhs > rhs ? lhs : rhs; + } + template static inline constexpr const T ABS(const T v) { + return v >= 0 ? v : -v; + } +#else + + // Using GCC extensions, but Travis GCC version does not like it and gives + // "error: statement-expressions are not allowed outside functions nor in template-argument lists" + #define MIN(a, b) \ + ({__typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + _a < _b ? _a : _b;}) + + #define MAX(a, b) \ + ({__typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + _a > _b ? _a : _b;}) + + #define ABS(a) \ + ({__typeof__(a) _a = (a); \ + _a >= 0 ? _a : -_a;}) + +#endif + +#define MIN3(a, b, c) MIN(MIN(a, b), c) +#define MIN4(a, b, c, d) MIN(MIN3(a, b, c), d) +#define MIN5(a, b, c, d, e) MIN(MIN4(a, b, c, d), e) +#define MAX3(a, b, c) MAX(MAX(a, b), c) +#define MAX4(a, b, c, d) MAX(MAX3(a, b, c), d) +#define MAX5(a, b, c, d, e) MAX(MAX4(a, b, c, d), e) #define UNEAR_ZERO(x) ((x) < 0.000001) #define NEAR_ZERO(x) WITHIN(x, -0.000001, 0.000001) @@ -192,7 +228,6 @@ // Maths macros that can be overridden by HAL // #define ATAN2(y, x) atan2(y, x) -#define FABS(x) fabs(x) #define POW(x, y) pow(x, y) #define SQRT(x) sqrt(x) #define CEIL(x) ceil(x) diff --git a/Marlin/malyanlcd.cpp b/Marlin/malyanlcd.cpp index 894b8ae64..e72d188bb 100644 --- a/Marlin/malyanlcd.cpp +++ b/Marlin/malyanlcd.cpp @@ -72,7 +72,7 @@ int inbound_count; // Everything written needs the high bit set. void write_to_lcd_P(const char * const message) { char encoded_message[MAX_CURLY_COMMAND]; - uint8_t message_length = min(strlen_P(message), sizeof(encoded_message)); + uint8_t message_length = MIN(strlen_P(message), sizeof(encoded_message)); for (uint8_t i = 0; i < message_length; i++) encoded_message[i] = pgm_read_byte(&message[i]) | 0x80; @@ -82,7 +82,7 @@ void write_to_lcd_P(const char * const message) { void write_to_lcd(const char * const message) { char encoded_message[MAX_CURLY_COMMAND]; - const uint8_t message_length = min(strlen(message), sizeof(encoded_message)); + const uint8_t message_length = MIN(strlen(message), sizeof(encoded_message)); for (uint8_t i = 0; i < message_length; i++) encoded_message[i] = message[i] | 0x80; diff --git a/Marlin/nozzle.cpp b/Marlin/nozzle.cpp index da43e264b..8bff692e4 100644 --- a/Marlin/nozzle.cpp +++ b/Marlin/nozzle.cpp @@ -78,7 +78,7 @@ do_blocking_move_to(start.x, start.y, start.z); const uint8_t zigs = objects << 1; - const bool horiz = FABS(diffx) >= FABS(diffy); // Do a horizontal wipe? + const bool horiz = ABS(diffx) >= ABS(diffy); // Do a horizontal wipe? const float P = (horiz ? diffx : diffy) / zigs; // Period of each zig / zag const point_t *side; for (uint8_t j = 0; j < strokes; j++) { @@ -171,11 +171,11 @@ break; case 2: // Raise by Z-park height - do_blocking_move_to_z(min(current_position[Z_AXIS] + park.z, Z_MAX_POS), fr_z); + do_blocking_move_to_z(MIN(current_position[Z_AXIS] + park.z, Z_MAX_POS), fr_z); break; default: // Raise to at least the Z-park height - do_blocking_move_to_z(max(park.z, current_position[Z_AXIS]), fr_z); + do_blocking_move_to_z(MAX(park.z, current_position[Z_AXIS]), fr_z); } do_blocking_move_to_xy(park.x, park.y, fr_xy); diff --git a/Marlin/planner.cpp b/Marlin/planner.cpp index 0e9f4fccf..f504665f0 100644 --- a/Marlin/planner.cpp +++ b/Marlin/planner.cpp @@ -745,7 +745,7 @@ void Planner::calculate_trapezoid_for_block(block_t* const block, const float &e if (plateau_steps < 0) { accelerate_steps = CEIL(intersection_distance(initial_rate, final_rate, accel, block->step_event_count)); NOLESS(accelerate_steps, 0); // Check limits due to numerical round-off - accelerate_steps = min((uint32_t)accelerate_steps, block->step_event_count);//(We can cast here to unsigned, because the above line ensures that we are above zero) + accelerate_steps = MIN((uint32_t)accelerate_steps, block->step_event_count);//(We can cast here to unsigned, because the above line ensures that we are above zero) plateau_steps = 0; #if ENABLED(BEZIER_JERK_CONTROL) @@ -809,7 +809,7 @@ void Planner::reverse_pass_kernel(block_t* const current, const block_t* const n // for max allowable speed if block is decelerating and nominal length is false. const float new_entry_speed = (TEST(current->flag, BLOCK_BIT_NOMINAL_LENGTH) || max_entry_speed <= next->entry_speed) ? max_entry_speed - : min(max_entry_speed, max_allowable_speed(-current->acceleration, next->entry_speed, current->millimeters)); + : MIN(max_entry_speed, max_allowable_speed(-current->acceleration, next->entry_speed, current->millimeters)); if (new_entry_speed != current->entry_speed) { current->entry_speed = new_entry_speed; SBI(current->flag, BLOCK_BIT_RECALCULATE); @@ -835,7 +835,7 @@ void Planner::reverse_pass() { // for max allowable speed if block is decelerating and nominal length is false. const float new_entry_speed = TEST(current->flag, BLOCK_BIT_NOMINAL_LENGTH) ? max_entry_speed - : min(max_entry_speed, max_allowable_speed(-current->acceleration, MINIMUM_PLANNER_SPEED, current->millimeters)); + : MIN(max_entry_speed, max_allowable_speed(-current->acceleration, MINIMUM_PLANNER_SPEED, current->millimeters)); if (current->entry_speed != new_entry_speed) { current->entry_speed = new_entry_speed; SBI(current->flag, BLOCK_BIT_RECALCULATE); @@ -860,7 +860,7 @@ void Planner::forward_pass_kernel(const block_t* const previous, block_t* const // guaranteed to be reached. No need to recheck. if (!TEST(previous->flag, BLOCK_BIT_NOMINAL_LENGTH)) { if (previous->entry_speed < current->entry_speed) { - const float new_entry_speed = min(current->entry_speed, max_allowable_speed(-previous->acceleration, previous->entry_speed, previous->millimeters)); + const float new_entry_speed = MIN(current->entry_speed, max_allowable_speed(-previous->acceleration, previous->entry_speed, previous->millimeters)); // Check for junction speed change if (current->entry_speed != new_entry_speed) { current->entry_speed = new_entry_speed; @@ -1360,7 +1360,7 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] } #endif // PREVENT_COLD_EXTRUSION #if ENABLED(PREVENT_LENGTHY_EXTRUDE) - if (labs(de * e_factor[extruder]) > (int32_t)axis_steps_per_mm[E_AXIS_N] * (EXTRUDE_MAXLENGTH)) { // It's not important to get max. extrusion length in a precision < 1mm, so save some cycles and cast to int + if (ABS(de * e_factor[extruder]) > (int32_t)axis_steps_per_mm[E_AXIS_N] * (EXTRUDE_MAXLENGTH)) { // It's not important to get max. extrusion length in a precision < 1mm, so save some cycles and cast to int position[E_AXIS] = target[E_AXIS]; // Behave as if the move really took place, but ignore E part #if HAS_POSITION_FLOAT position_float[E_AXIS] = target_float[E_AXIS]; @@ -1401,7 +1401,7 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] if (de < 0) SBI(dm, E_AXIS); const float esteps_float = de * e_factor[extruder]; - const int32_t esteps = abs(esteps_float) + 0.5; + const int32_t esteps = ABS(esteps_float) + 0.5; // Wait for the next available block uint8_t next_buffer_head; @@ -1416,26 +1416,26 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] // Number of steps for each axis // See http://www.corexy.com/theory.html #if CORE_IS_XY - block->steps[A_AXIS] = labs(da + db); - block->steps[B_AXIS] = labs(da - db); - block->steps[Z_AXIS] = labs(dc); + block->steps[A_AXIS] = ABS(da + db); + block->steps[B_AXIS] = ABS(da - db); + block->steps[Z_AXIS] = ABS(dc); #elif CORE_IS_XZ - block->steps[A_AXIS] = labs(da + dc); - block->steps[Y_AXIS] = labs(db); - block->steps[C_AXIS] = labs(da - dc); + block->steps[A_AXIS] = ABS(da + dc); + block->steps[Y_AXIS] = ABS(db); + block->steps[C_AXIS] = ABS(da - dc); #elif CORE_IS_YZ - block->steps[X_AXIS] = labs(da); - block->steps[B_AXIS] = labs(db + dc); - block->steps[C_AXIS] = labs(db - dc); + block->steps[X_AXIS] = ABS(da); + block->steps[B_AXIS] = ABS(db + dc); + block->steps[C_AXIS] = ABS(db - dc); #elif IS_SCARA - block->steps[A_AXIS] = labs(da); - block->steps[B_AXIS] = labs(db); - block->steps[Z_AXIS] = labs(dc); + block->steps[A_AXIS] = ABS(da); + block->steps[B_AXIS] = ABS(db); + block->steps[Z_AXIS] = ABS(dc); #else // default non-h-bot planning - block->steps[A_AXIS] = labs(da); - block->steps[B_AXIS] = labs(db); - block->steps[C_AXIS] = labs(dc); + block->steps[A_AXIS] = ABS(da); + block->steps[B_AXIS] = ABS(db); + block->steps[C_AXIS] = ABS(dc); #endif block->steps[E_AXIS] = esteps; @@ -1636,7 +1636,7 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] delta_mm[E_AXIS] = esteps_float * steps_to_mm[E_AXIS_N]; if (block->steps[A_AXIS] < MIN_STEPS_PER_SEGMENT && block->steps[B_AXIS] < MIN_STEPS_PER_SEGMENT && block->steps[C_AXIS] < MIN_STEPS_PER_SEGMENT) { - block->millimeters = FABS(delta_mm[E_AXIS]); + block->millimeters = ABS(delta_mm[E_AXIS]); } else if (!millimeters) { block->millimeters = SQRT( @@ -1727,7 +1727,7 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] // Calculate and limit speed in mm/sec for each axis float current_speed[NUM_AXIS], speed_factor = 1.0; // factor <1 decreases speed LOOP_XYZE(i) { - const float cs = FABS((current_speed[i] = delta_mm[i] * inverse_secs)); + const float cs = ABS((current_speed[i] = delta_mm[i] * inverse_secs)); #if ENABLED(DISTINCT_E_FACTORS) if (i == E_AXIS) i += extruder; #endif @@ -1765,7 +1765,7 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] const uint32_t max_x_segment_time = MAX3(xs0, xs1, xs2), max_y_segment_time = MAX3(ys0, ys1, ys2), - min_xy_segment_time = min(max_x_segment_time, max_y_segment_time); + min_xy_segment_time = MIN(max_x_segment_time, max_y_segment_time); if (min_xy_segment_time < MAX_FREQ_TIME_US) { const float low_sf = speed_factor * min_xy_segment_time / (MAX_FREQ_TIME_US); NOMORE(speed_factor, low_sf); @@ -1949,7 +1949,7 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] vmax_junction = MINIMUM_PLANNER_SPEED; } else { - junction_cos_theta = max(junction_cos_theta, -0.999999); // Check for numerical round-off to avoid divide by zero. + junction_cos_theta = MAX(junction_cos_theta, -0.999999); // Check for numerical round-off to avoid divide by zero. const float sin_theta_d2 = SQRT(0.5 * (1.0 - junction_cos_theta)); // Trig half angle identity. Always positive. // TODO: Technically, the acceleration used in calculation needs to be limited by the minimum of the @@ -1979,7 +1979,7 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] float safe_speed = block->nominal_speed; uint8_t limited = 0; LOOP_XYZE(i) { - const float jerk = FABS(current_speed[i]), maxj = max_jerk[i]; + const float jerk = ABS(current_speed[i]), maxj = max_jerk[i]; if (jerk > maxj) { if (limited) { const float mjerk = maxj * block->nominal_speed; @@ -1999,7 +1999,7 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] // The junction velocity will be shared between successive segments. Limit the junction velocity to their minimum. // Pick the smaller of the nominal speeds. Higher speed shall not be achieved at the junction during coasting. - vmax_junction = min(block->nominal_speed, previous_nominal_speed); + vmax_junction = MIN(block->nominal_speed, previous_nominal_speed); // Factor to multiply the previous / current nominal velocities to get componentwise limited velocities. float v_factor = 1; @@ -2019,9 +2019,9 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] // Calculate jerk depending on whether the axis is coasting in the same direction or reversing. const float jerk = (v_exit > v_entry) ? // coasting axis reversal - ( (v_entry > 0 || v_exit < 0) ? (v_exit - v_entry) : max(v_exit, -v_entry) ) + ( (v_entry > 0 || v_exit < 0) ? (v_exit - v_entry) : MAX(v_exit, -v_entry) ) : // v_exit <= v_entry coasting axis reversal - ( (v_entry < 0 || v_exit > 0) ? (v_entry - v_exit) : max(-v_exit, v_entry) ); + ( (v_entry < 0 || v_exit > 0) ? (v_entry - v_exit) : MAX(-v_exit, v_entry) ); if (jerk > max_jerk[axis]) { v_factor *= max_jerk[axis] / jerk; @@ -2048,7 +2048,7 @@ void Planner::_buffer_steps(const int32_t (&target)[XYZE] const float v_allowable = max_allowable_speed(-block->acceleration, MINIMUM_PLANNER_SPEED, block->millimeters); // If stepper ISR is disabled, this indicates buffer_segment wants to add a split block. // In this case start with the max. allowed speed to avoid an interrupted first move. - block->entry_speed = STEPPER_ISR_ENABLED() ? MINIMUM_PLANNER_SPEED : min(vmax_junction, v_allowable); + block->entry_speed = STEPPER_ISR_ENABLED() ? MINIMUM_PLANNER_SPEED : MIN(vmax_junction, v_allowable); // Initialize planner efficiency flags // Set flag if block will always reach maximum junction speed regardless of entry/exit speeds. diff --git a/Marlin/planner.h b/Marlin/planner.h index b754dc4b4..35ce6c280 100644 --- a/Marlin/planner.h +++ b/Marlin/planner.h @@ -706,7 +706,7 @@ class Planner { }; -#define PLANNER_XY_FEEDRATE() (min(planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS])) +#define PLANNER_XY_FEEDRATE() (MIN(planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS])) extern Planner planner; diff --git a/Marlin/planner_bezier.cpp b/Marlin/planner_bezier.cpp index d6dd35f1c..6fc80c9ad 100644 --- a/Marlin/planner_bezier.cpp +++ b/Marlin/planner_bezier.cpp @@ -65,7 +65,7 @@ inline static float eval_bezier(float a, float b, float c, float d, float t) { * We approximate Euclidean distance with the sum of the coordinates * offset (so-called "norm 1"), which is quicker to compute. */ -inline static float dist1(float x1, float y1, float x2, float y2) { return FABS(x1 - x2) + FABS(y1 - y2); } +inline static float dist1(float x1, float y1, float x2, float y2) { return ABS(x1 - x2) + ABS(y1 - y2); } /** * The algorithm for computing the step is loosely based on the one in Kig diff --git a/Marlin/servo.cpp b/Marlin/servo.cpp index 7a1c2b8c6..e1d11573a 100644 --- a/Marlin/servo.cpp +++ b/Marlin/servo.cpp @@ -259,7 +259,7 @@ int8_t Servo::attach(const int pin, const int min, const int max) { if (pin > 0) servo_info[this->servoIndex].Pin.nbr = pin; pinMode(servo_info[this->servoIndex].Pin.nbr, OUTPUT); // set servo pin to output - // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128 + // todo min/max check: ABS(min - MIN_PULSE_WIDTH) /4 < 128 this->min = (MIN_PULSE_WIDTH - min) / 4; //resolution of min/max is 4 uS this->max = (MAX_PULSE_WIDTH - max) / 4; diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 6e4021c4f..23c5e1e9c 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -792,8 +792,8 @@ void Temperature::manage_heater() { updateTemperaturesFromRawValues(); // also resets the watchdog #if ENABLED(HEATER_0_USES_MAX6675) - if (current_temperature[0] > min(HEATER_0_MAXTEMP, MAX6675_TMAX - 1.0)) max_temp_error(0); - if (current_temperature[0] < max(HEATER_0_MINTEMP, MAX6675_TMIN + .01)) min_temp_error(0); + if (current_temperature[0] > MIN(HEATER_0_MAXTEMP, MAX6675_TMAX - 1.0)) max_temp_error(0); + if (current_temperature[0] < MAX(HEATER_0_MINTEMP, MAX6675_TMIN + .01)) min_temp_error(0); #endif #if WATCH_HOTENDS || WATCH_THE_BED || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN || HEATER_IDLE_HANDLER @@ -826,7 +826,7 @@ void Temperature::manage_heater() { #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) // Make sure measured temperatures are close together - if (FABS(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) + if (ABS(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP)); #endif @@ -1078,7 +1078,7 @@ void Temperature::updateTemperaturesFromRawValues() { * a return value of 1. */ int8_t Temperature::widthFil_to_size_ratio() { - if (FABS(filament_width_nominal - filament_width_meas) <= FILWIDTH_ERROR_MARGIN) + if (ABS(filament_width_nominal - filament_width_meas) <= FILWIDTH_ERROR_MARGIN) return int(100.0 * filament_width_nominal / filament_width_meas) - 100; return 0; } diff --git a/Marlin/temperature.h b/Marlin/temperature.h index 247f27338..c30bb396c 100644 --- a/Marlin/temperature.h +++ b/Marlin/temperature.h @@ -106,7 +106,7 @@ enum ADCSensorState : char { // get all oversampled sensor readings #define MIN_ADC_ISR_LOOPS 10 -#define ACTUAL_ADC_SAMPLES max(int(MIN_ADC_ISR_LOOPS), int(SensorsReady)) +#define ACTUAL_ADC_SAMPLES MAX(int(MIN_ADC_ISR_LOOPS), int(SensorsReady)) #if HAS_PID_HEATING #define PID_K2 (1.0-PID_K1) @@ -449,7 +449,7 @@ class Temperature { #endif target_temperature_bed = #ifdef BED_MAXTEMP - min(celsius, BED_MAXTEMP) + MIN(celsius, BED_MAXTEMP) #else celsius #endif @@ -472,7 +472,7 @@ class Temperature { #endif FORCE_INLINE static bool wait_for_heating(const uint8_t e) { - return degTargetHotend(e) > TEMP_HYSTERESIS && abs(degHotend(e) - degTargetHotend(e)) > TEMP_HYSTERESIS; + return degTargetHotend(e) > TEMP_HYSTERESIS && ABS(degHotend(e) - degTargetHotend(e)) > TEMP_HYSTERESIS; } /** diff --git a/Marlin/ubl.h b/Marlin/ubl.h index 5a401df2f..62233865b 100644 --- a/Marlin/ubl.h +++ b/Marlin/ubl.h @@ -235,7 +235,7 @@ class unified_bed_leveling { const float xratio = (rx0 - mesh_index_to_xpos(x1_i)) * (1.0 / (MESH_X_DIST)), z1 = z_values[x1_i][yi]; - return z1 + xratio * (z_values[min(x1_i, GRID_MAX_POINTS_X - 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, GRID_MAX_POINTS_X - 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. } @@ -269,7 +269,7 @@ class unified_bed_leveling { const float yratio = (ry0 - mesh_index_to_ypos(y1_i)) * (1.0 / (MESH_Y_DIST)), z1 = z_values[xi][y1_i]; - return z1 + yratio * (z_values[xi][min(y1_i, GRID_MAX_POINTS_Y - 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, GRID_MAX_POINTS_Y - 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. } @@ -295,11 +295,11 @@ class unified_bed_leveling { const float z1 = calc_z0(rx0, mesh_index_to_xpos(cx), z_values[cx][cy], - mesh_index_to_xpos(cx + 1), z_values[min(cx, GRID_MAX_POINTS_X - 2) + 1][cy]); + mesh_index_to_xpos(cx + 1), z_values[MIN(cx, GRID_MAX_POINTS_X - 2) + 1][cy]); const float z2 = calc_z0(rx0, - mesh_index_to_xpos(cx), z_values[cx][min(cy, GRID_MAX_POINTS_Y - 2) + 1], - mesh_index_to_xpos(cx + 1), z_values[min(cx, GRID_MAX_POINTS_X - 2) + 1][min(cy, GRID_MAX_POINTS_Y - 2) + 1]); + mesh_index_to_xpos(cx), z_values[cx][MIN(cy, GRID_MAX_POINTS_Y - 2) + 1], + mesh_index_to_xpos(cx + 1), z_values[MIN(cx, GRID_MAX_POINTS_X - 2) + 1][MIN(cy, GRID_MAX_POINTS_Y - 2) + 1]); float z0 = calc_z0(ry0, mesh_index_to_ypos(cy), z1, diff --git a/Marlin/ubl_G29.cpp b/Marlin/ubl_G29.cpp index 48aec52b3..a4c7b3328 100644 --- a/Marlin/ubl_G29.cpp +++ b/Marlin/ubl_G29.cpp @@ -446,7 +446,7 @@ if (parser.seen('B')) { g29_card_thickness = parser.has_value() ? parser.value_float() : measure_business_card_thickness((float) Z_CLEARANCE_BETWEEN_PROBES); - if (FABS(g29_card_thickness) > 1.5) { + if (ABS(g29_card_thickness) > 1.5) { SERIAL_PROTOCOLLNPGM("?Error in Business Card measurement."); return; } @@ -791,7 +791,7 @@ save_ubl_active_state_and_disable(); // Disable bed level correction for probing do_blocking_move_to(0.5 * (MESH_MAX_X - (MESH_MIN_X)), 0.5 * (MESH_MAX_Y - (MESH_MIN_Y)), in_height); - //, min(planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS]) / 2.0); + //, MIN(planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS]) / 2.0); planner.synchronize(); SERIAL_PROTOCOLPGM("Place shim under nozzle"); @@ -811,7 +811,7 @@ do_blocking_move_to_z(current_position[Z_AXIS] + Z_CLEARANCE_BETWEEN_PROBES); - const float thickness = abs(z1 - z2); + const float thickness = ABS(z1 - z2); if (g29_verbose_level > 1) { SERIAL_PROTOCOLPGM("Business Card is "); @@ -1494,10 +1494,10 @@ #include "vector_3.h" void unified_bed_leveling::tilt_mesh_based_on_probed_grid(const bool do_3_pt_leveling) { - constexpr int16_t x_min = max(MIN_PROBE_X, MESH_MIN_X), - x_max = min(MAX_PROBE_X, MESH_MAX_X), - y_min = max(MIN_PROBE_Y, MESH_MIN_Y), - y_max = min(MAX_PROBE_Y, MESH_MAX_Y); + constexpr int16_t x_min = MAX(MIN_PROBE_X, MESH_MIN_X), + x_max = MIN(MAX_PROBE_X, MESH_MAX_X), + y_min = MAX(MIN_PROBE_Y, MESH_MIN_Y), + y_max = MIN(MAX_PROBE_Y, MESH_MAX_Y); bool abort_flag = false; @@ -1765,7 +1765,7 @@ SERIAL_ECHOPGM("Extrapolating mesh..."); - const float weight_scaled = weight_factor * max(MESH_X_DIST, MESH_Y_DIST); + const float weight_scaled = weight_factor * MAX(MESH_X_DIST, MESH_Y_DIST); for (uint8_t jx = 0; jx < GRID_MAX_POINTS_X; jx++) for (uint8_t jy = 0; jy < GRID_MAX_POINTS_Y; jy++) diff --git a/Marlin/ubl_motion.cpp b/Marlin/ubl_motion.cpp index 369c88b80..9ffdff3bb 100644 --- a/Marlin/ubl_motion.cpp +++ b/Marlin/ubl_motion.cpp @@ -382,11 +382,11 @@ inverse_kinematics(raw); // this writes delta[ABC] from raw[XYZE] // should move the feedrate scaling to scara inverse_kinematics - const float adiff = FABS(delta[A_AXIS] - scara_oldA), - bdiff = FABS(delta[B_AXIS] - scara_oldB); + const float adiff = ABS(delta[A_AXIS] - scara_oldA), + bdiff = ABS(delta[B_AXIS] - scara_oldB); scara_oldA = delta[A_AXIS]; scara_oldB = delta[B_AXIS]; - float s_feedrate = max(adiff, bdiff) * scara_feed_factor; + float s_feedrate = MAX(adiff, bdiff) * scara_feed_factor; planner.buffer_segment(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], in_raw[E_AXIS], s_feedrate, active_extruder); diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 7babb7580..eedf441d8 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -618,7 +618,7 @@ uint16_t max_display_update_time = 0; screen_changed = false; } if (screen_items > 0 && encoderLine >= screen_items - limit) { - encoderLine = max(0, screen_items - limit); + encoderLine = MAX(0, screen_items - limit); encoderPosition = encoderLine * (ENCODER_STEPS_PER_MENU_ITEM); } if (is_menu) { @@ -1570,7 +1570,7 @@ void lcd_quick_feedback(const bool clear_buttons) { * */ void _lcd_preheat(const int16_t endnum, const int16_t temph, const int16_t tempb, const int16_t fan) { - if (temph > 0) thermalManager.setTargetHotend(min(heater_maxtemp[endnum], temph), endnum); + if (temph > 0) thermalManager.setTargetHotend(MIN(heater_maxtemp[endnum], temph), endnum); #if HAS_HEATED_BED if (tempb >= 0) thermalManager.setTargetBed(tempb); #else @@ -2109,7 +2109,7 @@ void lcd_quick_feedback(const bool clear_buttons) { char UBL_LCD_GCODE[16]; const int ind = ubl_height_amount > 0 ? 9 : 10; strcpy_P(UBL_LCD_GCODE, PSTR("G29 P6 C -")); - sprintf_P(&UBL_LCD_GCODE[ind], PSTR(".%i"), abs(ubl_height_amount)); + sprintf_P(&UBL_LCD_GCODE[ind], PSTR(".%i"), ABS(ubl_height_amount)); lcd_enqueue_command(UBL_LCD_GCODE); } @@ -2431,7 +2431,7 @@ void lcd_quick_feedback(const bool clear_buttons) { if (encoderPosition) { step_scaler += (int32_t)encoderPosition; x_plot += step_scaler / (ENCODER_STEPS_PER_MENU_ITEM); - if (abs(step_scaler) >= ENCODER_STEPS_PER_MENU_ITEM) step_scaler = 0; + if (ABS(step_scaler) >= ENCODER_STEPS_PER_MENU_ITEM) step_scaler = 0; encoderPosition = 0; lcdDrawUpdate = LCDVIEW_REDRAW_NOW; } @@ -2843,7 +2843,7 @@ void lcd_quick_feedback(const bool clear_buttons) { do_blocking_move_to_xy(rx, ry); lcd_synchronize(); - move_menu_scale = max(PROBE_MANUALLY_STEP, MIN_STEPS_PER_SEGMENT / float(DEFAULT_XYZ_STEPS_PER_UNIT)); + move_menu_scale = MAX(PROBE_MANUALLY_STEP, MIN_STEPS_PER_SEGMENT / float(DEFAULT_XYZ_STEPS_PER_UNIT)); lcd_goto_screen(lcd_move_z); } @@ -3615,8 +3615,8 @@ void lcd_quick_feedback(const bool clear_buttons) { #define MINTEMP_ALL MIN3(HEATER_0_MINTEMP, HEATER_1_MINTEMP, HEATER_2_MINTEMP) #define MAXTEMP_ALL MAX3(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP) #elif HOTENDS > 1 - #define MINTEMP_ALL min(HEATER_0_MINTEMP, HEATER_1_MINTEMP) - #define MAXTEMP_ALL max(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP) + #define MINTEMP_ALL MIN(HEATER_0_MINTEMP, HEATER_1_MINTEMP) + #define MAXTEMP_ALL MAX(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP) #else #define MINTEMP_ALL HEATER_0_MINTEMP #define MAXTEMP_ALL HEATER_0_MAXTEMP @@ -5237,7 +5237,7 @@ void lcd_update() { #endif - const bool encoderPastThreshold = (abs(encoderDiff) >= ENCODER_PULSES_PER_STEP); + const bool encoderPastThreshold = (ABS(encoderDiff) >= ENCODER_PULSES_PER_STEP); if (encoderPastThreshold || lcd_clicked) { if (encoderPastThreshold) { int32_t encoderMultiplier = 1; @@ -5245,7 +5245,7 @@ void lcd_update() { #if ENABLED(ENCODER_RATE_MULTIPLIER) if (encoderRateMultiplierEnabled) { - int32_t encoderMovementSteps = abs(encoderDiff) / ENCODER_PULSES_PER_STEP; + int32_t encoderMovementSteps = ABS(encoderDiff) / ENCODER_PULSES_PER_STEP; if (lastEncoderMovementMillis) { // Note that the rate is always calculated between two passes through the diff --git a/Marlin/ultralcd_impl_DOGM.h b/Marlin/ultralcd_impl_DOGM.h index 511a0e9a4..b39bb3a1f 100644 --- a/Marlin/ultralcd_impl_DOGM.h +++ b/Marlin/ultralcd_impl_DOGM.h @@ -603,7 +603,7 @@ void lcd_implementation_clear() { } // Automatically cleared by Picture Loop name_hash = ((name_hash << 1) | (name_hash >> 7)) ^ filename[l]; // rotate, xor if (filename_scroll_hash != name_hash) { // If the hash changed... filename_scroll_hash = name_hash; // Save the new hash - filename_scroll_max = max(0, lcd_strlen(longFilename) - maxlen); // Update the scroll limit + filename_scroll_max = MAX(0, lcd_strlen(longFilename) - maxlen); // Update the scroll limit filename_scroll_pos = 0; // Reset scroll to the start lcd_status_update_delay = 8; // Don't scroll right away } diff --git a/Marlin/ultralcd_impl_HD44780.h b/Marlin/ultralcd_impl_HD44780.h index 6a5755a3a..54e734850 100644 --- a/Marlin/ultralcd_impl_HD44780.h +++ b/Marlin/ultralcd_impl_HD44780.h @@ -492,12 +492,12 @@ void lcd_printPGM_utf(const char *str, uint8_t n=LCD_WIDTH) { // Scroll the PSTR 'text' in a 'len' wide field for 'time' milliseconds at position col,line void lcd_scroll(const int16_t col, const int16_t line, const char* const text, const int16_t len, const int16_t time) { char tmp[LCD_WIDTH + 1] = {0}; - int16_t n = max(lcd_strlen_P(text) - len, 0); + const int16_t n = MAX(lcd_strlen_P(text) - len, 0); for (int16_t i = 0; i <= n; i++) { - strncpy_P(tmp, text + i, min(len, LCD_WIDTH)); + strncpy_P(tmp, text + i, MIN(len, LCD_WIDTH)); lcd.setCursor(col, line); lcd_print(tmp); - delay(time / max(n, 1)); + delay(time / MAX(n, 1)); } } @@ -1032,7 +1032,7 @@ static void lcd_implementation_status_screen() { name_hash = ((name_hash << 1) | (name_hash >> 7)) ^ filename[l]; // rotate, xor if (filename_scroll_hash != name_hash) { // If the hash changed... filename_scroll_hash = name_hash; // Save the new hash - filename_scroll_max = max(0, lcd_strlen(longFilename) - n); // Update the scroll limit + filename_scroll_max = MAX(0, lcd_strlen(longFilename) - n); // Update the scroll limit filename_scroll_pos = 0; // Reset scroll to the start lcd_status_update_delay = 8; // Don't scroll right away } @@ -1345,7 +1345,7 @@ static void lcd_implementation_status_screen() { //dump_custom_char("at entry:", &new_char); clear_custom_char(&new_char); - const uint8_t ypix = min(upper_left.y_pixel_offset + pixels_per_y_mesh_pnt, ULTRA_Y_PIXELS_PER_CHAR); + const uint8_t ypix = MIN(upper_left.y_pixel_offset + pixels_per_y_mesh_pnt, ULTRA_Y_PIXELS_PER_CHAR); for (j = upper_left.y_pixel_offset; j < ypix; j++) { i = upper_left.x_pixel_mask; for (k = 0; k < pixels_per_x_mesh_pnt; k++) {