33 #ifndef _IR_RECEIVE_HPP
34 #define _IR_RECEIVE_HPP
36 #if defined(DEBUG) && !defined(LOCAL_DEBUG)
42 #if defined(TRACE) && !defined(LOCAL_TRACE)
73 #if !defined(NO_LED_FEEDBACK_CODE)
81 #if !defined(NO_LED_FEEDBACK_CODE)
94 #if !defined(NO_LED_FEEDBACK_CODE)
97 (void) aFeedbackLEDPin;
119 #if defined(ESP8266) || defined(ESP32)
123 #if defined(_IR_MEASURE_TIMING) && defined(_IR_TIMING_TEST_PIN)
128 #if defined(TIMER_REQUIRES_RESET_INTR_PENDING)
134 uint8_t tIRInputLevel = *
irparams.IRReceivePinPortInputRegister &
irparams.IRReceivePinMask;
159 #if defined(_IR_MEASURE_TIMING) && defined(_IR_TIMING_TEST_PIN)
180 #if defined(_IR_MEASURE_TIMING) && defined(_IR_TIMING_TEST_PIN)
197 #if !defined(IR_REMOTE_DISABLE_RECEIVE_COMPLETE_CALLBACK)
206 #if defined(_IR_MEASURE_TIMING) && defined(_IR_TIMING_TEST_PIN)
231 #if !defined(IR_REMOTE_DISABLE_RECEIVE_COMPLETE_CALLBACK)
245 #if defined(_IR_MEASURE_TIMING) && defined(_IR_TIMING_TEST_PIN)
254 #if !defined(NO_LED_FEEDBACK_CODE)
260 #ifdef _IR_MEASURE_TIMING
269 #if defined(TIMER_INTR_NAME) || defined(ISR)
270 # if defined(TIMER_INTR_NAME)
271 ISR (TIMER_INTR_NAME)
290 void IRrecv::begin(uint_fast8_t aReceivePin,
bool aEnableLEDFeedback, uint_fast8_t aFeedbackLEDPin) {
293 #if !defined(NO_LED_FEEDBACK_CODE)
295 if (aEnableLEDFeedback) {
300 (void) aEnableLEDFeedback;
301 (void) aFeedbackLEDPin;
304 #if defined(_IR_MEASURE_TIMING) && defined(_IR_TIMING_TEST_PIN)
316 # if defined(__digitalPinToBit)
317 if (__builtin_constant_p(aReceivePinNumber)) {
318 irparams.IRReceivePinMask = 1UL << (__digitalPinToBit(aReceivePinNumber));
320 irparams.IRReceivePinMask = digitalPinToBitMask(aReceivePinNumber);
323 irparams.IRReceivePinMask = digitalPinToBitMask(aReceivePinNumber);
325 # if defined(__digitalPinToPINReg)
329 if (__builtin_constant_p(aReceivePinNumber)) {
330 irparams.IRReceivePinPortInputRegister = __digitalPinToPINReg(aReceivePinNumber);
332 irparams.IRReceivePinPortInputRegister = portInputRegister(digitalPinToPort(aReceivePinNumber));
335 irparams.IRReceivePinPortInputRegister = portInputRegister(digitalPinToPort(aReceivePinNumber));
342 #if !defined(IR_REMOTE_DISABLE_RECEIVE_COMPLETE_CALLBACK)
366 #ifdef _IR_MEASURE_TIMING
379 #ifdef _IR_MEASURE_TIMING
423 #if defined(SEND_PWM_BY_TIMER) && !defined(SEND_PWM_DOES_NOT_USE_RECEIVE_TIMER)
478 #if defined(LOCAL_DEBUG)
479 Serial.print(F(
"Overflow happened, try to increase the \"RAW_BUFFER_LENGTH\" value of "));
481 Serial.println(F(
" with #define RAW_BUFFER_LENGTH=<biggerValue>"));
541 #if defined(DECODE_NEC) || defined(DECODE_ONKYO)
548 #if defined(DECODE_PANASONIC) || defined(DECODE_KASEIKYO)
555 #if defined(DECODE_DENON)
562 #if defined(DECODE_SONY)
569 #if defined(DECODE_RC5)
576 #if defined(DECODE_RC6)
583 #if defined(DECODE_LG)
590 #if defined(DECODE_JVC)
597 #if defined(DECODE_SAMSUNG)
607 #if defined(DECODE_BEO)
614 #if defined(DECODE_FAST)
621 #if defined(DECODE_WHYNTER)
628 #if defined(DECODE_LEGO_PF)
635 #if defined(DECODE_BOSEWAVE)
642 #if defined(DECODE_MAGIQUEST)
652 #if defined(DECODE_DISTANCE_WIDTH)
662 #if defined(DECODE_HASH)
703 uint16_t aOneSpaceMicros, uint16_t aZeroMarkMicros,
bool aMSBfirst) {
707 bool isPulseDistanceProtocol = (aOneMarkMicros == aZeroMarkMicros);
712 for (uint_fast8_t i = aNumberOfBits; i > 0; i--) {
714 unsigned int tMarkTicks;
715 unsigned int tSpaceTicks;
718 if (isPulseDistanceProtocol) {
724 tSpaceTicks = *tRawBufPointer++;
725 tBitValue =
matchSpace(tSpaceTicks, aOneSpaceMicros);
732 tMarkTicks = *tRawBufPointer++;
733 tBitValue =
matchMark(tMarkTicks, aOneMarkMicros);
746 tDecodedData |= tMask;
763 uint16_t aZeroMarkMicros, uint16_t aOneSpaceMicros, uint16_t aZeroSpaceMicros,
bool aMSBfirst) {
765 (void) aZeroSpaceMicros;
767 bool isPulseDistanceProtocol = (aOneMarkMicros == aZeroMarkMicros);
772 for (uint_fast8_t i = aNumberOfBits; i > 0; i--) {
774 unsigned int tMarkTicks;
775 unsigned int tSpaceTicks;
778 if (isPulseDistanceProtocol) {
783 (void) aZeroSpaceMicros;
785 tSpaceTicks = *tRawBufPointer++;
786 tBitValue =
matchSpace(tSpaceTicks, aOneSpaceMicros);
791 tMarkTicks = *tRawBufPointer++;
792 tBitValue =
matchMark(tMarkTicks, aOneMarkMicros);
805 tDecodedData |= tMask;
828 uint16_t aZeroMarkMicros, uint16_t aOneSpaceMicros, uint16_t aZeroSpaceMicros,
bool aMSBfirst) {
832 bool isPulseDistanceProtocol = (aOneMarkMicros == aZeroMarkMicros);
837 for (uint_fast8_t i = aNumberOfBits; i > 0; i--) {
839 unsigned int tMarkTicks;
840 unsigned int tSpaceTicks;
843 if (isPulseDistanceProtocol) {
847 tMarkTicks = *tRawBufPointer++;
848 tSpaceTicks = *tRawBufPointer++;
849 tBitValue =
matchSpace(tSpaceTicks, aOneSpaceMicros);
852 if (!
matchMark(tMarkTicks, aOneMarkMicros)) {
853 #if defined(LOCAL_DEBUG)
854 Serial.print(F(
"Mark="));
856 Serial.print(F(
" is not "));
857 Serial.print(aOneMarkMicros);
858 Serial.print(F(
". Index="));
859 Serial.print(aNumberOfBits - i);
871 tMarkTicks = *tRawBufPointer++;
872 tBitValue =
matchMark(tMarkTicks, aOneMarkMicros);
873 tSpaceTicks = *tRawBufPointer++;
885 tDecodedData |= tMask;
893 if (isPulseDistanceProtocol) {
894 if (!
matchSpace(tSpaceTicks, aZeroSpaceMicros)) {
895 #if defined(LOCAL_DEBUG)
896 Serial.print(F(
"Space="));
898 Serial.print(F(
" is not "));
899 Serial.print(aOneSpaceMicros);
900 Serial.print(F(
" or "));
901 Serial.print(aZeroSpaceMicros);
902 Serial.print(F(
". Index="));
903 Serial.print(aNumberOfBits - i);
909 if (!
matchMark(tMarkTicks, aZeroMarkMicros)) {
910 #if defined(LOCAL_DEBUG)
911 Serial.print(F(
"Mark="));
913 Serial.print(F(
" is not "));
914 Serial.print(aOneMarkMicros);
915 Serial.print(F(
" or "));
916 Serial.print(aZeroMarkMicros);
917 Serial.print(F(
". Index="));
918 Serial.print(aNumberOfBits - i);
928 if (aZeroSpaceMicros == aOneSpaceMicros
931 if (!
matchSpace(tSpaceTicks, aOneSpaceMicros)) {
932 #if defined(LOCAL_DEBUG)
933 Serial.print(F(
"Space="));
935 Serial.print(F(
" is not "));
936 Serial.print(aOneSpaceMicros);
937 Serial.print(F(
". Index="));
938 Serial.print(aNumberOfBits - i);
959 aProtocolConstants->
Flags);
993 uint_fast8_t tLevelOfCurrentInterval;
1031 return tLevelOfCurrentInterval;
1037 #define FNV_PRIME_32 16777619
1038 #define FNV_BASIS_32 2166136261
1046 if (newval * 10 < oldval * 8) {
1049 if (oldval * 10 < newval * 8) {
1101 if (aResults->
rawlen < 6) {
1105 for (uint8_t i = 3; i < aResults->
rawlen; i++) {
1111 aResults->
value = hash;
1112 aResults->
bits = 32;
1129 #if defined(LOCAL_TRACE)
1131 Serial.println(F(
": Header mark length is wrong"));
1136 #if defined(LOCAL_TRACE)
1138 Serial.println(F(
": Header space length is wrong"));
1152 #
if defined(ENABLE_FULL_REPEAT_CHECK)
1165 bool matchTicks(uint16_t aMeasuredTicks, uint16_t aMatchValueMicros) {
1166 #if defined(LOCAL_TRACE)
1167 Serial.print(F(
"Testing: "));
1168 Serial.print(
TICKS_LOW(aMatchValueMicros), DEC);
1169 Serial.print(F(
" <= "));
1170 Serial.print(aMeasuredTicks, DEC);
1171 Serial.print(F(
" <= "));
1172 Serial.print(
TICKS_HIGH(aMatchValueMicros), DEC);
1174 bool passed = ((aMeasuredTicks >=
TICKS_LOW(aMatchValueMicros)) && (aMeasuredTicks <=
TICKS_HIGH(aMatchValueMicros)));
1175 #if defined(LOCAL_TRACE)
1177 Serial.println(F(
" => passed"));
1179 Serial.println(F(
" => FAILED"));
1185 bool MATCH(uint16_t measured_ticks, uint16_t desired_us) {
1186 return matchTicks(measured_ticks, desired_us);
1193 bool matchMark(uint16_t aMeasuredTicks, uint16_t aMatchValueMicros) {
1194 #if defined(LOCAL_TRACE)
1195 Serial.print(F(
"Testing mark (actual vs desired): "));
1197 Serial.print(F(
"us vs "));
1198 Serial.print(aMatchValueMicros, DEC);
1199 Serial.print(F(
"us: "));
1201 Serial.print(F(
" <= "));
1203 Serial.print(F(
" <= "));
1209 #if defined(LOCAL_TRACE)
1211 Serial.println(F(
" => passed"));
1213 Serial.println(F(
" => FAILED"));
1220 return matchMark(measured_ticks, desired_us);
1227 bool matchSpace(uint16_t aMeasuredTicks, uint16_t aMatchValueMicros) {
1228 #if defined(LOCAL_TRACE)
1229 Serial.print(F(
"Testing space (actual vs desired): "));
1231 Serial.print(F(
"us vs "));
1232 Serial.print(aMatchValueMicros, DEC);
1233 Serial.print(F(
"us: "));
1235 Serial.print(F(
" <= "));
1237 Serial.print(F(
" <= "));
1243 #if defined(LOCAL_TRACE)
1245 Serial.println(F(
" => passed"));
1247 Serial.println(F(
" => FAILED"));
1254 return matchSpace(measured_ticks, desired_us);
1277 aSerial->print(F(
"Space of "));
1279 aSerial->print(F(
" us between two detected transmission is smaller than the minimal gap of "));
1281 aSerial->println(F(
" us known for implemented protocols like NEC, Sony, RC% etc.."));
1282 aSerial->println(F(
"But it can be OK for some yet unsupported protocols, and especially for repeats."));
1283 aSerial->println(F(
"If you get unexpected results, try to increase the RECORD_GAP_MICROS in IRremote.h."));
1299 #if defined(DECODE_ONKYO)
1300 aSerial->print(F(
"Onkyo, "));
1301 #elif defined(DECODE_NEC)
1302 aSerial->print(F(
"NEC/NEC2/Onkyo/Apple, "));
1304 #if defined(DECODE_PANASONIC) || defined(DECODE_KASEIKYO)
1305 aSerial->print(F(
"Panasonic/Kaseikyo, "));
1307 #if defined(DECODE_DENON)
1308 aSerial->print(F(
"Denon/Sharp, "));
1310 #if defined(DECODE_SONY)
1311 aSerial->print(F(
"Sony, "));
1313 #if defined(DECODE_RC5)
1314 aSerial->print(F(
"RC5, "));
1316 #if defined(DECODE_RC6)
1317 aSerial->print(F(
"RC6, "));
1319 #if defined(DECODE_LG)
1320 aSerial->print(F(
"LG, "));
1322 #if defined(DECODE_JVC)
1323 aSerial->print(F(
"JVC, "));
1325 #if defined(DECODE_SAMSUNG)
1326 aSerial->print(F(
"Samsung, "));
1331 #if defined(DECODE_BEO)
1332 aSerial->print(F(
"Bang & Olufsen, "));
1334 #if defined(DECODE_FAST)
1335 aSerial->print(F(
"FAST, "));
1337 #if defined(DECODE_WHYNTER)
1338 aSerial->print(F(
"Whynter, "));
1340 #if defined(DECODE_LEGO_PF)
1341 aSerial->print(F(
"Lego Power Functions, "));
1343 #if defined(DECODE_BOSEWAVE)
1344 aSerial->print(F(
"Bosewave, "));
1346 #if defined(DECODE_MAGIQUEST)
1347 aSerial->print(F(
"MagiQuest, "));
1349 #if defined(DECODE_DISTANCE_WIDTH)
1350 aSerial->print(F(
"Universal Pulse Distance Width, "));
1352 #if defined(DECODE_HASH)
1353 aSerial->print(F(
"Hash "));
1355 #if defined(NO_DECODER) // for sending raw only
1381 aSerial->print(F(
", "));
1383 aSerial->print(F(
", "));
1385 aSerial->print(F(
", "));
1387 aSerial->print(F(
", "));
1389 aSerial->print(F(
", "));
1398 uint8_t tMaximumTick = 0;
1401 if (tMaximumTick < tTick) {
1402 tMaximumTick = tTick;
1405 return tMaximumTick;
1408 uint8_t tMaximumTick = 0;
1411 if (tMaximumTick < tTick) {
1412 tMaximumTick = tTick;
1415 return tMaximumTick;
1422 uint8_t tMaximumTick = 0;
1424 if (aSearchSpaceInsteadOfMark) {
1431 if (tMaximumTick < tTick) {
1432 tMaximumTick = tTick;
1435 return tMaximumTick;
1439 uint16_t tSumOfDurationTicks = 0;
1459 #if defined(DECODE_DISTANCE_WIDTH)
1460 uint_fast8_t tNumberOfArrayData = 0;
1462 # if __INT_WIDTH__ < 32
1463 aSerial->print(F(
"Send on a 8 bit platform with: "));
1465 if(tNumberOfArrayData > 1) {
1467 aSerial->print(F(
" uint32_t tRawData[]={0x"));
1469 aSerial->print(F(
"Send on a 32 bit platform with: "));
1471 if(tNumberOfArrayData > 1) {
1473 aSerial->print(F(
" uint64_t tRawData[]={0x"));
1475 for (uint_fast8_t i = 0; i < tNumberOfArrayData; ++i) {
1476 # if (__INT_WIDTH__ < 32)
1479 PrintULL::print(aSerial,
decodedIRData.decodedRawDataArray[i], HEX);
1481 if (i != tNumberOfArrayData - 1) {
1482 aSerial->print(F(
", 0x"));
1485 aSerial->println(F(
"};"));
1486 aSerial->print(F(
" "));
1489 aSerial->print(F(
"Send with: "));
1491 aSerial->print(F(
"IrSender.send"));
1494 aSerial->print(F(
"Send with: IrSender.send"));
1497 #if defined(DECODE_DISTANCE_WIDTH)
1501 aSerial->print(F(
"(0x"));
1502 #if defined(DECODE_MAGIQUEST)
1504 # if (__INT_WIDTH__ < 32)
1519 aSerial->print(F(
", 0x"));
1522 aSerial->print(F(
", 2, "));
1525 aSerial->print(F(
", <numberOfRepeats>"));
1528 #if defined(DECODE_DISTANCE_WIDTH)
1533 aSerial->print(
"PulseDistanceWidth");
1534 if(tNumberOfArrayData > 1) {
1535 aSerial->print(
"FromArray(38, ");
1537 aSerial->print(
"(38, ");
1541 if(tNumberOfArrayData > 1) {
1542 aSerial->print(F(
", &tRawData[0], "));
1544 aSerial->print(F(
", 0x"));
1545 # if (__INT_WIDTH__ < 32)
1550 aSerial->print(F(
", "));
1553 aSerial->print(F(
", PROTOCOL_IS_"));
1556 aSerial->print(
'M');
1558 aSerial->print(
'L');
1560 aSerial->print(F(
"SB_FIRST, <RepeatPeriodMillis>, <numberOfRepeats>"));
1563 #if defined(DECODE_PANASONIC) || defined(DECODE_KASEIKYO)
1565 aSerial->print(F(
", 0x"));
1569 aSerial->print(F(
");"));
1581 aSerial->print(F(
"P="));
1584 #if defined(DECODE_HASH)
1585 aSerial->print(F(
" #=0x"));
1586 # if (__INT_WIDTH__ < 32)
1592 aSerial->print(
' ');
1594 aSerial->println(F(
" bits received"));
1599 aSerial->print(F(
" A=0x"));
1602 aSerial->print(F(
" C=0x"));
1605 aSerial->print(F(
" Raw=0x"));
1606 #if (__INT_WIDTH__ < 32)
1613 aSerial->print(F(
" R"));
1627 aSerial->print(F(
"rawData["));
1629 aSerial->println(F(
"]: "));
1634 aSerial->print(F(
" -"));
1635 if (aOutputMicrosecondsInsteadOfTicks) {
1642 uint_fast8_t tCounterForNewline = 6;
1645 #if defined(DECODE_DENON) || defined(DECODE_MAGIQUEST)
1654 tCounterForNewline = 0;
1659 uint16_t tSumOfDurationTicks = 0;
1662 if (aOutputMicrosecondsInsteadOfTicks) {
1665 tDuration = tCurrentTicks;
1667 tSumOfDurationTicks += tCurrentTicks;
1670 aSerial->print(
'-');
1672 aSerial->print(F(
" +"));
1676 if (aOutputMicrosecondsInsteadOfTicks && tDuration < 1000) {
1677 aSerial->print(
' ');
1679 if (aOutputMicrosecondsInsteadOfTicks && tDuration < 100) {
1680 aSerial->print(
' ');
1682 if (tDuration < 10) {
1683 aSerial->print(
' ');
1685 aSerial->print(tDuration, DEC);
1688 aSerial->print(
',');
1691 tCounterForNewline++;
1692 if ((tCounterForNewline % 8) == 0) {
1698 aSerial->print(
"Sum: ");
1699 if (aOutputMicrosecondsInsteadOfTicks) {
1700 aSerial->println((uint32_t) tSumOfDurationTicks *
MICROS_PER_TICK, DEC);
1702 aSerial->println(tSumOfDurationTicks, DEC);
1719 if (aOutputMicrosecondsInsteadOfTicks) {
1720 aSerial->print(F(
"uint16_t rawData["));
1722 aSerial->print(F(
"uint8_t rawTicks["));
1726 aSerial->print(F(
"] = {"));
1739 if (aOutputMicrosecondsInsteadOfTicks) {
1740 aSerial->print(tDuration);
1746 tTicks = (tTicks > UINT8_MAX) ? UINT8_MAX : tTicks;
1747 aSerial->print(tTicks);
1750 if (!(i & 1)) aSerial->print(
' ');
1754 aSerial->print(F(
"};"));
1757 aSerial->print(F(
" // "));
1761 aSerial->println(
"");
1787 *aArrayPtr = (tTicks > UINT8_MAX) ? UINT8_MAX : tTicks;
1803 aSerial->print(F(
"uint16_t"));
1804 aSerial->print(F(
" address = 0x"));
1806 aSerial->println(
';');
1808 aSerial->print(F(
"uint16_t"));
1809 aSerial->print(F(
" command = 0x"));
1811 aSerial->println(
';');
1814 #if __INT_WIDTH__ < 32
1815 aSerial->print(F(
"uint32_t rawData = 0x"));
1817 aSerial->print(F(
"uint64_t rawData = 0x"));
1819 #if (__INT_WIDTH__ < 32)
1824 aSerial->println(
';');
1829 #if defined(__AVR__)
1867 aResults->
value = 0;
1871 #if defined(DECODE_NEC)
1878 #if defined(DECODE_SONY)
1885 #if defined(DECODE_RC5)
1896 #if defined(DECODE_RC6)
1908 #if defined(DECODE_LG)
1913 #if defined(DECODE_JVC)
1920 #if defined(DECODE_SAMSUNG)
1927 #if defined(DECODE_DENON)
1946 #if defined(_IR_MEASURE_TIMING)
1947 #undef _IR_MEASURE_TIMING
1949 #if defined(LOCAL_TRACE)
1952 #if defined(LOCAL_DEBUG)
1955 #endif // _IR_RECEIVE_HPP