ESPHome  2024.11.1
samsung_protocol.cpp
Go to the documentation of this file.
1 #include "samsung_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.samsung";
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 = 560;
17 
19  dst->set_carrier_frequency(38000);
20  dst->reserve(4 + data.nbits * 2u);
21 
22  dst->item(HEADER_HIGH_US, HEADER_LOW_US);
23 
24  for (uint8_t bit = data.nbits; bit > 0; bit--) {
25  if ((data.data >> (bit - 1)) & 1) {
26  dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
27  } else {
28  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
29  }
30  }
31 
32  dst->item(FOOTER_HIGH_US, FOOTER_LOW_US);
33 }
35  SamsungData out{
36  .data = 0,
37  .nbits = 0,
38  };
39  if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
40  return {};
41 
42  for (out.nbits = 0; out.nbits < 64; out.nbits++) {
43  if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
44  out.data = (out.data << 1) | 1;
45  } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
46  out.data = (out.data << 1) | 0;
47  } else if (out.nbits >= 31) {
48  if (!src.expect_mark(FOOTER_HIGH_US))
49  return {};
50  return out;
51  } else {
52  return {};
53  }
54  }
55 
56  if (!src.expect_mark(FOOTER_HIGH_US))
57  return {};
58  return out;
59 }
61  ESP_LOGI(TAG, "Received Samsung: data=0x%" PRIX64 ", nbits=%d", data.data, data.nbits);
62 }
63 
64 } // namespace remote_base
65 } // namespace esphome
optional< SamsungData > decode(RemoteReceiveData src) override
void dump(const SamsungData &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
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.cpp:74
void encode(RemoteTransmitData *dst, const SamsungData &data) override