From 9f7ee7be320868a893e070bdadc07c0fe164af77 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 21 Feb 2016 22:25:49 -0800 Subject: [PATCH] Use macros where possible Apply `constrain`, `NOMORE`, `NOLESS` and `CRITICAL_SECTION` macros where possible. --- Marlin_main.cpp | 5 ++--- Sd2Card.cpp | 4 ++-- SdBaseFile.cpp | 9 ++++----- SdVolume.cpp | 2 +- planner.cpp | 2 +- qr_solve.cpp | 3 +-- servo.cpp | 17 +++++------------ temperature.cpp | 4 ++-- ultralcd.cpp | 10 +++++----- 9 files changed, 23 insertions(+), 33 deletions(-) diff --git a/Marlin_main.cpp b/Marlin_main.cpp index 90016035..812ae85f 100644 --- a/Marlin_main.cpp +++ b/Marlin_main.cpp @@ -3066,8 +3066,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 @@ -5113,7 +5112,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/Sd2Card.cpp b/Sd2Card.cpp index dbb025f5..19dc84c7 100644 --- a/Sd2Card.cpp +++ b/Sd2Card.cpp @@ -88,7 +88,7 @@ static uint8_t spiRec() { uint8_t data = 0; // no interrupts during byte receive - about 8 us - cli(); + CRITICAL_SECTION_START; // output pin high - like sending 0XFF fastDigitalWrite(SPI_MOSI_PIN, HIGH); @@ -106,7 +106,7 @@ fastDigitalWrite(SPI_SCK_PIN, LOW); } // enable interrupts - sei(); + CRITICAL_SECTION_END; return data; } //------------------------------------------------------------------------------ diff --git a/SdBaseFile.cpp b/SdBaseFile.cpp index 3806ac98..365c5e50 100644 --- a/SdBaseFile.cpp +++ b/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/SdVolume.cpp b/SdVolume.cpp index a9ef8ea7..fb721290 100644 --- a/SdVolume.cpp +++ b/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/planner.cpp b/planner.cpp index 7f54c895..f2b45e69 100644 --- a/planner.cpp +++ b/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/qr_solve.cpp b/qr_solve.cpp index 1e085c95..c038ba11 100644 --- a/qr_solve.cpp +++ b/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/servo.cpp b/servo.cpp index 2dc1f51b..df2681d8 100644 --- a/servo.cpp +++ b/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/temperature.cpp b/temperature.cpp index b45a9f92..cce288d2 100644 --- a/temperature.cpp +++ b/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/ultralcd.cpp b/ultralcd.cpp index 54d6f32f..96524e20 100644 --- a/ultralcd.cpp +++ b/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++) { \ @@ -835,8 +835,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; line_to_current(axis); lcdDrawUpdate = 1; @@ -2174,8 +2174,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;