ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ira211_protocol.cpp
Go to the documentation of this file.
1#include "ira211_protocol.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
6
7namespace esphome::remote_base {
8
9ESPHOME_LOG_TAG(TAG, "remote.ira211");
10
11static constexpr uint32_t CARRIER_FREQUENCY = 38000;
12static constexpr uint32_t T_US = 800;
13static constexpr uint32_t HEADER_US = 19 * T_US / 2; // 7600 us, also the gap that separates frames
14// Midway between the longest data run (9 bits, 7200 us) and the gap, so a gap captured a little short still
15// ends the frame
16static constexpr uint32_t GAP_MIN_US = HEADER_US - T_US / 4;
17static constexpr uint8_t MIN_HALF_DEGREES = static_cast<uint8_t>(IRA211_MIN_TEMPERATURE * 2);
18static constexpr uint8_t MAX_HALF_DEGREES = static_cast<uint8_t>(IRA211_MAX_TEMPERATURE * 2);
19
20static constexpr uint8_t DEVICE_ID = 0x66;
21static constexpr uint8_t BITS_PER_PACKET = 10;
22static constexpr uint8_t MIN_PACKETS = 4;
23static constexpr uint8_t MAX_PACKETS = 7;
24static constexpr uint8_t MAX_BITS = MAX_PACKETS * BITS_PER_PACKET;
25
26// Fields each command carries, in wire order after the command byte: temperature (whole degrees
27// then tenths), mode, fan.
28static constexpr uint8_t FIELD_TEMPERATURE = 1 << 0;
29static constexpr uint8_t FIELD_MODE = 1 << 1;
30static constexpr uint8_t FIELD_FAN = 1 << 2;
31
32struct CommandLayout {
33 IRA211Command command;
34 uint8_t fields;
35};
36
37static const CommandLayout LAYOUTS[] PROGMEM = {
38 {IRA211Command::IRA211_COMMAND_TEMP_UP, FIELD_TEMPERATURE},
39 {IRA211Command::IRA211_COMMAND_TEMP_DOWN, FIELD_TEMPERATURE},
42 {IRA211Command::IRA211_COMMAND_POWER, FIELD_TEMPERATURE | FIELD_MODE | FIELD_FAN},
43 {IRA211Command::IRA211_COMMAND_SYNC, FIELD_TEMPERATURE | FIELD_MODE | FIELD_FAN},
44};
45
46static optional<CommandLayout> find_layout(IRA211Command command) {
47 for (const auto &slot : LAYOUTS) {
48 CommandLayout layout;
49 progmem_memcpy(&layout, &slot, sizeof(layout));
50 if (layout.command == command)
51 return layout;
52 }
53 return {};
54}
55
56// Device ID, command, fields, checksum
57static uint8_t packet_count(const CommandLayout &layout) {
58 return 3 + ((layout.fields & FIELD_TEMPERATURE) ? 2 : 0) + ((layout.fields & FIELD_MODE) ? 1 : 0) +
59 ((layout.fields & FIELD_FAN) ? 1 : 0);
60}
61
62// Value to wire byte and back; the transform is its own inverse
63static uint8_t wire_xform(uint8_t value) { return reverse_bits(static_cast<uint8_t>(~value)); }
64
65static bool is_known_mode(uint8_t value) {
66 switch (static_cast<IRA211Mode>(value)) {
70 return true;
71 default:
72 return false;
73 }
74}
75
76static bool is_known_fan(uint8_t value) {
77 switch (static_cast<IRA211Fan>(value)) {
82 return true;
83 default:
84 return false;
85 }
86}
87
88static uint8_t checksum(const uint8_t *bytes, uint8_t count) {
89 uint8_t sum = count;
90 for (uint8_t i = 0; i < count; i++)
91 sum += reverse_bits(bytes[i]);
92 return reverse_bits(sum);
93}
94
95bool IRA211Data::operator==(const IRA211Data &rhs) const {
96 if (this->command != rhs.command)
97 return false;
98 const optional<CommandLayout> layout = find_layout(this->command);
99 if (!layout.has_value())
100 return true;
101 if ((layout->fields & FIELD_TEMPERATURE) && this->half_degrees != rhs.half_degrees)
102 return false;
103 if ((layout->fields & FIELD_MODE) && this->mode != rhs.mode)
104 return false;
105 return !(layout->fields & FIELD_FAN) || this->fan == rhs.fan;
106}
107
109 const optional<CommandLayout> layout = find_layout(data.command);
110 if (!layout.has_value())
111 return;
112
113 uint8_t bytes[MAX_PACKETS];
114 uint8_t count = 0;
115 bytes[count++] = DEVICE_ID;
116 bytes[count++] = wire_xform(static_cast<uint8_t>(data.command));
117 if (layout->fields & FIELD_TEMPERATURE) {
118 bytes[count++] = wire_xform(data.half_degrees / 2);
119 bytes[count++] = wire_xform((data.half_degrees & 1) ? 5 : 0);
120 }
121 if (layout->fields & FIELD_MODE)
122 bytes[count++] = wire_xform(static_cast<uint8_t>(data.mode));
123 if (layout->fields & FIELD_FAN)
124 bytes[count++] = wire_xform(static_cast<uint8_t>(data.fan));
125 bytes[count] = checksum(bytes, count);
126 count++;
127
128 dst->set_carrier_frequency(CARRIER_FREQUENCY);
129 // Preamble, at most one item per bit, trailing mark and gap
130 dst->reserve(4 + count * BITS_PER_PACKET + 2);
131 dst->item(HEADER_US, T_US);
132 dst->item(T_US, HEADER_US);
133
134 // Each packet is a 1 bit, the byte MSB first, then a 0 bit; runs of equal bits become one mark or space
135 bool level = true;
136 uint32_t run = 0;
137 for (uint8_t p = 0; p < count; p++) {
138 const uint16_t packet = (1 << 9) | (bytes[p] << 1);
139 for (int8_t i = BITS_PER_PACKET - 1; i >= 0; i--) {
140 const bool bit = (packet >> i) & 1;
141 if (bit != level && run != 0) {
142 if (level) {
143 dst->mark(run * T_US);
144 } else {
145 dst->space(run * T_US);
146 }
147 run = 0;
148 }
149 level = bit;
150 run++;
151 }
152 }
153 // Every packet ends with a 0 bit, so the frame ends with a space, closed by a trailing mark
154 dst->space(run * T_US);
155 dst->mark(T_US);
156 dst->space(HEADER_US);
157}
158
160 if (!src.expect_item(HEADER_US, T_US) || !src.expect_item(T_US, HEADER_US))
161 return {};
162
163 // Read bits until the end-of-frame gap, checking packet framing and collecting bytes as they arrive
164 uint8_t bytes[MAX_PACKETS]{};
165 uint8_t bits = 0;
166 while (src.is_valid()) {
167 const int32_t raw = src.peek();
168 const bool is_mark = raw > 0;
169 const uint32_t duration = is_mark ? raw : -raw;
170 // A mark followed by the gap or the end of the data is the trailing mark, not a data bit
171 if (is_mark && (!src.is_valid(1) || src.peek(1) <= -static_cast<int32_t>(GAP_MIN_US)))
172 break;
173 if (!is_mark && duration >= GAP_MIN_US)
174 break;
175 const uint32_t run = (duration + T_US / 2) / T_US;
176 if (run == 0 || bits + run > MAX_BITS)
177 return {};
178 for (uint32_t i = 0; i < run; i++, bits++) {
179 const uint8_t pos = bits % BITS_PER_PACKET;
180 if ((pos == 0 && !is_mark) || (pos == BITS_PER_PACKET - 1 && is_mark))
181 return {};
182 if (pos != 0 && pos != BITS_PER_PACKET - 1) {
183 uint8_t &byte = bytes[bits / BITS_PER_PACKET];
184 byte = (byte << 1) | (is_mark ? 1 : 0);
185 }
186 }
187 src.advance();
188 }
189
190 const uint8_t count = bits / BITS_PER_PACKET;
191 if (bits % BITS_PER_PACKET != 0 || count < MIN_PACKETS || bytes[0] != DEVICE_ID)
192 return {};
193 IRA211Data out{};
194 out.command = static_cast<IRA211Command>(wire_xform(bytes[1]));
195 const optional<CommandLayout> layout = find_layout(out.command);
196 if (!layout.has_value() || packet_count(*layout) != count || checksum(bytes, count - 1) != bytes[count - 1]) {
197 ESP_LOGV(TAG, "Invalid frame (%u bits)", bits);
198 return {};
199 }
200
201 // A checksum-valid frame can still carry values outside what the thermostat accepts
202 uint8_t index = 2;
203 if (layout->fields & FIELD_TEMPERATURE) {
204 const uint8_t whole = wire_xform(bytes[index++]);
205 const uint8_t tenths = wire_xform(bytes[index++]);
206 const uint16_t half_degrees = whole * 2 + (tenths >= 5 ? 1 : 0);
207 if (half_degrees < MIN_HALF_DEGREES || half_degrees > MAX_HALF_DEGREES)
208 return {};
209 out.half_degrees = static_cast<uint8_t>(half_degrees);
210 }
211 if (layout->fields & FIELD_MODE) {
212 const uint8_t mode = wire_xform(bytes[index++]);
213 if (!is_known_mode(mode))
214 return {};
215 out.mode = static_cast<IRA211Mode>(mode);
216 }
217 if (layout->fields & FIELD_FAN) {
218 const uint8_t fan = wire_xform(bytes[index++]);
219 if (!is_known_fan(fan))
220 return {};
221 out.fan = static_cast<IRA211Fan>(fan);
222 }
223 return out;
224}
225
226// Indexed by IRA211Command
227PROGMEM_STRING_TABLE(IRA211CommandStrings, "UNKNOWN", "TEMP_UP", "TEMP_DOWN", "UNKNOWN", "MODE", "FAN", "POWER",
228 "SYNC");
229
230static const LogString *mode_to_string(IRA211Mode mode) {
231 switch (mode) {
233 return LOG_STR("Protection");
235 return LOG_STR("Timer");
237 return LOG_STR("Comfort");
238 default:
239 return LOG_STR("Unknown");
240 }
241}
242
243static const LogString *fan_to_string(IRA211Fan fan) {
244 switch (fan) {
246 return LOG_STR("Auto");
248 return LOG_STR("1/3");
250 return LOG_STR("2/3");
252 return LOG_STR("3/3");
253 default:
254 return LOG_STR("Unknown");
255 }
256}
257
259 const optional<CommandLayout> layout = find_layout(data.command);
260 const uint8_t fields = layout.has_value() ? layout->fields : 0;
261 const unsigned whole = data.half_degrees / 2;
262 const unsigned tenths = (data.half_degrees & 1) ? 5 : 0;
263 const auto *command = LOG_STR_ARG(IRA211CommandStrings::get_log_str(data.command, 0));
264 if (fields == (FIELD_TEMPERATURE | FIELD_MODE | FIELD_FAN)) {
265 ESP_LOGI(TAG, "Received IRA211: command=%s, temperature=%u.%uC, mode=%s, fan=%s", command, whole, tenths,
266 LOG_STR_ARG(mode_to_string(data.mode)), LOG_STR_ARG(fan_to_string(data.fan)));
267 } else if (fields == FIELD_TEMPERATURE) {
268 ESP_LOGI(TAG, "Received IRA211: command=%s, temperature=%u.%uC", command, whole, tenths);
269 } else if (fields == FIELD_MODE) {
270 ESP_LOGI(TAG, "Received IRA211: command=%s, mode=%s", command, LOG_STR_ARG(mode_to_string(data.mode)));
271 } else if (fields == FIELD_FAN) {
272 ESP_LOGI(TAG, "Received IRA211: command=%s, fan=%s", command, LOG_STR_ARG(fan_to_string(data.fan)));
273 } else {
274 ESP_LOGI(TAG, "Received IRA211: command=%s", command);
275 }
276}
277
278} // namespace esphome::remote_base
BedjetMode mode
BedJet operating mode.
uint8_t checksum
Definition bl0906.h:3
uint8_t raw[35]
Definition bl0939.h:0
optional< IRA211Data > decode(RemoteReceiveData src)
void encode(RemoteTransmitData *dst, const IRA211Data &data)
void dump(const IRA211Data &data)
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:87
void item(uint32_t mark, uint32_t space)
Definition remote_base.h:66
std::vector< uint8_t > & out
Definition gif.cpp:49
unsigned count
Definition gif.cpp:51
uint8_t duration
Definition msa3xx.h:0
ESPHOME_LOG_TAG(TAG, "remote.abbwelcome")
PROGMEM_STRING_TABLE(Hob2HoodCommandNames, "light_off", "light_on", "fan_off", "fan_low", "fan_medium", "fan_high", "fan_max", "unknown")
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:59
size_t size_t pos
Definition helpers.h:1530
const void * src
Definition hal.h:68
uint8_t reverse_bits(uint8_t x)
Reverse the order of 8 bits.
Definition helpers.h:1343
static void uint32_t
bool operator==(const IRA211Data &rhs) const
Compares the command and only the fields that command carries.