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

Cleanup and small refactoring

This commit is contained in:
Georgi Filipov 2025-01-02 22:04:59 +02:00
parent 01b9f78576
commit 859e80e864
2 changed files with 23 additions and 31 deletions

View File

@ -1,4 +1,5 @@
#include "samsung.h" #include "samsung.h"
#include "esphome/core/helpers.h"
namespace esphome { namespace esphome {
namespace samsung { namespace samsung {
@ -6,13 +7,13 @@ namespace samsung {
void SamsungClimate::transmit_state() { void SamsungClimate::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) {
set_and_send_power_state_(false); 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) {
set_and_send_power_state_(true); send_power_state_(true);
} }
current_climate_mode_ = this->mode; current_climate_mode_ = this->mode;
@ -31,30 +32,27 @@ void SamsungClimate::send_() {
auto transmit = this->transmitter_->transmit(); auto transmit = this->transmitter_->transmit();
auto *data = transmit.get_data(); auto *data = transmit.get_data();
data->set_carrier_frequency(SAMSUNG_IR_FREQUENCY);
// Header(2) + 2 * MSG_HDR(2 * Item(2)) + 21 Bytes * 8 Bits * Bit(2) + Last Mark (1)
data->reserve(2 + 2 * 2 * 2 + 21 * 8 * 2 + 1);
data->set_carrier_frequency(SAMSUNG_IR_FREQUENCY_HZ);
// Header // Header
data->mark(SAMSUNG_AIRCON1_HDR_MARK); data->item(SAMSUNG_AIRCON1_HDR_MARK, SAMSUNG_AIRCON1_HDR_SPACE);
data->space(SAMSUNG_AIRCON1_HDR_SPACE);
for (int i = 0; i < 21; i++) { for (int i = 0; i < 21; i++) {
if (i == 7 || i == 14) { if (i == 7 || i == 14) {
data->mark(SAMSUNG_AIRCON1_BIT_MARK); data->item(SAMSUNG_AIRCON1_BIT_MARK, SAMSUNG_AIRCON1_ONE_SPACE);
data->space(SAMSUNG_AIRCON1_MSG_SPACE); data->item(SAMSUNG_AIRCON1_HDR_MARK, SAMSUNG_AIRCON1_HDR_SPACE);
data->mark(SAMSUNG_AIRCON1_HDR_MARK);
data->space(SAMSUNG_AIRCON1_HDR_SPACE);
} }
uint8_t send_byte = protocol_.raw[i]; uint8_t send_byte = protocol_.raw[i];
for (int y = 0; y < 8; y++) { for (int y = 0; y < 8; y++) {
if (send_byte & 0x01) { if (send_byte & 0x01) {
data->mark(SAMSUNG_AIRCON1_BIT_MARK); data->item(SAMSUNG_AIRCON1_BIT_MARK, SAMSUNG_AIRCON1_ONE_SPACE);
data->space(SAMSUNG_AIRCON1_ONE_SPACE);
} else { } else {
data->mark(SAMSUNG_AIRCON1_BIT_MARK); data->item(SAMSUNG_AIRCON1_BIT_MARK, SAMSUNG_AIRCON1_ZERO_SPACE);
data->space(SAMSUNG_AIRCON1_ZERO_SPACE);
} }
send_byte >>= 1; send_byte >>= 1;
@ -62,7 +60,6 @@ void SamsungClimate::send_() {
} }
data->mark(SAMSUNG_AIRCON1_BIT_MARK); data->mark(SAMSUNG_AIRCON1_BIT_MARK);
data->space(0);
transmit.perform(); transmit.perform();
} }
@ -75,16 +72,13 @@ void SamsungClimate::set_swing_(const climate::ClimateSwingMode swing_mode) {
break; break;
case climate::ClimateSwingMode::CLIMATE_SWING_HORIZONTAL: case climate::ClimateSwingMode::CLIMATE_SWING_HORIZONTAL:
protocol_.Swing = K_SAMSUNG_AC_SWING_H; protocol_.Swing = K_SAMSUNG_AC_SWING_H;
;
break; break;
case climate::ClimateSwingMode::CLIMATE_SWING_VERTICAL: case climate::ClimateSwingMode::CLIMATE_SWING_VERTICAL:
protocol_.Swing = K_SAMSUNG_AC_SWING_V; 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 = K_SAMSUNG_AC_SWING_OFF; protocol_.Swing = K_SAMSUNG_AC_SWING_OFF;
;
break; break;
} }
} }
@ -116,14 +110,12 @@ void SamsungClimate::set_mode_(const climate::ClimateMode climate_mode) {
/// Set the temperature. /// Set the temperature.
/// @param[in] temp The temperature in degrees celsius. /// @param[in] temp The temperature in degrees celsius.
void SamsungClimate::set_temp_(const uint8_t temp) { void SamsungClimate::set_temp_(const uint8_t temp) {
uint8_t new_temp = std::max(K_SAMSUNG_AC_MIN_TEMP, temp); protocol_.Temp = esphome::clamp<uint8_t>(temp, K_SAMSUNG_AC_MIN_TEMP, K_SAMSUNG_AC_MAX_TEMP);
new_temp = std::min(K_SAMSUNG_AC_MAX_TEMP, new_temp);
protocol_.Temp = new_temp - 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 SamsungClimate::set_and_send_power_state_(const bool on) { void SamsungClimate::send_power_state_(const bool on) {
static const uint8_t K_ON[K_SAMSUNG_AC_EXTENDED_STATE_LENGTH] = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0, static const uint8_t K_ON[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};

View File

@ -8,14 +8,13 @@ namespace samsung {
#define GETBITS8(data, offset, size) (((data) & (((uint8_t) UINT8_MAX >> (8 - (size))) << (offset))) >> (offset)) #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_HZ = 38000;
static const uint32_t SAMSUNG_IR_FREQUENCY = 38000; static const uint32_t SAMSUNG_AIRCON1_HDR_MARK = 3000;
static const int SAMSUNG_AIRCON1_HDR_MARK = 3000; static const uint32_t SAMSUNG_AIRCON1_HDR_SPACE = 9000;
static const int SAMSUNG_AIRCON1_HDR_SPACE = 9000; static const uint32_t SAMSUNG_AIRCON1_BIT_MARK = 500;
static const int SAMSUNG_AIRCON1_BIT_MARK = 500; static const uint32_t SAMSUNG_AIRCON1_ONE_SPACE = 1500;
static const int SAMSUNG_AIRCON1_ONE_SPACE = 1500; static const uint32_t SAMSUNG_AIRCON1_ZERO_SPACE = 500;
static const int SAMSUNG_AIRCON1_ZERO_SPACE = 500; static const uint32_t SAMSUNG_AIRCON1_MSG_SPACE = 2000;
static const int SAMSUNG_AIRCON1_MSG_SPACE = 2000;
const uint16_t K_SAMSUNG_AC_EXTENDED_STATE_LENGTH = 21; const uint16_t K_SAMSUNG_AC_EXTENDED_STATE_LENGTH = 21;
const uint16_t K_SAMSUNG_AC_SECTION_LENGTH = 7; const uint16_t K_SAMSUNG_AC_SECTION_LENGTH = 7;
@ -185,13 +184,14 @@ class SamsungClimate : public climate_ir::ClimateIR {
climate::CLIMATE_SWING_HORIZONTAL, climate::CLIMATE_SWING_BOTH}) {} climate::CLIMATE_SWING_HORIZONTAL, climate::CLIMATE_SWING_BOTH}) {}
protected: protected:
// Transmit via IR the state of this climate controller
void transmit_state() override; void transmit_state() override;
void send_(); void send_();
void send_power_state_(bool on);
void set_swing_(climate::ClimateSwingMode swing_mode); void set_swing_(climate::ClimateSwingMode swing_mode);
void set_mode_(climate::ClimateMode climate_mode); void set_mode_(climate::ClimateMode climate_mode);
void set_temp_(uint8_t temp); void set_temp_(uint8_t temp);
void set_and_send_power_state_(bool on);
void set_fan_(climate::ClimateFanMode fan_mode); void set_fan_(climate::ClimateFanMode fan_mode);
void checksum_(); void checksum_();