ESPHome 2025.12.0-dev
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, "Send Pronto: intros=%d", intros);
108 ESP_LOGD(TAG, "Send Pronto: repeats=%d", repeats);
109 if (NUMBERS_IN_PREAMBLE + intros + repeats != data.size()) { // inconsistent sizes
110 ESP_LOGE(TAG, "Inconsistent data, not sending");
111 return;
112 }
113
114 /*
115 * Generate a new microseconds timing array for sendRaw.
116 * If recorded by IRremote, intro contains the whole IR data and repeat is empty
117 */
118 dst->reserve(intros + repeats);
119
120 for (uint16_t i = 0; i < intros + repeats; i += 2) {
121 uint32_t duration0 = ((uint32_t) data[i + 0 + NUMBERS_IN_PREAMBLE]) * timebase;
122 duration0 = duration0 < MICROSECONDS_T_MAX ? duration0 : MICROSECONDS_T_MAX;
123
124 uint32_t duration1 = ((uint32_t) data[i + 1 + NUMBERS_IN_PREAMBLE]) * timebase;
125 duration1 = duration1 < MICROSECONDS_T_MAX ? duration1 : MICROSECONDS_T_MAX;
126
127 dst->item(duration0, duration1);
128 }
129}
130
131std::vector<uint16_t> encode_pronto(const std::string &str) {
132 size_t len = str.length() / (DIGITS_IN_PRONTO_NUMBER + 1) + 1;
133 std::vector<uint16_t> data;
134 const char *p = str.c_str();
135 char *endptr[1];
136
137 for (size_t i = 0; i < len; i++) {
138 uint16_t x = strtol(p, endptr, 16);
139 if (x == 0 && i >= NUMBERS_IN_PREAMBLE) {
140 // Alignment error?, bail immediately (often right result).
141 break;
142 }
143 data.push_back(x); // If input is conforming, there can be no overflow!
144 p = *endptr;
145 }
146
147 return data;
148}
149
150void ProntoProtocol::send_pronto_(RemoteTransmitData *dst, const std::string &str) {
151 std::vector<uint16_t> data = encode_pronto(str);
152 send_pronto_(dst, data);
153}
154
155void ProntoProtocol::encode(RemoteTransmitData *dst, const ProntoData &data) { send_pronto_(dst, data.data); }
156
157uint16_t ProntoProtocol::effective_frequency_(uint16_t frequency) {
158 return frequency > 0 ? frequency : FALLBACK_FREQUENCY;
159}
160
161uint16_t ProntoProtocol::to_timebase_(uint16_t frequency) {
162 return MICROSECONDS_IN_SECONDS / effective_frequency_(frequency);
163}
164
165uint16_t ProntoProtocol::to_frequency_code_(uint16_t frequency) {
166 return REFERENCE_FREQUENCY / effective_frequency_(frequency);
167}
168
169std::string ProntoProtocol::dump_digit_(uint8_t x) {
170 return std::string(1, (char) (x <= 9 ? ('0' + x) : ('A' + (x - 10))));
171}
172
173std::string ProntoProtocol::dump_number_(uint16_t number, bool end /* = false */) {
174 std::string num;
175
176 for (uint8_t i = 0; i < DIGITS_IN_PRONTO_NUMBER; ++i) {
177 uint8_t shifts = BITS_IN_HEXADECIMAL * (DIGITS_IN_PRONTO_NUMBER - 1 - i);
178 num += dump_digit_((number >> shifts) & HEX_MASK);
179 }
180
181 if (!end)
182 num += ' ';
183
184 return num;
185}
186
187std::string ProntoProtocol::dump_duration_(uint32_t duration, uint16_t timebase, bool end /* = false */) {
188 return dump_number_((duration + timebase / 2) / timebase, end);
189}
190
191std::string ProntoProtocol::compensate_and_dump_sequence_(const RawTimings &data, uint16_t timebase) {
192 std::string out;
193
194 for (int32_t t_length : data) {
195 uint32_t t_duration;
196 if (t_length > 0) {
197 // Mark
198 t_duration = t_length - MARK_EXCESS_MICROS;
199 } else {
200 t_duration = -t_length + MARK_EXCESS_MICROS;
201 }
202 out += dump_duration_(t_duration, timebase);
203 }
204
205 return out;
206}
207
209 ProntoData out;
210
211 uint16_t frequency = 38000U;
212 auto &data = src.get_raw_data();
213 std::string prontodata;
214
215 prontodata += dump_number_(frequency > 0 ? LEARNED_TOKEN : LEARNED_NON_MODULATED_TOKEN);
216 prontodata += dump_number_(to_frequency_code_(frequency));
217 prontodata += dump_number_((data.size() + 1) / 2);
218 prontodata += dump_number_(0);
219 uint16_t timebase = to_timebase_(frequency);
220 prontodata += compensate_and_dump_sequence_(data, timebase);
221
222 out.data = prontodata;
223 out.delta = -1;
224
225 return out;
226}
227
229 ESP_LOGI(TAG, "Received Pronto: data=");
230
231 const char *ptr = data.data.c_str();
232 size_t remaining = data.data.size();
233
234 // Log in chunks, always logging at least once (even for empty string)
235 do {
236 size_t chunk_size = remaining < PRONTO_LOG_CHUNK_SIZE ? remaining : PRONTO_LOG_CHUNK_SIZE;
237 ESP_LOGI(TAG, "%.*s", (int) chunk_size, ptr);
238 ptr += chunk_size;
239 remaining -= chunk_size;
240 } while (remaining > 0);
241}
242
243} // namespace remote_base
244} // 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:49
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:500
bool operator==(const ProntoData &rhs) const
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t x
Definition tt21100.cpp:5