ESPHome  2024.11.0
lg_protocol.cpp
Go to the documentation of this file.
1 #include "lg_protocol.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace remote_base {
6 
7 static const char *const TAG = "remote.lg";
8 
9 static const uint32_t HEADER_HIGH_US = 8000;
10 static const uint32_t HEADER_LOW_US = 4000;
11 static const uint32_t BIT_HIGH_US = 600;
12 static const uint32_t BIT_ONE_LOW_US = 1600;
13 static const uint32_t BIT_ZERO_LOW_US = 550;
14 
15 void LGProtocol::encode(RemoteTransmitData *dst, const LGData &data) {
16  dst->set_carrier_frequency(38000);
17  dst->reserve(2 + data.nbits * 2u);
18 
19  dst->item(HEADER_HIGH_US, HEADER_LOW_US);
20 
21  for (uint32_t mask = 1UL << (data.nbits - 1); mask != 0; mask >>= 1) {
22  if (data.data & 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  dst->mark(BIT_HIGH_US);
30 }
32  LGData out{
33  .data = 0,
34  .nbits = 0,
35  };
36  if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
37  return {};
38 
39  for (out.nbits = 0; out.nbits < 32; out.nbits++) {
40  if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
41  out.data = (out.data << 1) | 1;
42  } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
43  out.data = (out.data << 1) | 0;
44  } else if (out.nbits == 28) {
45  return out;
46  } else {
47  return {};
48  }
49  }
50 
51  return out;
52 }
53 void LGProtocol::dump(const LGData &data) {
54  ESP_LOGI(TAG, "Received LG: data=0x%08" PRIX32 ", nbits=%d", data.data, data.nbits);
55 }
56 
57 } // namespace remote_base
58 } // namespace esphome
optional< LGData > decode(RemoteReceiveData src) override
Definition: lg_protocol.cpp:31
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 dump(const LGData &data) override
Definition: lg_protocol.cpp:53
void encode(RemoteTransmitData *dst, const LGData &data) override
Definition: lg_protocol.cpp:15
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.cpp:74