src/ir_NEC.cpp
//+=============================================================================
// NECs have a repeat only 4 items long
//
/*
 * First check for right data length
 * Next check start bit
 * Next try the decode
 * Last check stop bit
 *
 * Decodes also Apple
 */
NECs have a repeat only 4 items long

First check for right data length
Next check start bit
Next try the decode
Last check stop bit

Decodes also Apple
NECs have a repeat only 4 items long First check for right data length Next check start bit Next try the decode Last check stop bit Decodes also Apple
bool IRrecv::decodeNEC() {
    if (decodedIRData.rawDataPtr->rawlen != ((2 * NEC_BITS) + 4) && (decodedIRData.rawDataPtr->rawlen != 4)) {
        DEBUG_PRINT(F("NEC: "));
        DEBUG_PRINT("Data length=");
        DEBUG_PRINT(decodedIRData.rawDataPtr->rawlen);
        DEBUG_PRINTLN(" is not 68 or 4");
        return false;
    }
    if (!matchMark(decodedIRData.rawDataPtr->rawbuf[1], NEC_HEADER_MARK)) {
        return false;
    }
    if (decodedIRData.rawDataPtr->rawlen == 4) {
        if (matchSpace(decodedIRData.rawDataPtr->rawbuf[2], NEC_REPEAT_HEADER_SPACE)
                && matchMark(decodedIRData.rawDataPtr->rawbuf[3], NEC_BIT_MARK)) {
            decodedIRData.flags = IRDATA_FLAGS_IS_REPEAT | IRDATA_FLAGS_IS_LSB_FIRST;
            decodedIRData.address = lastDecodedAddress;
            decodedIRData.command = lastDecodedCommand;
            decodedIRData.protocol = lastDecodedProtocol;
            return true;
        }
        return false;
    }
    if (!matchSpace(decodedIRData.rawDataPtr->rawbuf[2], NEC_HEADER_SPACE)) {
        DEBUG_PRINT(F("NEC: "));
        DEBUG_PRINTLN(F("Header space length is wrong"));
        return false;
    }
    if (!decodePulseDistanceData(NEC_BITS, 3, NEC_BIT_MARK, NEC_ONE_SPACE, NEC_ZERO_SPACE, PROTOCOL_IS_LSB_FIRST)) {
        DEBUG_PRINT(F("NEC: "));
        DEBUG_PRINTLN(F("Decode failed"));
        return false;
    }
    if (!matchMark(decodedIRData.rawDataPtr->rawbuf[3 + (2 * NEC_BITS)], NEC_BIT_MARK)) {
        DEBUG_PRINT(F("NEC: "));
        DEBUG_PRINTLN(F("Stop bit mark length is wrong"));
        return false;
    }
    LongUnion tValue;
    tValue.ULong = decodedIRData.decodedRawData;
    decodedIRData.command = tValue.UByte.MidHighByte; 
    if (tValue.UWord.LowWord == APPLE_ADDRESS) {
        decodedIRData.protocol = APPLE;
        decodedIRData.address = tValue.UByte.HighByte;
    } else {
        if (tValue.UByte.LowByte == (uint8_t) (~tValue.UByte.MidLowByte)) {
            decodedIRData.address = tValue.UByte.LowByte; 
        } else {
            decodedIRData.address = tValue.UWord.LowWord; 
        }
        if (tValue.UByte.MidHighByte == (uint8_t) (~tValue.UByte.HighByte)) {
            decodedIRData.protocol = NEC;
        } else {
            decodedIRData.protocol = ONKYO;
            decodedIRData.command = tValue.UWord.HighWord; 
        }
    }
    decodedIRData.numberOfBits = NEC_BITS;
    return true;
}
bool IRrecv :: decodeNEC ( ) { if ( decodedIRData . rawDataPtr -> rawlen != ( ( 2 * NEC_BITS ) + 4 ) && ( decodedIRData . rawDataPtr -> rawlen != 4 ) ) { DEBUG_PRINT ( F ( " " ) ) ; DEBUG_PRINT ( " " ) ; DEBUG_PRINT ( decodedIRData . rawDataPtr -> rawlen ) ; DEBUG_PRINTLN ( " " ) ; return false ; } if ( ! matchMark ( decodedIRData . rawDataPtr -> rawbuf [ 1 ] , NEC_HEADER_MARK ) ) { return false ; } if ( decodedIRData . rawDataPtr -> rawlen == 4 ) { if ( matchSpace ( decodedIRData . rawDataPtr -> rawbuf [ 2 ] , NEC_REPEAT_HEADER_SPACE ) && matchMark ( decodedIRData . rawDataPtr -> rawbuf [ 3 ] , NEC_BIT_MARK ) ) { decodedIRData . flags = IRDATA_FLAGS_IS_REPEAT | IRDATA_FLAGS_IS_LSB_FIRST ; decodedIRData . address = lastDecodedAddress ; decodedIRData . command = lastDecodedCommand ; decodedIRData . protocol = lastDecodedProtocol ; return true ; } return false ; } if ( ! matchSpace ( decodedIRData . rawDataPtr -> rawbuf [ 2 ] , NEC_HEADER_SPACE ) ) { DEBUG_PRINT ( F ( " " ) ) ; DEBUG_PRINTLN ( F ( " " ) ) ; return false ; } if ( ! decodePulseDistanceData ( NEC_BITS , 3 , NEC_BIT_MARK , NEC_ONE_SPACE , NEC_ZERO_SPACE , PROTOCOL_IS_LSB_FIRST ) ) { DEBUG_PRINT ( F ( " " ) ) ; DEBUG_PRINTLN ( F ( " " ) ) ; return false ; } if ( ! matchMark ( decodedIRData . rawDataPtr -> rawbuf [ 3 + ( 2 * NEC_BITS ) ] , NEC_BIT_MARK ) ) { DEBUG_PRINT ( F ( " " ) ) ; DEBUG_PRINTLN ( F ( " " ) ) ; return false ; } LongUnion tValue ; tValue . ULong = decodedIRData . decodedRawData ; decodedIRData . command = tValue . UByte . MidHighByte ; if ( tValue . UWord . LowWord == APPLE_ADDRESS ) { decodedIRData . protocol = APPLE ; decodedIRData . address = tValue . UByte . HighByte ; } else { if ( tValue . UByte . LowByte == ( uint8_t ) ( ~ tValue . UByte . MidLowByte ) ) { decodedIRData . address = tValue . UByte . LowByte ; } else { decodedIRData . address = tValue . UWord . LowWord ; } if ( tValue . UByte . MidHighByte == ( uint8_t ) ( ~ tValue . UByte . HighByte ) ) { decodedIRData . protocol = NEC ; } else { decodedIRData . protocol = ONKYO ; decodedIRData . command = tValue . UWord . HighWord ; } } decodedIRData . numberOfBits = NEC_BITS ; return true ; }
+
NECs have a repeat only 4 items long
+ NECs have a repeat only 4 items long
// Check we have the right amount of data (68). The +4 is for initial gap, start bit mark and space + stop bit mark.
 // Check header "mark" this must be done for repeat and data
 // Check for repeat - here we have another header space length
 // Check command header space
 // Stop bit
 // Success
 //    decodedIRData.flags = IRDATA_FLAGS_IS_LSB_FIRST; // Not required, since this is the start value
 // 8 bit
 // Address
 /*
         * Apple
         */ /*
         * NEC
         */ // standard 8 bit address NEC protocol
 // first 8 bit
 // extended NEC protocol
 // first 16 bit
 // Check for command if it is 8 bit NEC or 16 bit ONKYO
 // 16 bit command
 /*
             * Old NEC plausibility check below, now it is just ONKYO :-)
             */ //            DEBUG_PRINT(F("NEC: "));
 //            DEBUG_PRINT(F("Command=0x"));
 //            DEBUG_PRINT(tValue.UByte.MidHighByte, HEX);
 //            DEBUG_PRINT(F(" is not inverted value of 0x"));
 //            DEBUG_PRINTLN(tValue.UByte.HighByte, HEX);
 //            decodedIRData.flags = IRDATA_FLAGS_PARITY_FAILED | IRDATA_FLAGS_IS_LSB_FIRST;
