ESPHome  2024.11.0
pronto_protocol.cpp
Go to the documentation of this file.
1 /*
2  * @file irPronto.cpp
3  * @brief In this file, the functions IRrecv::compensateAndPrintPronto and IRsend::sendPronto are defined.
4  *
5  * See http://www.harctoolbox.org/Glossary.html#ProntoSemantics
6  * Pronto database http://www.remotecentral.com/search.htm
7  *
8  * This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
9  *
10  ************************************************************************************
11  * MIT License
12  *
13  * Copyright (c) 2020 Bengt Martensson
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a copy
16  * of this software and associated documentation files (the "Software"), to deal
17  * in the Software without restriction, including without limitation the rights
18  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19  * copies of the Software, and to permit persons to whom the Software is furnished
20  * to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in all
23  * copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
26  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
27  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
28  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
30  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  ************************************************************************************
33  */
34 
35 #include "pronto_protocol.h"
36 #include "esphome/core/log.h"
37 
38 namespace esphome {
39 namespace remote_base {
40 
41 static const char *const TAG = "remote.pronto";
42 
43 bool ProntoData::operator==(const ProntoData &rhs) const {
44  std::vector<uint16_t> data1 = encode_pronto(data);
45  std::vector<uint16_t> data2 = encode_pronto(rhs.data);
46 
47  uint32_t total_diff = 0;
48  // Don't need to check the last one, it's the large gap at the end.
49  for (std::vector<uint16_t>::size_type i = 0; i < data1.size() - 1; ++i) {
50  int diff = data2[i] - data1[i];
51  diff *= diff;
52  if (rhs.delta == -1 && diff > 9)
53  return false;
54 
55  total_diff += diff;
56  }
57 
58  return total_diff <= (rhs.delta == -1 ? data1.size() * 3 : rhs.delta);
59 }
60 
61 // DO NOT EXPORT from this file
62 static const uint16_t MICROSECONDS_T_MAX = 0xFFFFU;
63 static const uint16_t LEARNED_TOKEN = 0x0000U;
64 static const uint16_t LEARNED_NON_MODULATED_TOKEN = 0x0100U;
65 static const uint16_t BITS_IN_HEXADECIMAL = 4U;
66 static const uint16_t DIGITS_IN_PRONTO_NUMBER = 4U;
67 static const uint16_t NUMBERS_IN_PREAMBLE = 4U;
68 static const uint16_t HEX_MASK = 0xFU;
69 static const uint32_t REFERENCE_FREQUENCY = 4145146UL;
70 static const uint16_t FALLBACK_FREQUENCY = 64767U; // To use with frequency = 0;
71 static const uint32_t MICROSECONDS_IN_SECONDS = 1000000UL;
72 static const uint16_t PRONTO_DEFAULT_GAP = 45000;
73 static const uint16_t MARK_EXCESS_MICROS = 20;
74 
75 static uint16_t to_frequency_k_hz(uint16_t code) {
76  if (code == 0)
77  return 0;
78 
79  return ((REFERENCE_FREQUENCY / code) + 500) / 1000;
80 }
81 
82 /*
83  * Parse the string given as Pronto Hex, and send it a number of times given as argument.
84  */
85 void ProntoProtocol::send_pronto_(RemoteTransmitData *dst, const std::vector<uint16_t> &data) {
86  if (data.size() < 4)
87  return;
88 
89  uint16_t timebase = (MICROSECONDS_IN_SECONDS * data[1] + REFERENCE_FREQUENCY / 2) / REFERENCE_FREQUENCY;
90  uint16_t khz;
91  switch (data[0]) {
92  case LEARNED_TOKEN: // normal, "learned"
93  khz = to_frequency_k_hz(data[1]);
94  break;
95  case LEARNED_NON_MODULATED_TOKEN: // non-demodulated, "learned"
96  khz = 0U;
97  break;
98  default:
99  return; // There are other types, but they are not handled yet.
100  }
101  ESP_LOGD(TAG, "Send Pronto: frequency=%dkHz", khz);
102  dst->set_carrier_frequency(khz * 1000);
103 
104  uint16_t intros = 2 * data[2];
105  uint16_t repeats = 2 * data[3];
106  ESP_LOGD(TAG, "Send Pronto: intros=%d", intros);
107  ESP_LOGD(TAG, "Send Pronto: repeats=%d", repeats);
108  if (NUMBERS_IN_PREAMBLE + intros + repeats != data.size()) { // inconsistent sizes
109  ESP_LOGE(TAG, "Inconsistent data, not sending");
110  return;
111  }
112 
113  /*
114  * Generate a new microseconds timing array for sendRaw.
115  * If recorded by IRremote, intro contains the whole IR data and repeat is empty
116  */
117  dst->reserve(intros + repeats);
118 
119  for (uint16_t i = 0; i < intros + repeats; i += 2) {
120  uint32_t duration0 = ((uint32_t) data[i + 0 + NUMBERS_IN_PREAMBLE]) * timebase;
121  duration0 = duration0 < MICROSECONDS_T_MAX ? duration0 : MICROSECONDS_T_MAX;
122 
123  uint32_t duration1 = ((uint32_t) data[i + 1 + NUMBERS_IN_PREAMBLE]) * timebase;
124  duration1 = duration1 < MICROSECONDS_T_MAX ? duration1 : MICROSECONDS_T_MAX;
125 
126  dst->item(duration0, duration1);
127  }
128 }
129 
130 std::vector<uint16_t> encode_pronto(const std::string &str) {
131  size_t len = str.length() / (DIGITS_IN_PRONTO_NUMBER + 1) + 1;
132  std::vector<uint16_t> data;
133  const char *p = str.c_str();
134  char *endptr[1];
135 
136  for (size_t i = 0; i < len; i++) {
137  uint16_t x = strtol(p, endptr, 16);
138  if (x == 0 && i >= NUMBERS_IN_PREAMBLE) {
139  // Alignment error?, bail immediately (often right result).
140  break;
141  }
142  data.push_back(x); // If input is conforming, there can be no overflow!
143  p = *endptr;
144  }
145 
146  return data;
147 }
148 
149 void ProntoProtocol::send_pronto_(RemoteTransmitData *dst, const std::string &str) {
150  std::vector<uint16_t> data = encode_pronto(str);
151  send_pronto_(dst, data);
152 }
153 
154 void ProntoProtocol::encode(RemoteTransmitData *dst, const ProntoData &data) { send_pronto_(dst, data.data); }
155 
156 uint16_t ProntoProtocol::effective_frequency_(uint16_t frequency) {
157  return frequency > 0 ? frequency : FALLBACK_FREQUENCY;
158 }
159 
160 uint16_t ProntoProtocol::to_timebase_(uint16_t frequency) {
161  return MICROSECONDS_IN_SECONDS / effective_frequency_(frequency);
162 }
163 
164 uint16_t ProntoProtocol::to_frequency_code_(uint16_t frequency) {
165  return REFERENCE_FREQUENCY / effective_frequency_(frequency);
166 }
167 
168 std::string ProntoProtocol::dump_digit_(uint8_t x) {
169  return std::string(1, (char) (x <= 9 ? ('0' + x) : ('A' + (x - 10))));
170 }
171 
172 std::string ProntoProtocol::dump_number_(uint16_t number, bool end /* = false */) {
173  std::string num;
174 
175  for (uint8_t i = 0; i < DIGITS_IN_PRONTO_NUMBER; ++i) {
176  uint8_t shifts = BITS_IN_HEXADECIMAL * (DIGITS_IN_PRONTO_NUMBER - 1 - i);
177  num += dump_digit_((number >> shifts) & HEX_MASK);
178  }
179 
180  if (!end)
181  num += ' ';
182 
183  return num;
184 }
185 
186 std::string ProntoProtocol::dump_duration_(uint32_t duration, uint16_t timebase, bool end /* = false */) {
187  return dump_number_((duration + timebase / 2) / timebase, end);
188 }
189 
190 std::string ProntoProtocol::compensate_and_dump_sequence_(const RawTimings &data, uint16_t timebase) {
191  std::string out;
192 
193  for (int32_t t_length : data) {
194  uint32_t t_duration;
195  if (t_length > 0) {
196  // Mark
197  t_duration = t_length - MARK_EXCESS_MICROS;
198  } else {
199  t_duration = -t_length + MARK_EXCESS_MICROS;
200  }
201  out += dump_duration_(t_duration, timebase);
202  }
203 
204  return out;
205 }
206 
208  ProntoData out;
209 
210  uint16_t frequency = 38000U;
211  auto &data = src.get_raw_data();
212  std::string prontodata;
213 
214  prontodata += dump_number_(frequency > 0 ? LEARNED_TOKEN : LEARNED_NON_MODULATED_TOKEN);
215  prontodata += dump_number_(to_frequency_code_(frequency));
216  prontodata += dump_number_((data.size() + 1) / 2);
217  prontodata += dump_number_(0);
218  uint16_t timebase = to_timebase_(frequency);
219  prontodata += compensate_and_dump_sequence_(data, timebase);
220 
221  out.data = prontodata;
222  out.delta = -1;
223 
224  return out;
225 }
226 
227 void ProntoProtocol::dump(const ProntoData &data) {
228  std::string rest;
229 
230  rest = data.data;
231  ESP_LOGI(TAG, "Received Pronto: data=");
232  while (true) {
233  ESP_LOGI(TAG, "%s", rest.substr(0, 230).c_str());
234  if (rest.size() > 230) {
235  rest = rest.substr(230);
236  } else {
237  break;
238  }
239  }
240 }
241 
242 } // namespace remote_base
243 } // namespace esphome
const RawTimings & get_raw_data() const
Definition: remote_base.h:53
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:34
uint16_t x
Definition: tt21100.cpp:17
void item(uint32_t mark, uint32_t space)
Definition: remote_base.h:29
bool operator==(const ProntoData &rhs) const
std::vector< int32_t > RawTimings
Definition: remote_base.h:23
std::vector< uint16_t > encode_pronto(const std::string &str)
uint16_le_t frequency
Definition: bl0942.h:72
optional< ProntoData > decode(RemoteReceiveData src) override
std::string size_t len
Definition: helpers.h:293
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t end[39]
Definition: sun_gtil2.cpp:31
void encode(RemoteTransmitData *dst, const ProntoData &data) override
void dump(const ProntoData &data) override