ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
panasonic_ac_frame.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <cstring>
6
8#include "esphome/core/hal.h"
10
12
13/*
14Panasonic A/C state: 27 bytes, sent LSB first as two sections (8 bytes + 19 bytes).
15
16Based on IRremoteESP8266 by Kristian Lauszus and David Conran (LGPL-2.1):
17https://github.com/crankyoldgit/IRremoteESP8266/blob/master/src/ir_Panasonic.cpp
18Cross-checked against the reverse engineering notes by Mathieu:
19https://github.com/r45635/HVAC-IR-Control/blob/master/Protocol/Panasonic%20HVAC%20IR%20Protocol%20specification.pdf
20https://www.instructables.com/Reverse-engineering-of-an-Air-Conditioning-control/
21and captures from the remote of a CS-NE9PKE.
22
23B0-B12: static 0x0220E004000000060220E00400
24B13: b0 power (CKP: toggle), b4-6 mode, other bits per model
25B14: b1-5 temperature
26B16: b0-3 vertical vane (1 highest to 5 lowest, 15 auto), b4-7 fan speed + 3
27B17, B21, B23, B25: per model
28B26: checksum, sum of B0-B25 plus 0xF4 (the first section sums to 0x10C, so it is the sum of B8-B25)
29Stateless buttons (quiet/powerful, nanoe-G) send an 8 byte second section, which is ignored.
30*/
31
32static constexpr size_t STATE_LENGTH = 27;
33static constexpr size_t SECTION1_LENGTH = 8;
34static constexpr size_t SECTION2_LENGTH = STATE_LENGTH - SECTION1_LENGTH;
35static constexpr uint8_t CHECKSUM_INIT = 0xF4;
36
37// PKE is not an IRremoteESP8266 model: it is JKE with B13 b3 set, as sent by the remote of a CS-NE9PKE (CWA75C4410).
38enum class Model : uint8_t { DKE, JKE, LKE, NKE, CKP, RKR, PKE };
39
40enum class Mode : uint8_t { AUTO = 0, DRY = 2, COOL = 3, HEAT = 4, FAN = 6 };
41
42// Raw fan speeds, as used by IRremoteESP8266. 0 = minimum, 4 = maximum.
43static constexpr uint8_t FAN_SPEED_MIN = 0;
44static constexpr uint8_t FAN_SPEED_LOW = 1;
45static constexpr uint8_t FAN_SPEED_MEDIUM = 2;
46static constexpr uint8_t FAN_SPEED_HIGH = 3;
47static constexpr uint8_t FAN_SPEED_MAX = 4;
48static constexpr uint8_t FAN_SPEED_AUTO = 7;
49static constexpr uint8_t FAN_SPEED_OFFSET = 3;
50
51static constexpr uint8_t VANE_V_HIGHEST = 0x1;
52static constexpr uint8_t VANE_V_MIDDLE = 0x3;
53static constexpr uint8_t VANE_V_LOWEST = 0x5;
54static constexpr uint8_t VANE_V_AUTO = 0xF;
55
56// The remote's frost protection ("+8/10 C Heat") is a mode of its own that only offers 8 C and 10 C. Both are
57// understood when receiving, 10 C is what we send.
58static constexpr uint8_t TEMP_MAINTENANCE = 10;
59static constexpr uint8_t TEMP_MIN_RECEIVE = 8;
60static constexpr uint8_t TEMP_MIN = 16; // Celsius
61static constexpr uint8_t TEMP_MAX = 30; // Celsius
62static constexpr uint8_t TEMP_FAN_MODE = 27; // Celsius. The remote always sends this in fan mode
63
65struct ModelBytes {
66 uint8_t b13_or;
67 uint8_t b17;
68 uint8_t b21_or;
69 uint8_t b23;
70 uint8_t b25;
71};
72
73// In the order of the Model enum
74static constexpr ModelBytes MODEL_BYTES[] PROGMEM = {
75 {0x00, 0x06, 0x00, 0x01, 0x06}, // DKE
76 {0x00, 0x00, 0x00, 0x81, 0x00}, // JKE
77 {0x02, 0x06, 0x00, 0x81, 0x00}, // LKE
78 {0x00, 0x06, 0x00, 0x81, 0x00}, // NKE
79 {0x00, 0x00, 0x10, 0x01, 0x00}, // CKP
80 {0x08, 0x06, 0x00, 0x89, 0x00}, // RKR
81 {0x08, 0x00, 0x00, 0x81, 0x00}, // PKE
82};
83
86 bool power{false}; // For CKP this means "toggle power"
88 uint8_t temperature{25};
89 uint8_t fan_speed{FAN_SPEED_AUTO};
90 uint8_t vane_v{VANE_V_AUTO};
91};
92
94inline void fill_template(uint8_t *state) {
95 memset(state, 0, STATE_LENGTH);
96 state[0] = 0x02;
97 state[1] = 0x20;
98 state[2] = 0xE0;
99 state[3] = 0x04;
100 state[7] = 0x06;
101 state[8] = 0x02;
102 state[9] = 0x20;
103 state[10] = 0xE0;
104 state[11] = 0x04;
105 state[15] = 0x80;
106 state[19] = 0x0E;
107 state[20] = 0xE0;
108}
109
110inline uint8_t calc_checksum(const uint8_t *state, size_t len) { return sum_bytes(state, len - 1, CHECKSUM_INIT); }
111
113inline void encode_frame(const FrameParams &p, uint8_t *state) {
115 ModelBytes model;
116 progmem_memcpy(&model, &MODEL_BYTES[static_cast<uint8_t>(p.model)], sizeof(model));
117 state[13] = model.b13_or | (p.power ? 0x01 : 0x00) | ((static_cast<uint8_t>(p.mode) & 0x07) << 4);
118 const uint8_t temp = p.mode == Mode::FAN ? TEMP_FAN_MODE : p.temperature;
119 state[14] = (temp & 0x1F) << 1;
120 state[16] = (p.vane_v & 0x0F) | (((p.fan_speed + FAN_SPEED_OFFSET) & 0x0F) << 4);
121 state[17] = model.b17;
122 state[21] = model.b21_or;
123 state[23] = model.b23;
124 state[25] = model.b25;
125 state[STATE_LENGTH - 1] = calc_checksum(state, STATE_LENGTH);
126}
127
129inline bool decode_frame(const uint8_t *state, FrameParams &p) {
130 // The first 13 bytes are static
131 uint8_t expected[STATE_LENGTH];
132 fill_template(expected);
133 if (memcmp(state, expected, 13) != 0)
134 return false;
135 if (state[STATE_LENGTH - 1] != calc_checksum(state, STATE_LENGTH))
136 return false;
137
138 const uint8_t mode = (state[13] >> 4) & 0x07;
139 switch (static_cast<Mode>(mode)) {
140 case Mode::AUTO:
141 case Mode::DRY:
142 case Mode::COOL:
143 case Mode::HEAT:
144 case Mode::FAN:
145 break;
146 default:
147 return false;
148 }
149
150 const uint8_t temp = (state[14] >> 1) & 0x1F;
151 if (temp < TEMP_MIN_RECEIVE || temp > TEMP_MAX)
152 return false;
153
154 const uint8_t fan = static_cast<uint8_t>((state[16] >> 4) - FAN_SPEED_OFFSET); // Wraps below the offset
155 if (fan > FAN_SPEED_MAX && fan != FAN_SPEED_AUTO)
156 return false;
157
158 p.power = state[13] & 0x01;
159 p.mode = static_cast<Mode>(mode);
160 p.temperature = temp;
161 p.fan_speed = fan;
162 p.vane_v = state[16] & 0x0F;
163 return true;
164}
165
166// IR timings [us]
167static constexpr uint32_t CARRIER_FREQUENCY = 36700;
168static constexpr uint32_t HEADER_MARK = 3456;
169static constexpr uint32_t HEADER_SPACE = 1728;
170static constexpr uint32_t BIT_MARK = 432;
171static constexpr uint32_t BIT_ONE_SPACE = 1296;
172static constexpr uint32_t BIT_ZERO_SPACE = 432;
173static constexpr uint32_t SECTION_GAP = 10000;
174static constexpr remote_base::BitTimings BIT_TIMINGS{
175 .bit_mark = BIT_MARK, .one_space = BIT_ONE_SPACE, .zero_space = BIT_ZERO_SPACE};
176
179 return data.peek_mark(BIT_MARK) && (!data.is_valid(1) || data.peek_space_at_least(3 * BIT_ONE_SPACE, 1));
180}
181
183inline size_t receive_section(remote_base::RemoteReceiveData &data, uint8_t *bytes) {
184 if (!data.expect_item(HEADER_MARK, HEADER_SPACE) || !data.expect_bytes<BIT_TIMINGS>(bytes, SECTION1_LENGTH))
185 return 0;
186 size_t len = SECTION1_LENGTH;
187 if (!peek_footer(data)) {
188 if (!data.expect_bytes<BIT_TIMINGS>(bytes + SECTION1_LENGTH, SECTION2_LENGTH - SECTION1_LENGTH) ||
189 !peek_footer(data))
190 return 0;
191 len = SECTION2_LENGTH;
192 }
193 data.advance();
194 return len;
195}
196
202inline bool read_state(remote_base::RemoteReceiveData &data, uint8_t *state) {
203 uint8_t section[SECTION2_LENGTH];
204 size_t len = receive_section(data, section);
205
206 uint8_t expected[STATE_LENGTH];
207 fill_template(expected);
208
209 if (len == SECTION1_LENGTH) {
210 if (memcmp(section, expected, SECTION1_LENGTH) != 0)
211 return false;
212 // The gap and the second section follow in a full capture. Anything else is just the receiver's idle space
213 if (!data.is_valid(1) || !data.expect_space(SECTION_GAP))
214 return false;
215 len = receive_section(data, section);
216 }
217 if (len != SECTION2_LENGTH)
218 return false;
219
220 memcpy(state, expected, SECTION1_LENGTH);
221 memcpy(state + SECTION1_LENGTH, section, SECTION2_LENGTH);
222 return true;
223}
224
225} // namespace esphome::panasonic_ir
BedjetMode mode
BedJet operating mode.
bool state
Definition fan.h:2
bool peek_footer(const remote_base::RemoteReceiveData &data)
A footer mark, followed by the end of the capture or a long space (idle time or section gap),...
void encode_frame(const FrameParams &p, uint8_t *state)
Encodes p as is: the temperature is not clamped, except that fan only mode always sends 27 C.
bool decode_frame(const uint8_t *state, FrameParams &p)
Decode a state. Returns false if the state doesn't look like a valid Panasonic A/C state.
void fill_template(uint8_t *state)
The part of the state that doesn't depend on the settings. Built in code, so it doesn't take up RAM o...
size_t receive_section(remote_base::RemoteReceiveData &data, uint8_t *bytes)
Reads header + bytes + footer mark of a section of either length. Returns the number of bytes read,...
uint8_t calc_checksum(const uint8_t *state, size_t len)
bool read_state(remote_base::RemoteReceiveData &data, uint8_t *state)
Reads a state from a received capture.
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Definition helpers.h:939
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
const void size_t len
Definition hal.h:68
static void uint32_t
Per model bytes. B17 is the horizontal vane on DKE and RKR, always sent in the middle (0x06).
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