diff --git a/Marlin/src/HAL/HAL_ESP32/HAL_Servo_ESP32.cpp b/Marlin/src/HAL/HAL_ESP32/HAL_Servo_ESP32.cpp index e702539ca9..5f2dd40e19 100644 --- a/Marlin/src/HAL/HAL_ESP32/HAL_Servo_ESP32.cpp +++ b/Marlin/src/HAL/HAL_ESP32/HAL_Servo_ESP32.cpp @@ -48,8 +48,8 @@ void Servo::detach() { ledcDetachPin(this->pin); } int Servo::read() { return this->degrees; } -void Servo::write(int degrees) { - this->degrees = constrain(degrees, MIN_ANGLE, MAX_ANGLE); +void Servo::write(int inDegrees) { + this->degrees = constrain(inDegrees, MIN_ANGLE, MAX_ANGLE); int us = map(this->degrees, MIN_ANGLE, MAX_ANGLE, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); int duty = map(us, 0, TAU_USEC, 0, MAX_COMPARE); ledcWrite(channel, duty); diff --git a/Marlin/src/HAL/shared/servo.cpp b/Marlin/src/HAL/shared/servo.cpp index af33853665..7f80730450 100644 --- a/Marlin/src/HAL/shared/servo.cpp +++ b/Marlin/src/HAL/shared/servo.cpp @@ -127,7 +127,7 @@ void Servo::writeMicroseconds(int value) { byte channel = this->servoIndex; if (channel < MAX_SERVOS) { // ensure channel is valid // ensure pulse width is valid - value = constrain(value, SERVO_MIN(), SERVO_MAX()) - (TRIM_DURATION); + LIMIT(value, SERVO_MIN(), SERVO_MAX()) - (TRIM_DURATION); value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009 CRITICAL_SECTION_START; diff --git a/Marlin/src/feature/bedlevel/abl/abl.cpp b/Marlin/src/feature/bedlevel/abl/abl.cpp index 85ec693203..c70cf62203 100644 --- a/Marlin/src/feature/bedlevel/abl/abl.cpp +++ b/Marlin/src/feature/bedlevel/abl/abl.cpp @@ -369,10 +369,10 @@ float bilinear_z_offset(const float raw[XYZ]) { cy1 = CELL_INDEX(Y, current_position[Y_AXIS]), cx2 = CELL_INDEX(X, destination[X_AXIS]), cy2 = CELL_INDEX(Y, destination[Y_AXIS]); - cx1 = constrain(cx1, 0, ABL_BG_POINTS_X - 2); - cy1 = constrain(cy1, 0, ABL_BG_POINTS_Y - 2); - cx2 = constrain(cx2, 0, ABL_BG_POINTS_X - 2); - cy2 = constrain(cy2, 0, ABL_BG_POINTS_Y - 2); + LIMIT(cx1, 0, ABL_BG_POINTS_X - 2); + LIMIT(cy1, 0, ABL_BG_POINTS_Y - 2); + LIMIT(cx2, 0, ABL_BG_POINTS_X - 2); + LIMIT(cy2, 0, ABL_BG_POINTS_Y - 2); // Start and end in the same cell? No split needed. if (cx1 == cx2 && cy1 == cy2) { diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp index d3b012da5a..4bb29d7e0b 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl_motion.cpp @@ -443,8 +443,8 @@ int8_t cell_xi = (raw[X_AXIS] - (MESH_MIN_X)) * (1.0f / (MESH_X_DIST)), cell_yi = (raw[Y_AXIS] - (MESH_MIN_Y)) * (1.0f / (MESH_Y_DIST)); - cell_xi = constrain(cell_xi, 0, (GRID_MAX_POINTS_X) - 1); - cell_yi = constrain(cell_yi, 0, (GRID_MAX_POINTS_Y) - 1); + LIMIT(cell_xi, 0, (GRID_MAX_POINTS_X) - 1); + LIMIT(cell_yi, 0, (GRID_MAX_POINTS_Y) - 1); const float x0 = mesh_index_to_xpos(cell_xi), // 64 byte table lookup avoids mul+add y0 = mesh_index_to_ypos(cell_yi); diff --git a/Marlin/src/gcode/bedlevel/G26.cpp b/Marlin/src/gcode/bedlevel/G26.cpp index 817e5646a6..b95fe8d165 100644 --- a/Marlin/src/gcode/bedlevel/G26.cpp +++ b/Marlin/src/gcode/bedlevel/G26.cpp @@ -337,9 +337,9 @@ inline bool look_for_lines_to_connect() { sx = _GET_MESH_X( i ) + (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)); // right edge ex = _GET_MESH_X(i + 1) - (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)); // left edge - sx = constrain(sx, X_MIN_POS + 1, X_MAX_POS - 1); + LIMIT(sx, X_MIN_POS + 1, X_MAX_POS - 1); sy = ey = constrain(_GET_MESH_Y(j), Y_MIN_POS + 1, Y_MAX_POS - 1); - ex = constrain(ex, X_MIN_POS + 1, X_MAX_POS - 1); + LIMIT(ex, X_MIN_POS + 1, X_MAX_POS - 1); if (position_is_reachable(sx, sy) && position_is_reachable(ex, ey)) print_line_from_here_to_there(sx, sy, g26_layer_height, ex, ey, g26_layer_height); @@ -358,8 +358,8 @@ inline bool look_for_lines_to_connect() { ey = _GET_MESH_Y(j + 1) - (INTERSECTION_CIRCLE_RADIUS - (CROSSHAIRS_SIZE)); // bottom edge sx = ex = constrain(_GET_MESH_X(i), X_MIN_POS + 1, X_MAX_POS - 1); - sy = constrain(sy, Y_MIN_POS + 1, Y_MAX_POS - 1); - ey = constrain(ey, Y_MIN_POS + 1, Y_MAX_POS - 1); + LIMIT(sy, Y_MIN_POS + 1, Y_MAX_POS - 1); + LIMIT(ey, Y_MIN_POS + 1, Y_MAX_POS - 1); if (position_is_reachable(sx, sy) && position_is_reachable(ex, ey)) print_line_from_here_to_there(sx, sy, g26_layer_height, ex, ey, g26_layer_height); @@ -832,10 +832,10 @@ void GcodeSuite::G26() { // Check to make sure this segment is entirely on the bed, skip if not. if (!position_is_reachable(rx, ry) || !position_is_reachable(xe, ye)) continue; #else // not, we need to skip - rx = constrain(rx, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops - ry = constrain(ry, Y_MIN_POS + 1, Y_MAX_POS - 1); - xe = constrain(xe, X_MIN_POS + 1, X_MAX_POS - 1); - ye = constrain(ye, Y_MIN_POS + 1, Y_MAX_POS - 1); + LIMIT(rx, X_MIN_POS + 1, X_MAX_POS - 1); // This keeps us from bumping the endstops + LIMIT(ry, Y_MIN_POS + 1, Y_MAX_POS - 1); + LIMIT(xe, X_MIN_POS + 1, X_MAX_POS - 1); + LIMIT(ye, Y_MIN_POS + 1, Y_MAX_POS - 1); #endif print_line_from_here_to_there(rx, ry, g26_layer_height, xe, ye, g26_layer_height); diff --git a/Marlin/src/gcode/bedlevel/abl/G29.cpp b/Marlin/src/gcode/bedlevel/abl/G29.cpp index 2a4b4f85bb..64ae54ef21 100644 --- a/Marlin/src/gcode/bedlevel/abl/G29.cpp +++ b/Marlin/src/gcode/bedlevel/abl/G29.cpp @@ -318,8 +318,8 @@ G29_TYPE GcodeSuite::G29() { // Get nearest i / j from rx / ry i = (rx - bilinear_start[X_AXIS] + 0.5 * xGridSpacing) / xGridSpacing; j = (ry - bilinear_start[Y_AXIS] + 0.5 * yGridSpacing) / yGridSpacing; - i = constrain(i, 0, GRID_MAX_POINTS_X - 1); - j = constrain(j, 0, GRID_MAX_POINTS_Y - 1); + LIMIT(i, 0, GRID_MAX_POINTS_X - 1); + LIMIT(j, 0, GRID_MAX_POINTS_Y - 1); } if (WITHIN(i, 0, GRID_MAX_POINTS_X - 1) && WITHIN(j, 0, GRID_MAX_POINTS_Y)) { set_bed_leveling_enabled(false); diff --git a/Marlin/src/gcode/calibrate/M48.cpp b/Marlin/src/gcode/calibrate/M48.cpp index 9f0fc714d6..ca54ae8f9c 100644 --- a/Marlin/src/gcode/calibrate/M48.cpp +++ b/Marlin/src/gcode/calibrate/M48.cpp @@ -163,8 +163,8 @@ void GcodeSuite::M48() { Y_current = Y_probe_location - (Y_PROBE_OFFSET_FROM_EXTRUDER) + sin(RADIANS(angle)) * radius; #if DISABLED(DELTA) - X_current = constrain(X_current, X_MIN_POS, X_MAX_POS); - Y_current = constrain(Y_current, Y_MIN_POS, Y_MAX_POS); + LIMIT(X_current, X_MIN_POS, X_MAX_POS); + LIMIT(Y_current, Y_MIN_POS, Y_MAX_POS); #else // If we have gone out too far, we can do a simple fix and scale the numbers // back in closer to the origin. diff --git a/Marlin/src/lcd/extui_malyan_lcd.cpp b/Marlin/src/lcd/extui_malyan_lcd.cpp index e9e7dfdd59..6b83827382 100644 --- a/Marlin/src/lcd/extui_malyan_lcd.cpp +++ b/Marlin/src/lcd/extui_malyan_lcd.cpp @@ -115,9 +115,8 @@ void process_lcd_c_command(const char* command) { switch (command[0]) { case 'C': // Cope with both V1 early rev and later LCDs. case 'S': { - int raw_feedrate = atoi(command + 1); - feedrate_percentage = raw_feedrate * 10; - feedrate_percentage = constrain(feedrate_percentage, 10, 999); + feedrate_percentage = atoi(command + 1) * 10; + LIMIT(feedrate_percentage, 10, 999); } break; case 'T': { thermalManager.setTargetHotend(atoi(command + 1), 0); diff --git a/Marlin/src/lcd/menu/game/brickout.cpp b/Marlin/src/lcd/menu/game/brickout.cpp index ad23031360..1cb19a5cd5 100644 --- a/Marlin/src/lcd/menu/game/brickout.cpp +++ b/Marlin/src/lcd/menu/game/brickout.cpp @@ -67,8 +67,7 @@ void reset_ball() { void BrickoutGame::game_screen() { if (game_frame()) { // Run logic twice for finer resolution // Update Paddle Position - paddle_x = (int8_t)ui.encoderPosition; - paddle_x = constrain(paddle_x, 0, (LCD_PIXEL_WIDTH - (PADDLE_W)) / (PADDLE_VEL)); + paddle_x = constrain(int8_t(ui.encoderPosition), 0, (LCD_PIXEL_WIDTH - (PADDLE_W)) / (PADDLE_VEL)); ui.encoderPosition = paddle_x; paddle_x *= (PADDLE_VEL); diff --git a/Marlin/src/lcd/menu/game/invaders.cpp b/Marlin/src/lcd/menu/game/invaders.cpp index 55a1e4a849..b54566c3f7 100644 --- a/Marlin/src/lcd/menu/game/invaders.cpp +++ b/Marlin/src/lcd/menu/game/invaders.cpp @@ -263,8 +263,7 @@ void InvadersGame::game_screen() { if (ui.first_page) { // Update Cannon Position - int16_t ep = int16_t(ui.encoderPosition); - ep = constrain(ep, 0, (LCD_PIXEL_WIDTH - (CANNON_W)) / (CANNON_VEL)); + int16_t ep = constrain(int16_t(ui.encoderPosition), 0, (LCD_PIXEL_WIDTH - (CANNON_W)) / (CANNON_VEL)); ui.encoderPosition = ep; ep *= (CANNON_VEL); diff --git a/Marlin/src/lcd/menu/menu_configuration.cpp b/Marlin/src/lcd/menu/menu_configuration.cpp index c1395e22fc..f01f2fd986 100644 --- a/Marlin/src/lcd/menu/menu_configuration.cpp +++ b/Marlin/src/lcd/menu/menu_configuration.cpp @@ -70,7 +70,7 @@ static void lcd_factory_settings() { return; } bar_percent += (int8_t)ui.encoderPosition; - bar_percent = constrain(bar_percent, 0, 100); + LIMIT(bar_percent, 0, 100); ui.encoderPosition = 0; draw_menu_item_static(0, PSTR(MSG_PROGRESS_BAR_TEST), true, true); lcd_moveto((LCD_WIDTH) / 2 - 2, LCD_HEIGHT - 2); diff --git a/Marlin/src/lcd/ultralcd.cpp b/Marlin/src/lcd/ultralcd.cpp index d652d90abf..2fb2b209e9 100644 --- a/Marlin/src/lcd/ultralcd.cpp +++ b/Marlin/src/lcd/ultralcd.cpp @@ -548,7 +548,7 @@ void MarlinUI::status_screen() { else if ((old_frm < 100 && new_frm > 100) || (old_frm > 100 && new_frm < 100)) new_frm = 100; - new_frm = constrain(new_frm, 10, 999); + LIMIT(new_frm, 10, 999); if (old_frm != new_frm) { feedrate_percentage = new_frm; diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp index 561d2b6469..a755cafca4 100644 --- a/Marlin/src/module/planner.cpp +++ b/Marlin/src/module/planner.cpp @@ -1157,7 +1157,7 @@ void Planner::recalculate() { } float t = autotemp_min + high * autotemp_factor; - t = constrain(t, autotemp_min, autotemp_max); + LIMIT(t, autotemp_min, autotemp_max); if (t < oldt) t = t * (1 - float(AUTOTEMP_OLDWEIGHT)) + oldt * float(AUTOTEMP_OLDWEIGHT); oldt = t; thermalManager.setTargetHotend(t, 0); diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index 43efb79333..d7e4423bc4 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -460,7 +460,7 @@ temp_range_t Temperature::temp_range[HOTENDS] = ARRAY_BY_HOTENDS(sensor_heater_0 if (cycles > 0) { const long max_pow = GHV(MAX_BED_POWER, PID_MAX); bias += (d * (t_high - t_low)) / (t_low + t_high); - bias = constrain(bias, 20, max_pow - 20); + LIMIT(bias, 20, max_pow - 20); d = (bias > max_pow >> 1) ? max_pow - 1 - bias : bias; SERIAL_ECHOPAIR(MSG_BIAS, bias, MSG_D, d, MSG_T_MIN, min, MSG_T_MAX, max); @@ -874,7 +874,7 @@ float Temperature::get_pid_output_hotend(const uint8_t e) { } #endif // PID_EXTRUSION_SCALING - pid_output = constrain(pid_output, 0, PID_MAX); + LIMIT(pid_output, 0, PID_MAX); } temp_dState[ee] = temp_hotend[ee].current; @@ -1070,7 +1070,7 @@ void Temperature::manage_heater() { if (filament_sensor) { meas_shift_index = filwidth_delay_index[0] - meas_delay_cm; if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed - meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY); + LIMIT(meas_shift_index, 0, MAX_MEASUREMENT_DELAY); planner.calculate_volumetric_for_width_sensor(measurement_delay[meas_shift_index]); } #endif // FILAMENT_WIDTH_SENSOR