ESPHome 2025.11.0-dev
Loading...
Searching...
No Matches
dyson_protocol.cpp
Go to the documentation of this file.
1#include "dyson_protocol.h"
2#include "esphome/core/log.h"
3
4#include <cinttypes>
5
6namespace esphome {
7namespace remote_base {
8
9static const char *const TAG = "remote.dyson";
10
11// pulsewidth [µs]
12constexpr uint32_t PW_MARK_US = 780;
13constexpr uint32_t PW_SHORT_US = 720;
14constexpr uint32_t PW_LONG_US = 1500;
15constexpr uint32_t PW_START_US = 2280;
16
17// MSB of 15 bit dyson code
18constexpr uint16_t MSB_DYSON = (1 << 14);
19
20// required symbols in transmit buffer = (start_symbol + 15 data_symbols)
21constexpr uint32_t N_SYMBOLS_REQ = 2u * (1 + 15);
22
24 uint32_t raw_code = (data.code << 2) + (data.index & 3);
25 dst->set_carrier_frequency(36000);
26 dst->reserve(N_SYMBOLS_REQ + 1);
28 for (uint16_t mask = MSB_DYSON; mask != 0; mask >>= 1) {
29 if (mask == (mask & raw_code)) {
31 } else {
33 }
34 }
35 dst->mark(PW_MARK_US); // final carrier pulse
36}
37
39 uint32_t n_received = static_cast<uint32_t>(src.size());
40 uint16_t raw_code = 0;
41 DysonData data{
42 .code = 0,
43 .index = 0,
44 };
45 if (n_received < N_SYMBOLS_REQ)
46 return {}; // invalid frame length
48 return {}; // start not found
49 for (uint16_t mask = MSB_DYSON; mask != 0; mask >>= 1) {
51 raw_code &= ~mask; // zero detected
52 } else if (src.expect_item(PW_MARK_US, PW_LONG_US)) {
53 raw_code |= mask; // one detected
54 } else {
55 return {}; // invalid data item
56 }
57 }
58 data.code = raw_code >> 2; // extract button code
59 data.index = raw_code & 3; // extract rolling index
60 if (src.expect_mark(PW_MARK_US)) { // check total length
61 return data;
62 }
63 return {}; // frame not complete
64}
65
66void DysonProtocol::dump(const DysonData &data) {
67 ESP_LOGI(TAG, "Dyson: code=0x%x rolling index=%d", data.code, data.index);
68}
69
70} // namespace remote_base
71} // namespace esphome
optional< DysonData > decode(RemoteReceiveData src) override
void encode(RemoteTransmitData *dst, const DysonData &data) override
void dump(const DysonData &data) override
bool expect_item(uint32_t mark, uint32_t space)
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:30
void item(uint32_t mark, uint32_t space)
Definition remote_base.h:25
constexpr uint32_t PW_SHORT_US
constexpr uint16_t MSB_DYSON
constexpr uint32_t PW_LONG_US
constexpr uint32_t N_SYMBOLS_REQ
constexpr uint32_t PW_MARK_US
constexpr uint16_t PW_START_US
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7