ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
pioneer_wt_protocol.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
8
9namespace esphome::pioneer_wt {
10
11// Frame layout: A5 01 | flags | type | seq | ack | 00 | len | crc_hi crc_lo | class class | body
12static constexpr size_t HEADER_SIZE = 10;
13static constexpr uint8_t FRAME_START_1 = 0xA5;
14static constexpr uint8_t FRAME_START_2 = 0x01;
15// The length byte counts the whole frame.
16static constexpr size_t LEN_POS = 7;
17static constexpr size_t CRC_POS = 8;
18static constexpr size_t MAX_FRAME_SIZE = 255;
19
20static constexpr uint8_t FLAG_NO_ACK = 0x00;
21static constexpr uint8_t FLAG_WANT_ACK = 0x01;
22static constexpr uint8_t TYPE_DATA = 0x21;
23static constexpr uint8_t TYPE_REPLY = 0x23;
24
25static constexpr uint8_t CLASS_COMMAND = 0x0A;
26static constexpr uint8_t CLASS_NETWORK = 0x0B;
27static constexpr uint8_t CLASS_STATUS = 0x0C;
28// First class byte of a reply
29static constexpr uint8_t CLASS_REPLY = 0x80;
30
31// Attribute IDs
32static constexpr uint16_t ATTR_POWER = 0x0001;
33static constexpr uint16_t ATTR_SETPOINT_C100 = 0x0002;
34static constexpr uint16_t ATTR_INDOOR_TEMP_C100 = 0x0003;
35static constexpr uint16_t ATTR_FAN_SPEED = 0x0005;
36static constexpr uint16_t ATTR_SWING_VERTICAL = 0x0011;
37static constexpr uint16_t ATTR_MODE = 0x0012;
38static constexpr uint16_t ATTR_ECO = 0x0013;
39static constexpr uint16_t ATTR_DISPLAY = 0x001E;
40static constexpr uint16_t ATTR_BEEPER = 0x0025;
41static constexpr uint16_t ATTR_FAN_AUTO = 0x0073;
42static constexpr uint16_t ATTR_FAN_EXTRA_1 = 0x0224;
43static constexpr uint16_t ATTR_FAN_EXTRA_2 = 0x0225;
44static constexpr uint16_t ATTR_SETPOINT_F = 0x0227;
45
46static constexpr uint8_t SWING_ON = 0x01;
47static constexpr uint8_t SWING_OFF = 0x08;
48
50uint16_t frame_crc(const uint8_t *frame, size_t len);
51
52// A5 01, ..., whole-frame length at 7, CRC (2) at 8, body
53using FrameLength = FrameLengthU8<LengthField{.offset = LEN_POS}>;
56
57inline bool checksum_valid(const uint8_t *frame, size_t len) {
58 return frame_crc(frame, len) == encode_uint16(frame[CRC_POS], frame[CRC_POS + 1]);
59}
60
62class AttributeReader final {
63 public:
64 AttributeReader(const uint8_t *data, size_t len) : data_(data), len_(len) {}
66 bool next();
67 uint16_t id() const { return this->id_; }
68 uint32_t value() const { return this->value_; }
70 bool stopped_on_unknown() const { return this->unknown_; }
71
72 protected:
73 const uint8_t *data_;
74 size_t len_;
75 size_t pos_{0};
77 uint16_t id_{0};
78 bool unknown_{false};
79};
80
82size_t build_frame(uint8_t *out, uint8_t flags, uint8_t type, uint8_t seq, uint8_t ack, uint8_t class_1,
83 uint8_t class_2, const uint8_t *body, size_t len);
84
86inline uint8_t *put_attr(uint8_t *p, uint16_t id, uint8_t value) {
87 p = put_be16(p, id);
88 *p++ = value;
89 return p;
90}
91
92} // namespace esphome::pioneer_wt
Walks the attribute list in a status body. Stops at the first ID whose size is unknown.
bool stopped_on_unknown() const
True when the walk stopped early on an ID of unknown size.
bool next()
Moves to the next attribute. Returns false at the end of the body or at an unknown ID.
AttributeReader(const uint8_t *data, size_t len)
uint16_t flags
uint8_t type
std::vector< uint8_t > & out
Definition gif.cpp:49
uint8_t * put_attr(uint8_t *p, uint16_t id, uint8_t value)
Writes a one-byte attribute (2-byte ID, then the value) at p. Returns the new end.
bool checksum_valid(const uint8_t *frame, size_t len)
FrameLengthU8< LengthField{.offset=LEN_POS}> FrameLength
size_t build_frame(uint8_t *out, uint8_t flags, uint8_t type, uint8_t seq, uint8_t ack, uint8_t class_1, uint8_t class_2, const uint8_t *body, size_t len)
Builds a frame into out, which must hold HEADER_SIZE + 2 + len bytes. Returns the frame length.
uint16_t frame_crc(const uint8_t *frame, size_t len)
CRC-16/XMODEM over a whole frame, skipping the two checksum bytes at offset 8.
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
Definition helpers.h:1296
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1227
static void uint32_t
One frame type for LengthFrameReader: a FrameHeader, a length rule from above, then any of the option...
Whole-frame length: the field counts every byte of the frame, header included.
FrameFormat option: the shortest frame, when that is more than its header and length field.
offset: where the length field starts, counted from the first header byte.
uint16_t seq
uint8_t ack