ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
mirage_protocol.cpp
Go to the documentation of this file.
1#include "mirage_protocol.h"
3#include "esphome/core/log.h"
4
5namespace esphome::remote_base {
6
7static const char *const TAG = "remote.mirage";
8
9constexpr uint32_t HEADER_MARK_US = 8360;
10constexpr uint32_t HEADER_SPACE_US = 4248;
11constexpr uint32_t BIT_MARK_US = 554;
12constexpr uint32_t BIT_ONE_SPACE_US = 1592;
13constexpr uint32_t BIT_ZERO_SPACE_US = 545;
14
15constexpr unsigned int MIRAGE_IR_PACKET_BIT_SIZE = 120;
16// Max data bytes in packet (excluding checksum)
18
21 ESP_LOGI(TAG, "Transmit Mirage: %s", format_hex_pretty_to(hex_buf, data.data.data(), data.data.size()));
22 dst->set_carrier_frequency(38000);
23 dst->reserve(5 + ((data.data.size() + 1) * 2));
24 dst->mark(HEADER_MARK_US);
25 dst->space(HEADER_SPACE_US);
26 dst->mark(BIT_MARK_US);
27 uint8_t checksum = 0;
28 for (uint8_t item : data.data) {
29 this->encode_byte_(dst, item);
30 checksum += (item >> 4) + (item & 0xF);
31 }
32 this->encode_byte_(dst, checksum);
33}
34
36 for (uint8_t b = 0; b < 8; b++) {
37 if (item & (1UL << b)) {
38 dst->space(BIT_ONE_SPACE_US);
39 } else {
40 dst->space(BIT_ZERO_SPACE_US);
41 }
42 dst->mark(BIT_MARK_US);
43 }
44}
45
46optional<MirageData> MirageProtocol::decode(RemoteReceiveData src) {
47 if (!src.expect_item(HEADER_MARK_US, HEADER_SPACE_US)) {
48 return {};
49 }
50 if (!src.expect_mark(BIT_MARK_US)) {
51 return {};
52 }
53 size_t size = src.size() - src.get_index() - 1;
55 return {};
57 uint8_t checksum = 0;
58 MirageData out;
59 while (size > 0) {
60 uint8_t data = 0;
61 for (uint8_t b = 0; b < 8; b++) {
62 if (src.expect_space(BIT_ONE_SPACE_US)) {
63 data |= (1UL << b);
64 } else if (!src.expect_space(BIT_ZERO_SPACE_US)) {
65 return {};
66 }
67 if (!src.expect_mark(BIT_MARK_US)) {
68 return {};
69 }
70 size -= 2;
71 }
72 if (size > 0) {
73 checksum += (data >> 4) + (data & 0xF);
74 out.data.push_back(data);
75 } else if (checksum != data) {
76 return {};
77 }
78 }
79 return out;
80}
81
84 ESP_LOGI(TAG, "Received Mirage: %s", format_hex_pretty_to(hex_buf, data.data.data(), data.data.size()));
85}
86
87} // namespace esphome::remote_base
uint8_t checksum
Definition bl0906.h:3
void encode_byte_(RemoteTransmitData *dst, uint8_t item)
void dump(const MirageData &data) override
void encode(RemoteTransmitData *dst, const MirageData &data) override
optional< MirageData > decode(RemoteReceiveData src) override
bool expect_item(uint32_t mark, uint32_t space)
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:29
constexpr unsigned int MIRAGE_IR_PACKET_BIT_SIZE
constexpr size_t MIRAGE_MAX_DATA_BYTES
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:341
uint16_t size
Definition helpers.cpp:25
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1386
static void uint32_t
std::vector< uint8_t > data