Infrared remote library for Arduino: send and receive infrared signals with multiple protocols Port from Arduino-IRremote https://github.com/z3t0/Arduino-IRremote

Dependents:   mbed-os-example-FinalReal_copy

Committer:
eunmango
Date:
Sun Jun 16 04:49:45 2019 +0000
Revision:
8:9d9b1e1f9b1b
Parent:
0:70c8e56bac45
s

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yuhki50 0:70c8e56bac45 1 #include "IRremote.h"
yuhki50 0:70c8e56bac45 2 #include "IRremoteInt.h"
yuhki50 0:70c8e56bac45 3
yuhki50 0:70c8e56bac45 4 //==============================================================================
yuhki50 0:70c8e56bac45 5 // N N EEEEE CCCC
yuhki50 0:70c8e56bac45 6 // NN N E C
yuhki50 0:70c8e56bac45 7 // N N N EEE C
yuhki50 0:70c8e56bac45 8 // N NN E C
yuhki50 0:70c8e56bac45 9 // N N EEEEE CCCC
yuhki50 0:70c8e56bac45 10 //==============================================================================
yuhki50 0:70c8e56bac45 11
yuhki50 0:70c8e56bac45 12 #define NEC_BITS 32
yuhki50 0:70c8e56bac45 13 #define NEC_HDR_MARK 9000
yuhki50 0:70c8e56bac45 14 #define NEC_HDR_SPACE 4500
yuhki50 0:70c8e56bac45 15 #define NEC_BIT_MARK 560
yuhki50 0:70c8e56bac45 16 #define NEC_ONE_SPACE 1690
yuhki50 0:70c8e56bac45 17 #define NEC_ZERO_SPACE 560
yuhki50 0:70c8e56bac45 18 #define NEC_RPT_SPACE 2250
yuhki50 0:70c8e56bac45 19
yuhki50 0:70c8e56bac45 20 //+=============================================================================
yuhki50 0:70c8e56bac45 21 #if SEND_NEC
yuhki50 0:70c8e56bac45 22 void IRsend::sendNEC (unsigned long data, int nbits)
yuhki50 0:70c8e56bac45 23 {
eunmango 8:9d9b1e1f9b1b 24 // Set IR carrier frequency
eunmango 8:9d9b1e1f9b1b 25 enableIROut(38);
yuhki50 0:70c8e56bac45 26
eunmango 8:9d9b1e1f9b1b 27 // Header
eunmango 8:9d9b1e1f9b1b 28 mark(NEC_HDR_MARK);
eunmango 8:9d9b1e1f9b1b 29 space(NEC_HDR_SPACE);
yuhki50 0:70c8e56bac45 30
eunmango 8:9d9b1e1f9b1b 31 // Data
eunmango 8:9d9b1e1f9b1b 32 for (unsigned long mask = 1UL << (nbits - 1); mask; mask >>= 1) {
eunmango 8:9d9b1e1f9b1b 33 if (data & mask) {
eunmango 8:9d9b1e1f9b1b 34 mark(NEC_BIT_MARK);
eunmango 8:9d9b1e1f9b1b 35 space(NEC_ONE_SPACE);
eunmango 8:9d9b1e1f9b1b 36 } else {
eunmango 8:9d9b1e1f9b1b 37 mark(NEC_BIT_MARK);
eunmango 8:9d9b1e1f9b1b 38 space(NEC_ZERO_SPACE);
eunmango 8:9d9b1e1f9b1b 39 }
eunmango 8:9d9b1e1f9b1b 40 }
yuhki50 0:70c8e56bac45 41
eunmango 8:9d9b1e1f9b1b 42 // Footer
eunmango 8:9d9b1e1f9b1b 43 mark(NEC_BIT_MARK);
eunmango 8:9d9b1e1f9b1b 44 space(0); // Always end with the LED off
yuhki50 0:70c8e56bac45 45 }
yuhki50 0:70c8e56bac45 46 #endif
yuhki50 0:70c8e56bac45 47
yuhki50 0:70c8e56bac45 48 //+=============================================================================
yuhki50 0:70c8e56bac45 49 // NECs have a repeat only 4 items long
yuhki50 0:70c8e56bac45 50 //
yuhki50 0:70c8e56bac45 51 #if DECODE_NEC
yuhki50 0:70c8e56bac45 52 bool IRrecv::decodeNEC (decode_results *results)
yuhki50 0:70c8e56bac45 53 {
eunmango 8:9d9b1e1f9b1b 54 long data = 0; // We decode in to here; Start with nothing
eunmango 8:9d9b1e1f9b1b 55 int offset = 1; // Index in to results; Skip first entry!?
yuhki50 0:70c8e56bac45 56
eunmango 8:9d9b1e1f9b1b 57 // Check header "mark"
eunmango 8:9d9b1e1f9b1b 58 if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) return false ;
eunmango 8:9d9b1e1f9b1b 59 offset++;
yuhki50 0:70c8e56bac45 60
eunmango 8:9d9b1e1f9b1b 61 // Check for repeat
eunmango 8:9d9b1e1f9b1b 62 /*if ( (irparams.rawlen == 4)
eunmango 8:9d9b1e1f9b1b 63 && MATCH_SPACE(results->rawbuf[offset ], NEC_RPT_SPACE)
eunmango 8:9d9b1e1f9b1b 64 && MATCH_MARK (results->rawbuf[offset+1], NEC_BIT_MARK )
eunmango 8:9d9b1e1f9b1b 65 ) {
eunmango 8:9d9b1e1f9b1b 66 results->bits = 0;
eunmango 8:9d9b1e1f9b1b 67 results->value = REPEAT;
eunmango 8:9d9b1e1f9b1b 68 results->decode_type = NEC;
eunmango 8:9d9b1e1f9b1b 69 return true;
eunmango 8:9d9b1e1f9b1b 70 }
eunmango 8:9d9b1e1f9b1b 71 */
yuhki50 0:70c8e56bac45 72
eunmango 8:9d9b1e1f9b1b 73 // Check we have enough data
eunmango 8:9d9b1e1f9b1b 74 if (irparams.rawlen < (2 * NEC_BITS) + 4) return false ;
eunmango 8:9d9b1e1f9b1b 75
eunmango 8:9d9b1e1f9b1b 76 // Check header "space"
eunmango 8:9d9b1e1f9b1b 77 if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) return false ;
eunmango 8:9d9b1e1f9b1b 78 offset++;
yuhki50 0:70c8e56bac45 79
eunmango 8:9d9b1e1f9b1b 80 int forInverse[32] = {0};
eunmango 8:9d9b1e1f9b1b 81 int j = 31;
eunmango 8:9d9b1e1f9b1b 82 // Build the data
eunmango 8:9d9b1e1f9b1b 83 for (int i = 0; i < NEC_BITS; i++) {
eunmango 8:9d9b1e1f9b1b 84 // Check data "mark"
eunmango 8:9d9b1e1f9b1b 85 if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) return false ;
eunmango 8:9d9b1e1f9b1b 86 offset++;
yuhki50 0:70c8e56bac45 87 // Suppend this bit
eunmango 8:9d9b1e1f9b1b 88 if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE ))
eunmango 8:9d9b1e1f9b1b 89 forInverse[i] = 1;
eunmango 8:9d9b1e1f9b1b 90 else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE))
eunmango 8:9d9b1e1f9b1b 91 forInverse[i] = 0;
eunmango 8:9d9b1e1f9b1b 92 else return false ;
eunmango 8:9d9b1e1f9b1b 93 offset++;
eunmango 8:9d9b1e1f9b1b 94
eunmango 8:9d9b1e1f9b1b 95 }
eunmango 8:9d9b1e1f9b1b 96
yuhki50 0:70c8e56bac45 97
eunmango 8:9d9b1e1f9b1b 98 for(int j = 0; j < 8; j++){ //
eunmango 8:9d9b1e1f9b1b 99 data = (data << 1) | forInverse[j];
eunmango 8:9d9b1e1f9b1b 100 }
eunmango 8:9d9b1e1f9b1b 101 for(int j = 8; j < 16; j++){ //
eunmango 8:9d9b1e1f9b1b 102 data = (data << 1) | forInverse[j];
eunmango 8:9d9b1e1f9b1b 103 }
eunmango 8:9d9b1e1f9b1b 104 for(int j = 23; j > 15; j--){ //
eunmango 8:9d9b1e1f9b1b 105 data = (data << 1) | forInverse[j];
eunmango 8:9d9b1e1f9b1b 106 }
eunmango 8:9d9b1e1f9b1b 107 for(int j = 31; j > 23; j--){ //
eunmango 8:9d9b1e1f9b1b 108 data = (data << 1) | forInverse[j];
eunmango 8:9d9b1e1f9b1b 109 }
yuhki50 0:70c8e56bac45 110
eunmango 8:9d9b1e1f9b1b 111
eunmango 8:9d9b1e1f9b1b 112
eunmango 8:9d9b1e1f9b1b 113
eunmango 8:9d9b1e1f9b1b 114 // Success
eunmango 8:9d9b1e1f9b1b 115 results->bits = NEC_BITS;
eunmango 8:9d9b1e1f9b1b 116 results->value = data;
eunmango 8:9d9b1e1f9b1b 117 results->decode_type = NEC;
eunmango 8:9d9b1e1f9b1b 118
eunmango 8:9d9b1e1f9b1b 119 return true;
yuhki50 0:70c8e56bac45 120 }
eunmango 8:9d9b1e1f9b1b 121 #endif