ESPHome  2024.11.0
mirage_protocol.cpp
Go to the documentation of this file.
1 #include "mirage_protocol.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace remote_base {
6 
7 static const char *const TAG = "remote.mirage";
8 
9 constexpr uint32_t HEADER_MARK_US = 8360;
10 constexpr uint32_t HEADER_SPACE_US = 4248;
11 constexpr uint32_t BIT_MARK_US = 554;
12 constexpr uint32_t BIT_ONE_SPACE_US = 1592;
13 constexpr uint32_t BIT_ZERO_SPACE_US = 545;
14 
15 constexpr unsigned int MIRAGE_IR_PACKET_BIT_SIZE = 120;
16 
18  ESP_LOGI(TAG, "Transive Mirage: %s", format_hex_pretty(data.data).c_str());
19  dst->set_carrier_frequency(38000);
20  dst->reserve(5 + ((data.data.size() + 1) * 2));
21  dst->mark(HEADER_MARK_US);
22  dst->space(HEADER_SPACE_US);
23  dst->mark(BIT_MARK_US);
24  uint8_t checksum = 0;
25  for (uint8_t item : data.data) {
26  this->encode_byte_(dst, item);
27  checksum += (item >> 4) + (item & 0xF);
28  }
29  this->encode_byte_(dst, checksum);
30 }
31 
33  for (uint8_t b = 0; b < 8; b++) {
34  if (item & (1UL << b)) {
35  dst->space(BIT_ONE_SPACE_US);
36  } else {
37  dst->space(BIT_ZERO_SPACE_US);
38  }
39  dst->mark(BIT_MARK_US);
40  }
41 }
42 
44  if (!src.expect_item(HEADER_MARK_US, HEADER_SPACE_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 < MIRAGE_IR_PACKET_BIT_SIZE * 2)
52  return {};
53  size = MIRAGE_IR_PACKET_BIT_SIZE * 2;
54  uint8_t checksum = 0;
55  MirageData out;
56  while (size > 0) {
57  uint8_t data = 0;
58  for (uint8_t b = 0; b < 8; b++) {
59  if (src.expect_space(BIT_ONE_SPACE_US)) {
60  data |= (1UL << b);
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 >> 4) + (data & 0xF);
71  out.data.push_back(data);
72  } else if (checksum != data) {
73  return {};
74  }
75  }
76  return out;
77 }
78 
79 void MirageProtocol::dump(const MirageData &data) {
80  ESP_LOGI(TAG, "Received Mirage: %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
void encode(RemoteTransmitData *dst, const MirageData &data) override
uint8_t checksum
Definition: bl0906.h:210
std::vector< uint8_t > data
void encode_byte_(RemoteTransmitData *dst, uint8_t item)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
optional< MirageData > decode(RemoteReceiveData src) override
constexpr unsigned int MIRAGE_IR_PACKET_BIT_SIZE
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.cpp:74
void dump(const MirageData &data) override