Apply LIMIT macro

This commit is contained in:
Scott Lahteine 2019-07-10 03:33:28 -05:00
parent 9817976375
commit ed0e6afacb
14 changed files with 31 additions and 34 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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) {

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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.

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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