ESPHome  2024.4.1
dish_protocol.cpp
Go to the documentation of this file.
1 #include "dish_protocol.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace remote_base {
6 
7 static const char *const TAG = "remote.dish";
8 
9 static const uint32_t HEADER_HIGH_US = 400;
10 static const uint32_t HEADER_LOW_US = 6100;
11 static const uint32_t BIT_HIGH_US = 400;
12 static const uint32_t BIT_ONE_LOW_US = 1700;
13 static const uint32_t BIT_ZERO_LOW_US = 2800;
14 
16  dst->reserve(138);
17  dst->set_carrier_frequency(57600);
18 
19  // HEADER
20  dst->item(HEADER_HIGH_US, HEADER_LOW_US);
21 
22  // Typically a DISH device needs to get a command a total of
23  // at least 4 times to accept it.
24  for (uint i = 0; i < 4; i++) {
25  // COMMAND (function, in MSB)
26  for (uint8_t mask = 1UL << 5; mask; mask >>= 1) {
27  if (data.command & mask) {
28  dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
29  } else {
30  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
31  }
32  }
33 
34  // ADDRESS (unit code, in LSB)
35  for (uint8_t mask = 1UL; mask < 1UL << 4; mask <<= 1) {
36  if ((data.address - 1) & mask) {
37  dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
38  } else {
39  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
40  }
41  }
42  // PADDING
43  for (uint j = 0; j < 6; j++)
44  dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
45 
46  // FOOTER
47  dst->item(HEADER_HIGH_US, HEADER_LOW_US);
48  }
49 }
51  DishData data{
52  .address = 0,
53  .command = 0,
54  };
55  if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
56  return {};
57 
58  for (uint8_t mask = 1UL << 5; mask != 0; mask >>= 1) {
59  if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
60  data.command |= mask;
61  } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
62  data.command &= ~mask;
63  } else {
64  return {};
65  }
66  }
67 
68  for (uint8_t mask = 1UL; mask < 1UL << 5; mask <<= 1) {
69  if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
70  data.address |= mask;
71  } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
72  data.address &= ~mask;
73  } else {
74  return {};
75  }
76  }
77  for (uint j = 0; j < 6; j++) {
78  if (!src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
79  return {};
80  }
81  }
82  data.address++;
83 
84  src.expect_item(HEADER_HIGH_US, HEADER_LOW_US);
85 
86  return data;
87 }
88 
89 void DishProtocol::dump(const DishData &data) {
90  ESP_LOGI(TAG, "Received Dish: address=0x%02X, command=0x%02X", data.address, data.command);
91 }
92 
93 } // namespace remote_base
94 } // namespace esphome
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< DishData > decode(RemoteReceiveData src) override
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void encode(RemoteTransmitData *dst, const DishData &data) override
bool expect_item(uint32_t mark, uint32_t space)
Definition: remote_base.cpp:74
void dump(const DishData &data) override