ESPHome  2024.11.3
haier_protocol.cpp
Go to the documentation of this file.
1 #include "haier_protocol.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace remote_base {
6 
7 static const char *const TAG = "remote.haier";
8 
9 constexpr uint32_t HEADER_LOW_US = 3100;
10 constexpr uint32_t HEADER_HIGH_US = 4400;
11 constexpr uint32_t BIT_MARK_US = 540;
12 constexpr uint32_t BIT_ONE_SPACE_US = 1650;
13 constexpr uint32_t BIT_ZERO_SPACE_US = 580;
14 constexpr unsigned int HAIER_IR_PACKET_BIT_SIZE = 112;
15 
17  for (uint8_t mask = 1 << 7; mask != 0; mask >>= 1) {
18  if (item & mask) {
19  dst->space(BIT_ONE_SPACE_US);
20  } else {
21  dst->space(BIT_ZERO_SPACE_US);
22  }
23  dst->mark(BIT_MARK_US);
24  }
25 }
26 
28  dst->set_carrier_frequency(38000);
29  dst->reserve(5 + ((data.data.size() + 1) * 2));
30  dst->mark(HEADER_LOW_US);
31  dst->space(HEADER_LOW_US);
32  dst->mark(HEADER_LOW_US);
33  dst->space(HEADER_HIGH_US);
34  dst->mark(BIT_MARK_US);
35  uint8_t checksum = 0;
36  for (uint8_t item : data.data) {
37  this->encode_byte_(dst, item);
38  checksum += item;
39  }
40  this->encode_byte_(dst, checksum);
41 }
42 
44  if (!src.expect_item(HEADER_LOW_US, HEADER_LOW_US) || !src.expect_item(HEADER_LOW_US, HEADER_HIGH_US)) {
45  return {};
46  }
47  if (!src.expect_mark(BIT_MARK_US)) {
48  return {};
49  }
50  size_t size = src.size() - src.get_index() - 1;
51  if (size < HAIER_IR_PACKET_BIT_SIZE * 2)
52  return {};
53  size = HAIER_IR_PACKET_BIT_SIZE * 2;
54  uint8_t checksum = 0;
55  HaierData out;
56  while (size > 0) {
57  uint8_t data = 0;
58  for (uint8_t mask = 0x80; mask != 0; mask >>= 1) {
59  if (src.expect_space(BIT_ONE_SPACE_US)) {
60  data |= mask;
61  } else if (!src.expect_space(BIT_ZERO_SPACE_US)) {
62  return {};
63  }
64  if (!src.expect_mark(BIT_MARK_US)) {
65  return {};
66  }
67  size -= 2;
68  }
69  if (size > 0) {
70  checksum += data;
71  out.data.push_back(data);
72  } else if (checksum != data) {
73  return {};
74  }
75  }
76  return out;
77 }
78 
79 void HaierProtocol::dump(const HaierData &data) {
80  ESP_LOGI(TAG, "Received Haier: %s", format_hex_pretty(data.data).c_str());
81 }
82 
83 } // namespace remote_base
84 } // namespace esphome
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
Definition: helpers.cpp:364
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:34
optional< HaierData > decode(RemoteReceiveData src) override
uint8_t checksum
Definition: bl0906.h:210
void encode(RemoteTransmitData *dst, const HaierData &data) override
std::vector< uint8_t > data
void encode_byte_(RemoteTransmitData *dst, uint8_t item)
constexpr unsigned int HAIER_IR_PACKET_BIT_SIZE
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.cpp:74
void dump(const HaierData &data) override