ESPHome 2025.5.0
Loading...
Searching...
No Matches
beo4_protocol.cpp
Go to the documentation of this file.
1#include "beo4_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.beo4";
10
11// beo4 pulse width, high=carrier_pulse low=data_pulse
12constexpr uint16_t PW_CARR_US = 200; // carrier pulse length
13constexpr uint16_t PW_ZERO_US = 2925; // + 200 = 3125 µs
14constexpr uint16_t PW_SAME_US = 6050; // + 200 = 6250 µs
15constexpr uint16_t PW_ONE_US = 9175; // + 200 = 9375 µs
16constexpr uint16_t PW_STOP_US = 12300; // + 200 = 12500 µs
17constexpr uint16_t PW_START_US = 15425; // + 200 = 15625 µs
18
19// beo4 pulse codes
20constexpr uint8_t PC_ZERO = (PW_CARR_US + PW_ZERO_US) / 3125; // =1
21constexpr uint8_t PC_SAME = (PW_CARR_US + PW_SAME_US) / 3125; // =2
22constexpr uint8_t PC_ONE = (PW_CARR_US + PW_ONE_US) / 3125; // =3
23constexpr uint8_t PC_STOP = (PW_CARR_US + PW_STOP_US) / 3125; // =4
24constexpr uint8_t PC_START = (PW_CARR_US + PW_START_US) / 3125; // =5
25
26// beo4 number of data bits = beoLink+beoSrc+beoCmd = 1+8+8 = 17
27constexpr uint32_t N_BITS = 1 + 8 + 8;
28
29// required symbols = 2*(start_sequence + n_bits + stop) = 2*(3+17+1) = 42
30constexpr uint32_t N_SYM = 2 + ((3 + 17 + 1) * 2u); // + 2 = 44
31
32// states finite-state-machine decoder
33enum class RxSt { RX_IDLE, RX_DATA, RX_STOP };
34
36 uint32_t beo_code = ((uint32_t) data.source << 8) + (uint32_t) data.command;
37 uint32_t jc = 0, ic = 0;
38 uint32_t cur_bit = 0;
39 uint32_t pre_bit = 0;
40 dst->set_carrier_frequency(455000);
41 dst->reserve(N_SYM);
42
43 // start sequence=zero,zero,start
47
48 // the data-bit BeoLink is always 0
50
51 // The B&O trick to avoid extra long and extra short
52 // code-frames by extracting the data-bits from left
53 // to right, then comparing current with previous bit
54 // and set pulse to "same" "one" or "zero"
55 for (jc = 15, ic = 0; ic < 16; ic++, jc--) {
56 cur_bit = ((beo_code) >> jc) & 1;
57 if (cur_bit == pre_bit) {
59 } else if (1 == cur_bit) {
61 } else {
63 }
64 pre_bit = cur_bit;
65 }
66 // complete the frame with stop-symbol and final carrier pulse
68 dst->mark(PW_CARR_US);
69}
70
72 int32_t n_sym = src.size();
73 Beo4Data data{
74 .source = 0,
75 .command = 0,
76 .repeats = 0,
77 };
78 // suppress dummy codes (TSO7000 hiccups)
79 if (n_sym > 42) {
80 static uint32_t beo_code = 0;
81 RxSt fsm = RxSt::RX_IDLE;
82 int32_t ic = 0;
83 int32_t jc = 0;
84 uint32_t pre_bit = 0;
85 uint32_t cnt_bit = 0;
86 ESP_LOGD(TAG, "Beo4: n_sym=%" PRId32, n_sym);
87 for (jc = 0, ic = 0; ic < (n_sym - 1); ic += 2, jc++) {
88 int32_t pulse_width = src[ic] - src[ic + 1];
89 // suppress TSOP7000 (dummy pulses)
90 if (pulse_width > 1500) {
91 int32_t pulse_code = (pulse_width + 1560) / 3125;
92 switch (fsm) {
93 case RxSt::RX_IDLE: {
94 beo_code = 0;
95 cnt_bit = 0;
96 pre_bit = 0;
97 if (PC_START == pulse_code) {
98 fsm = RxSt::RX_DATA;
99 }
100 break;
101 }
102 case RxSt::RX_DATA: {
103 uint32_t cur_bit = 0;
104 switch (pulse_code) {
105 case PC_ZERO: {
106 cur_bit = pre_bit = 0;
107 break;
108 }
109 case PC_SAME: {
110 cur_bit = pre_bit;
111 break;
112 }
113 case PC_ONE: {
114 cur_bit = pre_bit = 1;
115 break;
116 }
117 default: {
118 fsm = RxSt::RX_IDLE;
119 break;
120 }
121 }
122 beo_code = (beo_code << 1) + cur_bit;
123 if (++cnt_bit == N_BITS) {
124 fsm = RxSt::RX_STOP;
125 }
126 break;
127 }
128 case RxSt::RX_STOP: {
129 if (PC_STOP == pulse_code) {
130 data.source = (uint8_t) ((beo_code >> 8) & 0xff);
131 data.command = (uint8_t) ((beo_code) &0xff);
132 data.repeats++;
133 }
134 if ((n_sym - ic) < 42) {
135 return data;
136 } else {
137 fsm = RxSt::RX_IDLE;
138 }
139 break;
140 }
141 }
142 }
143 }
144 }
145 return {}; // decoding failed
146}
147
148void Beo4Protocol::dump(const Beo4Data &data) {
149 ESP_LOGI(TAG, "Beo4: source=0x%02x command=0x%02x repeats=%d ", data.source, data.command, data.repeats);
150}
151
152} // namespace remote_base
153} // namespace esphome
void dump(const Beo4Data &data) override
optional< Beo4Data > decode(RemoteReceiveData src) override
void encode(RemoteTransmitData *dst, const Beo4Data &data) override
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
constexpr uint32_t N_BITS
constexpr uint32_t N_SYM
constexpr uint8_t PC_ONE
constexpr uint16_t PW_ONE_US
constexpr uint16_t PW_CARR_US
constexpr uint8_t PC_START
constexpr uint16_t PW_ZERO_US
constexpr uint8_t PC_STOP
constexpr uint8_t PC_SAME
constexpr uint8_t PC_ZERO
constexpr uint16_t PW_STOP_US
constexpr uint16_t PW_SAME_US
constexpr uint16_t PW_START_US
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7