ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
dooya_protocol.cpp
Go to the documentation of this file.
1#include "dooya_protocol.h"
2#include "esphome/core/log.h"
3
4namespace esphome::remote_base {
5
6static const char *const TAG = "remote.dooya";
7
8static constexpr uint32_t HEADER_HIGH_US = 5000;
9static constexpr uint32_t HEADER_LOW_US = 1500;
10static constexpr uint32_t BIT_ZERO_HIGH_US = 350;
11static constexpr uint32_t BIT_ZERO_LOW_US = 750;
12static constexpr uint32_t BIT_ONE_HIGH_US = 750;
13static constexpr uint32_t BIT_ONE_LOW_US = 350;
14
17 dst->reserve(2 + 40 * 2u);
18
19 dst->item(HEADER_HIGH_US, HEADER_LOW_US);
20
21 for (uint32_t mask = 1UL << (23); mask != 0; mask >>= 1) {
22 if (data.id & mask) {
23 dst->item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US);
24 } else {
25 dst->item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US);
26 }
27 }
28
29 for (uint32_t mask = 1UL << (7); mask != 0; mask >>= 1) {
30 if (data.channel & mask) {
31 dst->item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US);
32 } else {
33 dst->item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US);
34 }
35 }
36
37 for (uint32_t mask = 1UL << (3); mask != 0; mask >>= 1) {
38 if (data.button & mask) {
39 dst->item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US);
40 } else {
41 dst->item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US);
42 }
43 }
44
45 for (uint32_t mask = 1UL << (3); mask != 0; mask >>= 1) {
46 if (data.check & mask) {
47 dst->item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US);
48 } else {
49 dst->item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US);
50 }
51 }
52}
53optional<DooyaData> DooyaProtocol::decode(RemoteReceiveData src) {
54 DooyaData out{
55 .id = 0,
56 .channel = 0,
57 .button = 0,
58 .check = 0,
59 };
60 if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
61 return {};
62
63 for (uint8_t i = 0; i < 24; i++) {
64 if (src.expect_item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US)) {
65 out.id = (out.id << 1) | 1;
66 } else if (src.expect_item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US)) {
67 out.id = (out.id << 1) | 0;
68 } else {
69 return {};
70 }
71 }
72
73 for (uint8_t i = 0; i < 8; i++) {
74 if (src.expect_item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US)) {
75 out.channel = (out.channel << 1) | 1;
76 } else if (src.expect_item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US)) {
77 out.channel = (out.channel << 1) | 0;
78 } else {
79 return {};
80 }
81 }
82
83 for (uint8_t i = 0; i < 4; i++) {
84 if (src.expect_item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US)) {
85 out.button = (out.button << 1) | 1;
86 } else if (src.expect_item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US)) {
87 out.button = (out.button << 1) | 0;
88 } else {
89 return {};
90 }
91 }
92
93 for (uint8_t i = 0; i < 3; i++) {
94 if (src.expect_item(BIT_ONE_HIGH_US, BIT_ONE_LOW_US)) {
95 out.check = (out.check << 1) | 1;
96 } else if (src.expect_item(BIT_ZERO_HIGH_US, BIT_ZERO_LOW_US)) {
97 out.check = (out.check << 1) | 0;
98 } else {
99 return {};
100 }
101 }
102 // Last bit is not received properly but can be decoded
103 if (src.expect_mark(BIT_ONE_HIGH_US)) {
104 out.check = (out.check << 1) | 1;
105 } else if (src.expect_mark(BIT_ZERO_HIGH_US)) {
106 out.check = (out.check << 1) | 0;
107 } else {
108 return {};
109 }
110
111 return out;
112}
114 ESP_LOGI(TAG, "Received Dooya: id=0x%08" PRIX32 ", channel=%d, button=%d, check=%d", data.id, data.channel,
115 data.button, data.check);
116}
117
118} // namespace esphome::remote_base
void encode(RemoteTransmitData *dst, const DooyaData &data) override
optional< DooyaData > decode(RemoteReceiveData src) override
void dump(const DooyaData &data) override
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
static void uint32_t