diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index b0cbb8802..0cea623ef 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -3076,8 +3076,7 @@ inline void gcode_G28() { apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); - if (eqnBVector[ind] - z_tmp < min_diff) - min_diff = eqnBVector[ind] - z_tmp; + NOMORE(min_diff, eqnBVector[ind] - z_tmp); if (diff >= 0.0) SERIAL_PROTOCOLPGM(" +"); // Include + for column alignment @@ -5147,7 +5146,7 @@ inline void gcode_M400() { st_synchronize(); } */ inline void gcode_M405() { if (code_seen('D')) meas_delay_cm = code_value(); - if (meas_delay_cm > MAX_MEASUREMENT_DELAY) meas_delay_cm = MAX_MEASUREMENT_DELAY; + NOMORE(meas_delay_cm, MAX_MEASUREMENT_DELAY); if (delay_index2 == -1) { //initialize the ring buffer if it has not been done since startup int temp_ratio = widthFil_to_size_ratio(); diff --git a/Marlin/SdBaseFile.cpp b/Marlin/SdBaseFile.cpp index 301dfc259..b28aeb7b6 100644 --- a/Marlin/SdBaseFile.cpp +++ b/Marlin/SdBaseFile.cpp @@ -1049,9 +1049,8 @@ int16_t SdBaseFile::read(void* buf, uint16_t nbyte) { if (!isOpen() || !(flags_ & O_READ)) goto fail; // max bytes left in file - if (nbyte >= (fileSize_ - curPosition_)) { - nbyte = fileSize_ - curPosition_; - } + NOMORE(nbyte, fileSize_ - curPosition_); + // amount left to read toRead = nbyte; while (toRead > 0) { @@ -1077,7 +1076,7 @@ int16_t SdBaseFile::read(void* buf, uint16_t nbyte) { uint16_t n = toRead; // amount to be read from current block - if (n > (512 - offset)) n = 512 - offset; + NOMORE(n, 512 - offset); // no buffering needed if n == 512 if (n == 512 && block != vol_->cacheBlockNumber()) { @@ -1758,7 +1757,7 @@ int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) { uint16_t n = 512 - blockOffset; // lesser of space and amount to write - if (n > nToWrite) n = nToWrite; + NOMORE(n, nToWrite); // block for data write uint32_t block = vol_->clusterStartBlock(curCluster_) + blockOfCluster; diff --git a/Marlin/SdVolume.cpp b/Marlin/SdVolume.cpp index a9ef8ea75..fb7212905 100644 --- a/Marlin/SdVolume.cpp +++ b/Marlin/SdVolume.cpp @@ -296,7 +296,7 @@ int32_t SdVolume::freeClusterCount() { for (uint32_t lba = fatStartBlock_; todo; todo -= n, lba++) { if (!cacheRawBlock(lba, CACHE_FOR_READ)) return -1; - if (todo < n) n = todo; + NOMORE(n, todo); if (fatType_ == 16) { for (uint16_t i = 0; i < n; i++) { if (cacheBuffer_.fat16[i] == 0) free++; diff --git a/Marlin/planner.cpp b/Marlin/planner.cpp index 628c8bdc6..f5ebeb310 100644 --- a/Marlin/planner.cpp +++ b/Marlin/planner.cpp @@ -381,7 +381,7 @@ void plan_init() { block_t* block = &block_buffer[block_index]; if (block->steps[X_AXIS] || block->steps[Y_AXIS] || block->steps[Z_AXIS]) { float se = (float)block->steps[E_AXIS] / block->step_event_count * block->nominal_speed; // mm/sec; - if (se > high) high = se; + NOLESS(high, se); } block_index = next_block_index(block_index); } diff --git a/Marlin/qr_solve.cpp b/Marlin/qr_solve.cpp index 1e085c95b..c038ba111 100644 --- a/Marlin/qr_solve.cpp +++ b/Marlin/qr_solve.cpp @@ -203,8 +203,7 @@ double r8mat_amax(int m, int n, double a[]) double value = r8_abs(a[0 + 0 * m]); for (int j = 0; j < n; j++) { for (int i = 0; i < m; i++) { - if (value < r8_abs(a[i + j * m])) - value = r8_abs(a[i + j * m]); + NOLESS(value, r8_abs(a[i + j * m])); } } return value; diff --git a/Marlin/servo.cpp b/Marlin/servo.cpp index c66fdc9d3..58fef48bb 100644 --- a/Marlin/servo.cpp +++ b/Marlin/servo.cpp @@ -269,9 +269,7 @@ void Servo::detach() { void Servo::write(int value) { if (value < MIN_PULSE_WIDTH) { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) - if (value < 0) value = 0; - if (value > 180) value = 180; - value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX()); + value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN(), SERVO_MAX()); } this->writeMicroseconds(value); } @@ -280,18 +278,13 @@ void Servo::writeMicroseconds(int value) { // calculate and store the values for the given channel byte channel = this->servoIndex; if (channel < MAX_SERVOS) { // ensure channel is valid - if (value < SERVO_MIN()) // ensure pulse width is valid - value = SERVO_MIN(); - else if (value > SERVO_MAX()) - value = SERVO_MAX(); - - value = value - TRIM_DURATION; + // ensure pulse width is valid + value = constrain(value, SERVO_MIN(), SERVO_MAX()) - TRIM_DURATION; value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009 - uint8_t oldSREG = SREG; - cli(); + CRITICAL_SECTION_START; servo_info[channel].ticks = value; - SREG = oldSREG; + CRITICAL_SECTION_END; } } diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index fa3c3b128..eb5d95770 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -672,7 +672,7 @@ void manage_heater() { // the nominal filament diameter then square it to get an area meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY); float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2); - if (vm < 0.01) vm = 0.01; + NOLESS(vm, 0.01); volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm; } #endif //FILAMENT_SENSOR @@ -836,7 +836,7 @@ static void updateTemperaturesFromRawValues() { int widthFil_to_size_ratio() { float temp = filament_width_meas; if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out - else if (temp > MEASURED_UPPER_LIMIT) temp = MEASURED_UPPER_LIMIT; + else NOMORE(temp, MEASURED_UPPER_LIMIT); return filament_width_nominal / temp * 100; } diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 4c3ce6c31..6a9db9873 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -133,7 +133,7 @@ static void lcd_status_screen(); encoderRateMultiplierEnabled = false; \ if (encoderPosition > 0x8000) encoderPosition = 0; \ uint8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \ - if (encoderLine < currentMenuViewOffset) currentMenuViewOffset = encoderLine; \ + NOMORE(currentMenuViewOffset, encoderLine); \ uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \ bool wasClicked = LCD_CLICKED, itemSelected; \ for (uint8_t _drawLineNr = 0; _drawLineNr < LCD_HEIGHT; _drawLineNr++, _lineNr++) { \ @@ -827,8 +827,8 @@ static void _lcd_move(const char* name, AxisEnum axis, int min, int max) { if (encoderPosition != 0) { refresh_cmd_timeout(); current_position[axis] += float((int)encoderPosition) * move_menu_scale; - if (min_software_endstops && current_position[axis] < min) current_position[axis] = min; - if (max_software_endstops && current_position[axis] > max) current_position[axis] = max; + if (min_software_endstops) NOLESS(current_position[axis], min); + if (max_software_endstops) NOMORE(current_position[axis], max); encoderPosition = 0; if (movesplanned() <= 3) line_to_current(axis); @@ -2239,8 +2239,8 @@ char* ftostr52(const float& x) { if (encoderPosition != 0) { refresh_cmd_timeout(); current_position[Z_AXIS] += float((int)encoderPosition) * MBL_Z_STEP; - if (min_software_endstops && current_position[Z_AXIS] < Z_MIN_POS) current_position[Z_AXIS] = Z_MIN_POS; - if (max_software_endstops && current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS; + if (min_software_endstops) NOLESS(current_position[Z_AXIS], Z_MIN_POS); + if (max_software_endstops) NOMORE(current_position[Z_AXIS], Z_MAX_POS); encoderPosition = 0; line_to_current(Z_AXIS); lcdDrawUpdate = 2;