ESPHome 2026.11.0b6
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
50struct Code {
51 uint8_t value; // a climate enum, all of which are uint8_t
52 uint8_t code;
53};
54// The first entry of each table is the fallback in both directions
55static constexpr Code MODES[] PROGMEM = {
58};
59static constexpr Code FANS[] PROGMEM = {
64};
65static constexpr Code SWINGS[] PROGMEM = {
70};
71
72// Returns the entry whose value (or code) matches key, else the first entry. Entries are copied out of PROGMEM.
73static Code find_entry(const Code *table, size_t count, uint8_t key, bool match_code) {
74 Code entry{};
75 for (size_t i = 0; i < count; i++) {
76 progmem_memcpy(&entry, &table[i], sizeof(entry));
77 if ((match_code ? entry.code : entry.value) == key)
78 return entry;
79 }
80 progmem_memcpy(&entry, table, sizeof(entry));
81 return entry;
82}
83template<size_t N> static uint8_t to_code(const Code (&table)[N], uint8_t value) {
84 return find_entry(table, N, value, false).code;
85}
86template<typename T, size_t N> static T from_code(const Code (&table)[N], uint8_t code) {
87 return static_cast<T>(find_entry(table, N, code, true).value);
88}
89
90// Inverted count of the one bits in a section, skipping the two nibbles that hold the checksum itself.
91// See https://github.com/crankyoldgit/IRremoteESP8266/issues/1538#issuecomment-894645947
92static uint8_t section_checksum(const uint8_t *section) {
93 uint32_t ones =
94 __builtin_popcount(section[0]) + __builtin_popcount(section[1] & 0x0F) + __builtin_popcount(section[2] & 0xF0);
95 for (uint8_t i = 3; i < SAMSUNG_AC_SECTION_LENGTH; i++) {
96 ones += __builtin_popcount(section[i]);
97 }
98 return ~static_cast<uint8_t>(ones);
99}
100static uint8_t stored_checksum(const uint8_t *section) { return (section[1] >> 4) | (section[2] << 4); }
101static void store_checksum(uint8_t *section) {
102 const uint8_t sum = section_checksum(section);
103 section[1] = (section[1] & 0x0F) | (sum << 4);
104 section[2] = (section[2] & 0xF0) | (sum >> 4);
105}
106
108 : climate_ir::ClimateIR(
109 SAMSUNG_AC_TEMP_MIN, SAMSUNG_AC_TEMP_MAX, 1.0f, true, true,
113 progmem_memcpy(this->state_.data(), RESET_STATE, SAMSUNG_AC_STATE_LENGTH);
114 // The unit's power state is unknown at boot, so the first "on" command sends a power frame
115 set_field(this->state_.data(), POWER_1, 0);
116 set_field(this->state_.data(), POWER_2, 0);
117}
118
120 uint8_t *state = this->state_.data();
121 const bool was_on = is_powered_on(state);
122 const bool on = this->mode != climate::CLIMATE_MODE_OFF;
123
124 if (on) {
125 progmem_memcpy(state, RESET_STATE, SAMSUNG_AC_STATE_LENGTH);
126 set_field(state, MODE, to_code(MODES, this->mode));
127 } // else keep the last mode so the power off frame still carries a valid state
128
129 const uint8_t temp = clamp<uint8_t>(this->target_temperature, SAMSUNG_AC_TEMP_MIN, SAMSUNG_AC_TEMP_MAX);
130 set_field(state, TEMP, temp - SAMSUNG_AC_TEMP_MIN);
131 set_field(state, SWING, to_code(SWINGS, this->swing_mode));
132 set_field(state, FAN, to_code(FANS, this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO)));
133 set_field(state, POWER_1, on ? POWER_ON : 0);
134 set_field(state, POWER_2, on ? POWER_ON : 0);
135
136 if (on != was_on) {
137 // A power change is sent as an extended frame with the timer block between the two state sections
138 uint8_t timer[SAMSUNG_AC_SECTION_LENGTH];
139 progmem_memcpy(timer, TIMER_SECTION, SAMSUNG_AC_SECTION_LENGTH);
140 const uint8_t *sections[] = {state, timer, state + SAMSUNG_AC_SECTION_LENGTH};
141 this->send_sections_(sections, 3);
142 } else {
143 const uint8_t *sections[] = {state, state + SAMSUNG_AC_SECTION_LENGTH};
144 this->send_sections_(sections, 2);
145 }
146}
147
148void SamsungClimate::send_sections_(const uint8_t *const *sections, uint8_t count) {
149 auto transmit = this->transmitter_->transmit();
150 auto *data = transmit.get_data();
151
152 // Header(2) + (sections - 1) * separator(4) + bits(2 each) + trailing mark(1)
153 data->reserve(2 + (count - 1) * 4 + count * SAMSUNG_AC_SECTION_LENGTH * 8 * 2 + 1);
154 data->set_carrier_frequency(CARRIER_FREQUENCY_HZ);
155 data->item(HEADER_MARK_US, HEADER_SPACE_US);
156
157 for (uint8_t s = 0; s < count; s++) {
158 if (s != 0) {
159 data->item(BIT_MARK_US, SECTION_SPACE_US);
160 data->item(HEADER_MARK_US, HEADER_SPACE_US);
161 }
162 uint8_t section[SAMSUNG_AC_SECTION_LENGTH];
163 memcpy(section, sections[s], SAMSUNG_AC_SECTION_LENGTH);
164 store_checksum(section);
165 data->item_bytes<BIT_TIMINGS>(section, SAMSUNG_AC_SECTION_LENGTH);
166 }
167
168 data->mark(BIT_MARK_US);
169 transmit.perform();
170}
171
173 if (!data.expect_item(HEADER_MARK_US, HEADER_SPACE_US)) {
174 return false;
175 }
176
177 // Decode into a local buffer so a bad frame leaves the stored state untouched
178 uint8_t frame[EXTENDED_LENGTH];
179 uint8_t length = EXTENDED_LENGTH;
180 for (uint8_t s = 0; s < EXTENDED_LENGTH / SAMSUNG_AC_SECTION_LENGTH; s++) {
181 if (s != 0) {
182 const bool have_separator =
183 data.expect_item(BIT_MARK_US, SECTION_SPACE_US) && data.expect_item(HEADER_MARK_US, HEADER_SPACE_US);
184 if (!have_separator) {
185 // A third section is optional; a missing separator after the second one ends the frame
186 if (s >= SAMSUNG_AC_STATE_LENGTH / SAMSUNG_AC_SECTION_LENGTH) {
187 length = s * SAMSUNG_AC_SECTION_LENGTH;
188 break;
189 }
190 ESP_LOGW(TAG, "Missing section separator before section %u", s);
191 return false;
192 }
193 }
194 if (!data.expect_bytes<BIT_TIMINGS>(frame + s * SAMSUNG_AC_SECTION_LENGTH, SAMSUNG_AC_SECTION_LENGTH)) {
195 ESP_LOGW(TAG, "Bad bit in section %u", s);
196 return false;
197 }
198 }
199 if (!data.expect_mark(BIT_MARK_US)) {
200 ESP_LOGVV(TAG, "Footer fail");
201 return false;
202 }
203 for (uint8_t offset = 0; offset < length; offset += SAMSUNG_AC_SECTION_LENGTH) {
204 if (stored_checksum(frame + offset) != section_checksum(frame + offset)) {
205 ESP_LOGW(TAG, "Checksum mismatch in section %u", offset / SAMSUNG_AC_SECTION_LENGTH);
206 return false;
207 }
208 }
209
210 // In an extended frame the middle section is the timer block and the last one the second state section
211 uint8_t *state = this->state_.data();
212 memcpy(state, frame, SAMSUNG_AC_SECTION_LENGTH);
213 memcpy(state + SAMSUNG_AC_SECTION_LENGTH, frame + length - SAMSUNG_AC_SECTION_LENGTH, SAMSUNG_AC_SECTION_LENGTH);
214
215 this->swing_mode = from_code<climate::ClimateSwingMode>(SWINGS, get_field(state, SWING));
216 this->target_temperature = get_field(state, TEMP) + SAMSUNG_AC_TEMP_MIN;
217 this->fan_mode = from_code<climate::ClimateFanMode>(FANS, get_field(state, FAN));
218 this->mode =
219 is_powered_on(state) ? from_code<climate::ClimateMode>(MODES, get_field(state, MODE)) : climate::CLIMATE_MODE_OFF;
220
221 ESP_LOGD(TAG, "Received %u byte frame, power %s", length, ONOFF(is_powered_on(state)));
222 this->publish_state();
223 return true;
224}
225
226} // 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:172
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:148
const char * key
bool state
Definition fan.h:2
unsigned count
Definition gif.cpp:51
uint16_t MODE
Definition ina2xx_base.h:4
constexpr float BME680_GAS_LOOKUP_TABLE_1[16] PROGMEM
Definition bme680.cpp:28
@ 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")
value[LAYOUT.day_index()]
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:41
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:29