Use macros where possible

Apply `constrain`, `NOMORE`, `NOLESS` and `CRITICAL_SECTION` macros
wherever possible.
This commit is contained in:
Scott Lahteine 2016-02-21 22:17:32 -08:00
parent c8f76bb8aa
commit 209f5f21e0
8 changed files with 21 additions and 31 deletions

View File

@ -3076,8 +3076,7 @@ inline void gcode_G28() {
apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp);
if (eqnBVector[ind] - z_tmp < min_diff) NOMORE(min_diff, eqnBVector[ind] - z_tmp);
min_diff = eqnBVector[ind] - z_tmp;
if (diff >= 0.0) if (diff >= 0.0)
SERIAL_PROTOCOLPGM(" +"); // Include + for column alignment SERIAL_PROTOCOLPGM(" +"); // Include + for column alignment
@ -5147,7 +5146,7 @@ inline void gcode_M400() { st_synchronize(); }
*/ */
inline void gcode_M405() { inline void gcode_M405() {
if (code_seen('D')) meas_delay_cm = code_value(); 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 if (delay_index2 == -1) { //initialize the ring buffer if it has not been done since startup
int temp_ratio = widthFil_to_size_ratio(); int temp_ratio = widthFil_to_size_ratio();

View File

@ -1049,9 +1049,8 @@ int16_t SdBaseFile::read(void* buf, uint16_t nbyte) {
if (!isOpen() || !(flags_ & O_READ)) goto fail; if (!isOpen() || !(flags_ & O_READ)) goto fail;
// max bytes left in file // max bytes left in file
if (nbyte >= (fileSize_ - curPosition_)) { NOMORE(nbyte, fileSize_ - curPosition_);
nbyte = fileSize_ - curPosition_;
}
// amount left to read // amount left to read
toRead = nbyte; toRead = nbyte;
while (toRead > 0) { while (toRead > 0) {
@ -1077,7 +1076,7 @@ int16_t SdBaseFile::read(void* buf, uint16_t nbyte) {
uint16_t n = toRead; uint16_t n = toRead;
// amount to be read from current block // amount to be read from current block
if (n > (512 - offset)) n = 512 - offset; NOMORE(n, 512 - offset);
// no buffering needed if n == 512 // no buffering needed if n == 512
if (n == 512 && block != vol_->cacheBlockNumber()) { 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; uint16_t n = 512 - blockOffset;
// lesser of space and amount to write // lesser of space and amount to write
if (n > nToWrite) n = nToWrite; NOMORE(n, nToWrite);
// block for data write // block for data write
uint32_t block = vol_->clusterStartBlock(curCluster_) + blockOfCluster; uint32_t block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;

View File

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

View File

@ -381,7 +381,7 @@ void plan_init() {
block_t* block = &block_buffer[block_index]; block_t* block = &block_buffer[block_index];
if (block->steps[X_AXIS] || block->steps[Y_AXIS] || block->steps[Z_AXIS]) { 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; 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); block_index = next_block_index(block_index);
} }

View File

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

View File

@ -269,9 +269,7 @@ void Servo::detach() {
void Servo::write(int value) { 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 < 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; value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN(), SERVO_MAX());
if (value > 180) value = 180;
value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());
} }
this->writeMicroseconds(value); this->writeMicroseconds(value);
} }
@ -280,18 +278,13 @@ void Servo::writeMicroseconds(int value) {
// calculate and store the values for the given channel // calculate and store the values for the given channel
byte channel = this->servoIndex; byte channel = this->servoIndex;
if (channel < MAX_SERVOS) { // ensure channel is valid if (channel < MAX_SERVOS) { // ensure channel is valid
if (value < SERVO_MIN()) // ensure pulse width is valid // ensure pulse width is valid
value = SERVO_MIN(); value = constrain(value, SERVO_MIN(), SERVO_MAX()) - TRIM_DURATION;
else if (value > SERVO_MAX())
value = SERVO_MAX();
value = value - TRIM_DURATION;
value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009 value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
uint8_t oldSREG = SREG; CRITICAL_SECTION_START;
cli();
servo_info[channel].ticks = value; servo_info[channel].ticks = value;
SREG = oldSREG; CRITICAL_SECTION_END;
} }
} }

View File

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

View File

@ -133,7 +133,7 @@ static void lcd_status_screen();
encoderRateMultiplierEnabled = false; \ encoderRateMultiplierEnabled = false; \
if (encoderPosition > 0x8000) encoderPosition = 0; \ if (encoderPosition > 0x8000) encoderPosition = 0; \
uint8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \ uint8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \
if (encoderLine < currentMenuViewOffset) currentMenuViewOffset = encoderLine; \ NOMORE(currentMenuViewOffset, encoderLine); \
uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \ uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \
bool wasClicked = LCD_CLICKED, itemSelected; \ bool wasClicked = LCD_CLICKED, itemSelected; \
for (uint8_t _drawLineNr = 0; _drawLineNr < LCD_HEIGHT; _drawLineNr++, _lineNr++) { \ 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) { if (encoderPosition != 0) {
refresh_cmd_timeout(); refresh_cmd_timeout();
current_position[axis] += float((int)encoderPosition) * move_menu_scale; current_position[axis] += float((int)encoderPosition) * move_menu_scale;
if (min_software_endstops && current_position[axis] < min) current_position[axis] = min; if (min_software_endstops) NOLESS(current_position[axis], min);
if (max_software_endstops && current_position[axis] > max) current_position[axis] = max; if (max_software_endstops) NOMORE(current_position[axis], max);
encoderPosition = 0; encoderPosition = 0;
if (movesplanned() <= 3) if (movesplanned() <= 3)
line_to_current(axis); line_to_current(axis);
@ -2239,8 +2239,8 @@ char* ftostr52(const float& x) {
if (encoderPosition != 0) { if (encoderPosition != 0) {
refresh_cmd_timeout(); refresh_cmd_timeout();
current_position[Z_AXIS] += float((int)encoderPosition) * MBL_Z_STEP; 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 (min_software_endstops) NOLESS(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 (max_software_endstops) NOMORE(current_position[Z_AXIS], Z_MAX_POS);
encoderPosition = 0; encoderPosition = 0;
line_to_current(Z_AXIS); line_to_current(Z_AXIS);
lcdDrawUpdate = 2; lcdDrawUpdate = 2;