| 1 | /* IRLibDecodeBase.cpp |
|---|
| 2 | * Part of IRLib Library for Arduino receiving, decoding, and sending |
|---|
| 3 | * infrared signals. See COPYRIGHT.txt and LICENSE.txt for more information. |
|---|
| 4 | */ |
|---|
| 5 | /* |
|---|
| 6 | * This module contains the base classes for decoding. You will not create instances |
|---|
| 7 | * of these classes, rather you will use them as base classes in creating derived |
|---|
| 8 | * protocol specific decoders. |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | #if !defined(ARDUINO_CIRCUITPLAY_NRF52840) |
|---|
| 12 | |
|---|
| 13 | #include "IRLibDecodeBase.h" |
|---|
| 14 | #include "IRLibHardware.h" |
|---|
| 15 | |
|---|
| 16 | IRdecodeBase::IRdecodeBase(void) { |
|---|
| 17 | recvGlobal.decoderWantsData=false; //turned on by enableIRIn. |
|---|
| 18 | recvGlobal.decodeBuffer=recvGlobal.recvBuffer;//default buffer |
|---|
| 19 | ignoreHeader=false; |
|---|
| 20 | resetDecoder(); |
|---|
| 21 | }; |
|---|
| 22 | |
|---|
| 23 | /* |
|---|
| 24 | * Reinitialize the decoder clearing out previous data. |
|---|
| 25 | */ |
|---|
| 26 | void IRdecodeBase::resetDecoder(void) { |
|---|
| 27 | protocolNum= UNKNOWN; |
|---|
| 28 | value=0; |
|---|
| 29 | address=0; |
|---|
| 30 | bits=0; |
|---|
| 31 | }; |
|---|
| 32 | void IRdecodeBase::dumpResults(bool verbose) { |
|---|
| 33 | int i;uint32_t Extent;int interval; |
|---|
| 34 | if((protocolNum>89) || (protocolNum<=LAST_PROTOCOL)) { |
|---|
| 35 | Serial.print(F("Decoded ")); Serial.print(Pnames(protocolNum)); |
|---|
| 36 | Serial.print(F("(")); Serial.print(protocolNum,DEC); |
|---|
| 37 | Serial.print(F("): Value:")); Serial.print(value, HEX); |
|---|
| 38 | Serial.print(F(" Adrs:" )); Serial.print(address, HEX); |
|---|
| 39 | }; |
|---|
| 40 | Serial.print(F(" (")); Serial.print(bits, DEC); Serial.print(F(" bits) ")); |
|---|
| 41 | if(recvGlobal.didAutoResume) Serial.print(F("Auto Resumed")); |
|---|
| 42 | Serial.println(); |
|---|
| 43 | if(!verbose) |
|---|
| 44 | return; |
|---|
| 45 | Serial.print(F("Raw samples(")); Serial.print(recvGlobal.decodeLength, DEC); |
|---|
| 46 | Serial.print(F("): Gap:")); Serial.println(recvGlobal.decodeBuffer[0], DEC); |
|---|
| 47 | Serial.print(F(" Head: m")); Serial.print(recvGlobal.decodeBuffer[1], DEC); |
|---|
| 48 | Serial.print(F(" s")); Serial.println(recvGlobal.decodeBuffer[2], DEC); |
|---|
| 49 | int LowSpace= 32767; int LowMark= 32767; |
|---|
| 50 | int HiSpace=0; int HiMark= 0; |
|---|
| 51 | Extent=recvGlobal.decodeBuffer[1]+recvGlobal.decodeBuffer[2]; |
|---|
| 52 | for (i = 3; i < recvGlobal.decodeLength; i++) { |
|---|
| 53 | Extent+=(interval= recvGlobal.decodeBuffer[i]); |
|---|
| 54 | if (i % 2) { |
|---|
| 55 | LowMark=min(LowMark, interval); HiMark=max(HiMark, interval); |
|---|
| 56 | Serial.print(i/2-1,DEC); Serial.print(F(":m")); |
|---|
| 57 | } |
|---|
| 58 | else { |
|---|
| 59 | if(interval>0)LowSpace=min(LowSpace, interval); HiSpace=max (HiSpace, interval); |
|---|
| 60 | Serial.print(F(" s")); |
|---|
| 61 | } |
|---|
| 62 | Serial.print(interval, DEC); |
|---|
| 63 | int j=i-1; |
|---|
| 64 | if ((j % 2)==1)Serial.print(F("\t")); |
|---|
| 65 | if ((j % 4)==1)Serial.print(F("\t ")); |
|---|
| 66 | if ((j % 8)==1)Serial.println(); |
|---|
| 67 | if ((j % 32)==1)Serial.println(); |
|---|
| 68 | } |
|---|
| 69 | Serial.println(); |
|---|
| 70 | Serial.print(F("Extent=")); Serial.println(Extent,DEC); |
|---|
| 71 | Serial.print(F("Mark min:")); Serial.print(LowMark,DEC);Serial.print(F("\t max:")); Serial.println(HiMark,DEC); |
|---|
| 72 | Serial.print(F("Space min:")); Serial.print(LowSpace,DEC);Serial.print(F("\t max:")); Serial.println(HiSpace,DEC); |
|---|
| 73 | Serial.println(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /* We use a generic routine because most protocols have the same basic structure. |
|---|
| 77 | * Previous versions of this method would handle protocols with variable marks |
|---|
| 78 | * or variable spaces. However we have discovered that only Sony protocol uses |
|---|
| 79 | * variable marks so we have stripped out that portion of the code. This changes |
|---|
| 80 | * the number of necessary parameters. We no longer need markOne and markZero |
|---|
| 81 | * because they are both the same which we will pass in markData. Note this new |
|---|
| 82 | * version will handle up to 48 bits putting the most significant 16 bits in |
|---|
| 83 | * "this.address" in the least significant 32 bits in "this.data". We could have |
|---|
| 84 | * allowed for 64 bit but we have not seen generic protocols that large. |
|---|
| 85 | */ |
|---|
| 86 | bool IRdecodeBase::decodeGeneric(uint8_t expectedLength, |
|---|
| 87 | uint16_t headMark, uint16_t headSpace, uint16_t markData, |
|---|
| 88 | uint16_t spaceOne, uint16_t spaceZero) { |
|---|
| 89 | resetDecoder();//This used to be in the receiver getResults. |
|---|
| 90 | // If "expectedLenght" or "headMark" or "headSpace" are zero or if "ignoreHeader" |
|---|
| 91 | // is true then don't perform these tests. This is because some protocols need |
|---|
| 92 | // to do their own custom header work. |
|---|
| 93 | uint64_t data = 0; |
|---|
| 94 | bufIndex_t Max=recvGlobal.decodeLength-1; |
|---|
| 95 | if (expectedLength) { |
|---|
| 96 | if (recvGlobal.decodeLength != expectedLength) return RAW_COUNT_ERROR; |
|---|
| 97 | } |
|---|
| 98 | if(!ignoreHeader) { |
|---|
| 99 | if (headMark) { |
|---|
| 100 | if (!MATCH(recvGlobal.decodeBuffer[1],headMark)) return HEADER_MARK_ERROR(headMark); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | if (headSpace) { |
|---|
| 104 | if (!MATCH(recvGlobal.decodeBuffer[2],headSpace)) return HEADER_SPACE_ERROR(headSpace); |
|---|
| 105 | } |
|---|
| 106 | offset=3;//skip initial gap plus two header items |
|---|
| 107 | while (offset < Max) { |
|---|
| 108 | if (!MATCH (recvGlobal.decodeBuffer[offset],markData)) return DATA_MARK_ERROR(markData); |
|---|
| 109 | offset++; |
|---|
| 110 | if (MATCH(recvGlobal.decodeBuffer[offset],spaceOne)) { |
|---|
| 111 | data = (data << 1) | 1; |
|---|
| 112 | } |
|---|
| 113 | else if (MATCH (recvGlobal.decodeBuffer[offset],spaceZero)) { |
|---|
| 114 | data <<= 1; |
|---|
| 115 | } |
|---|
| 116 | else return DATA_SPACE_ERROR(spaceZero); |
|---|
| 117 | offset++; |
|---|
| 118 | } |
|---|
| 119 | bits = (offset - 1) / 2 -1;//didn't encode stop bit |
|---|
| 120 | // Success |
|---|
| 121 | value = (uint32_t)data; //low order 32 bits |
|---|
| 122 | address = (uint16_t) (data>>32); //high order 16 bits |
|---|
| 123 | return true; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /* |
|---|
| 127 | * These MATCH methods used to be macros but we saved nearly |
|---|
| 128 | * 800 bytes of program space by making them actual methods. |
|---|
| 129 | */ |
|---|
| 130 | |
|---|
| 131 | bool IRdecodeBase::MATCH(int16_t val,int16_t expected){ |
|---|
| 132 | #ifdef IRLIB_USE_PERCENT |
|---|
| 133 | return (val >= (uint16_t)(expected*(1.0-PERCENT_TOLERANCE/100.0))) |
|---|
| 134 | && (val <= (uint16_t)(expected*(1.0+PERCENT_TOLERANCE/100.0))); |
|---|
| 135 | #else |
|---|
| 136 | return ABS_MATCH(val,expected,DEFAULT_ABS_TOLERANCE); |
|---|
| 137 | #endif |
|---|
| 138 | } |
|---|
| 139 | bool IRdecodeBase::ABS_MATCH(int16_t val,int16_t expected,int16_t tolerance){ |
|---|
| 140 | return (val >= (expected-tolerance)) && (val <= (expected+tolerance)); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /* |
|---|
| 144 | * The RC5 and RC6 and similar protocols used phase encoding and leave a |
|---|
| 145 | * routine to extract zeros and ones. This routine gets one undecoded |
|---|
| 146 | * level at a time from the raw buffer. personally The RC5/6 decoding |
|---|
| 147 | * is easier if the data is broken into time intervals. |
|---|
| 148 | * E.g. if the buffer has MARK for 2 time intervals and SPACE for 1, |
|---|
| 149 | * successive calls to getRClevel will return MARK, MARK, SPACE. |
|---|
| 150 | * The variables "offset" and "used" are updated to keep track of the |
|---|
| 151 | * current position. The variable "t1" is the time interval for a single |
|---|
| 152 | * bit in microseconds. Returns ERROR if the measured time interval is |
|---|
| 153 | * not a multiple of "t1". |
|---|
| 154 | */ |
|---|
| 155 | IRdecodeRC::RCLevel IRdecodeRC::getRClevel(uint8_t *used, const uint16_t t1) { |
|---|
| 156 | if (offset >= recvGlobal.decodeLength) { |
|---|
| 157 | // After end of recorded buffer, assume SPACE. |
|---|
| 158 | return SPACE; |
|---|
| 159 | } |
|---|
| 160 | uint16_t width = recvGlobal.decodeBuffer[offset]; |
|---|
| 161 | IRdecodeRC::RCLevel val; |
|---|
| 162 | if ((offset) % 2) val=MARK; else val=SPACE; |
|---|
| 163 | uint8_t avail; |
|---|
| 164 | if (MATCH(width, t1)) { |
|---|
| 165 | avail = 1; |
|---|
| 166 | } |
|---|
| 167 | else if (MATCH(width, 2*t1)) { |
|---|
| 168 | avail = 2; |
|---|
| 169 | } |
|---|
| 170 | else if (MATCH(width, 3*t1)) { |
|---|
| 171 | avail = 3; |
|---|
| 172 | } else { |
|---|
| 173 | if((ignoreHeader) && (offset==1) && (width<t1)){ |
|---|
| 174 | avail =1; |
|---|
| 175 | } else { |
|---|
| 176 | return ERROR; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | (*used)++; |
|---|
| 180 | if (*used >= avail) { |
|---|
| 181 | *used = 0; |
|---|
| 182 | (offset)++; |
|---|
| 183 | } |
|---|
| 184 | return val; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | #endif //!defined(__NRF52) |
|---|