ESPHome  2024.12.2
pioneer_protocol.cpp
Go to the documentation of this file.
1 #include "pioneer_protocol.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace remote_base {
6 
7 static const char *const TAG = "remote.pioneer";
8 
9 static const uint32_t HEADER_HIGH_US = 9000;
10 static const uint32_t HEADER_LOW_US = 4500;
11 static const uint32_t BIT_HIGH_US = 560;
12 static const uint32_t BIT_ONE_LOW_US = 1690;
13 static const uint32_t BIT_ZERO_LOW_US = 560;
14 static const uint32_t TRAILER_SPACE_US = 25500;
15 
17  uint32_t address1 = ((data.rc_code_1 & 0xff00) | (~(data.rc_code_1 >> 8) & 0xff));
18  uint32_t address2 = ((data.rc_code_2 & 0xff00) | (~(data.rc_code_2 >> 8) & 0xff));
19  uint32_t command1 = 0;
20  uint32_t command2 = 0;
21 
22  for (uint32_t bit = 0; bit < 4; bit++) {
23  if ((data.rc_code_1 >> bit) & 1)
24  command1 |= (1UL << (7 - bit));
25  }
26 
27  for (uint32_t bit = 0; bit < 4; bit++) {
28  if ((data.rc_code_1 >> (bit + 4)) & 1)
29  command1 |= (1UL << (3 - bit));
30  }
31 
32  for (uint32_t bit = 0; bit < 4; bit++) {
33  if ((data.rc_code_2 >> bit) & 1)
34  command2 |= (1UL << (7 - bit));
35  }
36 
37  for (uint32_t bit = 0; bit < 4; bit++) {
38  if ((data.rc_code_2 >> (bit + 4)) & 1)
39  command2 |= (1UL << (3 - bit));
40  }
41 
42  command1 = (command1 << 8) | ((~command1) & 0xff);
43  command2 = (command2 << 8) | ((~command2) & 0xff);
44 
45  if (data.rc_code_2 == 0) {
46  dst->reserve(68);
47  } else {
48  dst->reserve((68 * 2) + 1);
49  }
50 
51  dst->set_carrier_frequency(40000);
52 
53  dst->item(HEADER_HIGH_US, HEADER_LOW_US);
54  for (uint32_t mask = 1UL << 15; mask; mask >>= 1) {
55  if (address1 & mask) {
56  dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
57  } else {
58  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
59  }
60  }
61 
62  for (uint32_t mask = 1UL << 15; mask; mask >>= 1) {
63  if (command1 & mask) {
64  dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
65  } else {
66  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
67  }
68  }
69 
70  dst->mark(BIT_HIGH_US);
71 
72  if (data.rc_code_2 != 0) {
73  dst->space(TRAILER_SPACE_US);
74  dst->item(HEADER_HIGH_US, HEADER_LOW_US);
75  for (uint32_t mask = 1UL << 15; mask; mask >>= 1) {
76  if (address2 & mask) {
77  dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
78  } else {
79  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
80  }
81  }
82 
83  for (uint32_t mask = 1UL << 15; mask; mask >>= 1) {
84  if (command2 & mask) {
85  dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
86  } else {
87  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
88  }
89  }
90 
91  dst->mark(BIT_HIGH_US);
92  }
93 }
95  uint16_t address1 = 0;
96  uint16_t command1 = 0;
97 
98  PioneerData data{
99  .rc_code_1 = 0,
100  .rc_code_2 = 0,
101  };
102  if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
103  return {};
104 
105  for (uint32_t mask = 1UL << 15; mask != 0; mask >>= 1) {
106  if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
107  address1 |= mask;
108  } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
109  address1 &= ~mask;
110  } else {
111  return {};
112  }
113  }
114 
115  for (uint32_t mask = 1UL << 15; mask != 0; mask >>= 1) {
116  if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
117  command1 |= mask;
118  } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
119  command1 &= ~mask;
120  } else {
121  return {};
122  }
123  }
124 
125  if (!src.expect_mark(BIT_HIGH_US))
126  return {};
127 
128  if ((address1 >> 8) != ((~address1) & 0xff))
129  return {};
130 
131  if ((command1 >> 8) != ((~command1) & 0xff))
132  return {};
133 
134  for (uint32_t bit = 0; bit < 4; bit++) {
135  if ((~command1 >> bit) & 1)
136  data.rc_code_1 |= (1UL << (7 - bit));
137  }
138 
139  for (uint32_t bit = 0; bit < 4; bit++) {
140  if ((~command1 >> (bit + 4)) & 1)
141  data.rc_code_1 |= (1UL << (3 - bit));
142  }
143  data.rc_code_1 |= address1 & 0xff00;
144 
145  return data;
146 }
148  if (data.rc_code_2 == 0) {
149  ESP_LOGI(TAG, "Received Pioneer: rc_code_X=0x%04X", data.rc_code_1);
150  } else {
151  ESP_LOGI(TAG, "Received Pioneer: rc_code_1=0x%04X, rc_code_2=0x%04X", data.rc_code_1, data.rc_code_2);
152  }
153 }
154 
155 } // namespace remote_base
156 } // namespace esphome
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
void encode(RemoteTransmitData *dst, const PioneerData &data) override
optional< PioneerData > decode(RemoteReceiveData src) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void dump(const PioneerData &data) override
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.cpp:74