ESPHome 2025.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 {
5namespace remote_base {
6
7static const char *const TAG = "remote.samsung36";
8
9static const uint8_t NBITS = 78;
10
11static const uint32_t HEADER_HIGH_US = 4500;
12static const uint32_t HEADER_LOW_US = 4500;
13static const uint32_t BIT_HIGH_US = 500;
14static const uint32_t BIT_ONE_LOW_US = 1500;
15static const uint32_t BIT_ZERO_LOW_US = 500;
16static const uint32_t MIDDLE_HIGH_US = 500;
17static const uint32_t MIDDLE_LOW_US = 4500;
18static const uint32_t FOOTER_HIGH_US = 500;
19static 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
bool expect_item(uint32_t mark, uint32_t space)
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
void dump(const Samsung36Data &data) override
void encode(RemoteTransmitData *dst, const Samsung36Data &data) override
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7