Browse Source

Use macros where possible

Apply `constrain`, `NOMORE`, `NOLESS` and `CRITICAL_SECTION` macros
wherever possible.
Scott Lahteine 9 years ago
parent
commit
209f5f21e0

+ 2
- 3
Marlin/Marlin_main.cpp View File

3076
 
3076
 
3077
               apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp);
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
               if (diff >= 0.0)
3081
               if (diff >= 0.0)
3083
                 SERIAL_PROTOCOLPGM(" +");   // Include + for column alignment
3082
                 SERIAL_PROTOCOLPGM(" +");   // Include + for column alignment
5147
    */
5146
    */
5148
   inline void gcode_M405() {
5147
   inline void gcode_M405() {
5149
     if (code_seen('D')) meas_delay_cm = code_value();
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
     if (delay_index2 == -1) { //initialize the ring buffer if it has not been done since startup
5151
     if (delay_index2 == -1) { //initialize the ring buffer if it has not been done since startup
5153
       int temp_ratio = widthFil_to_size_ratio();
5152
       int temp_ratio = widthFil_to_size_ratio();

+ 4
- 5
Marlin/SdBaseFile.cpp View File

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

+ 1
- 1
Marlin/SdVolume.cpp View File

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

+ 1
- 1
Marlin/planner.cpp View File

381
       block_t* block = &block_buffer[block_index];
381
       block_t* block = &block_buffer[block_index];
382
       if (block->steps[X_AXIS] || block->steps[Y_AXIS] || block->steps[Z_AXIS]) {
382
       if (block->steps[X_AXIS] || block->steps[Y_AXIS] || block->steps[Z_AXIS]) {
383
         float se = (float)block->steps[E_AXIS] / block->step_event_count * block->nominal_speed; // mm/sec;
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
       block_index = next_block_index(block_index);
386
       block_index = next_block_index(block_index);
387
     }
387
     }

+ 1
- 2
Marlin/qr_solve.cpp View File

203
   double value = r8_abs(a[0 + 0 * m]);
203
   double value = r8_abs(a[0 + 0 * m]);
204
   for (int j = 0; j < n; j++) {
204
   for (int j = 0; j < n; j++) {
205
     for (int i = 0; i < m; i++) {
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
   return value;
209
   return value;

+ 5
- 12
Marlin/servo.cpp View File

269
 
269
 
270
 void Servo::write(int value) {
270
 void Servo::write(int value) {
271
   if (value < MIN_PULSE_WIDTH) { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
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
   this->writeMicroseconds(value);
274
   this->writeMicroseconds(value);
277
 }
275
 }
280
   // calculate and store the values for the given channel
278
   // calculate and store the values for the given channel
281
   byte channel = this->servoIndex;
279
   byte channel = this->servoIndex;
282
   if (channel < MAX_SERVOS) {  // ensure channel is valid
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
     value = usToTicks(value);  // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
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
     servo_info[channel].ticks = value;
286
     servo_info[channel].ticks = value;
294
-    SREG = oldSREG;
287
+    CRITICAL_SECTION_END;
295
   }
288
   }
296
 }
289
 }
297
 
290
 

+ 2
- 2
Marlin/temperature.cpp View File

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

+ 5
- 5
Marlin/ultralcd.cpp View File

133
     encoderRateMultiplierEnabled = false; \
133
     encoderRateMultiplierEnabled = false; \
134
     if (encoderPosition > 0x8000) encoderPosition = 0; \
134
     if (encoderPosition > 0x8000) encoderPosition = 0; \
135
     uint8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \
135
     uint8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \
136
-    if (encoderLine < currentMenuViewOffset) currentMenuViewOffset = encoderLine; \
136
+    NOMORE(currentMenuViewOffset, encoderLine); \
137
     uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \
137
     uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \
138
     bool wasClicked = LCD_CLICKED, itemSelected; \
138
     bool wasClicked = LCD_CLICKED, itemSelected; \
139
     for (uint8_t _drawLineNr = 0; _drawLineNr < LCD_HEIGHT; _drawLineNr++, _lineNr++) { \
139
     for (uint8_t _drawLineNr = 0; _drawLineNr < LCD_HEIGHT; _drawLineNr++, _lineNr++) { \
827
   if (encoderPosition != 0) {
827
   if (encoderPosition != 0) {
828
     refresh_cmd_timeout();
828
     refresh_cmd_timeout();
829
     current_position[axis] += float((int)encoderPosition) * move_menu_scale;
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
     encoderPosition = 0;
832
     encoderPosition = 0;
833
     if (movesplanned() <= 3)
833
     if (movesplanned() <= 3)
834
       line_to_current(axis);
834
       line_to_current(axis);
2239
     if (encoderPosition != 0) {
2239
     if (encoderPosition != 0) {
2240
       refresh_cmd_timeout();
2240
       refresh_cmd_timeout();
2241
       current_position[Z_AXIS] += float((int)encoderPosition) * MBL_Z_STEP;
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
       encoderPosition = 0;
2244
       encoderPosition = 0;
2245
       line_to_current(Z_AXIS);
2245
       line_to_current(Z_AXIS);
2246
       lcdDrawUpdate = 2;
2246
       lcdDrawUpdate = 2;

Loading…
Cancel
Save