ESPHome  2022.11.3
panasonic_protocol.cpp
Go to the documentation of this file.
1 #include "panasonic_protocol.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace remote_base {
6 
7 static const char *const TAG = "remote.panasonic";
8 
9 static const uint32_t HEADER_HIGH_US = 3502;
10 static const uint32_t HEADER_LOW_US = 1750;
11 static const uint32_t BIT_HIGH_US = 502;
12 static const uint32_t BIT_ZERO_LOW_US = 400;
13 static const uint32_t BIT_ONE_LOW_US = 1244;
14 
16  dst->reserve(100);
17  dst->item(HEADER_HIGH_US, HEADER_LOW_US);
18  dst->set_carrier_frequency(35000);
19 
20  uint32_t mask;
21  for (mask = 1UL << 15; mask != 0; mask >>= 1) {
22  if (data.address & mask) {
23  dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
24  } else {
25  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
26  }
27  }
28 
29  for (mask = 1UL << 31; mask != 0; mask >>= 1) {
30  if (data.command & mask) {
31  dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
32  } else {
33  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
34  }
35  }
36  dst->mark(BIT_HIGH_US);
37 }
39  PanasonicData out{
40  .address = 0,
41  .command = 0,
42  };
43  if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
44  return {};
45 
46  uint32_t mask;
47  for (mask = 1UL << 15; mask != 0; mask >>= 1) {
48  if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
49  out.address |= mask;
50  } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
51  out.address &= ~mask;
52  } else {
53  return {};
54  }
55  }
56 
57  for (mask = 1UL << 31; mask != 0; mask >>= 1) {
58  if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
59  out.command |= mask;
60  } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
61  out.command &= ~mask;
62  } else {
63  return {};
64  }
65  }
66 
67  return out;
68 }
70  ESP_LOGD(TAG, "Received Panasonic: address=0x%04X, command=0x%08X", data.address, data.command);
71 }
72 
73 } // namespace remote_base
74 } // namespace esphome
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:30
void item(uint32_t mark, uint32_t space)
Definition: remote_base.h:23
optional< PanasonicData > decode(RemoteReceiveData src) override
void encode(RemoteTransmitData *dst, const PanasonicData &data) override
void dump(const PanasonicData &data) override
Definition: a4988.cpp:4
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.h:111