1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-01 08:18:16 +00:00

Rename variables to follow project convention

This commit is contained in:
Georgi Filipov 2024-12-24 02:47:49 +02:00
parent 534d4822a0
commit b607f616cc
2 changed files with 98 additions and 104 deletions

View File

@ -4,30 +4,30 @@ namespace esphome {
namespace climate_ir_samsung { namespace climate_ir_samsung {
void SamsungClimateIR::transmit_state() { 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) { this->mode == climate::ClimateMode::CLIMATE_MODE_OFF) {
setAndSendPowerState(false); set_and_send_power_state_(false);
return; 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) { 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); set_mode_(this->mode);
setTemp(this->target_temperature); set_temp_(this->target_temperature);
setSwing(this->swing_mode); set_swing_(this->swing_mode);
setFan(this->fan_mode.has_value() ? this->fan_mode.value() : climate::CLIMATE_FAN_AUTO); 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. /// Send the current state of the climate object.
void SamsungClimateIR::send() { void SamsungClimateIR::send_() {
checksum(); checksum_();
auto transmit = this->transmitter_->transmit(); auto transmit = this->transmitter_->transmit();
auto *data = transmit.get_data(); auto *data = transmit.get_data();
@ -46,7 +46,7 @@ void SamsungClimateIR::send() {
data->space(SAMSUNG_AIRCON1_HDR_SPACE); 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++) { for (int y = 0; y < 8; y++) {
if (sendByte & 0x01) { if (sendByte & 0x01) {
@ -70,22 +70,22 @@ void SamsungClimateIR::send() {
} }
/// Set the vertical swing setting of the A/C. /// Set the vertical swing setting of the A/C.
void SamsungClimateIR::setSwing(const climate::ClimateSwingMode swingMode) { void SamsungClimateIR::set_swing_(const climate::ClimateSwingMode swing_mode) {
switch (swingMode) { switch (swing_mode) {
case climate::ClimateSwingMode::CLIMATE_SWING_BOTH: case climate::ClimateSwingMode::CLIMATE_SWING_BOTH:
protocol.Swing = kSamsungAcSwingBoth; protocol_.Swing = K_SAMSUNG_AC_SWING_BOTH;
break; break;
case climate::ClimateSwingMode::CLIMATE_SWING_HORIZONTAL: case climate::ClimateSwingMode::CLIMATE_SWING_HORIZONTAL:
protocol.Swing = kSamsungAcSwingH; protocol_.Swing = K_SAMSUNG_AC_SWING_H;
; ;
break; break;
case climate::ClimateSwingMode::CLIMATE_SWING_VERTICAL: case climate::ClimateSwingMode::CLIMATE_SWING_VERTICAL:
protocol.Swing = kSamsungAcSwingV; protocol_.Swing = K_SAMSUNG_AC_SWING_V;
; ;
break; break;
case climate::ClimateSwingMode::CLIMATE_SWING_OFF: case climate::ClimateSwingMode::CLIMATE_SWING_OFF:
default: default:
protocol.Swing = kSamsungAcSwingOff; protocol_.Swing = K_SAMSUNG_AC_SWING_OFF;
; ;
break; break;
} }
@ -93,102 +93,102 @@ void SamsungClimateIR::setSwing(const climate::ClimateSwingMode swingMode) {
/// Set the operating mode of the A/C. /// Set the operating mode of the A/C.
/// @param[in] climateMode The desired operating mode. /// @param[in] climateMode The desired operating mode.
void SamsungClimateIR::setMode(const climate::ClimateMode climateMode) { void SamsungClimateIR::set_mode_(const climate::ClimateMode climate_mode) {
switch (climateMode) { switch (climate_mode) {
case climate::ClimateMode::CLIMATE_MODE_HEAT: case climate::ClimateMode::CLIMATE_MODE_HEAT:
protocol.Mode = kSamsungAcHeat; protocol_.Mode = K_SAMSUNG_AC_HEAT;
break; break;
case climate::ClimateMode::CLIMATE_MODE_DRY: case climate::ClimateMode::CLIMATE_MODE_DRY:
protocol.Mode = kSamsungAcDry; protocol_.Mode = K_SAMSUNG_AC_DRY;
break; break;
case climate::ClimateMode::CLIMATE_MODE_COOL: case climate::ClimateMode::CLIMATE_MODE_COOL:
protocol.Mode = kSamsungAcCool; protocol_.Mode = K_SAMSUNG_AC_COOL;
break; break;
case climate::ClimateMode::CLIMATE_MODE_FAN_ONLY: case climate::ClimateMode::CLIMATE_MODE_FAN_ONLY:
protocol.Mode = kSamsungAcFan; protocol_.Mode = K_SAMSUNG_AC_FAN;
break; break;
case climate::ClimateMode::CLIMATE_MODE_HEAT_COOL: case climate::ClimateMode::CLIMATE_MODE_HEAT_COOL:
case climate::ClimateMode::CLIMATE_MODE_AUTO: case climate::ClimateMode::CLIMATE_MODE_AUTO:
default: default:
protocol.Mode = kSamsungAcAuto; protocol_.Mode = K_SAMSUNG_AC_AUTO;
break; break;
} }
} }
/// Set the temperature. /// Set the temperature.
/// @param[in] temp The temperature in degrees celsius. /// @param[in] temp The temperature in degrees celsius.
void SamsungClimateIR::setTemp(const uint8_t temp) { void SamsungClimateIR::set_temp_(const uint8_t temp) {
uint8_t newtemp = std::max(kSamsungAcMinTemp, temp); uint8_t newtemp = std::max(K_SAMSUNG_AC_MIN_TEMP, temp);
newtemp = std::min(kSamsungAcMaxTemp, newtemp); newtemp = std::min(K_SAMSUNG_AC_MAX_TEMP, newtemp);
protocol.Temp = newtemp - kSamsungAcMinTemp; protocol_.Temp = newtemp - K_SAMSUNG_AC_MIN_TEMP;
} }
/// Change the AC power state. /// Change the AC power state.
/// @param[in] on true, the AC is on. false, the AC is off. /// @param[in] on true, the AC is on. false, the AC is off.
void SamsungClimateIR::setAndSendPowerState(const bool on) { void SamsungClimateIR::set_and_send_power_state_(const bool on) {
static const uint8_t kOn[kSamsungAcExtendedStateLength] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0, 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, 0xD2, 0x0F, 0x00, 0x00, 0x00, 0x00,
0x01, 0xE2, 0xFE, 0x71, 0x80, 0x11, 0xF0}; 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, 0xD2, 0x0F, 0x00, 0x00, 0x00, 0x00,
0x01, 0x02, 0xFF, 0x71, 0x80, 0x11, 0xC0}; 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. /// Set the fan speed.
void SamsungClimateIR::setFan(const climate::ClimateFanMode fanMode) { void SamsungClimateIR::set_fan_(const climate::ClimateFanMode fan_mode) {
switch (fanMode) { switch (fan_mode) {
case climate::ClimateFanMode::CLIMATE_FAN_LOW: case climate::ClimateFanMode::CLIMATE_FAN_LOW:
protocol.Fan = kSamsungAcFanAuto; protocol_.Fan = K_SAMSUNG_AC_FAN_LOW;
break; break;
case climate::ClimateFanMode::CLIMATE_FAN_MEDIUM: case climate::ClimateFanMode::CLIMATE_FAN_MEDIUM:
protocol.Fan = kSamsungAcFanMed; protocol_.Fan = K_SAMSUNG_AC_FAN_MED;
break; break;
case climate::ClimateFanMode::CLIMATE_FAN_HIGH: case climate::ClimateFanMode::CLIMATE_FAN_HIGH:
protocol.Fan = kSamsungAcFanHigh; protocol_.Fan = K_SAMSUNG_AC_FAN_HIGH;
break; break;
case climate::ClimateFanMode::CLIMATE_FAN_AUTO: case climate::ClimateFanMode::CLIMATE_FAN_AUTO:
default: default:
protocol.Fan = kSamsungAcFanAuto; protocol_.Fan = K_SAMSUNG_AC_FAN_AUTO;
break; break;
} }
} }
/// Calculate the checksum for a given state section. /// Calculate the checksum_ for a given state section.
/// @param[in] section The array to calc the checksum of. /// @param[in] section The array to calc the checksum_ of.
/// @return The calculated checksum value. /// @return The calculated checksum_ value.
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1538#issuecomment-894645947 /// @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; 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. // 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. // 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. // The next 4 bytes.
sum += countBits(section + 3, 4); sum += count_bits(section + 3, 4);
// Bitwise invert the result. // Bitwise invert the result.
return sum ^ UINT8_MAX; return sum ^ UINT8_MAX;
} }
/// Update the checksum for the internal state. /// Update the checksum_ for the internal state.
void SamsungClimateIR::checksum(void) { void SamsungClimateIR::checksum_(void) {
uint8_t sectionsum = calcSectionChecksum(protocol.raw); uint8_t sectionsum = calc_section_checksum(protocol_.raw);
protocol.Sum1Upper = GETBITS8(sectionsum, kHighNibble, kNibbleSize); protocol_.Sum1Upper = GETBITS8(sectionsum, K_HIGH_NIBBLE, K_NIBBLE_SIZE);
protocol.Sum1Lower = GETBITS8(sectionsum, kLowNibble, kNibbleSize); protocol_.Sum1Lower = GETBITS8(sectionsum, K_LOW_NIBBLE, K_NIBBLE_SIZE);
sectionsum = calcSectionChecksum(protocol.raw + kSamsungAcSectionLength); sectionsum = calc_section_checksum(protocol_.raw + K_SAMSUNG_AC_SECTION_LENGTH);
protocol.Sum2Upper = GETBITS8(sectionsum, kHighNibble, kNibbleSize); protocol_.Sum2Upper = GETBITS8(sectionsum, K_HIGH_NIBBLE, K_NIBBLE_SIZE);
protocol.Sum2Lower = GETBITS8(sectionsum, kLowNibble, kNibbleSize); protocol_.Sum2Lower = GETBITS8(sectionsum, K_LOW_NIBBLE, K_NIBBLE_SIZE);
sectionsum = calcSectionChecksum(protocol.raw + kSamsungAcSectionLength * 2); sectionsum = calc_section_checksum(protocol_.raw + K_SAMSUNG_AC_SECTION_LENGTH * 2);
protocol.Sum3Upper = GETBITS8(sectionsum, kHighNibble, kNibbleSize); protocol_.Sum3Upper = GETBITS8(sectionsum, K_HIGH_NIBBLE, K_NIBBLE_SIZE);
protocol.Sum3Lower = GETBITS8(sectionsum, kLowNibble, kNibbleSize); protocol_.Sum3Lower = GETBITS8(sectionsum, K_LOW_NIBBLE, K_NIBBLE_SIZE);
} }
/// Count the number of bits of a certain type in an array. /// 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] 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) /// @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. /// @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) { const uint16_t init) {
uint16_t count = 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] 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) /// @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. /// @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; uint16_t count = init;
uint8_t bitsSoFar = length; uint8_t bitsSoFar = length;

View File

@ -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_ZERO_SPACE = 500;
static const int SAMSUNG_AIRCON1_MSG_SPACE = 2000; static const int SAMSUNG_AIRCON1_MSG_SPACE = 2000;
const uint16_t kSamsungAcExtendedStateLength = 21; const uint16_t K_SAMSUNG_AC_EXTENDED_STATE_LENGTH = 21;
const uint16_t kSamsungAcSectionLength = 7; const uint16_t K_SAMSUNG_AC_SECTION_LENGTH = 7;
// Temperature // Temperature
const uint8_t kSamsungAcMinTemp = 16; // C Mask 0b11110000 const uint8_t K_SAMSUNG_AC_MIN_TEMP = 16; // C Mask 0b11110000
const uint8_t kSamsungAcMaxTemp = 30; // C Mask 0b11110000 const uint8_t K_SAMSUNG_AC_MAX_TEMP = 30; // C Mask 0b11110000
const uint8_t kSamsungAcAutoTemp = 25; // C Mask 0b11110000
// Mode // Mode
const uint8_t kSamsungAcAuto = 0; const uint8_t K_SAMSUNG_AC_AUTO = 0;
const uint8_t kSamsungAcCool = 1; const uint8_t K_SAMSUNG_AC_COOL = 1;
const uint8_t kSamsungAcDry = 2; const uint8_t K_SAMSUNG_AC_DRY = 2;
const uint8_t kSamsungAcFan = 3; const uint8_t K_SAMSUNG_AC_FAN = 3;
const uint8_t kSamsungAcHeat = 4; const uint8_t K_SAMSUNG_AC_HEAT = 4;
// Fan // Fan
const uint8_t kSamsungAcFanAuto = 0; const uint8_t K_SAMSUNG_AC_FAN_AUTO = 0;
const uint8_t kSamsungAcFanLow = 2; const uint8_t K_SAMSUNG_AC_FAN_LOW = 2;
const uint8_t kSamsungAcFanMed = 4; const uint8_t K_SAMSUNG_AC_FAN_MED = 4;
const uint8_t kSamsungAcFanHigh = 5; const uint8_t K_SAMSUNG_AC_FAN_HIGH = 5;
const uint8_t kSamsungAcFanAuto2 = 6;
const uint8_t kSamsungAcFanTurbo = 7;
// Swing // Swing
const uint8_t kSamsungAcSwingV = 0b010; const uint8_t K_SAMSUNG_AC_SWING_V = 0b010;
const uint8_t kSamsungAcSwingH = 0b011; const uint8_t K_SAMSUNG_AC_SWING_H = 0b011;
const uint8_t kSamsungAcSwingBoth = 0b100; const uint8_t K_SAMSUNG_AC_SWING_BOTH = 0b100;
const uint8_t kSamsungAcSwingOff = 0b111; const uint8_t K_SAMSUNG_AC_SWING_OFF = 0b111;
// Power // Power
const uint8_t kNibbleSize = 4; const uint8_t K_NIBBLE_SIZE = 4;
const uint8_t kLowNibble = 0; const uint8_t K_LOW_NIBBLE = 0;
const uint8_t kHighNibble = 4; const uint8_t K_HIGH_NIBBLE = 4;
const uint8_t kModeBitsSize = 3;
// static const uint8_t kReset[kSamsungAcExtendedStateLength] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0, 0x01, 0x02, static const uint8_t K_RESET[K_SAMSUNG_AC_EXTENDED_STATE_LENGTH] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0,
// 0xAE, 0x71, 0x00, 0x15, 0xF0};
static const uint8_t kReset[kSamsungAcExtendedStateLength] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0,
0x01, 0x02, 0xAE, 0x71, 0x00, 0x15, 0xF0}; 0x01, 0x02, 0xAE, 0x71, 0x00, 0x15, 0xF0};
/// Native representation of a Samsung A/C message. /// Native representation of a Samsung A/C message.
union SamsungProtocol { 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 struct { // Standard message map
// Byte 0 // Byte 0
uint8_t : 8; uint8_t : 8;
@ -179,12 +173,12 @@ union SamsungProtocol {
}; };
class SamsungClimateIR : public climate_ir::ClimateIR { class SamsungClimateIR : public climate_ir::ClimateIR {
SamsungProtocol protocol; SamsungProtocol protocol_;
climate::ClimateMode current_climate_mode; climate::ClimateMode current_climate_mode_;
public: public:
SamsungClimateIR() 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_AUTO, climate::CLIMATE_FAN_LOW, climate::CLIMATE_FAN_MEDIUM,
climate::CLIMATE_FAN_HIGH}, climate::CLIMATE_FAN_HIGH},
{climate::CLIMATE_SWING_OFF, climate::CLIMATE_SWING_VERTICAL, {climate::CLIMATE_SWING_OFF, climate::CLIMATE_SWING_VERTICAL,
@ -193,18 +187,18 @@ class SamsungClimateIR : public climate_ir::ClimateIR {
protected: protected:
void transmit_state() override; void transmit_state() override;
void send(); void send_();
void setSwing(const climate::ClimateSwingMode swingMode); void set_swing_(const climate::ClimateSwingMode swing_mode);
void setMode(const climate::ClimateMode mode); void set_mode_(const climate::ClimateMode climate_mode);
void setTemp(const uint8_t temp); void set_temp_(const uint8_t temp);
void setAndSendPowerState(const bool on); void set_and_send_power_state_(const bool on);
void setFan(const climate::ClimateFanMode fanMode); void set_fan_(const climate::ClimateFanMode fan_mode);
void checksum(void); void checksum_();
static uint8_t calcSectionChecksum(const uint8_t *section); static uint8_t calc_section_checksum(const uint8_t *section);
static uint16_t countBits(const uint8_t *const start, const uint16_t length, const bool ones = true, static uint16_t count_bits(const uint8_t *const start, const uint16_t length, const bool ones = true,
const uint16_t init = 0); 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 climate_ir_samsung
} // namespace esphome } // namespace esphome