ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
samsung.cpp
Go to the documentation of this file.
1#include "samsung.h"
2
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
7#include <cstring>
8
9namespace esphome::samsung {
10
11ESPHOME_LOG_TAG(TAG, "samsung.climate");
12
13static constexpr uint32_t CARRIER_FREQUENCY_HZ = 38000;
14static constexpr uint32_t HEADER_MARK_US = 3000;
15static constexpr uint32_t HEADER_SPACE_US = 9000;
16static constexpr uint32_t BIT_MARK_US = 500;
17static constexpr remote_base::BitTimings BIT_TIMINGS{.bit_mark = BIT_MARK_US, .one_space = 1500, .zero_space = 500};
18static constexpr uint32_t SECTION_SPACE_US = 2000;
19
20static constexpr uint8_t EXTENDED_LENGTH = 3 * SAMSUNG_AC_SECTION_LENGTH;
21
22// A powered-on unit at 24 °C, cool mode, fan auto, swing off (from IRremoteESP8266); checksums are refilled on send
23static const uint8_t RESET_STATE[SAMSUNG_AC_STATE_LENGTH] PROGMEM = {0x02, 0x92, 0x0F, 0x00, 0x00, 0x00, 0xF0,
24 0x01, 0xE2, 0xFE, 0x71, 0x80, 0x11, 0xF0};
25// Middle section of a power on/off frame: an all-zero timer block
26static const uint8_t TIMER_SECTION[SAMSUNG_AC_SECTION_LENGTH] PROGMEM = {0x01, 0xD2, 0x0F, 0x00, 0x00, 0x00, 0x00};
27
28// Where each setting lives in the 14 byte state
29struct Field {
30 uint8_t byte;
31 uint8_t shift;
32 uint8_t mask;
33};
34static constexpr Field POWER_1{6, 4, 0b11};
35static constexpr Field SWING{9, 4, 0b111};
36static constexpr Field TEMP{11, 4, 0b1111};
37static constexpr Field FAN{12, 1, 0b111};
38static constexpr Field MODE{12, 4, 0b111};
39static constexpr Field POWER_2{13, 4, 0b11};
40static constexpr uint8_t POWER_ON = 0b11;
41
42static uint8_t get_field(const uint8_t *state, Field field) { return (state[field.byte] >> field.shift) & field.mask; }
43static void set_field(uint8_t *state, Field field, uint8_t value) {
44 state[field.byte] = (state[field.byte] & ~(field.mask << field.shift)) | ((value & field.mask) << field.shift);
45}
46static bool is_powered_on(const uint8_t *state) {
47 return get_field(state, POWER_1) == POWER_ON && get_field(state, POWER_2) == POWER_ON;
48}
49
51using climate_ir::IRCode;
53
54// Codes not listed fall back to auto (mode and fan) and to swing off
55static constexpr IRCode MODES[] PROGMEM = {
58};
59static constexpr IRCode FANS[] PROGMEM = {
64};
65static constexpr IRCode SWINGS[] PROGMEM = {
70};
71
72// Inverted count of the one bits in a section, skipping the two nibbles that hold the checksum itself.
73// See https://github.com/crankyoldgit/IRremoteESP8266/issues/1538#issuecomment-894645947
74static uint8_t section_checksum(const uint8_t *section) {
75 uint32_t ones =
76 __builtin_popcount(section[0]) + __builtin_popcount(section[1] & 0x0F) + __builtin_popcount(section[2] & 0xF0);
77 for (uint8_t i = 3; i < SAMSUNG_AC_SECTION_LENGTH; i++) {
78 ones += __builtin_popcount(section[i]);
79 }
80 return ~static_cast<uint8_t>(ones);
81}
82static uint8_t stored_checksum(const uint8_t *section) { return (section[1] >> 4) | (section[2] << 4); }
83static void store_checksum(uint8_t *section) {
84 const uint8_t sum = section_checksum(section);
85 section[1] = (section[1] & 0x0F) | (sum << 4);
86 section[2] = (section[2] & 0xF0) | (sum >> 4);
87}
88
90 : climate_ir::ClimateIR(
91 SAMSUNG_AC_TEMP_MIN, SAMSUNG_AC_TEMP_MAX, 1.0f, true, true,
95 progmem_memcpy(this->state_.data(), RESET_STATE, SAMSUNG_AC_STATE_LENGTH);
96 // The unit's power state is unknown at boot, so the first "on" command sends a power frame
97 set_field(this->state_.data(), POWER_1, 0);
98 set_field(this->state_.data(), POWER_2, 0);
99}
100
102 uint8_t *state = this->state_.data();
103 const bool was_on = is_powered_on(state);
104 const bool on = this->mode != climate::CLIMATE_MODE_OFF;
105
106 if (on) {
107 progmem_memcpy(state, RESET_STATE, SAMSUNG_AC_STATE_LENGTH);
108 set_field(state, MODE, to_ir_code(MODES, this->mode, 0));
109 } // else keep the last mode so the power off frame still carries a valid state
110
111 const uint8_t temp = clamp<uint8_t>(this->target_temperature, SAMSUNG_AC_TEMP_MIN, SAMSUNG_AC_TEMP_MAX);
112 set_field(state, TEMP, temp - SAMSUNG_AC_TEMP_MIN);
113 set_field(state, SWING, to_ir_code(SWINGS, this->swing_mode, 0b111));
114 set_field(state, FAN, to_ir_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO), 0));
115 set_field(state, POWER_1, on ? POWER_ON : 0);
116 set_field(state, POWER_2, on ? POWER_ON : 0);
117
118 if (on != was_on) {
119 // A power change is sent as an extended frame with the timer block between the two state sections
120 uint8_t timer[SAMSUNG_AC_SECTION_LENGTH];
121 progmem_memcpy(timer, TIMER_SECTION, SAMSUNG_AC_SECTION_LENGTH);
122 const uint8_t *sections[] = {state, timer, state + SAMSUNG_AC_SECTION_LENGTH};
123 this->send_sections_(sections, 3);
124 } else {
125 const uint8_t *sections[] = {state, state + SAMSUNG_AC_SECTION_LENGTH};
126 this->send_sections_(sections, 2);
127 }
128}
129
130void SamsungClimate::send_sections_(const uint8_t *const *sections, uint8_t count) {
131 auto transmit = this->transmitter_->transmit();
132 auto *data = transmit.get_data();
133
134 // Header(2) + (sections - 1) * separator(4) + bits(2 each) + trailing mark(1)
135 data->reserve(2 + (count - 1) * 4 + count * SAMSUNG_AC_SECTION_LENGTH * 8 * 2 + 1);
136 data->set_carrier_frequency(CARRIER_FREQUENCY_HZ);
137 data->item(HEADER_MARK_US, HEADER_SPACE_US);
138
139 for (uint8_t s = 0; s < count; s++) {
140 if (s != 0) {
141 data->item(BIT_MARK_US, SECTION_SPACE_US);
142 data->item(HEADER_MARK_US, HEADER_SPACE_US);
143 }
144 uint8_t section[SAMSUNG_AC_SECTION_LENGTH];
145 memcpy(section, sections[s], SAMSUNG_AC_SECTION_LENGTH);
146 store_checksum(section);
147 data->item_bytes<BIT_TIMINGS>(section, SAMSUNG_AC_SECTION_LENGTH);
148 }
149
150 data->mark(BIT_MARK_US);
151 transmit.perform();
152}
153
155 if (!data.expect_item(HEADER_MARK_US, HEADER_SPACE_US)) {
156 return false;
157 }
158
159 // Decode into a local buffer so a bad frame leaves the stored state untouched
160 uint8_t frame[EXTENDED_LENGTH];
161 uint8_t length = EXTENDED_LENGTH;
162 for (uint8_t s = 0; s < EXTENDED_LENGTH / SAMSUNG_AC_SECTION_LENGTH; s++) {
163 if (s != 0) {
164 const bool have_separator =
165 data.expect_item(BIT_MARK_US, SECTION_SPACE_US) && data.expect_item(HEADER_MARK_US, HEADER_SPACE_US);
166 if (!have_separator) {
167 // A third section is optional; a missing separator after the second one ends the frame
168 if (s >= SAMSUNG_AC_STATE_LENGTH / SAMSUNG_AC_SECTION_LENGTH) {
169 length = s * SAMSUNG_AC_SECTION_LENGTH;
170 break;
171 }
172 ESP_LOGW(TAG, "Missing section separator before section %u", s);
173 return false;
174 }
175 }
176 if (!data.expect_bytes<BIT_TIMINGS>(frame + s * SAMSUNG_AC_SECTION_LENGTH, SAMSUNG_AC_SECTION_LENGTH)) {
177 ESP_LOGW(TAG, "Bad bit in section %u", s);
178 return false;
179 }
180 }
181 if (!data.expect_mark(BIT_MARK_US)) {
182 ESP_LOGVV(TAG, "Footer fail");
183 return false;
184 }
185 for (uint8_t offset = 0; offset < length; offset += SAMSUNG_AC_SECTION_LENGTH) {
186 if (stored_checksum(frame + offset) != section_checksum(frame + offset)) {
187 ESP_LOGW(TAG, "Checksum mismatch in section %u", offset / SAMSUNG_AC_SECTION_LENGTH);
188 return false;
189 }
190 }
191
192 // In an extended frame the middle section is the timer block and the last one the second state section
193 uint8_t *state = this->state_.data();
194 memcpy(state, frame, SAMSUNG_AC_SECTION_LENGTH);
195 memcpy(state + SAMSUNG_AC_SECTION_LENGTH, frame + length - SAMSUNG_AC_SECTION_LENGTH, SAMSUNG_AC_SECTION_LENGTH);
196
197 this->swing_mode = from_ir_code(SWINGS, get_field(state, SWING), climate::CLIMATE_SWING_OFF);
198 this->target_temperature = get_field(state, TEMP) + SAMSUNG_AC_TEMP_MIN;
199 this->fan_mode = from_ir_code(FANS, get_field(state, FAN), climate::CLIMATE_FAN_AUTO);
200 this->mode = is_powered_on(state) ? from_ir_code(MODES, get_field(state, MODE), climate::CLIMATE_MODE_HEAT_COOL)
202
203 ESP_LOGD(TAG, "Received %u byte frame, power %s", length, ONOFF(is_powered_on(state)));
204 this->publish_state();
205 return true;
206}
207
208} // namespace esphome::samsung
ClimateMode mode
The active mode of the climate device.
Definition climate.h:317
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:311
float target_temperature
The target temperature of the climate device.
Definition climate.h:298
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:323
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:432
bool on_receive(remote_base::RemoteReceiveData data) override
Definition samsung.cpp:154
std::array< uint8_t, SAMSUNG_AC_STATE_LENGTH > state_
Definition samsung.h:28
void send_sections_(const uint8_t *const *sections, uint8_t count)
Send the given sections as one frame, each with its checksum filled in.
Definition samsung.cpp:130
bool state
Definition fan.h:2
unsigned count
Definition gif.cpp:51
uint16_t MODE
Definition ina2xx_base.h:4
const uint8_t STORE_YAML_DATA[] PROGMEM
The zstd YAML envelope generated by store_yaml, STORE_YAML_DATA_SIZE bytes in flash.
T from_ir_code(const IRCode(&table)[N], uint8_t code, T fallback)
The value for code, or fallback when the table has no such code; pass the current state to keep it.
Definition climate_ir.h:40
uint8_t to_ir_code(const IRCode(&table)[N], uint8_t value, uint8_t fallback)
The code for value, or fallback when the table has no such value.
Definition climate_ir.h:36
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_HORIZONTAL
The fan mode is set to Horizontal.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ CLIMATE_SWING_BOTH
The fan mode is set to Both.
@ CLIMATE_MODE_DRY
The climate device is set to dry/humidity mode.
@ CLIMATE_MODE_FAN_ONLY
The climate device only has the fan enabled, no heating or cooling is taking place.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
@ CLIMATE_FAN_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
ESPHOME_LOG_TAG(TAG, "samsung.climate")
Field
Order of the values in the answer to GetAllMeasurements.
Definition smt100.h:11
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:59
static void uint32_t
A bit is a mark (one_mark or zero_mark, both bit_mark unless set) and a space (one_space or zero_spac...
Definition remote_base.h:52