diff --git a/esphome/components/climate_ir_samsung/climate.py b/esphome/components/climate_ir_samsung/climate.py index fd1b7cc19a..7c1ba65391 100644 --- a/esphome/components/climate_ir_samsung/climate.py +++ b/esphome/components/climate_ir_samsung/climate.py @@ -7,12 +7,12 @@ CODEOWNERS = ["@jorofi"] AUTO_LOAD = ["climate_ir"] -samsung_ns = cg.esphome_ns.namespace("samsung") -SamsungClimate = samsung_ns.class_("SamsungClimate", climate_ir.ClimateIR) +samsung_ns = cg.esphome_ns.namespace("climate_ir_samsung") +SamsungClimateIR = samsung_ns.class_("SamsungClimateIR", climate_ir.ClimateIR) CONFIG_SCHEMA = climate_ir.CLIMATE_IR_WITH_RECEIVER_SCHEMA.extend( { - cv.GenerateID(): cv.declare_id(SamsungClimate) + cv.GenerateID(): cv.declare_id(SamsungClimateIR) } ) diff --git a/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp b/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp index 41b7fe7892..e79c553df8 100644 --- a/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp +++ b/esphome/components/climate_ir_samsung/climate_ir_samsung.cpp @@ -1,44 +1,4 @@ #include "climate_ir_samsung.h" -#include -#include - -/// Count the number of bits of a certain type in an array. -/// @param[in] start A ptr to the start of the byte array to calculate over. -/// @param[in] length How many bytes to use in the calculation. -/// @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 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++; - - if (ones || length == 0) - return count; - else - return (length * 8) - count; -} - -/// Count the number of bits of a certain type in an Integer. -/// @param[in] data The value you want bits counted for. Starting from the LSB. -/// @param[in] length How many bits to use in the calculation? Starts at the LSB -/// @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 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++; - - if (ones || length == 0) - return count; - else - return length - count; -} namespace esphome { namespace climate_ir_samsung { @@ -51,7 +11,6 @@ namespace climate_ir_samsung { if(current_climate_mode == climate::ClimateMode::CLIMATE_MODE_OFF && this->mode != climate::ClimateMode::CLIMATE_MODE_OFF) { setAndSendPowerState(true); - std::this_thread::sleep_for(std::chrono::seconds(2)); } current_climate_mode = this->mode; @@ -237,4 +196,42 @@ namespace climate_ir_samsung { protocol.Sum3Upper = GETBITS8(sectionsum, kHighNibble, kNibbleSize); protocol.Sum3Lower = GETBITS8(sectionsum, kLowNibble, kNibbleSize); } + + /// Count the number of bits of a certain type in an array. + /// @param[in] start A ptr to the start of the byte array to calculate over. + /// @param[in] length How many bytes to use in the calculation. + /// @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, 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++; + + if (ones || length == 0) + return count; + else + return (length * 8) - count; + } + + /// Count the number of bits of a certain type in an Integer. + /// @param[in] data The value you want bits counted for. Starting from the LSB. + /// @param[in] length How many bits to use in the calculation? Starts at the LSB + /// @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 count = init; + uint8_t bitsSoFar = length; + + for (uint64_t remainder = data; remainder && bitsSoFar; remainder >>= 1, bitsSoFar--) + if (remainder & 1) count++; + + if (ones || length == 0) + return count; + else + return length - count; + } }} diff --git a/esphome/components/climate_ir_samsung/climate_ir_samsung.h b/esphome/components/climate_ir_samsung/climate_ir_samsung.h index 5c7be0240d..7630ebdcc8 100644 --- a/esphome/components/climate_ir_samsung/climate_ir_samsung.h +++ b/esphome/components/climate_ir_samsung/climate_ir_samsung.h @@ -12,9 +12,6 @@ #define GETBITS8(data, offset, size) \ (((data) & (((uint8_t)UINT8_MAX >> (8 - (size))) << (offset))) >> (offset)) -uint16_t countBits(const uint8_t * const start, const uint16_t length, const bool ones = true, const uint16_t init = 0); -uint16_t countBits(const uint64_t data, const uint8_t length, const bool ones = true, const uint16_t init = 0); - namespace esphome { namespace climate_ir_samsung { @@ -181,12 +178,12 @@ namespace climate_ir_samsung { }; }; - class SamsungClimate : public climate_ir::ClimateIR { + class SamsungClimateIR : public climate_ir::ClimateIR { SamsungProtocol protocol; climate::ClimateMode current_climate_mode; - public: SamsungClimate() : + public: SamsungClimateIR() : climate_ir::ClimateIR( kSamsungAcMinTemp, kSamsungAcMaxTemp, 1.0f, true, true, {climate::CLIMATE_FAN_AUTO, climate::CLIMATE_FAN_LOW, climate::CLIMATE_FAN_MEDIUM, climate::CLIMATE_FAN_HIGH}, @@ -204,5 +201,7 @@ namespace climate_ir_samsung { 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, 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