From b69a124780abecfe0f7f24cb20e4902083d06b57 Mon Sep 17 00:00:00 2001 From: Georgi Filipov Date: Tue, 24 Dec 2024 01:46:55 +0200 Subject: [PATCH] Improve code quality --- .../climate_ir_samsung/climate_ir_samsung.cpp | 10 +++++----- .../climate_ir_samsung/climate_ir_samsung.h | 20 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp b/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp index e79c553df8..3c1fc83d23 100644 --- a/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp +++ b/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp @@ -19,13 +19,13 @@ namespace climate_ir_samsung { setTemp(this->target_temperature); setSwing(this->swing_mode); setFan(this->fan_mode.has_value() ? this->fan_mode.value() : climate::CLIMATE_FAN_AUTO); - + send(); } /// Send the current state of the climate object. void SamsungClimateIR::send() { - + checksum(); auto transmit = this->transmitter_->transmit(); @@ -139,7 +139,7 @@ namespace climate_ir_samsung { 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); send(); @@ -205,7 +205,7 @@ namespace climate_ir_samsung { /// @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, const uint16_t init) { uint16_t count = init; - + for (uint16_t offset = 0; offset < length; offset++) for (uint8_t currentbyte = *(start + offset); currentbyte; currentbyte >>= 1) if (currentbyte & 1) count++; @@ -225,7 +225,7 @@ namespace climate_ir_samsung { uint16_t SamsungClimateIR::countBits(const uint64_t data, const uint8_t length, const bool ones, const uint16_t init) { uint16_t count = init; uint8_t bitsSoFar = length; - + for (uint64_t remainder = data; remainder && bitsSoFar; remainder >>= 1, bitsSoFar--) if (remainder & 1) count++; diff --git a/esphome/components/climate_ir_samsung/climate_ir_samsung.h b/esphome/components/climate_ir_samsung/climate_ir_samsung.h index 7630ebdcc8..dce6123508 100644 --- a/esphome/components/climate_ir_samsung/climate_ir_samsung.h +++ b/esphome/components/climate_ir_samsung/climate_ir_samsung.h @@ -3,20 +3,22 @@ #include "esphome/components/climate/climate_mode.h" #include "esphome/components/climate_ir/climate_ir.h" -#define SAMSUNG_AIRCON1_HDR_MARK 3000 -#define SAMSUNG_AIRCON1_HDR_SPACE 9000 -#define SAMSUNG_AIRCON1_BIT_MARK 500 -#define SAMSUNG_AIRCON1_ONE_SPACE 1500 -#define SAMSUNG_AIRCON1_ZERO_SPACE 500 -#define SAMSUNG_AIRCON1_MSG_SPACE 2000 -#define GETBITS8(data, offset, size) \ - (((data) & (((uint8_t)UINT8_MAX >> (8 - (size))) << (offset))) >> (offset)) namespace esphome { namespace climate_ir_samsung { +#define GETBITS8(data, offset, size) \ + (((data) & (((uint8_t)UINT8_MAX >> (8 - (size))) << (offset))) >> (offset)) + static const char *const TAG = "samsung.climate"; static const uint32_t SAMSUNG_IR_FREQUENCY = 38000; + static const int SAMSUNG_AIRCON1_HDR_MARK = 3000; + static const int SAMSUNG_AIRCON1_HDR_SPACE = 9000; + static const int SAMSUNG_AIRCON1_BIT_MARK = 500; + 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; @@ -204,4 +206,4 @@ namespace climate_ir_samsung { static uint16_t countBits(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); }; -}} \ No newline at end of file +}}