|
| 1 | +/* |
| 2 | + * IRReceive.hpp |
| 3 | + * This file is exclusively included by IRremote.h to enable easy configuration of library switches |
| 4 | + * |
| 5 | + * Contains all protocol functions used by receiver and sender. |
| 6 | + * |
| 7 | + * This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote. |
| 8 | + * |
| 9 | + ************************************************************************************ |
| 10 | + * MIT License |
| 11 | + * |
| 12 | + * Copyright (c) 2009-2022 Ken Shirriff, Rafi Khan, Armin Joachimsmeyer |
| 13 | + * |
| 14 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 15 | + * of this software and associated documentation files (the "Software"), to deal |
| 16 | + * in the Software without restriction, including without limitation the rights |
| 17 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 18 | + * copies of the Software, and to permit persons to whom the Software is furnished |
| 19 | + * to do so, subject to the following conditions: |
| 20 | + * |
| 21 | + * The above copyright notice and this permission notice shall be included in all |
| 22 | + * copies or substantial portions of the Software. |
| 23 | + * |
| 24 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 25 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 26 | + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 27 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 28 | + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 29 | + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 30 | + * |
| 31 | + ************************************************************************************ |
| 32 | + */ |
| 33 | +#ifndef _IR_PROTOCOL_HPP |
| 34 | +#define _IR_PROTOCOL_HPP |
| 35 | + |
| 36 | +#if defined(DEBUG) && !defined(LOCAL_DEBUG) |
| 37 | +#define LOCAL_DEBUG |
| 38 | +#else |
| 39 | +//#define LOCAL_DEBUG // This enables debug output only for this file |
| 40 | +#endif |
| 41 | + |
| 42 | +/* |
| 43 | + * Check for additional characteristics of timing like length of mark for a constant mark protocol, |
| 44 | + * where space length determines the bit value. Requires up to 194 additional bytes of program memory. |
| 45 | + */ |
| 46 | +//#define DECODE_STRICT_CHECKS |
| 47 | +/** \addtogroup Receiving Receiving IR data for multiple protocols |
| 48 | + * @{ |
| 49 | + */ |
| 50 | + |
| 51 | +/* |
| 52 | + * !!Must be the same order as in decode_type_t in IRProtocol.h!!! |
| 53 | + */ |
| 54 | +const char *const ProtocolNames[] |
| 55 | +PROGMEM = { string_Unknown, string_PulseWidth, string_PulseDistance, string_Apple, string_Denon, string_JVC, string_LG, string_LG2, |
| 56 | + string_NEC, string_NEC2, string_Onkyo, string_Panasonic, string_Kaseikyo, string_Kaseikyo_Denon, string_Kaseikyo_Sharp, |
| 57 | + string_Kaseikyo_JVC, string_Kaseikyo_Mitsubishi, string_RC5, string_RC6, string_Samsung, string_SamsungLG, string_Sharp, |
| 58 | + string_Sony |
| 59 | +#if !defined(EXCLUDE_EXOTIC_PROTOCOLS) |
| 60 | + , string_BangOlufsen, string_BoseWave, string_Lego, string_MagiQuest, string_Whynter |
| 61 | +#endif |
| 62 | + }; |
| 63 | + |
| 64 | +#if defined(__AVR__) |
| 65 | +const __FlashStringHelper* getProtocolString(decode_type_t aProtocol) { |
| 66 | + const char *tProtocolStringPtr = (char*) pgm_read_word(&ProtocolNames[aProtocol]); |
| 67 | + return ((__FlashStringHelper*) (tProtocolStringPtr)); |
| 68 | +} |
| 69 | +#else |
| 70 | +const char* getProtocolString(decode_type_t aProtocol) { |
| 71 | + return ProtocolNames[aProtocol]; |
| 72 | +} |
| 73 | +#endif |
| 74 | + |
| 75 | +/** |
| 76 | + * Function to print decoded result and flags in one line. |
| 77 | + * A static function to be able to print data to send or copied received data. |
| 78 | + * Ends with println(). |
| 79 | + * |
| 80 | + * @param aSerial The Print object on which to write, for Arduino you can use &Serial. |
| 81 | + * @param aIRDataPtr Pointer to the data to be printed. |
| 82 | + * @param aPrintRepeatGap If true also print the gap before repeats. |
| 83 | + * @param aCheckForRecordGapsMicros If true, call CheckForRecordGapsMicros() which may do a long printout, |
| 84 | + * which in turn may block the proper detection of repeats. |
| 85 | + * @return true, if CheckForRecordGapsMicros() has printed a message, i.e. gap < |
| 86 | + * |
| 87 | + */ |
| 88 | +void printIRResultShort(Print *aSerial, IRData *aIRDataPtr, bool aPrintRepeatGap) { |
| 89 | + aSerial->print(F("Protocol=")); |
| 90 | + aSerial->print(getProtocolString(aIRDataPtr->protocol)); |
| 91 | + if (aIRDataPtr->protocol == UNKNOWN) { |
| 92 | +#if defined(DECODE_HASH) |
| 93 | + aSerial->print(F(" Hash=0x")); |
| 94 | + aSerial->print(aIRDataPtr->decodedRawData, HEX); |
| 95 | +#endif |
| 96 | +#if !defined(DISABLE_CODE_FOR_RECEIVER) |
| 97 | + aSerial->print(' '); |
| 98 | + aSerial->print((aIRDataPtr->rawDataPtr->rawlen + 1) / 2, DEC); |
| 99 | + aSerial->println(F(" bits (incl. gap and start) received")); |
| 100 | +#endif |
| 101 | + } else { |
| 102 | +#if defined(DECODE_DISTANCE_WIDTH) |
| 103 | + if (aIRDataPtr->protocol != PULSE_DISTANCE && aIRDataPtr->protocol != PULSE_WIDTH) { |
| 104 | +#endif |
| 105 | + /* |
| 106 | + * New decoders have address and command |
| 107 | + */ |
| 108 | + aSerial->print(F(" Address=0x")); |
| 109 | + aSerial->print(aIRDataPtr->address, HEX); |
| 110 | + |
| 111 | + aSerial->print(F(" Command=0x")); |
| 112 | + aSerial->print(aIRDataPtr->command, HEX); |
| 113 | + |
| 114 | + if (aIRDataPtr->flags & IRDATA_FLAGS_EXTRA_INFO) { |
| 115 | + aSerial->print(F(" Extra=0x")); |
| 116 | + aSerial->print(aIRDataPtr->extra, HEX); |
| 117 | + } |
| 118 | + |
| 119 | + if (aIRDataPtr->flags & IRDATA_FLAGS_PARITY_FAILED) { |
| 120 | + aSerial->print(F(" Parity fail")); |
| 121 | + } |
| 122 | + |
| 123 | + if (aIRDataPtr->flags & IRDATA_FLAGS_TOGGLE_BIT) { |
| 124 | + if (aIRDataPtr->protocol == NEC) { |
| 125 | + aSerial->print(F(" Special repeat")); |
| 126 | + } else { |
| 127 | + aSerial->print(F(" Toggle=1")); |
| 128 | + } |
| 129 | + } |
| 130 | +#if defined(DECODE_DISTANCE_WIDTH) |
| 131 | + } |
| 132 | +#endif |
| 133 | + if (aIRDataPtr->flags & (IRDATA_FLAGS_IS_AUTO_REPEAT | IRDATA_FLAGS_IS_REPEAT)) { |
| 134 | + aSerial->print(' '); |
| 135 | + if (aIRDataPtr->flags & IRDATA_FLAGS_IS_AUTO_REPEAT) { |
| 136 | + aSerial->print(F("Auto-")); |
| 137 | + } |
| 138 | + aSerial->print(F("Repeat")); |
| 139 | +#if !defined(DISABLE_CODE_FOR_RECEIVER) |
| 140 | + if (aPrintRepeatGap) { |
| 141 | + aSerial->print(F(" gap=")); |
| 142 | + aSerial->print((uint32_t) aIRDataPtr->rawDataPtr->rawbuf[0] * MICROS_PER_TICK); |
| 143 | + aSerial->print(F("us")); |
| 144 | + } |
| 145 | +#else |
| 146 | + (void)aPrintRepeatGap; |
| 147 | +#endif |
| 148 | + } |
| 149 | + |
| 150 | + /* |
| 151 | + * Print raw data |
| 152 | + */ |
| 153 | + if (!(aIRDataPtr->flags & IRDATA_FLAGS_IS_REPEAT) || aIRDataPtr->decodedRawData != 0) { |
| 154 | + aSerial->print(F(" Raw-Data=0x")); |
| 155 | + aSerial->print(aIRDataPtr->decodedRawData, HEX); |
| 156 | + |
| 157 | + /* |
| 158 | + * Print number of bits processed |
| 159 | + */ |
| 160 | + aSerial->print(' '); |
| 161 | + aSerial->print(aIRDataPtr->numberOfBits, DEC); |
| 162 | + aSerial->print(F(" bits")); |
| 163 | + |
| 164 | + if (aIRDataPtr->flags & IRDATA_FLAGS_IS_MSB_FIRST) { |
| 165 | + aSerial->println(F(" MSB first")); |
| 166 | + } else { |
| 167 | + aSerial->println(F(" LSB first")); |
| 168 | + } |
| 169 | + |
| 170 | + } else { |
| 171 | + aSerial->println(); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +/********************************************************************************************************************** |
| 177 | + * Function to bit reverse OLD MSB values of e.g. NEC. |
| 178 | + **********************************************************************************************************************/ |
| 179 | +uint8_t bitreverseOneByte(uint8_t aValue) { |
| 180 | +// uint8_t tReversedValue; |
| 181 | +// return __builtin_avr_insert_bits(0x01234567, aValue, tReversedValue); |
| 182 | +// 76543210 |
| 183 | + aValue = (aValue >> 4) | (aValue << 4); // Swap in groups of 4 |
| 184 | +// 32107654 |
| 185 | + aValue = ((aValue & 0xcc) >> 2) | ((aValue & 0x33) << 2); // Swap in groups of 2 |
| 186 | +// 10325476 |
| 187 | + aValue = ((aValue & 0xaa) >> 1) | ((aValue & 0x55) << 1); // Swap bit pairs |
| 188 | +// 01234567 |
| 189 | + return aValue; |
| 190 | +} |
| 191 | + |
| 192 | +uint32_t bitreverse32Bit(uint32_t aInput) { |
| 193 | +// __builtin_avr_insert_bits(); |
| 194 | + LongUnion tValue; |
| 195 | + tValue.UByte.HighByte = bitreverseOneByte(aInput); |
| 196 | + tValue.UByte.MidHighByte = bitreverseOneByte(aInput >> 8); |
| 197 | + tValue.UByte.MidLowByte = bitreverseOneByte(aInput >> 16); |
| 198 | + tValue.UByte.LowByte = bitreverseOneByte(aInput >> 24); |
| 199 | + return tValue.ULong; |
| 200 | +} |
| 201 | + |
| 202 | +/** @}*/ |
| 203 | + |
| 204 | +#if defined(LOCAL_DEBUG) |
| 205 | +#undef LOCAL_DEBUG |
| 206 | +#endif |
| 207 | +#endif // _IR_PROTOCOL_HPP |
0 commit comments