39 namespace remote_base {
41 static const char *
const TAG =
"remote.pronto";
47 uint32_t total_diff = 0;
49 for (std::vector<uint16_t>::size_type i = 0; i < data1.size() - 1; ++i) {
50 int diff = data2[i] - data1[i];
52 if (rhs.
delta == -1 && diff > 9)
58 return total_diff <= (rhs.
delta == -1 ? data1.size() * 3 : rhs.
delta);
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;
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;
75 static uint16_t to_frequency_k_hz(uint16_t code) {
79 return ((REFERENCE_FREQUENCY / code) + 500) / 1000;
89 uint16_t timebase = (MICROSECONDS_IN_SECONDS * data[1] + REFERENCE_FREQUENCY / 2) / REFERENCE_FREQUENCY;
93 khz = to_frequency_k_hz(data[1]);
95 case LEARNED_NON_MODULATED_TOKEN:
101 ESP_LOGD(TAG,
"Send Pronto: frequency=%dkHz", khz);
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()) {
109 ESP_LOGE(TAG,
"Inconsistent data, not sending");
117 dst->
reserve(intros + repeats);
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;
123 uint32_t duration1 = ((uint32_t) data[i + 1 + NUMBERS_IN_PREAMBLE]) * timebase;
124 duration1 = duration1 < MICROSECONDS_T_MAX ? duration1 : MICROSECONDS_T_MAX;
126 dst->
item(duration0, duration1);
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();
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) {
151 send_pronto_(dst, data);
156 uint16_t ProntoProtocol::effective_frequency_(uint16_t
frequency) {
157 return frequency > 0 ?
frequency : FALLBACK_FREQUENCY;
160 uint16_t ProntoProtocol::to_timebase_(uint16_t frequency) {
161 return MICROSECONDS_IN_SECONDS / effective_frequency_(frequency);
164 uint16_t ProntoProtocol::to_frequency_code_(uint16_t frequency) {
165 return REFERENCE_FREQUENCY / effective_frequency_(frequency);
168 std::string ProntoProtocol::dump_digit_(uint8_t
x) {
169 return std::string(1, (
char) (x <= 9 ? (
'0' + x) : (
'A' + (x - 10))));
172 std::string ProntoProtocol::dump_number_(uint16_t number,
bool end ) {
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);
186 std::string ProntoProtocol::dump_duration_(uint32_t duration, uint16_t timebase,
bool end ) {
187 return dump_number_((duration + timebase / 2) / timebase, end);
190 std::string ProntoProtocol::compensate_and_dump_sequence_(
const RawTimings &data, uint16_t timebase) {
193 for (int32_t t_length : data) {
197 t_duration = t_length - MARK_EXCESS_MICROS;
199 t_duration = -t_length + MARK_EXCESS_MICROS;
201 out += dump_duration_(t_duration, timebase);
210 uint16_t frequency = 38000U;
212 std::string prontodata;
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);
221 out.
data = prontodata;
231 ESP_LOGI(TAG,
"Received Pronto: data=");
233 ESP_LOGI(TAG,
"%s", rest.substr(0, 230).c_str());
234 if (rest.size() > 230) {
235 rest = rest.substr(230);
const RawTimings & get_raw_data() const
void set_carrier_frequency(uint32_t carrier_frequency)
void item(uint32_t mark, uint32_t space)
bool operator==(const ProntoData &rhs) const
std::vector< int32_t > RawTimings
std::vector< uint16_t > encode_pronto(const std::string &str)
optional< ProntoData > decode(RemoteReceiveData src) override
Implementation of SPI Controller mode.
void reserve(uint32_t len)
void encode(RemoteTransmitData *dst, const ProntoData &data) override
void dump(const ProntoData &data) override