ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
toshiba_ac_protocol.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3#include <cinttypes>
4
5namespace esphome::remote_base {
6
7static const char *const TAG = "remote.toshibaac";
8
9static constexpr uint32_t HEADER_HIGH_US = 4500;
10static constexpr uint32_t HEADER_LOW_US = 4500;
11static constexpr uint32_t BIT_HIGH_US = 560;
12static constexpr uint32_t BIT_ONE_LOW_US = 1690;
13static constexpr uint32_t BIT_ZERO_LOW_US = 560;
14static constexpr uint32_t FOOTER_HIGH_US = 560;
15static constexpr uint32_t FOOTER_LOW_US = 4500;
16static constexpr uint16_t PACKET_SPACE = 5500;
17
19 dst->set_carrier_frequency(38000);
20 dst->reserve((3 + (48 * 2)) * 3);
21
22 for (uint8_t repeat = 0; repeat < 2; repeat++) {
23 dst->item(HEADER_HIGH_US, HEADER_LOW_US);
24 for (uint8_t bit = 48; bit > 0; bit--) {
25 dst->mark(BIT_HIGH_US);
26 if ((data.rc_code_1 >> (bit - 1)) & 1) {
27 dst->space(BIT_ONE_LOW_US);
28 } else {
29 dst->space(BIT_ZERO_LOW_US);
30 }
31 }
32 dst->item(FOOTER_HIGH_US, FOOTER_LOW_US);
33 }
34
35 if (data.rc_code_2 != 0) {
36 dst->item(HEADER_HIGH_US, HEADER_LOW_US);
37 for (uint8_t bit = 48; bit > 0; bit--) {
38 dst->mark(BIT_HIGH_US);
39 if ((data.rc_code_2 >> (bit - 1)) & 1) {
40 dst->space(BIT_ONE_LOW_US);
41 } else {
42 dst->space(BIT_ZERO_LOW_US);
43 }
44 }
45 dst->item(FOOTER_HIGH_US, FOOTER_LOW_US);
46 }
47}
48
49optional<ToshibaAcData> ToshibaAcProtocol::decode(RemoteReceiveData src) {
50 uint64_t packet = 0;
51 ToshibaAcData out{
52 .rc_code_1 = 0,
53 .rc_code_2 = 0,
54 };
55 // *** Packet 1
56 if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
57 return {};
58 for (uint8_t bit_counter = 0; bit_counter < 48; bit_counter++) {
59 if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
60 packet = (packet << 1) | 1;
61 } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
62 packet = (packet << 1) | 0;
63 } else {
64 return {};
65 }
66 }
67 if (!src.expect_item(FOOTER_HIGH_US, PACKET_SPACE))
68 return {};
69
70 // *** Packet 2
71 if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
72 return {};
73 for (uint8_t bit_counter = 0; bit_counter < 48; bit_counter++) {
74 if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
75 out.rc_code_1 = (out.rc_code_1 << 1) | 1;
76 } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
77 out.rc_code_1 = (out.rc_code_1 << 1) | 0;
78 } else {
79 return {};
80 }
81 }
82 // The first two packets must match
83 if (packet != out.rc_code_1)
84 return {};
85 // The third packet isn't always present
86 if (!src.expect_item(FOOTER_HIGH_US, PACKET_SPACE))
87 return out;
88
89 // *** Packet 3
90 if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
91 return {};
92 for (uint8_t bit_counter = 0; bit_counter < 48; bit_counter++) {
93 if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
94 out.rc_code_2 = (out.rc_code_2 << 1) | 1;
95 } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
96 out.rc_code_2 = (out.rc_code_2 << 1) | 0;
97 } else {
98 return {};
99 }
100 }
101
102 return out;
103}
104
106 if (data.rc_code_2 != 0) {
107 ESP_LOGI(TAG, "Received Toshiba AC: rc_code_1=0x%" PRIX64 ", rc_code_2=0x%" PRIX64, data.rc_code_1, data.rc_code_2);
108 } else {
109 ESP_LOGI(TAG, "Received Toshiba AC: rc_code_1=0x%" PRIX64, data.rc_code_1);
110 }
111}
112
113} // namespace esphome::remote_base
bool expect_item(uint32_t mark, uint32_t space)
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:29
void item(uint32_t mark, uint32_t space)
Definition remote_base.h:24
void dump(const ToshibaAcData &data) override
optional< ToshibaAcData > decode(RemoteReceiveData src) override
void encode(RemoteTransmitData *dst, const ToshibaAcData &data) override
static void uint32_t