Introduce clamp as a template function (#1953)

This commit is contained in:
Stefan Agner
2021-07-14 07:08:18 +02:00
committed by GitHub
parent 08b67e7aea
commit 5cb0c11feb
13 changed files with 17 additions and 14 deletions
@@ -94,7 +94,7 @@ void LgIrClimate::transmit_state() {
// remote_state |= FAN_MODE_AUTO_DRY;
}
if (this->mode == climate::CLIMATE_MODE_COOL || this->mode == climate::CLIMATE_MODE_HEAT) {
auto temp = (uint8_t) roundf(clamp(this->target_temperature, TEMP_MIN, TEMP_MAX));
auto temp = (uint8_t) roundf(clamp<float>(this->target_temperature, TEMP_MIN, TEMP_MAX));
remote_state |= ((temp - 15) << TEMP_SHIFT);
}
}
+1 -1
View File
@@ -84,7 +84,7 @@ void CoolixClimate::transmit_state() {
}
if (this->mode != climate::CLIMATE_MODE_OFF) {
if (this->mode != climate::CLIMATE_MODE_FAN_ONLY) {
auto temp = (uint8_t) roundf(clamp(this->target_temperature, COOLIX_TEMP_MIN, COOLIX_TEMP_MAX));
auto temp = (uint8_t) roundf(clamp<float>(this->target_temperature, COOLIX_TEMP_MIN, COOLIX_TEMP_MAX));
remote_state |= COOLIX_TEMP_MAP[temp - COOLIX_TEMP_MIN];
} else {
remote_state |= COOLIX_FAN_TEMP_CODE;
+1 -1
View File
@@ -135,7 +135,7 @@ uint8_t DaikinClimate::temperature_() {
case climate::CLIMATE_MODE_DRY:
return 0xc0;
default:
uint8_t temperature = (uint8_t) roundf(clamp(this->target_temperature, DAIKIN_TEMP_MIN, DAIKIN_TEMP_MAX));
uint8_t temperature = (uint8_t) roundf(clamp<float>(this->target_temperature, DAIKIN_TEMP_MIN, DAIKIN_TEMP_MAX));
return temperature << 1;
}
}
+1 -1
View File
@@ -6,7 +6,7 @@ namespace fan {
FanSpeed speed_level_to_enum(int speed_level, int supported_speed_levels) {
const auto speed_ratio = static_cast<float>(speed_level) / (supported_speed_levels + 1);
const auto legacy_level = static_cast<int>(clamp(ceilf(speed_ratio * 3), 1, 3));
const auto legacy_level = clamp<int>(static_cast<int>(ceilf(speed_ratio * 3)), 1, 3);
return static_cast<FanSpeed>(legacy_level - 1);
}
+1 -1
View File
@@ -54,7 +54,7 @@ void FanStateCall::perform() const {
}
if (this->speed_.has_value()) {
const int speed_count = this->state_->get_traits().supported_speed_count();
this->state_->speed = static_cast<int>(clamp(*this->speed_, 1, speed_count));
this->state_->speed = clamp(*this->speed_, 1, speed_count);
}
FanStateRTCState saved{};
@@ -110,7 +110,7 @@ void FujitsuGeneralClimate::transmit_state() {
// Set temperature
uint8_t temperature_clamped =
(uint8_t) roundf(clamp(this->target_temperature, FUJITSU_GENERAL_TEMP_MIN, FUJITSU_GENERAL_TEMP_MAX));
(uint8_t) roundf(clamp<float>(this->target_temperature, FUJITSU_GENERAL_TEMP_MIN, FUJITSU_GENERAL_TEMP_MAX));
uint8_t temperature_offset = temperature_clamped - FUJITSU_GENERAL_TEMP_MIN;
SET_NIBBLE(remote_state, FUJITSU_GENERAL_TEMPERATURE_NIBBLE, temperature_offset);
+2 -2
View File
@@ -42,8 +42,8 @@ void MitsubishiClimate::transmit_state() {
break;
}
remote_state[7] =
(uint8_t) roundf(clamp(this->target_temperature, MITSUBISHI_TEMP_MIN, MITSUBISHI_TEMP_MAX) - MITSUBISHI_TEMP_MIN);
remote_state[7] = (uint8_t) roundf(clamp<float>(this->target_temperature, MITSUBISHI_TEMP_MIN, MITSUBISHI_TEMP_MAX) -
MITSUBISHI_TEMP_MIN);
ESP_LOGV(TAG, "Sending Mitsubishi target temp: %.1f state: %02X mode: %02X temp: %02X", this->target_temperature,
remote_state[5], remote_state[6], remote_state[7]);
@@ -130,7 +130,7 @@ void SSD1306::update() {
}
void SSD1306::set_brightness(float brightness) {
// validation
this->brightness_ = clamp(brightness, 0, 1);
this->brightness_ = clamp(brightness, 0.0F, 1.0F);
// now write the new brightness level to the display
this->command(SSD1306_COMMAND_SET_CONTRAST);
this->command(int(SSD1306_MAX_CONTRAST * (this->brightness_)));
@@ -126,7 +126,7 @@ void SSD1322::update() {
this->display();
}
void SSD1322::set_brightness(float brightness) {
this->brightness_ = clamp(brightness, 0, 1);
this->brightness_ = clamp(brightness, 0.0F, 1.0F);
// now write the new brightness level to the display
this->command(SSD1322_SETCONTRAST);
this->data(int(SSD1322_MAX_CONTRAST * (this->brightness_)));
@@ -100,7 +100,7 @@ void SSD1327::update() {
}
void SSD1327::set_brightness(float brightness) {
// validation
this->brightness_ = clamp(brightness, 0, 1);
this->brightness_ = clamp(brightness, 0.0F, 1.0F);
// now write the new brightness level to the display
this->command(SSD1327_SETCONTRAST);
this->command(int(SSD1327_MAX_CONTRAST * (this->brightness_)));
@@ -97,7 +97,7 @@ void SSD1331::update() {
}
void SSD1331::set_brightness(float brightness) {
// validation
this->brightness_ = clamp(brightness, 0, 1);
this->brightness_ = clamp(brightness, 0.0F, 1.0F);
// now write the new brightness level to the display
this->command(SSD1331_CONTRASTA); // 0x81
this->command(int(SSD1331_MAX_CONTRASTA * (this->brightness_)));