Bladeren bron

Use macros where possible

Apply `constrain`, `NOMORE`, `NOLESS` and `CRITICAL_SECTION` macros
wherever possible.
Scott Lahteine 8 jaren geleden
bovenliggende
commit
209f5f21e0
8 gewijzigde bestanden met toevoegingen van 21 en 31 verwijderingen
  1. 2
    3
      Marlin/Marlin_main.cpp
  2. 4
    5
      Marlin/SdBaseFile.cpp
  3. 1
    1
      Marlin/SdVolume.cpp
  4. 1
    1
      Marlin/planner.cpp
  5. 1
    2
      Marlin/qr_solve.cpp
  6. 5
    12
      Marlin/servo.cpp
  7. 2
    2
      Marlin/temperature.cpp
  8. 5
    5
      Marlin/ultralcd.cpp

+ 2
- 3
Marlin/Marlin_main.cpp Bestand weergeven

@@ -3076,8 +3076,7 @@ inline void gcode_G28() {
3076 3076
 
3077 3077
               apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp);
3078 3078
 
3079
-              if (eqnBVector[ind] - z_tmp < min_diff)
3080
-                min_diff = eqnBVector[ind] - z_tmp;
3079
+              NOMORE(min_diff, eqnBVector[ind] - z_tmp);
3081 3080
 
3082 3081
               if (diff >= 0.0)
3083 3082
                 SERIAL_PROTOCOLPGM(" +");   // Include + for column alignment
@@ -5147,7 +5146,7 @@ inline void gcode_M400() { st_synchronize(); }
5147 5146
    */
