forked from Arduino-IRremote/Arduino-IRremote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRReceive.hpp
1578 lines (1423 loc) · 53.1 KB
/
IRReceive.hpp
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
/*
* IRReceive.hpp
* This file is exclusively included by IRremote.h to enable easy configuration of library switches
*
* Contains all IRrecv class functions as well as other receiver related functions.
*
* This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
*
************************************************************************************
* MIT License
*
* Copyright (c) 2009-2021 Ken Shirriff, Rafi Khan, Armin Joachimsmeyer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
************************************************************************************
*/
#ifndef IR_RECEIVE_HPP
#define IR_RECEIVE_HPP
/** \addtogroup Receiving Receiving IR data for multiple protocols
* @{
*/
/**
* The receiver instance
*/
IRrecv IrReceiver;
/*
* The control structure instance
*/
struct irparams_struct irparams; // the irparams instance
/**
* Instantiate the IRrecv class. Multiple instantiation is not supported.
* @param IRReceivePin Arduino pin to use. No sanity check is made.
*/
IRrecv::IRrecv() {
decodedIRData.rawDataPtr = &irparams; // for decodePulseDistanceData() etc.
setReceivePin(0);
#if !defined(NO_LED_FEEDBACK_CODE)
setLEDFeedback(0, false);
#endif
}
IRrecv::IRrecv(uint8_t aReceivePin) {
decodedIRData.rawDataPtr = &irparams; // for decodePulseDistanceData() etc.
setReceivePin(aReceivePin);
#if !defined(NO_LED_FEEDBACK_CODE)
setLEDFeedback(0, false);
#endif
}
/**
* Instantiate the IRrecv class. Multiple instantiation is not supported.
* @param aReceivePin Arduino pin to use, where a demodulating IR receiver is connected.
* @param aFeedbackLEDPin if 0, then take board specific FEEDBACK_LED_ON() and FEEDBACK_LED_OFF() functions
*/
IRrecv::IRrecv(uint8_t aReceivePin, uint8_t aFeedbackLEDPin) {
decodedIRData.rawDataPtr = &irparams; // for decodePulseDistanceData() etc.
setReceivePin(aReceivePin);
#if !defined(NO_LED_FEEDBACK_CODE)
setLEDFeedback(aFeedbackLEDPin, false);
#else
(void) aFeedbackLEDPin;
#endif
}
/**********************************************************************************************************************
* Stream like API
**********************************************************************************************************************/
/**
* Initializes the receive and feedback pin
* @param aReceivePin The Arduino pin number, where a demodulating IR receiver is connected.
* @param aEnableLEDFeedback if true / ENABLE_LED_FEEDBACK, then let the feedback led blink on receiving IR signal
* @param aFeedbackLEDPin if 0 / USE_DEFAULT_FEEDBACK_LED_PIN, then take board specific FEEDBACK_LED_ON() and FEEDBACK_LED_OFF() functions
*/
void IRrecv::begin(uint8_t aReceivePin, bool aEnableLEDFeedback, uint8_t aFeedbackLEDPin) {
setReceivePin(aReceivePin);
#if !defined(NO_LED_FEEDBACK_CODE)
setLEDFeedback(aFeedbackLEDPin, aEnableLEDFeedback);
#else
(void) aEnableLEDFeedback;
(void) aFeedbackLEDPin;
#endif
enableIRIn();
}
/**
* Sets / changes the receiver pin number
*/
void IRrecv::setReceivePin(uint8_t aReceivePinNumber) {
irparams.IRReceivePin = aReceivePinNumber;
#if defined(__AVR__)
irparams.IRReceivePinMask = digitalPinToBitMask(aReceivePinNumber);
irparams.IRReceivePinPortInputRegister = portInputRegister(digitalPinToPort(aReceivePinNumber));
#endif
}
/**
* Configures the timer and the state machine for IR reception.
*/
void IRrecv::start() {
enableIRIn();
}
/**
* Configures the timer and the state machine for IR reception.
* @param aMicrosecondsToAddToGapCounter To compensate for microseconds the timer was stopped / disabled.
*/
void IRrecv::start(uint32_t aMicrosecondsToAddToGapCounter) {
enableIRIn();
noInterrupts();
irparams.TickCounterForISR += aMicrosecondsToAddToGapCounter / MICROS_PER_TICK;
interrupts();
}
/**
* Disables the timer for IR reception.
* Alias for
*/
void IRrecv::stop() {
disableIRIn();
}
/**
* Disables the timer for IR reception.
*/
void IRrecv::end() {
stop();
}
/**
* Configures the timer and the state machine for IR reception.
*/
void IRrecv::enableIRIn() {
noInterrupts();
// Setup pulse clock TickCounterForISR interrupt
timerConfigForReceive();
TIMER_ENABLE_RECEIVE_INTR; // Timer interrupt enable
TIMER_RESET_INTR_PENDING; // NOP for most platforms
// Initialize state machine state
resume();
interrupts(); // after resume to avoid running through STOP state 1 time before switching to IDLE
// Set pin modes
pinMode(irparams.IRReceivePin, INPUT);
}
/**
* Disables the timer for IR reception.
*/
void IRrecv::disableIRIn() {
TIMER_DISABLE_RECEIVE_INTR;
}
/**
* Returns status of reception
* @return true if no reception is on-going.
*/
bool IRrecv::isIdle() {
return (irparams.StateForISR == IR_REC_STATE_IDLE || irparams.StateForISR == IR_REC_STATE_STOP) ? true : false;
}
/**
* Restart the ISR state machine
* Enable receiving of the next value
*/
void IRrecv::resume() {
// check allows to call resume at arbitrary places or more than once
if (irparams.StateForISR == IR_REC_STATE_STOP) {
irparams.StateForISR = IR_REC_STATE_IDLE;
}
}
/**
* Is internally called by decode before calling decoders.
* Must be used to setup data, if you call decoders manually.
*/
void IRrecv::initDecodedIRData() {
if (irparams.OverflowFlag) {
// Copy overflow flag to decodedIRData.flags and reset it
irparams.OverflowFlag = false;
irparams.rawlen = 0; // otherwise we have OverflowFlag again at next ISR call
decodedIRData.flags = IRDATA_FLAGS_WAS_OVERFLOW;
IR_DEBUG_PRINTLN("Overflow happened");
} else {
decodedIRData.flags = IRDATA_FLAGS_EMPTY;
// save last protocol, command and address for repeat handling (where the are copied back :-))
lastDecodedProtocol = decodedIRData.protocol; // repeat patterns can be equal between protocols (e.g. NEC and LG), so we must keep the original one
lastDecodedCommand = decodedIRData.command;
lastDecodedAddress = decodedIRData.address;
}
decodedIRData.protocol = UNKNOWN;
decodedIRData.command = 0;
decodedIRData.address = 0;
decodedIRData.decodedRawData = 0;
decodedIRData.numberOfBits = 0;
}
/**
* Returns true if IR receiver data is available.
*/
bool IRrecv::available() {
return (irparams.StateForISR == IR_REC_STATE_STOP);
}
/**
* If IR receiver data is available, returns pointer to IrReceiver.decodedIRData, else NULL.
*/
IRData* IRrecv::read() {
if (irparams.StateForISR != IR_REC_STATE_STOP) {
return NULL;
}
if (decode()) {
return &decodedIRData;
} else {
return NULL;
}
}
/**
* The main decode function, attempts to decode the recently receive IR signal.
* @return false if no IR receiver data available, true if data available. Results of decoding are stored in IrReceiver.decodedIRData.
* The set of decoders used is determined by active definitions of the DECODE_<PROTOCOL> macros.
*/
bool IRrecv::decode() {
if (irparams.StateForISR != IR_REC_STATE_STOP) {
return false;
}
initDecodedIRData(); // sets IRDATA_FLAGS_WAS_OVERFLOW
if (decodedIRData.flags & IRDATA_FLAGS_WAS_OVERFLOW) {
/*
* Set OverflowFlag flag and return true here, to let the loop call resume or print raw data.
*/
decodedIRData.protocol = UNKNOWN;
return true;
}
#if defined(DECODE_NEC)
IR_TRACE_PRINTLN("Attempting NEC decode");
if (decodeNEC()) {
return true;
}
#endif
#if defined(DECODE_PANASONIC) || defined(DECODE_KASEIKYO)
IR_TRACE_PRINTLN("Attempting Panasonic/Kaseikyo decode");
if (decodeKaseikyo()) {
return true;
}
#endif
#if defined(DECODE_DENON)
IR_TRACE_PRINTLN("Attempting Denon/Sharp decode");
if (decodeDenon()) {
return true;
}
#endif
#if defined(DECODE_SONY)
IR_TRACE_PRINTLN("Attempting Sony decode");
if (decodeSony()) {
return true;
}
#endif
#if defined(DECODE_RC5)
IR_TRACE_PRINTLN("Attempting RC5 decode");
if (decodeRC5()) {
return true;
}
#endif
#if defined(DECODE_RC6)
IR_TRACE_PRINTLN("Attempting RC6 decode");
if (decodeRC6()) {
return true;
}
#endif
#if defined(DECODE_LG)
IR_TRACE_PRINTLN("Attempting LG decode");
if (decodeLG()) {
return true;
}
#endif
#if defined(DECODE_JVC)
IR_TRACE_PRINTLN("Attempting JVC decode");
if (decodeJVC()) {
return true;
}
#endif
#if defined(DECODE_SAMSUNG)
IR_TRACE_PRINTLN("Attempting Samsung decode");
if (decodeSamsung()) {
return true;
}
#endif
/*
* Start of the exotic protocols
*/
#if defined(DECODE_WHYNTER)
IR_TRACE_PRINTLN("Attempting Whynter decode");
if (decodeWhynter()) {
return true;
}
#endif
#if defined(DECODE_LEGO_PF)
IR_TRACE_PRINTLN("Attempting Lego Power Functions");
if (decodeLegoPowerFunctions()) {
return true;
}
#endif
#if defined(DECODE_BOSEWAVE)
IR_TRACE_PRINTLN("Attempting Bosewave decode");
if (decodeBoseWave()) {
return true;
}
#endif
#if defined(DECODE_MAGIQUEST)
IR_TRACE_PRINTLN("Attempting MagiQuest decode");
if (decodeMagiQuest()) {
return true;
}
#endif
/*
* Try the universal decoder for pulse width or pulse distance protocols
*/
#if defined(DECODE_DISTANCE)
IR_TRACE_PRINTLN("Attempting universal Distance decode");
if (decodeDistance()) {
return true;
}
#endif
/*
* Last resort is the universal hash decode which always return true
*/
#if defined(DECODE_HASH)
IR_TRACE_PRINTLN("Hash decode");
// decodeHash returns a hash on any input.
// Thus, it needs to be last in the list.
// If you add any decodes, add them before this.
if (decodeHash()) {
return true;
}
#endif
/*
* Return true here, to let the loop decide to call resume or to print raw data.
*/
return true;
}
/**********************************************************************************************************************
* Common decode functions
**********************************************************************************************************************/
/**
* Decode pulse width protocols. Currently only used for sony protocol.
* The space (pause) has constant length, the length of the mark determines the bit value.
* Each bit looks like: MARK_1 + SPACE -> 1 or : MARK_0 + SPACE -> 0
*
* Input is IrReceiver.decodedIRData.rawDataPtr->rawbuf[]
* Output is IrReceiver.decodedIRData.decodedRawData
*
* @param aStartOffset must point to a mark
* @return true if decoding was successful
*/
bool IRrecv::decodePulseWidthData(uint8_t aNumberOfBits, uint8_t aStartOffset, uint16_t aOneMarkMicros, uint16_t aZeroMarkMicros,
uint16_t aBitSpaceMicros, bool aMSBfirst) {
uint16_t *tRawBufPointer = &decodedIRData.rawDataPtr->rawbuf[aStartOffset];
uint32_t tDecodedData = 0;
if (aMSBfirst) {
for (uint_fast8_t i = 0; i < aNumberOfBits; i++) {
// Check for variable length mark indicating a 0 or 1
if (matchMark(*tRawBufPointer, aOneMarkMicros)) {
tDecodedData = (tDecodedData << 1) | 1;
IR_TRACE_PRINT('1');
} else if (matchMark(*tRawBufPointer, aZeroMarkMicros)) {
tDecodedData = (tDecodedData << 1) | 0;
IR_TRACE_PRINT('0');
} else {
IR_DEBUG_PRINT(F("Mark="));
IR_DEBUG_PRINT(*tRawBufPointer * MICROS_PER_TICK);
IR_DEBUG_PRINT(F(" is not "));
IR_DEBUG_PRINT(aOneMarkMicros);
IR_DEBUG_PRINT(F(" or "));
IR_DEBUG_PRINT(aZeroMarkMicros);
IR_DEBUG_PRINT(' ');
return false;
}
tRawBufPointer++;
if (tRawBufPointer < &decodedIRData.rawDataPtr->rawbuf[decodedIRData.rawDataPtr->rawlen]) {
// Assume that last space, which is not recorded, is correct, since we can not check it
// Check for constant length space
if (!matchSpace(*tRawBufPointer, aBitSpaceMicros)) {
IR_DEBUG_PRINT(F("Space="));
IR_DEBUG_PRINT(*tRawBufPointer * MICROS_PER_TICK);
IR_DEBUG_PRINT(F(" is not "));
IR_DEBUG_PRINT(aBitSpaceMicros);
IR_DEBUG_PRINT(' ');
return false;
}
tRawBufPointer++;
}
}
IR_TRACE_PRINTLN("");
} else {
for (uint32_t tMask = 1UL; aNumberOfBits > 0; tMask <<= 1, aNumberOfBits--) {
// Check for variable length mark indicating a 0 or 1
if (matchMark(*tRawBufPointer, aOneMarkMicros)) {
tDecodedData |= tMask; // set the bit
IR_TRACE_PRINT('1');
} else if (matchMark(*tRawBufPointer, aZeroMarkMicros)) {
// do not set the bit
IR_TRACE_PRINT('0');
} else {
IR_DEBUG_PRINT(F("Mark="));
IR_DEBUG_PRINT(*tRawBufPointer * MICROS_PER_TICK);
IR_DEBUG_PRINT(F(" is not "));
IR_DEBUG_PRINT(aOneMarkMicros);
IR_DEBUG_PRINT(F(" or "));
IR_DEBUG_PRINT(aZeroMarkMicros);
IR_DEBUG_PRINT(' ');
return false;
}
tRawBufPointer++;
if (tRawBufPointer < &decodedIRData.rawDataPtr->rawbuf[decodedIRData.rawDataPtr->rawlen]) {
// Assume that last space, which is not recorded, is correct, since we can not check it
// Check for constant length space
if (!matchSpace(*tRawBufPointer, aBitSpaceMicros)) {
IR_DEBUG_PRINT(F("Space="));
IR_DEBUG_PRINT(*tRawBufPointer * MICROS_PER_TICK);
IR_DEBUG_PRINT(F(" is not "));
IR_DEBUG_PRINT(aBitSpaceMicros);
IR_DEBUG_PRINT(' ');
return false;
}
tRawBufPointer++;
}
}
IR_TRACE_PRINTLN("");
}
decodedIRData.decodedRawData = tDecodedData;
return true;
}
/**
* Decode pulse distance protocols.
* The mark (pulse) has constant length, the length of the space determines the bit value.
* Each bit looks like: MARK + SPACE_1 -> 1
* or : MARK + SPACE_0 -> 0
*
* Input is IrReceiver.decodedIRData.rawDataPtr->rawbuf[]
* Output is IrReceiver.decodedIRData.decodedRawData
*
* @param aStartOffset must point to a mark
* @return true if decoding was successful
*/
bool IRrecv::decodePulseDistanceData(uint8_t aNumberOfBits, uint8_t aStartOffset, uint16_t aBitMarkMicros, uint16_t aOneSpaceMicros,
uint16_t aZeroSpaceMicros, bool aMSBfirst) {
uint16_t *tRawBufPointer = &decodedIRData.rawDataPtr->rawbuf[aStartOffset];
uint32_t tDecodedData = 0;
if (aMSBfirst) {
for (uint_fast8_t i = 0; i < aNumberOfBits; i++) {
// Check for constant length mark
if (!matchMark(*tRawBufPointer, aBitMarkMicros)) {
IR_DEBUG_PRINT(F("Mark="));
IR_DEBUG_PRINT(*tRawBufPointer * MICROS_PER_TICK);
IR_DEBUG_PRINT(F(" is not "));
IR_DEBUG_PRINT(aBitMarkMicros);
IR_DEBUG_PRINT(' ');
return false;
}
tRawBufPointer++;
// Check for variable length space indicating a 0 or 1
if (matchSpace(*tRawBufPointer, aOneSpaceMicros)) {
tDecodedData = (tDecodedData << 1) | 1;
IR_TRACE_PRINT('1');
} else if (matchSpace(*tRawBufPointer, aZeroSpaceMicros)) {
tDecodedData = (tDecodedData << 1) | 0;
IR_TRACE_PRINT('0');
} else {
IR_DEBUG_PRINT(F("Space="));
IR_DEBUG_PRINT(*tRawBufPointer * MICROS_PER_TICK);
IR_DEBUG_PRINT(F(" is not "));
IR_DEBUG_PRINT(aOneSpaceMicros);
IR_DEBUG_PRINT(F(" or "));
IR_DEBUG_PRINT(aZeroSpaceMicros);
IR_DEBUG_PRINT(' ');
return false;
}
tRawBufPointer++;
}
IR_TRACE_PRINTLN("");
} else {
for (uint32_t tMask = 1UL; aNumberOfBits > 0; tMask <<= 1, aNumberOfBits--) {
// Check for constant length mark
if (!matchMark(*tRawBufPointer, aBitMarkMicros)) {
IR_DEBUG_PRINT(F("Mark="));
IR_DEBUG_PRINT(*tRawBufPointer * MICROS_PER_TICK);
IR_DEBUG_PRINT(F(" is not "));
IR_DEBUG_PRINT(aBitMarkMicros);
IR_DEBUG_PRINT(' ');
return false;
}
tRawBufPointer++;
// Check for variable length space indicating a 0 or 1
if (matchSpace(*tRawBufPointer, aOneSpaceMicros)) {
tDecodedData |= tMask; // set the bit
IR_TRACE_PRINT('1');
} else if (matchSpace(*tRawBufPointer, aZeroSpaceMicros)) {
// do not set the bit
IR_TRACE_PRINT('0');
} else {
IR_DEBUG_PRINT(F("Space="));
IR_DEBUG_PRINT(*tRawBufPointer * MICROS_PER_TICK);
IR_DEBUG_PRINT(F(" is not "));
IR_DEBUG_PRINT(aOneSpaceMicros);
IR_DEBUG_PRINT(F(" or "));
IR_DEBUG_PRINT(aZeroSpaceMicros);
IR_DEBUG_PRINT(' ');
return false;
}
tRawBufPointer++;
}
IR_TRACE_PRINTLN("");
}
decodedIRData.decodedRawData = tDecodedData;
return true;
}
/*
* Static variables for the getBiphaselevel function
*/
uint8_t sBiphaseDecodeRawbuffOffset; // Index into raw timing array
uint16_t sCurrentTimingIntervals; // Number of aBiphaseTimeUnit intervals of the current rawbuf[sBiphaseDecodeRawbuffOffset] timing.
uint8_t sUsedTimingIntervals; // Number of already used intervals of sCurrentTimingIntervals.
uint16_t sBiphaseTimeUnit;
void IRrecv::initBiphaselevel(uint8_t aRCDecodeRawbuffOffset, uint16_t aBiphaseTimeUnit) {
sBiphaseDecodeRawbuffOffset = aRCDecodeRawbuffOffset;
sBiphaseTimeUnit = aBiphaseTimeUnit;
sUsedTimingIntervals = 0;
}
/**
* Gets the level of one time interval (aBiphaseTimeUnit) at a time from the raw buffer.
* The RC5/6 decoding is easier if the data is broken into time intervals.
* E.g. if the buffer has mark for 2 time intervals and space for 1,
* successive calls to getBiphaselevel will return 1, 1, 0.
*
* _ _ _ _ _ _ _ _ _ _ _ _ _
* _____| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |_| |
* ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ Significant clock edge
* _ _ _ ___ _ ___ ___ _ - Mark
* Data _____| |___| |_| |_| |_| |___| |___| |_| | - Data starts with a mark->space bit
* 1 0 0 0 1 1 0 1 0 1 1 - Space
* A mark to space at a significant clock edge results in a 1
* A space to mark at a significant clock edge results in a 0 (for RC6)
* Returns current level [MARK or SPACE] or -1 for error (measured time interval is not a multiple of sBiphaseTimeUnit).
*/
uint8_t IRrecv::getBiphaselevel() {
uint8_t tLevelOfCurrentInterval; // 0 (SPACE) or 1 (MARK)
if (sBiphaseDecodeRawbuffOffset >= decodedIRData.rawDataPtr->rawlen) {
return SPACE; // After end of recorded buffer, assume space.
}
tLevelOfCurrentInterval = (sBiphaseDecodeRawbuffOffset) & 1; // on odd rawbuf offsets we have mark timings
/*
* Setup data if sUsedTimingIntervals is 0
*/
if (sUsedTimingIntervals == 0) {
uint16_t tCurrentTimingWith = decodedIRData.rawDataPtr->rawbuf[sBiphaseDecodeRawbuffOffset];
uint16_t tMarkExcessCorrection = (tLevelOfCurrentInterval == MARK) ? MARK_EXCESS_MICROS : -MARK_EXCESS_MICROS;
if (matchTicks(tCurrentTimingWith, (sBiphaseTimeUnit) + tMarkExcessCorrection)) {
sCurrentTimingIntervals = 1;
} else if (matchTicks(tCurrentTimingWith, (2 * sBiphaseTimeUnit) + tMarkExcessCorrection)) {
sCurrentTimingIntervals = 2;
} else if (matchTicks(tCurrentTimingWith, (3 * sBiphaseTimeUnit) + tMarkExcessCorrection)) {
sCurrentTimingIntervals = 3;
} else {
return -1;
}
}
// We use another interval from tCurrentTimingIntervals
sUsedTimingIntervals++;
// keep track of current timing offset
if (sUsedTimingIntervals >= sCurrentTimingIntervals) {
// we have used all intervals of current timing, switch to next timing value
sUsedTimingIntervals = 0;
sBiphaseDecodeRawbuffOffset++;
}
IR_TRACE_PRINTLN(tLevelOfCurrentInterval);
return tLevelOfCurrentInterval;
}
#if defined(DECODE_HASH)
/**********************************************************************************************************************
* Internal Hash decode function
**********************************************************************************************************************/
/**
* Compare two (tick) values
* Use a tolerance of 20% to enable e.g. 500 and 600 (NEC timing) to be equal
* @return 0 if newval is shorter, 1 if newval is equal, and 2 if newval is longer
*/
uint8_t IRrecv::compare(unsigned int oldval, unsigned int newval) {
if (newval * 10 < oldval * 8) {
return 0;
}
if (oldval * 10 < newval * 8) {
return 2;
}
return 1;
}
#define FNV_PRIME_32 16777619 ///< used for decodeHash()
#define FNV_BASIS_32 2166136261 ///< used for decodeHash()
/**
* decodeHash - decode an arbitrary IR code.
* Instead of decoding using a standard encoding scheme
* (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value.
*
* The algorithm: look at the sequence of MARK signals, and see if each one
* is shorter (0), the same length (1), or longer (2) than the previous.
* Do the same with the SPACE signals. Hash the resulting sequence of 0's,
* 1's, and 2's to a 32-bit value. This will give a unique value for each
* different code (probably), for most code systems.
*
* Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param
* Converts the raw code values into a 32-bit hash code.
* Hopefully this code is unique for each button.
* This isn't a "real" decoding, just an arbitrary value.
*
* see: http://www.righto.com/2010/01/using-arbitrary-remotes-with-arduino.html
*/
bool IRrecv::decodeHash() {
unsigned long hash = FNV_BASIS_32; // the result is the same no matter if we use a long or unsigned long variable
// Require at least 6 samples to prevent triggering on noise
if (decodedIRData.rawDataPtr->rawlen < 6) {
return false;
}
#if RAW_BUFFER_LENGTH <= 254 // saves around 75 bytes program space and speeds up ISR
uint8_t i;
#else
uint16_t i;
#endif
for (i = 1; (i + 2) < decodedIRData.rawDataPtr->rawlen; i++) {
uint8_t value = compare(decodedIRData.rawDataPtr->rawbuf[i], decodedIRData.rawDataPtr->rawbuf[i + 2]);
// Add value into the hash
hash = (hash * FNV_PRIME_32) ^ value;
}
decodedIRData.decodedRawData = hash;
decodedIRData.numberOfBits = 32;
decodedIRData.protocol = UNKNOWN;
return true;
}
bool IRrecv::decodeHashOld(decode_results *aResults) {
unsigned long hash = FNV_BASIS_32;
// Require at least 6 samples to prevent triggering on noise
if (aResults->rawlen < 6) {
return false;
}
for (unsigned int i = 1; (i + 2) < aResults->rawlen; i++) {
uint8_t value = compare(aResults->rawbuf[i], aResults->rawbuf[i + 2]);
// Add value into the hash
hash = (hash * FNV_PRIME_32) ^ value;
}
aResults->value = hash;
aResults->bits = 32;
aResults->decode_type = UNKNOWN;
decodedIRData.protocol = UNKNOWN;
return true;
}
#endif // DECODE_HASH
/**********************************************************************************************************************
* Match functions
**********************************************************************************************************************/
/**
* Match function without compensating for marks exceeded or spaces shortened by demodulator hardware
* Currently not used
*/
bool matchTicks(uint16_t aMeasuredTicks, uint16_t aMatchValueMicros) {
#ifdef TRACE
Serial.print(F("Testing: "));
Serial.print(TICKS_LOW(aMatchValueMicros), DEC);
Serial.print(F(" <= "));
Serial.print(aMeasuredTicks, DEC);
Serial.print(F(" <= "));
Serial.print(TICKS_HIGH(aMatchValueMicros), DEC);
#endif
bool passed = ((aMeasuredTicks >= TICKS_LOW(aMatchValueMicros)) && (aMeasuredTicks <= TICKS_HIGH(aMatchValueMicros)));
#ifdef TRACE
if (passed) {
Serial.println(F("?; passed"));
} else {
Serial.println(F("?; FAILED"));
}
#endif
return passed;
}
bool MATCH(uint16_t measured_ticks, uint16_t desired_us) {
return matchTicks(measured_ticks, desired_us);
}
/**
* Compensate for marks exceeded by demodulator hardware
*/
bool matchMark(uint16_t aMeasuredTicks, uint16_t aMatchValueMicros) {
#ifdef TRACE
Serial.print(F("Testing mark (actual vs desired): "));
Serial.print(aMeasuredTicks * MICROS_PER_TICK, DEC);
Serial.print(F("us vs "));
Serial.print(aMatchValueMicros, DEC);
Serial.print(F("us: "));
Serial.print(TICKS_LOW(aMatchValueMicros + MARK_EXCESS_MICROS) * MICROS_PER_TICK, DEC);
Serial.print(F(" <= "));
Serial.print(aMeasuredTicks * MICROS_PER_TICK, DEC);
Serial.print(F(" <= "));
Serial.print(TICKS_HIGH(aMatchValueMicros + MARK_EXCESS_MICROS) * MICROS_PER_TICK, DEC);
#endif
// compensate for marks exceeded by demodulator hardware
bool passed = ((aMeasuredTicks >= TICKS_LOW(aMatchValueMicros + MARK_EXCESS_MICROS))
&& (aMeasuredTicks <= TICKS_HIGH(aMatchValueMicros + MARK_EXCESS_MICROS)));
#ifdef TRACE
if (passed) {
Serial.println(F("?; passed"));
} else {
Serial.println(F("?; FAILED"));
}
#endif
return passed;
}
bool MATCH_MARK(uint16_t measured_ticks, uint16_t desired_us) {
return matchMark(measured_ticks, desired_us);
}
/**
* Compensate for spaces shortened by demodulator hardware
*/
bool matchSpace(uint16_t aMeasuredTicks, uint16_t aMatchValueMicros) {
#ifdef TRACE
Serial.print(F("Testing space (actual vs desired): "));
Serial.print(aMeasuredTicks * MICROS_PER_TICK, DEC);
Serial.print(F("us vs "));
Serial.print(aMatchValueMicros, DEC);
Serial.print(F("us: "));
Serial.print(TICKS_LOW(aMatchValueMicros - MARK_EXCESS_MICROS) * MICROS_PER_TICK, DEC);
Serial.print(F(" <= "));
Serial.print(aMeasuredTicks * MICROS_PER_TICK, DEC);
Serial.print(F(" <= "));
Serial.print(TICKS_HIGH(aMatchValueMicros - MARK_EXCESS_MICROS) * MICROS_PER_TICK, DEC);
#endif
// compensate for spaces shortened by demodulator hardware
bool passed = ((aMeasuredTicks >= TICKS_LOW(aMatchValueMicros - MARK_EXCESS_MICROS))
&& (aMeasuredTicks <= TICKS_HIGH(aMatchValueMicros - MARK_EXCESS_MICROS)));
#ifdef TRACE
if (passed) {
Serial.println(F("?; passed"));
} else {
Serial.println(F("?; FAILED"));
}
#endif
return passed;
}
bool MATCH_SPACE(uint16_t measured_ticks, uint16_t desired_us) {
return matchSpace(measured_ticks, desired_us);
}
/**
* Getter function for MARK_EXCESS_MICROS
*/
int getMarkExcessMicros() {
return MARK_EXCESS_MICROS;
}
/*
* Check if protocol is not detected and detected space between two transmissions
* is smaller than known value for protocols (Sony with around 24 ms)
*/
void CheckForRecordGapsMicros(Print *aSerial, IRData *aIRDataPtr) {
/*
* Check if protocol is not detected and detected space between two transmissions
* is smaller than known value for protocols (Sony with around 24 ms)
*/
if (aIRDataPtr->protocol <= PULSE_WIDTH && aIRDataPtr->rawDataPtr->rawbuf[0] < RECORD_GAP_MICROS_WARNING_THRESHOLD) {
aSerial->println();
aSerial->print(F("Space between two detected transmission is greater than "));
aSerial->print(RECORD_GAP_MICROS);
aSerial->print(F(" but smaller than the minimal gap of "));
aSerial->print(RECORD_GAP_MICROS_WARNING_THRESHOLD);
aSerial->println(F(" known for a protocol."));
aSerial->println(F("If you get unexpected results, try to increase the RECORD_GAP_MICROS in IRremote.h."));
aSerial->println();
}
}
/**********************************************************************************************************************
* Print functions
* Since a library should not allocate the "Serial" object, all functions require a pointer to a Print object.
**********************************************************************************************************************/
void printActiveIRProtocols(Print *aSerial) {
#if defined(DECODE_NEC)
aSerial->print(F("NEC, "));
#endif
#if defined(DECODE_PANASONIC) || defined(DECODE_KASEIKYO)
aSerial->print(F("Panasonic/Kaseikyo, "));
#endif
#if defined(DECODE_DENON)
aSerial->print(F("Denon/Sharp, "));
#endif
#if defined(DECODE_SONY)
aSerial->print(F("Sony, "));
#endif
#if defined(DECODE_RC5)
aSerial->print(F("RC5, "));
#endif
#if defined(DECODE_RC6)
aSerial->print(F("RC6, "));
#endif
#if defined(DECODE_LG)
aSerial->print(F("LG, "));
#endif
#if defined(DECODE_JVC)
aSerial->print(F("JVC, "));
#endif
#if defined(DECODE_SAMSUNG)
aSerial->print(F("Samsung, "));
#endif
/*
* Start of the exotic protocols
*/
#if defined(DECODE_WHYNTER)
aSerial->print(F("Whynter, "));
#endif
#if defined(DECODE_LEGO_PF)
aSerial->print(F("Lego Power Functions, "));
#endif
#if defined(DECODE_BOSEWAVE)
aSerial->print(F("Bosewave , "));
#endif
#if defined(DECODE_MAGIQUEST)
aSerial->print(F("MagiQuest, "));
#endif
#if defined(DECODE_DISTANCE)
aSerial->print(F("Universal Distance, "));
#endif
#if defined(DECODE_HASH)
aSerial->print(F("Hash "));
#endif
#if defined(NO_DECODER) // for sending raw only
(void)aSerial; // to avoid compiler warnings
#endif
}
/**
* Internal function to print decoded result and flags in one line.
* Ends with println().
*
* @param aSerial The Print object on which to write, for Arduino you can use &Serial.
*/
void printIRResultShort(Print *aSerial, IRData *aIRDataPtr, uint16_t aLeadingSpaceTicks) {
aSerial->print(F("Protocol="));
aSerial->print(getProtocolString(aIRDataPtr->protocol));
if (aIRDataPtr->protocol == UNKNOWN) {
#if defined(DECODE_HASH)
aSerial->print(F(" Hash=0x"));
aSerial->print(aIRDataPtr->decodedRawData, HEX);
#endif
aSerial->print(' ');
aSerial->print((aIRDataPtr->rawDataPtr->rawlen + 1) / 2, DEC);
aSerial->println(F(" bits received"));
} else {
/*
* New decoders have address and command
*/
aSerial->print(F(" Address=0x"));
aSerial->print(aIRDataPtr->address, HEX);
aSerial->print(F(" Command=0x"));
aSerial->print(aIRDataPtr->command, HEX);
if (aIRDataPtr->flags & IRDATA_FLAGS_EXTRA_INFO) {
aSerial->print(F(" Extra=0x"));
aSerial->print(aIRDataPtr->extra, HEX);
}
if (aIRDataPtr->flags & IRDATA_FLAGS_PARITY_FAILED) {
aSerial->print(F(" Parity fail"));
}
if (aIRDataPtr->flags & IRDATA_TOGGLE_BIT_MASK) {
aSerial->print(F(" Toggle=1"));
}
if (aIRDataPtr->flags & (IRDATA_FLAGS_IS_AUTO_REPEAT | IRDATA_FLAGS_IS_REPEAT)) {
aSerial->print(' ');
if (aIRDataPtr->flags & IRDATA_FLAGS_IS_AUTO_REPEAT) {
aSerial->print(F("Auto-"));
}
aSerial->print(F("Repeat"));
if (aLeadingSpaceTicks != 0) {
aSerial->print(F(" gap="));
aSerial->print((uint32_t) aLeadingSpaceTicks * MICROS_PER_TICK);
aSerial->print(F("us"));
}
}
/*
* Print raw data
*/
if (!(aIRDataPtr->flags & IRDATA_FLAGS_IS_REPEAT) || aIRDataPtr->decodedRawData != 0) {
aSerial->print(F(" Raw-Data=0x"));
aSerial->print(aIRDataPtr->decodedRawData, HEX);
/*
* Print number of bits processed
*/
aSerial->print(' ');
aSerial->print(aIRDataPtr->numberOfBits, DEC);
aSerial->print(F(" bits"));
if (aIRDataPtr->flags & IRDATA_FLAGS_IS_MSB_FIRST) {
aSerial->println(F(" MSB first"));
} else {
aSerial->println(F(" LSB first"));
}
} else {
aSerial->println();
}
CheckForRecordGapsMicros(aSerial, aIRDataPtr);
}
}
/**
* Function to print values and flags of IrReceiver.decodedIRData in one line.