diff --git a/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp b/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp index e1ac1028bf..a60b7b6138 100644 --- a/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp +++ b/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp @@ -4,30 +4,30 @@ namespace esphome { namespace climate_ir_samsung { void SamsungClimateIR::transmit_state() { - if (current_climate_mode != climate::ClimateMode::CLIMATE_MODE_OFF && + if (current_climate_mode_ != climate::ClimateMode::CLIMATE_MODE_OFF && this->mode == climate::ClimateMode::CLIMATE_MODE_OFF) { - setAndSendPowerState(false); + set_and_send_power_state_(false); return; } - if (current_climate_mode == climate::ClimateMode::CLIMATE_MODE_OFF && + if (current_climate_mode_ == climate::ClimateMode::CLIMATE_MODE_OFF && this->mode != climate::ClimateMode::CLIMATE_MODE_OFF) { - setAndSendPowerState(true); + set_and_send_power_state_(true); } - current_climate_mode = this->mode; + current_climate_mode_ = this->mode; - setMode(this->mode); - setTemp(this->target_temperature); - setSwing(this->swing_mode); - setFan(this->fan_mode.has_value() ? this->fan_mode.value() : climate::CLIMATE_FAN_AUTO); + set_mode_(this->mode); + set_temp_(this->target_temperature); + set_swing_(this->swing_mode); + set_fan_(this->fan_mode.has_value() ? this->fan_mode.value() : climate::CLIMATE_FAN_AUTO); - send(); + send_(); } /// Send the current state of the climate object. -void SamsungClimateIR::send() { - checksum(); +void SamsungClimateIR::send_() { + checksum_(); auto transmit = this->transmitter_->transmit(); auto *data = transmit.get_data(); @@ -46,7 +46,7 @@ void SamsungClimateIR::send() { data->space(SAMSUNG_AIRCON1_HDR_SPACE); } - uint8_t sendByte = protocol.raw[i]; + uint8_t sendByte = protocol_.raw[i]; for (int y = 0; y < 8; y++) { if (sendByte & 0x01) { @@ -70,22 +70,22 @@ void SamsungClimateIR::send() { } /// Set the vertical swing setting of the A/C. -void SamsungClimateIR::setSwing(const climate::ClimateSwingMode swingMode) { - switch (swingMode) { +void SamsungClimateIR::set_swing_(const climate::ClimateSwingMode swing_mode) { + switch (swing_mode) { case climate::ClimateSwingMode::CLIMATE_SWING_BOTH: - protocol.Swing = kSamsungAcSwingBoth; + protocol_.Swing = K_SAMSUNG_AC_SWING_BOTH; break; case climate::ClimateSwingMode::CLIMATE_SWING_HORIZONTAL: - protocol.Swing = kSamsungAcSwingH; + protocol_.Swing = K_SAMSUNG_AC_SWING_H; ; break; case climate::ClimateSwingMode::CLIMATE_SWING_VERTICAL: - protocol.Swing = kSamsungAcSwingV; + protocol_.Swing = K_SAMSUNG_AC_SWING_V; ; break; case climate::ClimateSwingMode::CLIMATE_SWING_OFF: default: - protocol.Swing = kSamsungAcSwingOff; + protocol_.Swing = K_SAMSUNG_AC_SWING_OFF; ; break; } @@ -93,102 +93,102 @@ void SamsungClimateIR::setSwing(const climate::ClimateSwingMode swingMode) { /// Set the operating mode of the A/C. /// @param[in] climateMode The desired operating mode. -void SamsungClimateIR::setMode(const climate::ClimateMode climateMode) { - switch (climateMode) { +void SamsungClimateIR::set_mode_(const climate::ClimateMode climate_mode) { + switch (climate_mode) { case climate::ClimateMode::CLIMATE_MODE_HEAT: - protocol.Mode = kSamsungAcHeat; + protocol_.Mode = K_SAMSUNG_AC_HEAT; break; case climate::ClimateMode::CLIMATE_MODE_DRY: - protocol.Mode = kSamsungAcDry; + protocol_.Mode = K_SAMSUNG_AC_DRY; break; case climate::ClimateMode::CLIMATE_MODE_COOL: - protocol.Mode = kSamsungAcCool; + protocol_.Mode = K_SAMSUNG_AC_COOL; break; case climate::ClimateMode::CLIMATE_MODE_FAN_ONLY: - protocol.Mode = kSamsungAcFan; + protocol_.Mode = K_SAMSUNG_AC_FAN; break; case climate::ClimateMode::CLIMATE_MODE_HEAT_COOL: case climate::ClimateMode::CLIMATE_MODE_AUTO: default: - protocol.Mode = kSamsungAcAuto; + protocol_.Mode = K_SAMSUNG_AC_AUTO; break; } } /// Set the temperature. /// @param[in] temp The temperature in degrees celsius. -void SamsungClimateIR::setTemp(const uint8_t temp) { - uint8_t newtemp = std::max(kSamsungAcMinTemp, temp); - newtemp = std::min(kSamsungAcMaxTemp, newtemp); - protocol.Temp = newtemp - kSamsungAcMinTemp; +void SamsungClimateIR::set_temp_(const uint8_t temp) { + uint8_t newtemp = std::max(K_SAMSUNG_AC_MIN_TEMP, temp); + newtemp = std::min(K_SAMSUNG_AC_MAX_TEMP, newtemp); + protocol_.Temp = newtemp - K_SAMSUNG_AC_MIN_TEMP; } /// Change the AC power state. /// @param[in] on true, the AC is on. false, the AC is off. -void SamsungClimateIR::setAndSendPowerState(const bool on) { - static const uint8_t kOn[kSamsungAcExtendedStateLength] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0, +void SamsungClimateIR::set_and_send_power_state_(const bool on) { + static const uint8_t kOn[K_SAMSUNG_AC_EXTENDED_STATE_LENGTH] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0, 0x01, 0xD2, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x01, 0xE2, 0xFE, 0x71, 0x80, 0x11, 0xF0}; - static const uint8_t kOff[kSamsungAcExtendedStateLength] = {0x02, 0xB2, 0x0F, 0x00, 0x00, 0x00, 0xC0, + static const uint8_t kOff[K_SAMSUNG_AC_EXTENDED_STATE_LENGTH] = {0x02, 0xB2, 0x0F, 0x00, 0x00, 0x00, 0xC0, 0x01, 0xD2, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0xFF, 0x71, 0x80, 0x11, 0xC0}; - std::memcpy(protocol.raw, on ? kOn : kOff, kSamsungAcExtendedStateLength); + std::memcpy(protocol_.raw, on ? kOn : kOff, K_SAMSUNG_AC_EXTENDED_STATE_LENGTH); - send(); + send_(); - std::memcpy(protocol.raw, kReset, kSamsungAcExtendedStateLength); + std::memcpy(protocol_.raw, K_RESET, K_SAMSUNG_AC_EXTENDED_STATE_LENGTH); } /// Set the fan speed. -void SamsungClimateIR::setFan(const climate::ClimateFanMode fanMode) { - switch (fanMode) { +void SamsungClimateIR::set_fan_(const climate::ClimateFanMode fan_mode) { + switch (fan_mode) { case climate::ClimateFanMode::CLIMATE_FAN_LOW: - protocol.Fan = kSamsungAcFanAuto; + protocol_.Fan = K_SAMSUNG_AC_FAN_LOW; break; case climate::ClimateFanMode::CLIMATE_FAN_MEDIUM: - protocol.Fan = kSamsungAcFanMed; + protocol_.Fan = K_SAMSUNG_AC_FAN_MED; break; case climate::ClimateFanMode::CLIMATE_FAN_HIGH: - protocol.Fan = kSamsungAcFanHigh; + protocol_.Fan = K_SAMSUNG_AC_FAN_HIGH; break; case climate::ClimateFanMode::CLIMATE_FAN_AUTO: default: - protocol.Fan = kSamsungAcFanAuto; + protocol_.Fan = K_SAMSUNG_AC_FAN_AUTO; break; } } -/// Calculate the checksum for a given state section. -/// @param[in] section The array to calc the checksum of. -/// @return The calculated checksum value. +/// Calculate the checksum_ for a given state section. +/// @param[in] section The array to calc the checksum_ of. +/// @return The calculated checksum_ value. /// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1538#issuecomment-894645947 -uint8_t SamsungClimateIR::calcSectionChecksum(const uint8_t *section) { +uint8_t SamsungClimateIR::calc_section_checksum(const uint8_t *section) { uint8_t sum = 0; - sum += countBits(*section, 8); // Include the entire first byte + sum += count_bits(*section, 8); // Include the entire first byte // The lower half of the second byte. - sum += countBits(GETBITS8(*(section + 1), kLowNibble, kNibbleSize), 8); + sum += count_bits(GETBITS8(*(section + 1), K_LOW_NIBBLE, K_NIBBLE_SIZE), 8); // The upper half of the third byte. - sum += countBits(GETBITS8(*(section + 2), kHighNibble, kNibbleSize), 8); + sum += count_bits(GETBITS8(*(section + 2), K_HIGH_NIBBLE, K_NIBBLE_SIZE), 8); // The next 4 bytes. - sum += countBits(section + 3, 4); + sum += count_bits(section + 3, 4); // Bitwise invert the result. return sum ^ UINT8_MAX; } -/// Update the checksum for the internal state. -void SamsungClimateIR::checksum(void) { - uint8_t sectionsum = calcSectionChecksum(protocol.raw); - protocol.Sum1Upper = GETBITS8(sectionsum, kHighNibble, kNibbleSize); - protocol.Sum1Lower = GETBITS8(sectionsum, kLowNibble, kNibbleSize); - sectionsum = calcSectionChecksum(protocol.raw + kSamsungAcSectionLength); - protocol.Sum2Upper = GETBITS8(sectionsum, kHighNibble, kNibbleSize); - protocol.Sum2Lower = GETBITS8(sectionsum, kLowNibble, kNibbleSize); - sectionsum = calcSectionChecksum(protocol.raw + kSamsungAcSectionLength * 2); - protocol.Sum3Upper = GETBITS8(sectionsum, kHighNibble, kNibbleSize); - protocol.Sum3Lower = GETBITS8(sectionsum, kLowNibble, kNibbleSize); +/// Update the checksum_ for the internal state. +void SamsungClimateIR::checksum_(void) { + uint8_t sectionsum = calc_section_checksum(protocol_.raw); + protocol_.Sum1Upper = GETBITS8(sectionsum, K_HIGH_NIBBLE, K_NIBBLE_SIZE); + protocol_.Sum1Lower = GETBITS8(sectionsum, K_LOW_NIBBLE, K_NIBBLE_SIZE); + sectionsum = calc_section_checksum(protocol_.raw + K_SAMSUNG_AC_SECTION_LENGTH); + protocol_.Sum2Upper = GETBITS8(sectionsum, K_HIGH_NIBBLE, K_NIBBLE_SIZE); + protocol_.Sum2Lower = GETBITS8(sectionsum, K_LOW_NIBBLE, K_NIBBLE_SIZE); + sectionsum = calc_section_checksum(protocol_.raw + K_SAMSUNG_AC_SECTION_LENGTH * 2); + protocol_.Sum3Upper = GETBITS8(sectionsum, K_HIGH_NIBBLE, K_NIBBLE_SIZE); + protocol_.Sum3Lower = GETBITS8(sectionsum, K_LOW_NIBBLE, K_NIBBLE_SIZE); } /// Count the number of bits of a certain type in an array. @@ -197,7 +197,7 @@ void SamsungClimateIR::checksum(void) { /// @param[in] ones Count the binary nr of `1` bits. False is count the `0`s. /// @param[in] init Starting value of the calculation to use. (Default is 0) /// @return The nr. of bits found of the given type found in the array. -uint16_t SamsungClimateIR::countBits(const uint8_t *const start, const uint16_t length, const bool ones, +uint16_t SamsungClimateIR::count_bits(const uint8_t *const start, const uint16_t length, const bool ones, const uint16_t init) { uint16_t count = init; @@ -218,7 +218,7 @@ uint16_t SamsungClimateIR::countBits(const uint8_t *const start, const uint16_t /// @param[in] ones Count the binary nr of `1` bits. False is count the `0`s. /// @param[in] init Starting value of the calculation to use. (Default is 0) /// @return The nr. of bits found of the given type found in the Integer. -uint16_t SamsungClimateIR::countBits(const uint64_t data, const uint8_t length, const bool ones, const uint16_t init) { +uint16_t SamsungClimateIR::count_bits(const uint64_t data, const uint8_t length, const bool ones, const uint16_t init) { uint16_t count = init; uint8_t bitsSoFar = length; diff --git a/esphome/components/climate_ir_samsung/climate_ir_samsung.h b/esphome/components/climate_ir_samsung/climate_ir_samsung.h index a1b5902971..0caaf3c335 100644 --- a/esphome/components/climate_ir_samsung/climate_ir_samsung.h +++ b/esphome/components/climate_ir_samsung/climate_ir_samsung.h @@ -17,49 +17,43 @@ static const int SAMSUNG_AIRCON1_ONE_SPACE = 1500; static const int SAMSUNG_AIRCON1_ZERO_SPACE = 500; static const int SAMSUNG_AIRCON1_MSG_SPACE = 2000; -const uint16_t kSamsungAcExtendedStateLength = 21; -const uint16_t kSamsungAcSectionLength = 7; +const uint16_t K_SAMSUNG_AC_EXTENDED_STATE_LENGTH = 21; +const uint16_t K_SAMSUNG_AC_SECTION_LENGTH = 7; // Temperature -const uint8_t kSamsungAcMinTemp = 16; // C Mask 0b11110000 -const uint8_t kSamsungAcMaxTemp = 30; // C Mask 0b11110000 -const uint8_t kSamsungAcAutoTemp = 25; // C Mask 0b11110000 +const uint8_t K_SAMSUNG_AC_MIN_TEMP = 16; // C Mask 0b11110000 +const uint8_t K_SAMSUNG_AC_MAX_TEMP = 30; // C Mask 0b11110000 // Mode -const uint8_t kSamsungAcAuto = 0; -const uint8_t kSamsungAcCool = 1; -const uint8_t kSamsungAcDry = 2; -const uint8_t kSamsungAcFan = 3; -const uint8_t kSamsungAcHeat = 4; +const uint8_t K_SAMSUNG_AC_AUTO = 0; +const uint8_t K_SAMSUNG_AC_COOL = 1; +const uint8_t K_SAMSUNG_AC_DRY = 2; +const uint8_t K_SAMSUNG_AC_FAN = 3; +const uint8_t K_SAMSUNG_AC_HEAT = 4; // Fan -const uint8_t kSamsungAcFanAuto = 0; -const uint8_t kSamsungAcFanLow = 2; -const uint8_t kSamsungAcFanMed = 4; -const uint8_t kSamsungAcFanHigh = 5; -const uint8_t kSamsungAcFanAuto2 = 6; -const uint8_t kSamsungAcFanTurbo = 7; +const uint8_t K_SAMSUNG_AC_FAN_AUTO = 0; +const uint8_t K_SAMSUNG_AC_FAN_LOW = 2; +const uint8_t K_SAMSUNG_AC_FAN_MED = 4; +const uint8_t K_SAMSUNG_AC_FAN_HIGH = 5; // Swing -const uint8_t kSamsungAcSwingV = 0b010; -const uint8_t kSamsungAcSwingH = 0b011; -const uint8_t kSamsungAcSwingBoth = 0b100; -const uint8_t kSamsungAcSwingOff = 0b111; +const uint8_t K_SAMSUNG_AC_SWING_V = 0b010; +const uint8_t K_SAMSUNG_AC_SWING_H = 0b011; +const uint8_t K_SAMSUNG_AC_SWING_BOTH = 0b100; +const uint8_t K_SAMSUNG_AC_SWING_OFF = 0b111; // Power -const uint8_t kNibbleSize = 4; -const uint8_t kLowNibble = 0; -const uint8_t kHighNibble = 4; -const uint8_t kModeBitsSize = 3; +const uint8_t K_NIBBLE_SIZE = 4; +const uint8_t K_LOW_NIBBLE = 0; +const uint8_t K_HIGH_NIBBLE = 4; -// static const uint8_t kReset[kSamsungAcExtendedStateLength] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0, 0x01, 0x02, -// 0xAE, 0x71, 0x00, 0x15, 0xF0}; -static const uint8_t kReset[kSamsungAcExtendedStateLength] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0, +static const uint8_t K_RESET[K_SAMSUNG_AC_EXTENDED_STATE_LENGTH] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0, 0x01, 0x02, 0xAE, 0x71, 0x00, 0x15, 0xF0}; /// Native representation of a Samsung A/C message. union SamsungProtocol { - uint8_t raw[kSamsungAcExtendedStateLength]; ///< State in code form. + uint8_t raw[K_SAMSUNG_AC_EXTENDED_STATE_LENGTH]; ///< State in code form. struct { // Standard message map // Byte 0 uint8_t : 8; @@ -179,12 +173,12 @@ union SamsungProtocol { }; class SamsungClimateIR : public climate_ir::ClimateIR { - SamsungProtocol protocol; - climate::ClimateMode current_climate_mode; + SamsungProtocol protocol_; + climate::ClimateMode current_climate_mode_; public: SamsungClimateIR() - : climate_ir::ClimateIR(kSamsungAcMinTemp, kSamsungAcMaxTemp, 1.0f, true, true, + : climate_ir::ClimateIR(K_SAMSUNG_AC_MIN_TEMP, K_SAMSUNG_AC_MAX_TEMP, 1.0f, true, true, {climate::CLIMATE_FAN_AUTO, climate::CLIMATE_FAN_LOW, climate::CLIMATE_FAN_MEDIUM, climate::CLIMATE_FAN_HIGH}, {climate::CLIMATE_SWING_OFF, climate::CLIMATE_SWING_VERTICAL, @@ -193,18 +187,18 @@ class SamsungClimateIR : public climate_ir::ClimateIR { protected: void transmit_state() override; - void send(); - void setSwing(const climate::ClimateSwingMode swingMode); - void setMode(const climate::ClimateMode mode); - void setTemp(const uint8_t temp); - void setAndSendPowerState(const bool on); - void setFan(const climate::ClimateFanMode fanMode); + void send_(); + void set_swing_(const climate::ClimateSwingMode swing_mode); + void set_mode_(const climate::ClimateMode climate_mode); + void set_temp_(const uint8_t temp); + void set_and_send_power_state_(const bool on); + void set_fan_(const climate::ClimateFanMode fan_mode); - void checksum(void); - static uint8_t calcSectionChecksum(const uint8_t *section); - static uint16_t countBits(const uint8_t *const start, const uint16_t length, const bool ones = true, + void checksum_(); + static uint8_t calc_section_checksum(const uint8_t *section); + static uint16_t count_bits(const uint8_t *const start, const uint16_t length, const bool ones = true, const uint16_t init = 0); - static uint16_t countBits(const uint64_t data, const uint8_t length, const bool ones = true, const uint16_t init = 0); + static uint16_t count_bits(const uint64_t data, const uint8_t length, const bool ones = true, const uint16_t init = 0); }; } // namespace climate_ir_samsung } // namespace esphome