ESPHome 2026.5.1
Loading...
Searching...
No Matches
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
5namespace esphome::remote_base {
6
7static const char *const TAG = "remote.samsung";
8
9static constexpr uint32_t HEADER_HIGH_US = 4500;
10static constexpr uint32_t HEADER_LOW_US = 4500;
11static constexpr uint32_t BIT_HIGH_US = 560;
12static constexpr uint32_t BIT_ONE_LOW_US = 1690;
13static constexpr uint32_t BIT_ZERO_LOW_US = 560;
14static constexpr uint32_t FOOTER_HIGH_US = 560;
15static constexpr uint32_t FOOTER_LOW_US = 560;
16
18 dst->set_carrier_frequency(38000);
19 dst->reserve(4 + data.nbits * 2u);
20
21 dst->item(HEADER_HIGH_US, HEADER_LOW_US);
22
23 for (uint8_t bit = data.nbits; bit > 0; bit--) {
24 if ((data.data >> (bit - 1)) & 1) {
25 dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
26 } else {
27 dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
28 }
29 }
30
31 dst->item(FOOTER_HIGH_US, FOOTER_LOW_US);
32}
33optional<SamsungData> SamsungProtocol::decode(RemoteReceiveData src) {
34 SamsungData out{
35 .data = 0,
36 .nbits = 0,
37 };
38 if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
39 return {};
40
41 for (out.nbits = 0; out.nbits < 64; out.nbits++) {
42 if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
43 out.data = (out.data << 1) | 1;
44 } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
45 out.data = (out.data << 1) | 0;
46 } else if (out.nbits >= 31) {
47 if (!src.expect_mark(FOOTER_HIGH_US))
48 return {};
49 return out;
50 } else {
51 return {};
52 }
53 }
54
55 if (!src.expect_mark(FOOTER_HIGH_US))
56 return {};
57 return out;
58}
60 ESP_LOGI(TAG, "Received Samsung: data=0x%" PRIX64 ", nbits=%d", data.data, data.nbits);
61}
62
63} // 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< SamsungData > decode(RemoteReceiveData src) override
void encode(RemoteTransmitData *dst, const SamsungData &data) override
void dump(const SamsungData &data) override
static void uint32_t