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