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