ESPHome 2026.1.4
Loading...
Searching...
No Matches
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
38namespace esphome {
39namespace remote_base {
40
41static const char *const TAG = "remote.pronto";
42
43bool 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
62static const uint16_t MICROSECONDS_T_MAX = 0xFFFFU;
63static const uint16_t LEARNED_TOKEN = 0x0000U;
64static const uint16_t LEARNED_NON_MODULATED_TOKEN = 0x0100U;
65static const uint16_t BITS_IN_HEXADECIMAL = 4U;
66static const uint16_t DIGITS_IN_PRONTO_NUMBER = 4U;
67static const uint16_t NUMBERS_IN_PREAMBLE = 4U;
68static const uint16_t HEX_MASK = 0xFU;
69static const uint32_t REFERENCE_FREQUENCY = 4145146UL;
70static const uint16_t FALLBACK_FREQUENCY = 64767U; // To use with frequency = 0;
71static const uint32_t MICROSECONDS_IN_SECONDS = 1000000UL;
72static const uint16_t PRONTO_DEFAULT_GAP = 45000;
73static const uint16_t MARK_EXCESS_MICROS = 20;
74static constexpr size_t PRONTO_LOG_CHUNK_SIZE = 230;
75
76static uint16_t to_frequency_k_hz(uint16_t code) {
77 if (code == 0)
78 return 0;
79
80 return ((REFERENCE_FREQUENCY / code) + 500) / 1000;
81}
82
83/*
84 * Parse the string given as Pronto Hex, and send it a number of times given as argument.
85 */
86void ProntoProtocol::send_pronto_(RemoteTransmitData *dst, const std::vector<uint16_t> &data) {
87 if (data.size() < 4)
88 return;
89
90 uint16_t timebase = (MICROSECONDS_IN_SECONDS * data[1] + REFERENCE_FREQUENCY / 2) / REFERENCE_FREQUENCY;
91 uint16_t khz;
92 switch (data[0]) {
93 case LEARNED_TOKEN: // normal, "learned"
94 khz = to_frequency_k_hz(data[1]);
95 break;
96 case LEARNED_NON_MODULATED_TOKEN: // non-demodulated, "learned"
97 khz = 0U;
98 break;
99 default:
100 return; // There are other types, but they are not handled yet.
101 }
102 ESP_LOGD(TAG, "Send Pronto: frequency=%dkHz", khz);
103 dst->set_carrier_frequency(khz * 1000);
104
105 uint16_t intros = 2 * data[2];
106 uint16_t repeats = 2 * data[3];
107 ESP_LOGD(TAG,
108 "Send Pronto: intros=%d\n"
109 "Send Pronto: repeats=%d",
110 intros, repeats);
111 if (NUMBERS_IN_PREAMBLE + intros + repeats != data.size()) { // inconsistent sizes
112 ESP_LOGE(TAG, "Inconsistent data, not sending");
113 return;
114 }
115
116 /*
117 * Generate a new microseconds timing array for sendRaw.
118 * If recorded by IRremote, intro contains the whole IR data and repeat is empty
119 */
120 dst->reserve(intros + repeats);
121
122 for (uint16_t i = 0; i < intros + repeats; i += 2) {
123 uint32_t duration0 = ((uint32_t) data[i + 0 + NUMBERS_IN_PREAMBLE]) * timebase;
124 duration0 = duration0 < MICROSECONDS_T_MAX ? duration0 : MICROSECONDS_T_MAX;
125
126 uint32_t duration1 = ((uint32_t) data[i + 1 + NUMBERS_IN_PREAMBLE]) * timebase;
127 duration1 = duration1 < MICROSECONDS_T_MAX ? duration1 : MICROSECONDS_T_MAX;
128
129 dst->item(duration0, duration1);
130 }
131}
132
133std::vector<uint16_t> encode_pronto(const std::string &str) {
134 size_t len = str.length() / (DIGITS_IN_PRONTO_NUMBER + 1) + 1;
135 std::vector<uint16_t> data;
136 const char *p = str.c_str();
137 char *endptr[1];
138
139 for (size_t i = 0; i < len; i++) {
140 uint16_t x = strtol(p, endptr, 16);
141 if (x == 0 && i >= NUMBERS_IN_PREAMBLE) {
142 // Alignment error?, bail immediately (often right result).
143 break;
144 }
145 data.push_back(x); // If input is conforming, there can be no overflow!
146 p = *endptr;
147 }
148
149 return data;
150}
151
152void ProntoProtocol::send_pronto_(RemoteTransmitData *dst, const std::string &str) {
153 std::vector<uint16_t> data = encode_pronto(str);
154 send_pronto_(dst, data);
155}
156
157void ProntoProtocol::encode(RemoteTransmitData *dst, const ProntoData &data) { send_pronto_(dst, data.data); }
158
159uint16_t ProntoProtocol::effective_frequency_(uint16_t frequency) {
160 return frequency > 0 ? frequency : FALLBACK_FREQUENCY;
161}
162
163uint16_t ProntoProtocol::to_timebase_(uint16_t frequency) {
164 return MICROSECONDS_IN_SECONDS / effective_frequency_(frequency);
165}
166
167uint16_t ProntoProtocol::to_frequency_code_(uint16_t frequency) {
168 return REFERENCE_FREQUENCY / effective_frequency_(frequency);
169}
170
171std::string ProntoProtocol::dump_digit_(uint8_t x) {
172 return std::string(1, (char) (x <= 9 ? ('0' + x) : ('A' + (x - 10))));
173}
174
175std::string ProntoProtocol::dump_number_(uint16_t number, bool end /* = false */) {
176 std::string num;
177
178 for (uint8_t i = 0; i < DIGITS_IN_PRONTO_NUMBER; ++i) {
179 uint8_t shifts = BITS_IN_HEXADECIMAL * (DIGITS_IN_PRONTO_NUMBER - 1 - i);
180 num += dump_digit_((number >> shifts) & HEX_MASK);
181 }
182
183 if (!end)
184 num += ' ';
185
186 return num;
187}
188
189std::string ProntoProtocol::dump_duration_(uint32_t duration, uint16_t timebase, bool end /* = false */) {
190 return dump_number_((duration + timebase / 2) / timebase, end);
191}
192
193std::string ProntoProtocol::compensate_and_dump_sequence_(const RawTimings &data, uint16_t timebase) {
194 std::string out;
195
196 for (int32_t t_length : data) {
197 uint32_t t_duration;
198 if (t_length > 0) {
199 // Mark
200 t_duration = t_length - MARK_EXCESS_MICROS;
201 } else {
202 t_duration = -t_length + MARK_EXCESS_MICROS;
203 }
204 out += dump_duration_(t_duration, timebase);
205 }
206
207 return out;
208}
209
211 ProntoData out;
212
213 uint16_t frequency = 38000U;
214 auto &data = src.get_raw_data();
215 std::string prontodata;
216
217 prontodata += dump_number_(frequency > 0 ? LEARNED_TOKEN : LEARNED_NON_MODULATED_TOKEN);
218 prontodata += dump_number_(to_frequency_code_(frequency));
219 prontodata += dump_number_((data.size() + 1) / 2);
220 prontodata += dump_number_(0);
221 uint16_t timebase = to_timebase_(frequency);
222 prontodata += compensate_and_dump_sequence_(data, timebase);
223
224 out.data = prontodata;
225 out.delta = -1;
226
227 return out;
228}
229
231 ESP_LOGI(TAG, "Received Pronto: data=");
232
233 const char *ptr = data.data.c_str();
234 size_t remaining = data.data.size();
235
236 // Log in chunks, always logging at least once (even for empty string)
237 do {
238 size_t chunk_size = remaining < PRONTO_LOG_CHUNK_SIZE ? remaining : PRONTO_LOG_CHUNK_SIZE;
239 ESP_LOGI(TAG, "%.*s", (int) chunk_size, ptr);
240 ptr += chunk_size;
241 remaining -= chunk_size;
242 } while (remaining > 0);
243}
244
245} // namespace remote_base
246} // namespace esphome
uint16_le_t frequency
Definition bl0942.h:6
void encode(RemoteTransmitData *dst, const ProntoData &data) override
optional< ProntoData > decode(RemoteReceiveData src) override
void dump(const ProntoData &data) override
const RawTimings & get_raw_data() const
Definition remote_base.h:54
uint8_t duration
Definition msa3xx.h:0
std::vector< uint16_t > encode_pronto(const std::string &str)
std::vector< int32_t > RawTimings
Definition remote_base.h:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:595
bool operator==(const ProntoData &rhs) const
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t x
Definition tt21100.cpp:5