ESPHome  2024.12.2
samsung36_protocol.cpp
Go to the documentation of this file.
1 #include "samsung36_protocol.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace remote_base {
6 
7 static const char *const TAG = "remote.samsung36";
8 
9 static const uint8_t NBITS = 78;
10 
11 static const uint32_t HEADER_HIGH_US = 4500;
12 static const uint32_t HEADER_LOW_US = 4500;
13 static const uint32_t BIT_HIGH_US = 500;
14 static const uint32_t BIT_ONE_LOW_US = 1500;
15 static const uint32_t BIT_ZERO_LOW_US = 500;
16 static const uint32_t MIDDLE_HIGH_US = 500;
17 static const uint32_t MIDDLE_LOW_US = 4500;
18 static const uint32_t FOOTER_HIGH_US = 500;
19 static const uint32_t FOOTER_LOW_US = 59000;
20 
22  dst->set_carrier_frequency(38000);
23  dst->reserve(NBITS);
24 
25  // send header
26  dst->item(HEADER_HIGH_US, HEADER_LOW_US);
27 
28  // send first 16 bits
29  for (uint32_t mask = 1UL << 15; mask != 0; mask >>= 1) {
30  if (data.address & 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 
37  // send middle header
38  dst->item(MIDDLE_HIGH_US, MIDDLE_LOW_US);
39 
40  // send last 20 bits
41  for (uint32_t mask = 1UL << 19; mask != 0; mask >>= 1) {
42  if (data.command & mask) {
43  dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
44  } else {
45  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
46  }
47  }
48 
49  // footer
50  dst->item(FOOTER_HIGH_US, FOOTER_LOW_US);
51 }
52 
54  Samsung36Data out{
55  .address = 0,
56  .command = 0,
57  };
58 
59  // check if header matches
60  if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
61  return {};
62 
63  // check if we have enough bits
64  if (src.size() != NBITS)
65  return {};
66 
67  // get the first 16 bits
68  for (uint8_t i = 0; i < 16; i++) {
69  out.address <<= 1UL;
70  if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
71  out.address |= 1UL;
72  } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
73  out.address |= 0UL;
74  } else {
75  return {};
76  }
77  }
78 
79  // check if the middle mark matches
80  if (!src.expect_item(MIDDLE_HIGH_US, MIDDLE_LOW_US)) {
81  return {};
82  }
83 
84  // get the last 20 bits
85  for (uint8_t i = 0; i < 20; i++) {
86  out.command <<= 1UL;
87  if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
88  out.command |= 1UL;
89  } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
90  out.command |= 0UL;
91  } else {
92  return {};
93  }
94  }
95 
96  return out;
97 }
99  ESP_LOGI(TAG, "Received Samsung36: address=0x%04X, command=0x%08" PRIX32, data.address, data.command);
100 }
101 
102 } // namespace remote_base
103 } // namespace esphome
void dump(const Samsung36Data &data) override
void encode(RemoteTransmitData *dst, const Samsung36Data &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< Samsung36Data > decode(RemoteReceiveData src) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.cpp:74