5148 5147
   inline void gcode_M405() {
5149 5148
     if (code_seen('D')) meas_delay_cm = code_value();
5150
-    if (meas_delay_cm > MAX_MEASUREMENT_DELAY) meas_delay_cm = MAX_MEASUREMENT_DELAY;
5149
+    NOMORE(meas_delay_cm, MAX_MEASUREMENT_DELAY);
5151 5150
 
5152 5151
     if (delay_index2 == -1) { //initialize the ring buffer if it has not been done since startup
5153 5152
       int temp_ratio = widthFil_to_size_ratio();

+ 4
- 5
Marlin/SdBaseFile.cpp Bestand weergeven

@@ -1049,9 +1049,8 @@ int16_t SdBaseFile::read(void* buf, uint16_t nbyte) {
1049 1049
   if (!isOpen() || !(flags_ & O_READ)) goto fail;
1050 1050
 
1051 1051
   // max bytes left in file
1052
-  if (nbyte >= (fileSize_ - curPosition_)) {
1053
-    nbyte = fileSize_ - curPosition_;
1054
-  }
1052
+  NOMORE(nbyte, fileSize_ - curPosition_);
1053
+
1055 1054
   // amount left to read
1056 1055
   toRead = nbyte;
1057 1056
   while (toRead > 0) {
@@ -1077,7 +1076,7 @@ int16_t SdBaseFile::read(void* buf, uint16_t nbyte) {
1077 1076
     uint16_t n = toRead;
1078 1077
 
1079 1078
     // amount to be read from current block
1080
-    if (n > (512 - offset)) n = 512 - offset;
1079
+    NOMORE(n, 512 - offset);
1081 1080
 
1082 1081
     // no buffering needed if n == 512
1083 1082
     if (n == 512 && block != vol_->cacheBlockNumber()) {
@@ -1758,7 +1757,7 @@ int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) {
1758 1757
     uint16_t n = 512 - blockOffset;
1759 1758
 
1760 1759
     // lesser of space and amount to write
1761
-    if (n > nToWrite) n = nToWrite;
1760
+    NOMORE(n, nToWrite);
1762 1761
 
1763 1762
     // block for data write
1764 1763
     uint32_t block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;

+ 1
- 1
Marlin/SdVolume.cpp Bestand weergeven

@@ -296,7 +296,7 @@ int32_t SdVolume::freeClusterCount() {
296 296
 
297 297
   for (uint32_t lba = fatStartBlock_; todo; todo -= n, lba++) {
298 298
     if (!cacheRawBlock(lba, CACHE_FOR_READ)) return -1;
299
-    if (todo < n) n = todo;
299
+    NOMORE(n, todo);
300 300
     if (fatType_ == 16) {
301 301
       for (uint16_t i = 0; i < n; i++) {
302 302
         if (cacheBuffer_.fat16[i] == 0) free++;

+ 1
- 1
Marlin/planner.cpp Bestand weergeven

@@ -381,7 +381,7 @@ void plan_init() {
381 381
       block_t* block = &block_buffer[block_index];
382 382
       if (block->steps[X_AXIS] || block->steps[Y_AXIS] || block->steps[Z_AXIS]) {
383 383
         float se = (float)block->steps[E_AXIS] / block->step_event_count * block->nominal_speed; // mm/sec;
384
-        if (se > high) high = se;
384
+        NOLESS(high, se);
385 385
       }
386 386
       block_index = next_block_index(block_index);
387 387
     }

+ 1
- 2
Marlin/qr_solve.cpp Bestand weergeven

@@ -203,8 +203,7 @@ double r8mat_amax(int m, int n, double a[])
203 203
   double value = r8_abs(a[0 + 0 * m]);
204 204
   for (int j = 0; j < n; j++) {
205 205
     for (int i = 0; i < m; i++) {
206
-      if (value < r8_abs(a[i + j * m]))
207
-        value = r8_abs(a[i + j * m]);
206
+      NOLESS(value, r8_abs(a[i + j * m]));
208 207
     }
209 208
   }
210 209
   return value;

+ 5
- 12
Marlin/servo.cpp Bestand weergeven

@@ -269,9 +269,7 @@ void Servo::detach() {
269 269
 
270 270
 void Servo::write(int value) {
271 271
   if (value < MIN_PULSE_WIDTH) { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
272
-    if (value < 0) value = 0;
273
-    if (value > 180) value = 180;
274
-    value = map(value, 0, 180, SERVO_MIN(),  SERVO_MAX());
272
+    value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN(), SERVO_MAX());
275 273
   }
276 274
   this->writeMicroseconds(value);
277 275
 }
@@ -280,18 +278,13 @@ void Servo::writeMicroseconds(int value) {
280 278
   // calculate and store the values for the given channel
281 279
   byte channel = this->servoIndex;
282 280
   if (channel < MAX_SERVOS) {  // ensure channel is valid
283
-    if (value < SERVO_MIN())   // ensure pulse width is valid
284
-      value = SERVO_MIN();
285
-    else if (value > SERVO_MAX())
286
-      value = SERVO_MAX();
287
-
288
-    value = value - TRIM_DURATION;
281
+    // ensure pulse width is valid
282
+    value = constrain(value, SERVO_MIN(), SERVO_MAX()) - TRIM_DURATION;
289 283
     value = usToTicks(value);  // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
290 284
 
291
-    uint8_t oldSREG = SREG;
292
-    cli();
285
+    CRITICAL_SECTION_START;
293 286
     servo_info[channel].ticks = value;
294
-    SREG = oldSREG;
287
+    CRITICAL_SECTION_END;
295 288
   }
296 289
 }
297 290
 

+ 2
- 2
Marlin/temperature.cpp Bestand weergeven

@@ -672,7 +672,7 @@ void manage_heater() {
672 672
       // the nominal filament diameter then square it to get an area
673 673
       meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
674 674
       float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2);
675
-      if (vm < 0.01) vm = 0.01;
675
+      NOLESS(vm, 0.01);
676 676
       volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
677 677
     }
678 678
   #endif //FILAMENT_SENSOR
@@ -836,7 +836,7 @@ static void updateTemperaturesFromRawValues() {
836 836
   int widthFil_to_size_ratio() {
837 837
     float temp = filament_width_meas;
838 838
     if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal;  //assume sensor cut out
839
-    else if (temp > MEASURED_UPPER_LIMIT) temp = MEASURED_UPPER_LIMIT;
839
+    else NOMORE(temp, MEASURED_UPPER_LIMIT);
840 840
     return filament_width_nominal / temp * 100;
841 841
   }
842 842
 

+ 5
- 5
Marlin/ultralcd.cpp Bestand weergeven

@@ -133,7 +133,7 @@ static void lcd_status_screen();
133 133
     encoderRateMultiplierEnabled = false; \
134 134
     if (encoderPosition > 0x8000) encoderPosition = 0; \
135 135
     uint8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \
136
-    if (encoderLine < currentMenuViewOffset) currentMenuViewOffset = encoderLine; \
136
+    NOMORE(currentMenuViewOffset, encoderLine); \
137 137
     uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \
138 138
     bool wasClicked = LCD_CLICKED, itemSelected; \
139 139
     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) {
827 827
   if (encoderPosition != 0) {
828 828
     refresh_cmd_timeout();
829 829
     current_position[axis] += float((int)encoderPosition) * move_menu_scale;
830
-    if (min_software_endstops && current_position[axis] < min) current_position[axis] = min;
831
-    if (max_software_endstops && current_position[axis] > max) current_position[axis] = max;
830
+    if (min_software_endstops) NOLESS(current_position[axis], min);
831
+    if (max_software_endstops) NOMORE(current_position[axis], max);
832 832
     encoderPosition = 0;
833 833
     if (movesplanned() <= 3)
834 834
       line_to_current(axis);
@@ -2239,8 +2239,8 @@ char* ftostr52(const float& x) {
2239 2239
     if (encoderPosition != 0) {
2240 2240
       refresh_cmd_timeout();
2241 2241
       current_position[Z_AXIS] += float((int)encoderPosition) * MBL_Z_STEP;
2242
-      if (min_software_endstops && current_position[Z_AXIS] < Z_MIN_POS) current_position[Z_AXIS] = Z_MIN_POS;
2243
-      if (max_software_endstops && current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
2242
+      if (min_software_endstops) NOLESS(current_position[Z_AXIS], Z_MIN_POS);
2243
+      if (max_software_endstops) NOMORE(current_position[Z_AXIS], Z_MAX_POS);
2244 2244
       encoderPosition = 0;
2245 2245
       line_to_current(Z_AXIS);
2246 2246
       lcdDrawUpdate = 2;

Laden…
Annuleren
Opslaan