-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRrecv.cpp
1819 lines (1741 loc) · 75.5 KB
/
IRrecv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2009 Ken Shirriff
// Copyright 2015 Mark Szabo
// Copyright 2015 Sebastien Warin
// Copyright 2017, 2019 David Conran
#include "IRrecv.h"
#include <stddef.h>
#ifndef UNIT_TEST
#if defined(ESP8266)
extern "C" {
#include <gpio.h>
#include <user_interface.h>
}
#endif // ESP8266
#include <Arduino.h>
#endif
#include <algorithm>
#ifdef UNIT_TEST
#include <cassert>
#endif // UNIT_TEST
#include "IRremoteESP8266.h"
#include "IRutils.h"
#ifdef UNIT_TEST
#undef ICACHE_RAM_ATTR
#define ICACHE_RAM_ATTR
#endif
#ifndef USE_IRAM_ATTR
#if defined(ESP8266)
#define USE_IRAM_ATTR ICACHE_RAM_ATTR
#endif // ESP8266
#if defined(ESP32)
#define USE_IRAM_ATTR IRAM_ATTR
#endif // ESP32
#endif // USE_IRAM_ATTR
#define ONCE 0
// Updated by David Conran (https://github.com/crankyoldgit) for receiving IR
// code on ESP32
// Updated by Sebastien Warin (http://sebastien.warin.fr) for receiving IR code
// on ESP8266
// Updated by markszabo (https://github.com/crankyoldgit/IRremoteESP8266) for
// sending IR code on ESP8266
// Globals
#ifndef UNIT_TEST
#if defined(ESP8266)
namespace _IRrecv {
static ETSTimer timer;
} // namespace _IRrecv
#endif // ESP8266
#if defined(ESP32)
// Required structs/types from:
// https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L28-L58
// These are needed to be able to directly manipulate the timer registers from
// inside an ISR. This is very very ugly.
// Ref: https://github.com/crankyoldgit/IRremoteESP8266/issues/1350
// Note: This will need to be updated if it ever changes.
//
// Start of Horrible Hack!
typedef struct {
union {
struct {
uint32_t reserved0: 10;
uint32_t alarm_en: 1;
/*When set alarm is enabled*/
uint32_t level_int_en: 1;
/*When set level type interrupt will be generated during alarm*/
uint32_t edge_int_en: 1;
/*When set edge type interrupt will be generated during alarm*/
uint32_t divider: 16;
/*Timer clock (T0/1_clk) pre-scale value.*/
uint32_t autoreload: 1;
/*When set timer 0/1 auto-reload at alarming is enabled*/
uint32_t increase: 1;
/*When set timer 0/1 time-base counter increment.
When cleared timer 0 time-base counter decrement.*/
uint32_t enable: 1;
/*When set timer 0/1 time-base counter is enabled*/
};
uint32_t val;
} config;
uint32_t cnt_low;
/*Register to store timer 0/1 time-base counter current value lower 32
bits.*/
uint32_t cnt_high;
/*Register to store timer 0 time-base counter current value higher 32
bits.*/
uint32_t update;
/*Write any value will trigger a timer 0 time-base counter value update
(timer 0 current value will be stored in registers above)*/
uint32_t alarm_low;
/*Timer 0 time-base counter value lower 32 bits that will trigger the
alarm*/
uint32_t alarm_high;
/*Timer 0 time-base counter value higher 32 bits that will trigger the
alarm*/
uint32_t load_low;
/*Lower 32 bits of the value that will load into timer 0 time-base counter*/
uint32_t load_high;
/*higher 32 bits of the value that will load into timer 0 time-base
counter*/
uint32_t reload;
/*Write any value will trigger timer 0 time-base counter reload*/
} hw_timer_reg_t;
typedef struct hw_timer_s {
hw_timer_reg_t * dev;
uint8_t num;
uint8_t group;
uint8_t timer;
portMUX_TYPE lock;
} hw_timer_t;
// End of Horrible Hack.
namespace _IRrecv {
static hw_timer_t * timer = NULL;
} // namespace _IRrecv
#endif // ESP32
using _IRrecv::timer;
#endif // UNIT_TEST
namespace _IRrecv { // Namespace extension
#if defined(ESP32)
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
#endif // ESP32
volatile irparams_t params;
irparams_t *params_save; // A copy of the interrupt state while decoding.
} // namespace _IRrecv
#if defined(ESP32)
using _IRrecv::mux;
#endif // ESP32
using _IRrecv::params;
using _IRrecv::params_save;
#ifndef UNIT_TEST
#if defined(ESP8266)
/// Interrupt handler for when the timer runs out.
/// It signals to the library that capturing of IR data has stopped.
/// @param[in] arg Unused. (ESP8266 Only)
static void USE_IRAM_ATTR read_timeout(void *arg __attribute__((unused))) {
os_intr_lock();
#endif // ESP8266
/// @cond IGNORE
#if defined(ESP32)
/// Interrupt handler for when the timer runs out.
/// It signals to the library that capturing of IR data has stopped.
/// @note ESP32 version
static void USE_IRAM_ATTR read_timeout(void) {
/// @endcond
portENTER_CRITICAL(&mux);
#endif // ESP32
if (params.rawlen) params.rcvstate = kStopState;
#if defined(ESP8266)
os_intr_unlock();
#endif // ESP8266
#if defined(ESP32)
portEXIT_CRITICAL(&mux);
#endif // ESP32
}
/// Interrupt handler for changes on the GPIO pin handling incoming IR messages.
static void USE_IRAM_ATTR gpio_intr() {
uint32_t now = micros();
static uint32_t start = 0;
#if defined(ESP8266)
uint32_t gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
os_timer_disarm(&timer);
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status);
#endif // ESP8266
// Grab a local copy of rawlen to reduce instructions used in IRAM.
// This is an ugly premature optimisation code-wise, but we do everything we
// can to save IRAM.
// It seems referencing the value via the structure uses more instructions.
// Less instructions means faster and less IRAM used.
// N.B. It saves about 13 bytes of IRAM.
uint16_t rawlen = params.rawlen;
if (rawlen >= params.bufsize) {
params.overflow = true;
params.rcvstate = kStopState;
}
if (params.rcvstate == kStopState) return;
if (params.rcvstate == kIdleState) {
params.rcvstate = kMarkState;
params.rawbuf[rawlen] = 1;
} else {
if (now < start)
params.rawbuf[rawlen] = (UINT32_MAX - start + now) / kRawTick;
else
params.rawbuf[rawlen] = (now - start) / kRawTick;
}
params.rawlen++;
start = now;
#if defined(ESP8266)
os_timer_arm(&timer, params.timeout, ONCE);
#endif // ESP8266
#if defined(ESP32)
// Reset the timeout.
//
// The following three lines of code are the equiv of:
// `timerWrite(timer, 0);`
// We can't call that routine safely from inside an ISR as that procedure
// is not stored in IRAM. Hence, we do it manually so that it's covered by
// USE_IRAM_ATTR in this ISR.
// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1350
// @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L106-L110
timer->dev->load_high = (uint32_t) 0;
timer->dev->load_low = (uint32_t) 0;
timer->dev->reload = 1;
// The next line is the same, but instead replaces:
// `timerAlarmEnable(timer);`
// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1350
// @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178
timer->dev->config.alarm_en = 1;
#endif // ESP32
}
#endif // UNIT_TEST
// Start of IRrecv class -------------------
/// Class constructor
/// Args:
/// @param[in] recvpin The GPIO pin the IR receiver module's data pin is
/// connected to.
/// @param[in] bufsize Nr. of entries to have in the capture buffer.
/// (Default: kRawBuf)
/// @param[in] timeout Nr. of milli-Seconds of no signal before we stop
/// capturing data. (Default: kTimeoutMs)
/// @param[in] save_buffer Use a second (save) buffer to decode from.
/// (Default: false)
/// @param[in] timer_num Nr. of the ESP32 timer to use (0 to 3) (ESP32 Only)
#if defined(ESP32)
IRrecv::IRrecv(const uint16_t recvpin, const uint16_t bufsize,
const uint8_t timeout, const bool save_buffer,
const uint8_t timer_num) {
// There are only 4 timers. 0 to 3.
_timer_num = std::min(timer_num, (uint8_t)3);
#else // ESP32
/// @cond IGNORE
/// Class constructor
/// Args:
/// @param[in] recvpin The GPIO pin the IR receiver module's data pin is
/// connected to.
/// @param[in] bufsize Nr. of entries to have in the capture buffer.
/// (Default: kRawBuf)
/// @param[in] timeout Nr. of milli-Seconds of no signal before we stop
/// capturing data. (Default: kTimeoutMs)
/// @param[in] save_buffer Use a second (save) buffer to decode from.
/// (Default: false)
IRrecv::IRrecv(const uint16_t recvpin, const uint16_t bufsize,
const uint8_t timeout, const bool save_buffer) {
/// @endcond
#endif // ESP32
params.recvpin = recvpin;
params.bufsize = bufsize;
// Ensure we are going to be able to store all possible values in the
// capture buffer.
params.timeout = std::min(timeout, (uint8_t)kMaxTimeoutMs);
params.rawbuf = new uint16_t[bufsize];
if (params.rawbuf == NULL) {
DPRINTLN(
"Could not allocate memory for the primary IR buffer.\n"
"Try a smaller size for CAPTURE_BUFFER_SIZE.\nRebooting!");
#ifndef UNIT_TEST
ESP.restart(); // Mem alloc failure. Reboot.
#endif
}
// If we have been asked to use a save buffer (for decoding), then create one.
if (save_buffer) {
params_save = new irparams_t;
params_save->rawbuf = new uint16_t[bufsize];
// Check we allocated the memory successfully.
if (params_save->rawbuf == NULL) {
DPRINTLN(
"Could not allocate memory for the second IR buffer.\n"
"Try a smaller size for CAPTURE_BUFFER_SIZE.\nRebooting!");
#ifndef UNIT_TEST
ESP.restart(); // Mem alloc failure. Reboot.
#endif
}
} else {
params_save = NULL;
}
#if DECODE_HASH
_unknown_threshold = kUnknownThreshold;
#endif // DECODE_HASH
_tolerance = kTolerance;
}
/// Class destructor
/// Cleans up after the object is no longer needed.
/// e.g. Frees up all memory used by the various buffers, and disables any
/// timers or interrupts used.
IRrecv::~IRrecv(void) {
disableIRIn();
#if defined(ESP32)
if (timer != NULL) timerEnd(timer); // Cleanup the ESP32 timeout timer.
#endif // ESP32
delete[] params.rawbuf;
if (params_save != NULL) {
delete[] params_save->rawbuf;
delete params_save;
}
}
/// Set up and (re)start the IR capture mechanism.
/// @param[in] pullup A flag indicating should the GPIO use the internal pullup
/// resistor. (Default: `false`. i.e. No.)
void IRrecv::enableIRIn(const bool pullup) {
// ESP32's seem to require explicitly setting the GPIO to INPUT etc.
// This wasn't required on the ESP8266s, but it shouldn't hurt to make sure.
if (pullup) {
#ifndef UNIT_TEST
pinMode(params.recvpin, INPUT_PULLUP);
} else {
pinMode(params.recvpin, INPUT);
#endif // UNIT_TEST
}
#if defined(ESP32)
// Initialise the ESP32 timer.
// 80MHz / 80 = 1 uSec granularity.
timer = timerBegin(_timer_num, 80, true);
// Set the timer so it only fires once, and set it's trigger in uSeconds.
timerAlarmWrite(timer, MS_TO_USEC(params.timeout), ONCE);
// Note: Interrupt needs to be attached before it can be enabled or disabled.
timerAttachInterrupt(timer, &read_timeout, true);
#endif // ESP32
// Initialise state machine variables
resume();
#ifndef UNIT_TEST
#if defined(ESP8266)
// Initialise ESP8266 timer.
os_timer_disarm(&timer);
os_timer_setfn(&timer, reinterpret_cast<os_timer_func_t *>(read_timeout),
NULL);
#endif // ESP8266
// Attach Interrupt
attachInterrupt(params.recvpin, gpio_intr, CHANGE);
#endif // UNIT_TEST
}
/// Stop collection of any received IR data.
/// Disable any timers and interrupts.
void IRrecv::disableIRIn(void) {
#ifndef UNIT_TEST
#if defined(ESP8266)
os_timer_disarm(&timer);
#endif // ESP8266
#if defined(ESP32)
timerAlarmDisable(timer);
#endif // ESP32
detachInterrupt(params.recvpin);
#endif // UNIT_TEST
}
/// Resume collection of received IR data.
/// @note This is required if `decode()` is successful and `save_buffer` was
/// not set when the class was instanciated.
/// @see IRrecv class constructor
void IRrecv::resume(void) {
params.rcvstate = kIdleState;
params.rawlen = 0;
params.overflow = false;
#if defined(ESP32)
timerAlarmDisable(timer);
#endif // ESP32
}
/// Make a copy of the interrupt state & buffer data.
/// Needed because irparams is marked as volatile, thus memcpy() isn't allowed.
/// Only call this when you know the interrupt handlers won't modify anything.
/// i.e. In kStopState.
/// @param[in] src Pointer to an irparams_t structure to copy from.
/// @param[out] dst Pointer to an irparams_t structure to copy to.
void IRrecv::copyIrParams(volatile irparams_t *src, irparams_t *dst) {
// Typecast src and dst addresses to (char *)
char *csrc = (char *)src; // NOLINT(readability/casting)
char *cdst = (char *)dst; // NOLINT(readability/casting)
// Save the pointer to the destination's rawbuf so we don't lose it as
// the for-loop/copy after this will overwrite it with src's rawbuf pointer.
// This isn't immediately obvious due to typecasting/different variable names.
uint16_t *dst_rawbuf_ptr;
dst_rawbuf_ptr = dst->rawbuf;
// Copy contents of src[] to dst[]
for (uint16_t i = 0; i < sizeof(irparams_t); i++) cdst[i] = csrc[i];
// Restore the buffer pointer
dst->rawbuf = dst_rawbuf_ptr;
// Copy the rawbuf
for (uint16_t i = 0; i < dst->bufsize; i++) dst->rawbuf[i] = src->rawbuf[i];
}
/// Obtain the maximum number of entries possible in the capture buffer.
/// i.e. It's size.
/// @return The size of the buffer that is in use by the object.
uint16_t IRrecv::getBufSize(void) { return params.bufsize; }
#if DECODE_HASH
/// Set the minimum length we will consider for reporting UNKNOWN message types.
/// @param[in] length Min nr. of mark/space pulses required to be considered.
void IRrecv::setUnknownThreshold(const uint16_t length) {
_unknown_threshold = length;
}
#endif // DECODE_HASH
/// Set the base tolerance percentage for matching incoming IR messages.
/// @param[in] percent An integer percentage. (0-100)
void IRrecv::setTolerance(const uint8_t percent) {
_tolerance = std::min(percent, (uint8_t)100);
}
/// Get the base tolerance percentage for matching incoming IR messages.
/// @return A integer percentage.
uint8_t IRrecv::getTolerance(void) { return _tolerance; }
#if ENABLE_NOISE_FILTER_OPTION
/// Remove or merge pulses in the capture buffer that are too short.
/// @param[in,out] results Ptr to the decode_results we are going to filter.
/// @param[in] floor Only allow values in the buffer large than this.
/// (in microSeconds)
void IRrecv::crudeNoiseFilter(decode_results *results, const uint16_t floor) {
if (floor == 0) return; // Nothing to do.
const uint16_t kTickFloor = floor / kRawTick;
const uint16_t kBufSize = getBufSize();
uint16_t offset = kStartOffset;
while (offset < results->rawlen && offset + 2 < kBufSize) {
uint16_t curr = results->rawbuf[offset];
uint16_t next = results->rawbuf[offset + 1];
uint16_t addition = curr + next;
if (curr < kTickFloor) { // Is it too short?
// Shuffle the buffer down. i.e. Remove the mark & space pair.
// Note: `memcpy()` can't be used as rawbuf is `volatile`.
for (uint16_t i = offset + 2; i <= results->rawlen && i < kBufSize; i++)
results->rawbuf[i - 2] = results->rawbuf[i];
if (offset > 1) { // There is a previous pair we can add to.
// Merge this pair into into the previous space.
results->rawbuf[offset - 1] += addition;
}
results->rawlen -= 2; // Adjust the length.
} else {
offset++; // Move along.
}
}
}
#endif // ENABLE_NOISE_FILTER_OPTION
/// Decodes the received IR message.
/// If the interrupt state is saved, we will immediately resume waiting
/// for the next IR message to avoid missing messages.
/// @note There is a trade-off here. Saving the state means less time lost until
/// we can receiving the next message vs. using more RAM. Choose appropriately.
/// @param[out] results A PTR to where the decoded IR message will be stored.
/// @param[out] save A PTR to an irparams_t instance in which to save
/// the interrupt's memory/state. NULL means don't save it.
/// @param[in] max_skip Maximum Nr. of pulses at the begining of a capture we
/// can skip when attempting to find a protocol we can successfully decode.
/// This parameter can dramatically improve detection of protocols
/// when there is light IR interference just before an incoming IR
/// message, however, it comes at a steep performace price.
/// (Default is 0. No skipping.)
/// @warning Increasing the `max_skip` value will dramatically (linearly)
/// increase the cpu time & usage to decode protocols.
/// e.g. 0 -> 1 will be a 2x increase in cpu usage/time.
/// 0 -> 2 will be a 3x increase etc.
/// If you are going to do this, consider disabling protocol decoding for
/// protocols you are not expecting.
/// @param[in] noise_floor Pulses below this size (in usecs) will be removed or
/// merged prior to any decoding. This is to try to remove noise/poor
/// readings & slightly increase the chances of a successful decode but at the
/// cost of data fidelity & integrity.
/// (Defaults to 0 usecs. i.e. Don't filter; which is safe!)
/// @warning DANGER: **Here Be Dragons!**
/// If you set the `noise_floor` value too high, it **WILL** break decoding
/// of some protocols. You have been warned!
/// **Any** non-zero value has the potential to **cook** the captured raw data
/// i.e. The raw data is going to lie to you.
/// It may obscure hardware, circuit, & environment issues thus making it
/// impossible to support you accurately or confidently.
/// Values of <= 50 usecs will probably be safe.
/// 51 - 100 usecs **might** be okay.
/// 100 - 150 usecs is "Danger, Will Robinson!".
/// 150 - 200 usecs expect broken protocols.
/// At 200+ usecs, you **have** protocols you can't decode!!
/// @return A boolean indicating if an IR message is ready or not.
bool IRrecv::decode(decode_results *results, irparams_t *save,
uint8_t max_skip, uint16_t noise_floor) {
// Proceed only if an IR message been received.
#ifndef UNIT_TEST
if (params.rcvstate != kStopState) return false;
#endif
// Clear the entry we are currently pointing to when we got the timeout.
// i.e. Stopped collecting IR data.
// It's junk as we never wrote an entry to it and can only confuse decoding.
// This is done here rather than logically the best place in read_timeout()
// as it saves a few bytes of ICACHE_RAM as that routine is bound to an
// interrupt. decode() is not stored in ICACHE_RAM.
// Another better option would be to zero the entire irparams.rawbuf[] on
// resume() but that is a much more expensive operation compare to this.
params.rawbuf[params.rawlen] = 0;
bool resumed = false; // Flag indicating if we have resumed.
// If we were requested to use a save buffer previously, do so.
if (save == NULL) save = params_save;
if (save == NULL) {
// We haven't been asked to copy it so use the existing memory.
#ifndef UNIT_TEST
results->rawbuf = params.rawbuf;
results->rawlen = params.rawlen;
results->overflow = params.overflow;
#endif
} else {
copyIrParams(¶ms, save); // Duplicate the interrupt's memory.
resume(); // It's now safe to rearm. The IR message won't be overridden.
resumed = true;
// Point the results at the saved copy.
results->rawbuf = save->rawbuf;
results->rawlen = save->rawlen;
results->overflow = save->overflow;
}
// Reset any previously partially processed results.
results->decode_type = UNKNOWN;
results->bits = 0;
results->value = 0;
results->address = 0;
results->command = 0;
results->repeat = false;
#if ENABLE_NOISE_FILTER_OPTION
crudeNoiseFilter(results, noise_floor);
#endif // ENABLE_NOISE_FILTER_OPTION
// Keep looking for protocols until we've run out of entries to skip or we
// find a valid protocol message.
for (uint16_t offset = kStartOffset;
offset <= (max_skip * 2) + kStartOffset;
offset += 2) {
#if DECODE_AIWA_RC_T501
DPRINTLN("Attempting Aiwa RC T501 decode");
// Try decodeAiwaRCT501() before decodeSanyoLC7461() & decodeNEC()
// because the protocols are similar. This protocol is more specific than
// those ones, so should go before them.
if (decodeAiwaRCT501(results, offset)) return true;
#endif
#if DECODE_SANYO
DPRINTLN("Attempting Sanyo LC7461 decode");
// Try decodeSanyoLC7461() before decodeNEC() because the protocols are
// similar in timings & structure, but the Sanyo one is much longer than the
// NEC protocol (42 vs 32 bits) so this one should be tried first to try to
// reduce false detection as a NEC packet.
if (decodeSanyoLC7461(results, offset)) return true;
#endif
#if DECODE_CARRIER_AC
DPRINTLN("Attempting Carrier AC decode");
// Try decodeCarrierAC() before decodeNEC() because the protocols are
// similar in timings & structure, but the Carrier one is much longer than
// the NEC protocol (3x32 bits vs 1x32 bits) so this one should be tried
// first to try to reduce false detection as a NEC packet.
if (decodeCarrierAC(results, offset)) return true;
#endif
#if DECODE_PIONEER
DPRINTLN("Attempting Pioneer decode");
// Try decodePioneer() before decodeNEC() because the protocols are
// similar in timings & structure, but the Pioneer one is much longer than
// the NEC protocol (2x32 bits vs 1x32 bits) so this one should be tried
// first to try to reduce false detection as a NEC packet.
if (decodePioneer(results, offset)) return true;
#endif
#if DECODE_EPSON
DPRINTLN("Attempting Epson decode");
// Try decodeEpson() before decodeNEC() because the protocols are
// similar in timings & structure, but the Epson one is much longer than the
// NEC protocol (3x32 identical bits vs 1x32 bits) so this one should be tried
// first to try to reduce false detection as a NEC packet.
if (decodeEpson(results, offset)) return true;
#endif
#if DECODE_NEC
DPRINTLN("Attempting NEC decode");
if (decodeNEC(results, offset)) return true;
#endif
#if DECODE_MILESTAG2
DPRINTLN("Attempting MilesTag2 decode");
// Try decodeMilestag2() before decodeSony() because the protocols are
// similar in timings & structure, but the Miles one differs in nbits
// so this one should be tried first to try to reduce false detection
if (decodeMilestag2(results, offset, kMilesTag2MsgBits) ||
decodeMilestag2(results, offset, kMilesTag2ShotBits)) return true;
#endif
#if DECODE_SONY
DPRINTLN("Attempting Sony decode");
if (decodeSony(results, offset)) return true;
#endif
#if DECODE_MITSUBISHI
DPRINTLN("Attempting Mitsubishi decode");
if (decodeMitsubishi(results, offset)) return true;
#endif
#if DECODE_MITSUBISHI_AC
DPRINTLN("Attempting Mitsubishi AC decode");
if (decodeMitsubishiAC(results, offset)) return true;
#endif
#if DECODE_MITSUBISHI2
DPRINTLN("Attempting Mitsubishi2 decode");
if (decodeMitsubishi2(results, offset)) return true;
#endif
#if DECODE_RC5
DPRINTLN("Attempting RC5 decode");
if (decodeRC5(results, offset)) return true;
#endif
#if DECODE_RC6
DPRINTLN("Attempting RC6 decode");
if (decodeRC6(results, offset)) return true;
#endif
#if DECODE_RCMM
DPRINTLN("Attempting RC-MM decode");
if (decodeRCMM(results, offset)) return true;
#endif
#if DECODE_FUJITSU_AC
// Fujitsu A/C needs to precede Panasonic and Denon as it has a short
// message which looks exactly the same as a Panasonic/Denon message.
DPRINTLN("Attempting Fujitsu A/C decode");
if (decodeFujitsuAC(results, offset)) return true;
#endif
#if DECODE_DENON
// Denon needs to precede Panasonic as it is a special case of Panasonic.
DPRINTLN("Attempting Denon decode");
if (decodeDenon(results, offset, kDenon48Bits) ||
decodeDenon(results, offset, kDenonBits) ||
decodeDenon(results, offset, kDenonLegacyBits))
return true;
#endif
#if DECODE_PANASONIC
DPRINTLN("Attempting Panasonic decode");
if (decodePanasonic(results, offset)) return true;
#endif
#if DECODE_LG
DPRINTLN("Attempting LG (28-bit) decode");
if (decodeLG(results, offset, kLgBits, true)) return true;
DPRINTLN("Attempting LG (32-bit) decode");
// LG32 should be tried before Samsung
if (decodeLG(results, offset, kLg32Bits, true)) return true;
#endif
#if DECODE_GICABLE
// Note: Needs to happen before JVC decode, because it looks similar except
// with a required NEC-like repeat code.
DPRINTLN("Attempting GICable decode");
if (decodeGICable(results, offset)) return true;
#endif
#if DECODE_JVC
DPRINTLN("Attempting JVC decode");
if (decodeJVC(results, offset)) return true;
#endif
#if DECODE_SAMSUNG
DPRINTLN("Attempting SAMSUNG decode");
if (decodeSAMSUNG(results, offset)) return true;
#endif
#if DECODE_SAMSUNG36
DPRINTLN("Attempting Samsung36 decode");
if (decodeSamsung36(results, offset)) return true;
#endif
#if DECODE_WHYNTER
DPRINTLN("Attempting Whynter decode");
if (decodeWhynter(results, offset)) return true;
#endif
#if DECODE_DISH
DPRINTLN("Attempting DISH decode");
if (decodeDISH(results, offset)) return true;
#endif
#if DECODE_SHARP
DPRINTLN("Attempting Sharp decode");
if (decodeSharp(results, offset)) return true;
#endif
#if DECODE_COOLIX
DPRINTLN("Attempting Coolix decode");
if (decodeCOOLIX(results, offset)) return true;
#endif
#if DECODE_NIKAI
DPRINTLN("Attempting Nikai decode");
if (decodeNikai(results, offset)) return true;
#endif
#if DECODE_KELVINATOR
// Kelvinator based-devices use a similar code to Gree ones, to avoid false
// matches this needs to happen before decodeGree().
DPRINTLN("Attempting Kelvinator decode");
if (decodeKelvinator(results, offset)) return true;
#endif
#if DECODE_DAIKIN
DPRINTLN("Attempting Daikin decode");
if (decodeDaikin(results, offset)) return true;
#endif
#if DECODE_DAIKIN2
DPRINTLN("Attempting Daikin2 decode");
if (decodeDaikin2(results, offset)) return true;
#endif
#if DECODE_DAIKIN216
DPRINTLN("Attempting Daikin216 decode");
if (decodeDaikin216(results, offset)) return true;
#endif
#if DECODE_TOSHIBA_AC
DPRINTLN("Attempting Toshiba AC 72bit decode");
if (decodeToshibaAC(results, offset)) return true;
DPRINTLN("Attempting Toshiba AC 80bit decode");
if (decodeToshibaAC(results, offset, kToshibaACBitsLong)) return true;
DPRINTLN("Attempting Toshiba AC 56bit decode");
if (decodeToshibaAC(results, offset, kToshibaACBitsShort)) return true;
#endif
#if DECODE_MIDEA
DPRINTLN("Attempting Midea decode");
if (decodeMidea(results, offset)) return true;
#endif
#if DECODE_MAGIQUEST
DPRINTLN("Attempting Magiquest decode");
if (decodeMagiQuest(results, offset)) return true;
#endif
/* NOTE: Disabled due to poor quality.
#if DECODE_SANYO
// The Sanyo S866500B decoder is very poor quality & depricated.
// *IF* you are going to enable it, do it near last to avoid false positive
// matches.
DPRINTLN("Attempting Sanyo SA8650B decode");
if (decodeSanyo(results, offset))
return true;
#endif
*/
#if DECODE_NEC
// Some devices send NEC-like codes that don't follow the true NEC spec.
// This should detect those. e.g. Apple TV remote etc.
// This needs to be done after all other codes that use strict and some
// other protocols that are NEC-like as well, as turning off strict may
// cause this to match other valid protocols.
DPRINTLN("Attempting NEC (non-strict) decode");
if (decodeNEC(results, offset, kNECBits, false)) {
results->decode_type = NEC_LIKE;
return true;
}
#endif
#if DECODE_LASERTAG
DPRINTLN("Attempting Lasertag decode");
if (decodeLasertag(results, offset)) return true;
#endif
#if DECODE_GREE
// Gree based-devices use a similar code to Kelvinator ones, to avoid false
// matches this needs to happen after decodeKelvinator().
DPRINTLN("Attempting Gree decode");
if (decodeGree(results, offset)) return true;
#endif
#if DECODE_HAIER_AC
DPRINTLN("Attempting Haier AC decode");
if (decodeHaierAC(results, offset)) return true;
#endif
#if DECODE_HAIER_AC_YRW02
DPRINTLN("Attempting Haier AC YR-W02 decode");
if (decodeHaierACYRW02(results, offset)) return true;
#endif
#if DECODE_HITACHI_AC424
// HitachiAc424 should be checked before HitachiAC, HitachiAC2,
// & HitachiAC184
DPRINTLN("Attempting Hitachi AC 424 decode");
if (decodeHitachiAc424(results, offset, kHitachiAc424Bits)) return true;
#endif // DECODE_HITACHI_AC424
#if DECODE_MITSUBISHI136
// Needs to happen before HitachiAc3 decode.
DPRINTLN("Attempting Mitsubishi136 decode");
if (decodeMitsubishi136(results, offset)) return true;
#endif // DECODE_MITSUBISHI136
#if DECODE_HITACHI_AC3
// HitachiAc3 should be checked before HitachiAC & HitachiAC2
// Attempt normal before the short version.
DPRINTLN("Attempting Hitachi AC3 decode");
// Order these in decreasing bit size, as it is more optimal.
if (decodeHitachiAc3(results, offset, kHitachiAc3Bits) ||
decodeHitachiAc3(results, offset, kHitachiAc3Bits - 4 * 8) ||
decodeHitachiAc3(results, offset, kHitachiAc3Bits - 6 * 8) ||
decodeHitachiAc3(results, offset, kHitachiAc3MinBits + 2 * 8) ||
decodeHitachiAc3(results, offset, kHitachiAc3MinBits))
return true;
#endif // DECODE_HITACHI_AC3
#if DECODE_HITACHI_AC344
// HitachiAC344 should be checked before HitachiAC
DPRINTLN("Attempting Hitachi AC344 decode");
if (decodeHitachiAC(results, offset, kHitachiAc344Bits, true, false))
return true;
#endif // DECODE_HITACHI_AC344
#if DECODE_HITACHI_AC2
// HitachiAC2 should be checked before HitachiAC
DPRINTLN("Attempting Hitachi AC2 decode");
if (decodeHitachiAC(results, offset, kHitachiAc2Bits)) return true;
#endif // DECODE_HITACHI_AC2
#if DECODE_HITACHI_AC
DPRINTLN("Attempting Hitachi AC decode");
if (decodeHitachiAC(results, offset, kHitachiAcBits)) return true;
#endif
#if DECODE_HITACHI_AC1
DPRINTLN("Attempting Hitachi AC1 decode");
if (decodeHitachiAC(results, offset, kHitachiAc1Bits)) return true;
#endif
#if DECODE_WHIRLPOOL_AC
DPRINTLN("Attempting Whirlpool AC decode");
if (decodeWhirlpoolAC(results, offset)) return true;
#endif
#if DECODE_SAMSUNG_AC
DPRINTLN("Attempting Samsung AC (extended) decode");
// Check the extended size first, as it should fail fast due to longer
// length.
if (decodeSamsungAC(results, offset, kSamsungAcExtendedBits, false))
return true;
// Now check for the more common length.
DPRINTLN("Attempting Samsung AC decode");
if (decodeSamsungAC(results, offset, kSamsungAcBits)) return true;
#endif
#if DECODE_ELECTRA_AC
DPRINTLN("Attempting Electra AC decode");
if (decodeElectraAC(results, offset)) return true;
#endif
#if DECODE_PANASONIC_AC
DPRINTLN("Attempting Panasonic AC decode");
if (decodePanasonicAC(results, offset)) return true;
DPRINTLN("Attempting Panasonic AC short decode");
if (decodePanasonicAC(results, offset, kPanasonicAcShortBits)) return true;
#endif
#if DECODE_LUTRON
DPRINTLN("Attempting Lutron decode");
if (decodeLutron(results, offset)) return true;
#endif
#if DECODE_MWM
DPRINTLN("Attempting MWM decode");
if (decodeMWM(results, offset)) return true;
#endif
#if DECODE_VESTEL_AC
DPRINTLN("Attempting Vestel AC decode");
if (decodeVestelAc(results, offset)) return true;
#endif
#if DECODE_MITSUBISHI112 || DECODE_TCL112AC
// Mitsubish112 and Tcl112 share the same decoder.
DPRINTLN("Attempting Mitsubishi112/TCL112AC decode");
if (decodeMitsubishi112(results, offset)) return true;
#endif // DECODE_MITSUBISHI112 || DECODE_TCL112AC
#if DECODE_TECO
DPRINTLN("Attempting Teco decode");
if (decodeTeco(results, offset)) return true;
#endif
#if DECODE_LEGOPF
DPRINTLN("Attempting LEGOPF decode");
if (decodeLegoPf(results, offset)) return true;
#endif
#if DECODE_MITSUBISHIHEAVY
DPRINTLN("Attempting MITSUBISHIHEAVY (152 bit) decode");
if (decodeMitsubishiHeavy(results, offset, kMitsubishiHeavy152Bits))
return true;
DPRINTLN("Attempting MITSUBISHIHEAVY (88 bit) decode");
if (decodeMitsubishiHeavy(results, offset, kMitsubishiHeavy88Bits))
return true;
#endif
#if DECODE_ARGO
DPRINTLN("Attempting Argo decode");
if (decodeArgo(results, offset)) return true;
#endif // DECODE_ARGO
#if DECODE_SHARP_AC
DPRINTLN("Attempting SHARP_AC decode");
if (decodeSharpAc(results, offset)) return true;
#endif
#if DECODE_GOODWEATHER
DPRINTLN("Attempting GOODWEATHER decode");
if (decodeGoodweather(results, offset)) return true;
#endif // DECODE_GOODWEATHER
#if DECODE_INAX
DPRINTLN("Attempting Inax decode");
if (decodeInax(results, offset)) return true;
#endif // DECODE_INAX
#if DECODE_TROTEC
DPRINTLN("Attempting Trotec decode");
if (decodeTrotec(results, offset)) return true;
#endif // DECODE_TROTEC
#if DECODE_DAIKIN160
DPRINTLN("Attempting Daikin160 decode");
if (decodeDaikin160(results, offset)) return true;
#endif // DECODE_DAIKIN160
#if DECODE_NEOCLIMA
DPRINTLN("Attempting Neoclima decode");
if (decodeNeoclima(results, offset)) return true;
#endif // DECODE_NEOCLIMA
#if DECODE_DAIKIN176
DPRINTLN("Attempting Daikin176 decode");
if (decodeDaikin176(results, offset)) return true;
#endif // DECODE_DAIKIN176
#if DECODE_DAIKIN128
DPRINTLN("Attempting Daikin128 decode");
if (decodeDaikin128(results, offset)) return true;
#endif // DECODE_DAIKIN128
#if DECODE_AMCOR
DPRINTLN("Attempting Amcor decode");
if (decodeAmcor(results, offset)) return true;
#endif // DECODE_AMCOR
#if DECODE_DAIKIN152
DPRINTLN("Attempting Daikin152 decode");
if (decodeDaikin152(results, offset)) return true;
#endif // DECODE_DAIKIN152
#if DECODE_SYMPHONY
DPRINTLN("Attempting Symphony decode");
if (decodeSymphony(results, offset)) return true;
#endif // DECODE_SYMPHONY
#if DECODE_DAIKIN64
DPRINTLN("Attempting Daikin64 decode");
if (decodeDaikin64(results, offset)) return true;
#endif // DECODE_DAIKIN64
#if DECODE_AIRWELL
DPRINTLN("Attempting Airwell decode");
if (decodeAirwell(results, offset)) return true;
#endif // DECODE_AIRWELL
#if DECODE_DELONGHI_AC
DPRINTLN("Attempting Delonghi AC decode");
if (decodeDelonghiAc(results, offset)) return true;
#endif // DECODE_DELONGHI_AC
#if DECODE_DOSHISHA
DPRINTLN("Attempting Doshisha decode");
if (decodeDoshisha(results, offset)) return true;
#endif // DECODE_DOSHISHA
#if DECODE_MULTIBRACKETS
DPRINTLN("Attempting Multibrackets decode");
if (decodeMultibrackets(results, offset)) return true;
#endif // DECODE_MULTIBRACKETS
#if DECODE_CARRIER_AC40
DPRINTLN("Attempting Carrier 40bit decode");
if (decodeCarrierAC40(results, offset)) return true;
#endif // DECODE_CARRIER_AC40
#if DECODE_CARRIER_AC64
DPRINTLN("Attempting Carrier 64bit decode");
if (decodeCarrierAC64(results, offset)) return true;
#endif // DECODE_CARRIER_AC64
#if DECODE_TECHNIBEL_AC
DPRINTLN("Attempting Technibel AC decode");
if (decodeTechnibelAc(results, offset)) return true;
#endif // DECODE_TECHNIBEL_AC
#if DECODE_CORONA_AC
DPRINTLN("Attempting CoronaAc decode");
if (decodeCoronaAc(results, offset)) return true;
#endif // DECODE_CORONA_AC
#if DECODE_MIDEA24
DPRINTLN("Attempting Midea-Nec decode");
if (decodeMidea24(results, offset)) return true;
#endif // DECODE_MIDEA24
#if DECODE_ZEPEAL
DPRINTLN("Attempting Zepeal decode");
if (decodeZepeal(results, offset)) return true;
#endif // DECODE_ZEPEAL
#if DECODE_SANYO_AC
DPRINTLN("Attempting Sanyo AC decode");
if (decodeSanyoAc(results, offset)) return true;
#endif // DECODE_SANYO_AC
#if DECODE_VOLTAS
DPRINTLN("Attempting Voltas decode");
if (decodeVoltas(results)) return true;
#endif // DECODE_VOLTAS
#if DECODE_METZ
DPRINTLN("Attempting Metz decode");
if (decodeMetz(results, offset)) return true;
#endif // DECODE_METZ
#if DECODE_TRANSCOLD
DPRINTLN("Attempting Transcold decode");
if (decodeTranscold(results, offset)) return true;
#endif // DECODE_TRANSCOLD
#if DECODE_MIRAGE
DPRINTLN("Attempting Mirage decode");
if (decodeMirage(results, offset)) return true;
#endif // DECODE_MIRAGE
#if DECODE_ELITESCREENS
DPRINTLN("Attempting EliteScreens decode");
if (decodeElitescreens(results, offset)) return true;
#endif // DECODE_ELITESCREENS
#if DECODE_PANASONIC_AC32
DPRINTLN("Attempting Panasonic AC (32bit) long decode");
if (decodePanasonicAC32(results, offset, kPanasonicAc32Bits)) return true;
DPRINTLN("Attempting Panasonic AC (32bit) short decode");
if (decodePanasonicAC32(results, offset, kPanasonicAc32Bits / 2))
return true;
#endif // DECODE_PANASONIC_AC32
#if DECODE_ECOCLIM
DPRINTLN("Attempting Ecoclim decode");
if (decodeEcoclim(results, offset, kEcoclimBits) ||
decodeEcoclim(results, offset, kEcoclimShortBits)) return true;
#endif // DECODE_ECOCLIM
// Typically new protocols are added above this line.
}