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;
20static constexpr uint8_t EXTENDED_LENGTH = 3 * SAMSUNG_AC_SECTION_LENGTH;
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};
26static const uint8_t TIMER_SECTION[SAMSUNG_AC_SECTION_LENGTH] PROGMEM = {0x01, 0xD2, 0x0F, 0x00, 0x00, 0x00, 0x00};
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};
39static constexpr Field POWER_2{13, 4, 0b11};
40static constexpr uint8_t POWER_ON = 0b11;
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);
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;
51using climate_ir::IRCode;
55static constexpr IRCode MODES[]
PROGMEM = {
59static constexpr IRCode FANS[]
PROGMEM = {
65static constexpr IRCode SWINGS[]
PROGMEM = {
74static uint8_t section_checksum(
const uint8_t *section) {
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]);
80 return ~static_cast<uint8_t>(ones);
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);
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);
97 set_field(this->state_.data(), POWER_1, 0);
98 set_field(this->state_.data(), POWER_2, 0);
103 const bool was_on = is_powered_on(
state);
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);
115 set_field(
state, POWER_1, on ? POWER_ON : 0);
116 set_field(
state, POWER_2, on ? POWER_ON : 0);
120 uint8_t timer[SAMSUNG_AC_SECTION_LENGTH];
122 const uint8_t *sections[] = {
state, timer,
state + SAMSUNG_AC_SECTION_LENGTH};
125 const uint8_t *sections[] = {
state,
state + SAMSUNG_AC_SECTION_LENGTH};
132 auto *data = transmit.get_data();
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);
139 for (uint8_t s = 0; s <
count; s++) {
141 data->item(BIT_MARK_US, SECTION_SPACE_US);
142 data->item(HEADER_MARK_US, HEADER_SPACE_US);
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);
150 data->mark(BIT_MARK_US);
155 if (!data.expect_item(HEADER_MARK_US, HEADER_SPACE_US)) {
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++) {
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) {
168 if (s >= SAMSUNG_AC_STATE_LENGTH / SAMSUNG_AC_SECTION_LENGTH) {
169 length = s * SAMSUNG_AC_SECTION_LENGTH;
172 ESP_LOGW(TAG,
"Missing section separator before section %u", s);
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);
181 if (!data.expect_mark(BIT_MARK_US)) {
182 ESP_LOGVV(TAG,
"Footer fail");
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);
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);
203 ESP_LOGD(TAG,
"Received %u byte frame, power %s",
length, ONOFF(is_powered_on(
state)));
ClimateMode mode
The active mode of the climate device.
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
float target_temperature
The target temperature of the climate device.
ClimateSwingMode swing_mode
The active swing mode of the climate device.
void publish_state()
Publish the state of the climate device, to be called from integrations.
RemoteTransmitterBase * transmitter_
bool on_receive(remote_base::RemoteReceiveData data) override
std::array< uint8_t, SAMSUNG_AC_STATE_LENGTH > state_
void transmit_state() override
void send_sections_(const uint8_t *const *sections, uint8_t count)
Send the given sections as one frame, each with its checksum filled in.
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.
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.
@ 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.
void progmem_memcpy(void *dst, const void *src, size_t len)
A bit is a mark (one_mark or zero_mark, both bit_mark unless set) and a space (one_space or zero_spac...