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