ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
sony_protocol.cpp
Go to the documentation of this file.
1#include "sony_protocol.h"
2#include "esphome/core/log.h"
3
4namespace esphome::remote_base {
5
6static const char *const TAG = "remote.sony";
7
8static constexpr uint32_t HEADER_HIGH_US = 2400;
9static constexpr uint32_t HEADER_LOW_US = 600;
10static constexpr uint32_t BIT_ONE_HIGH_US = 1200;
11static constexpr uint32_t BIT_ZERO_HIGH_US = 600;
12static constexpr uint32_t BIT_LOW_US = 600;
13
15 dst->set_carrier_frequency(40000);
16 dst->reserve(2 + data.nbits * 2u);
17
18 dst->item(HEADER_HIGH_US, HEADER_LOW_US);
19
20 for (uint32_t mask = 1UL << (data.nbits - 1); mask != 0; mask >>= 1) {
21 if (data.data & mask) {
22 dst->item(BIT_ONE_HIGH_US, BIT_LOW_US);
23 } else {
24 dst->item(BIT_ZERO_HIGH_US, BIT_LOW_US);
25 }
26 }
27}
28optional<SonyData> SonyProtocol::decode(RemoteReceiveData src) {
29 SonyData out{
30 .data = 0,
31 .nbits = 0,
32 };
33 if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
34 return {};
35
36 for (; out.nbits < 20; out.nbits++) {
37 uint32_t bit;
38 if (src.expect_mark(BIT_ONE_HIGH_US)) {
39 bit = 1;
40 } else if (src.expect_mark(BIT_ZERO_HIGH_US)) {
41 bit = 0;
42 } else if (out.nbits == 12 || out.nbits == 15) {
43 return out;
44 } else {
45 return {};
46 }
47
48 out.data = (out.data << 1UL) | bit;
49 if (src.expect_space(BIT_LOW_US)) {
50 // nothing needs to be done
51 } else if (src.peek_space_at_least(BIT_LOW_US)) {
52 out.nbits += 1;
53 if (out.nbits == 12 || out.nbits == 15 || out.nbits == 20)
54 return out;
55 return {};
56 } else {
57 return {};
58 }
59 }
60
61 return out;
62}
63void SonyProtocol::dump(const SonyData &data) {
64 ESP_LOGI(TAG, "Received Sony: data=0x%08" PRIX32 ", nbits=%d", data.data, data.nbits);
65}
66
67} // namespace esphome::remote_base
bool expect_item(uint32_t mark, uint32_t space)
bool peek_space_at_least(uint32_t length, uint32_t offset=0) const
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< SonyData > decode(RemoteReceiveData src) override
void encode(RemoteTransmitData *dst, const SonyData &data) override
void dump(const SonyData &data) override
static void uint32_t