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