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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 17 additions and 14 deletions

View file

@ -94,7 +94,7 @@ void LgIrClimate::transmit_state() {
// remote_state |= FAN_MODE_AUTO_DRY; // remote_state |= FAN_MODE_AUTO_DRY;
} }
if (this->mode == climate::CLIMATE_MODE_COOL || this->mode == climate::CLIMATE_MODE_HEAT) { 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); remote_state |= ((temp - 15) << TEMP_SHIFT);
} }
} }

View file

@ -84,7 +84,7 @@ void CoolixClimate::transmit_state() {
} }
if (this->mode != climate::CLIMATE_MODE_OFF) { if (this->mode != climate::CLIMATE_MODE_OFF) {
if (this->mode != climate::CLIMATE_MODE_FAN_ONLY) { 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]; remote_state |= COOLIX_TEMP_MAP[temp - COOLIX_TEMP_MIN];
} else { } else {
remote_state |= COOLIX_FAN_TEMP_CODE; remote_state |= COOLIX_FAN_TEMP_CODE;

View file

@ -135,7 +135,7 @@ uint8_t DaikinClimate::temperature_() {
case climate::CLIMATE_MODE_DRY: case climate::CLIMATE_MODE_DRY:
return 0xc0; return 0xc0;
default: 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; return temperature << 1;
} }
} }

View file

@ -6,7 +6,7 @@ namespace fan {
FanSpeed speed_level_to_enum(int speed_level, int supported_speed_levels) { 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 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); return static_cast<FanSpeed>(legacy_level - 1);
} }

View file

@ -54,7 +54,7 @@ void FanStateCall::perform() const {
} }
if (this->speed_.has_value()) { if (this->speed_.has_value()) {
const int speed_count = this->state_->get_traits().supported_speed_count(); 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{}; FanStateRTCState saved{};

View file

@ -110,7 +110,7 @@ void FujitsuGeneralClimate::transmit_state() {
// Set temperature // Set temperature
uint8_t temperature_clamped = 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; uint8_t temperature_offset = temperature_clamped - FUJITSU_GENERAL_TEMP_MIN;
SET_NIBBLE(remote_state, FUJITSU_GENERAL_TEMPERATURE_NIBBLE, temperature_offset); SET_NIBBLE(remote_state, FUJITSU_GENERAL_TEMPERATURE_NIBBLE, temperature_offset);

View file

@ -42,8 +42,8 @@ void MitsubishiClimate::transmit_state() {
break; break;
} }
remote_state[7] = remote_state[7] = (uint8_t) roundf(clamp<float>(this->target_temperature, MITSUBISHI_TEMP_MIN, MITSUBISHI_TEMP_MAX) -
(uint8_t) roundf(clamp(this->target_temperature, MITSUBISHI_TEMP_MIN, MITSUBISHI_TEMP_MAX) - MITSUBISHI_TEMP_MIN); MITSUBISHI_TEMP_MIN);
ESP_LOGV(TAG, "Sending Mitsubishi target temp: %.1f state: %02X mode: %02X temp: %02X", this->target_temperature, 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]); remote_state[5], remote_state[6], remote_state[7]);

View file

@ -130,7 +130,7 @@ void SSD1306::update() {
} }
void SSD1306::set_brightness(float brightness) { void SSD1306::set_brightness(float brightness) {
// validation // validation
this->brightness_ = clamp(brightness, 0, 1); this->brightness_ = clamp(brightness, 0.0F, 1.0F);
// now write the new brightness level to the display // now write the new brightness level to the display
this->command(SSD1306_COMMAND_SET_CONTRAST); this->command(SSD1306_COMMAND_SET_CONTRAST);
this->command(int(SSD1306_MAX_CONTRAST * (this->brightness_))); this->command(int(SSD1306_MAX_CONTRAST * (this->brightness_)));

View file

@ -126,7 +126,7 @@ void SSD1322::update() {
this->display(); this->display();
} }
void SSD1322::set_brightness(float brightness) { 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 // now write the new brightness level to the display
this->command(SSD1322_SETCONTRAST); this->command(SSD1322_SETCONTRAST);
this->data(int(SSD1322_MAX_CONTRAST * (this->brightness_))); this->data(int(SSD1322_MAX_CONTRAST * (this->brightness_)));

View file

@ -100,7 +100,7 @@ void SSD1327::update() {
} }
void SSD1327::set_brightness(float brightness) { void SSD1327::set_brightness(float brightness) {
// validation // validation
this->brightness_ = clamp(brightness, 0, 1); this->brightness_ = clamp(brightness, 0.0F, 1.0F);
// now write the new brightness level to the display // now write the new brightness level to the display
this->command(SSD1327_SETCONTRAST); this->command(SSD1327_SETCONTRAST);
this->command(int(SSD1327_MAX_CONTRAST * (this->brightness_))); this->command(int(SSD1327_MAX_CONTRAST * (this->brightness_)));

View file

@ -97,7 +97,7 @@ void SSD1331::update() {
} }
void SSD1331::set_brightness(float brightness) { void SSD1331::set_brightness(float brightness) {
// validation // validation
this->brightness_ = clamp(brightness, 0, 1); this->brightness_ = clamp(brightness, 0.0F, 1.0F);
// now write the new brightness level to the display // now write the new brightness level to the display
this->command(SSD1331_CONTRASTA); // 0x81 this->command(SSD1331_CONTRASTA); // 0x81
this->command(int(SSD1331_MAX_CONTRASTA * (this->brightness_))); this->command(int(SSD1331_MAX_CONTRASTA * (this->brightness_)));

View file

@ -287,13 +287,16 @@ void HighFrequencyLoopRequester::stop() {
} }
bool HighFrequencyLoopRequester::is_high_frequency() { return high_freq_num_requests > 0; } bool HighFrequencyLoopRequester::is_high_frequency() { return high_freq_num_requests > 0; }
float clamp(float val, float min, float max) { template<typename T> T clamp(const T val, const T min, const T max) {
if (val < min) if (val < min)
return min; return min;
if (val > max) if (val > max)
return max; return max;
return val; return val;
} }
template float clamp(float, float, float);
template int clamp(int, int, int);
float lerp(float completion, float start, float end) { return start + (end - start) * completion; } float lerp(float completion, float start, float end) { return start + (end - start) * completion; }
bool str_startswith(const std::string &full, const std::string &start) { return full.rfind(start, 0) == 0; } bool str_startswith(const std::string &full, const std::string &start) { return full.rfind(start, 0) == 0; }

View file

@ -80,7 +80,7 @@ class HighFrequencyLoopRequester {
* @param max The maximum value. * @param max The maximum value.
* @return val clamped in between min and max. * @return val clamped in between min and max.
*/ */
float clamp(float val, float min, float max); template<typename T> T clamp(T val, T min, T max);
/** Linearly interpolate between end start and end by completion. /** Linearly interpolate between end start and end by completion.
* *