Infrared remote library for Arduino: send and receive infrared signals with multiple protocols Port from Arduino-IRremote https://github.com/z3t0/Arduino-IRremote
ir_NEC.cpp@8:629ef37f2853, 2019-09-25 (annotated)
- Committer:
- JRM1986
- Date:
- Wed Sep 25 13:20:19 2019 +0000
- Revision:
- 8:629ef37f2853
- Parent:
- 0:70c8e56bac45
Remote Control Decoded - recieving codes from module;
Who changed what in which revision?
User | Revision | Line number | New 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 | { |
yuhki50 | 0:70c8e56bac45 | 24 | // Set IR carrier frequency |
yuhki50 | 0:70c8e56bac45 | 25 | enableIROut(38); |
yuhki50 | 0:70c8e56bac45 | 26 | |
yuhki50 | 0:70c8e56bac45 | 27 | // Header |
yuhki50 | 0:70c8e56bac45 | 28 | mark(NEC_HDR_MARK); |
yuhki50 | 0:70c8e56bac45 | 29 | space(NEC_HDR_SPACE); |
yuhki50 | 0:70c8e56bac45 | 30 | |
yuhki50 | 0:70c8e56bac45 | 31 | // Data |
yuhki50 | 0:70c8e56bac45 | 32 | for (unsigned long mask = 1UL << (nbits - 1); mask; mask >>= 1) { |
yuhki50 | 0:70c8e56bac45 | 33 | if (data & mask) { |
yuhki50 | 0:70c8e56bac45 | 34 | mark(NEC_BIT_MARK); |
yuhki50 | 0:70c8e56bac45 | 35 | space(NEC_ONE_SPACE); |
yuhki50 | 0:70c8e56bac45 | 36 | } else { |
yuhki50 | 0:70c8e56bac45 | 37 | mark(NEC_BIT_MARK); |
yuhki50 | 0:70c8e56bac45 | 38 | space(NEC_ZERO_SPACE); |
yuhki50 | 0:70c8e56bac45 | 39 | } |
yuhki50 | 0:70c8e56bac45 | 40 | } |
yuhki50 | 0:70c8e56bac45 | 41 | |
yuhki50 | 0:70c8e56bac45 | 42 | // Footer |
yuhki50 | 0:70c8e56bac45 | 43 | mark(NEC_BIT_MARK); |
yuhki50 | 0:70c8e56bac45 | 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 | { |
yuhki50 | 0:70c8e56bac45 | 54 | long data = 0; // We decode in to here; Start with nothing |
yuhki50 | 0:70c8e56bac45 | 55 | int offset = 1; // Index in to results; Skip first entry!? |
yuhki50 | 0:70c8e56bac45 | 56 | |
yuhki50 | 0:70c8e56bac45 | 57 | // Check header "mark" |
yuhki50 | 0:70c8e56bac45 | 58 | if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) return false ; |
yuhki50 | 0:70c8e56bac45 | 59 | offset++; |
yuhki50 | 0:70c8e56bac45 | 60 | |
yuhki50 | 0:70c8e56bac45 | 61 | // Check for repeat |
yuhki50 | 0:70c8e56bac45 | 62 | if ( (irparams.rawlen == 4) |
yuhki50 | 0:70c8e56bac45 | 63 | && MATCH_SPACE(results->rawbuf[offset ], NEC_RPT_SPACE) |
yuhki50 | 0:70c8e56bac45 | 64 | && MATCH_MARK (results->rawbuf[offset+1], NEC_BIT_MARK ) |
yuhki50 | 0:70c8e56bac45 | 65 | ) { |
yuhki50 | 0:70c8e56bac45 | 66 | results->bits = 0; |
yuhki50 | 0:70c8e56bac45 | 67 | results->value = REPEAT; |
yuhki50 | 0:70c8e56bac45 | 68 | results->decode_type = NEC; |
yuhki50 | 0:70c8e56bac45 | 69 | return true; |
yuhki50 | 0:70c8e56bac45 | 70 | } |
yuhki50 | 0:70c8e56bac45 | 71 | |
yuhki50 | 0:70c8e56bac45 | 72 | // Check we have enough data |
yuhki50 | 0:70c8e56bac45 | 73 | if (irparams.rawlen < (2 * NEC_BITS) + 4) return false ; |
yuhki50 | 0:70c8e56bac45 | 74 | |
yuhki50 | 0:70c8e56bac45 | 75 | // Check header "space" |
yuhki50 | 0:70c8e56bac45 | 76 | if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) return false ; |
yuhki50 | 0:70c8e56bac45 | 77 | offset++; |
yuhki50 | 0:70c8e56bac45 | 78 | |
yuhki50 | 0:70c8e56bac45 | 79 | // Build the data |
yuhki50 | 0:70c8e56bac45 | 80 | for (int i = 0; i < NEC_BITS; i++) { |
yuhki50 | 0:70c8e56bac45 | 81 | // Check data "mark" |
yuhki50 | 0:70c8e56bac45 | 82 | if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) return false ; |
yuhki50 | 0:70c8e56bac45 | 83 | offset++; |
yuhki50 | 0:70c8e56bac45 | 84 | // Suppend this bit |
yuhki50 | 0:70c8e56bac45 | 85 | if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE )) data = (data << 1) | 1 ; |
yuhki50 | 0:70c8e56bac45 | 86 | else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) data = (data << 1) | 0 ; |
yuhki50 | 0:70c8e56bac45 | 87 | else return false ; |
yuhki50 | 0:70c8e56bac45 | 88 | offset++; |
yuhki50 | 0:70c8e56bac45 | 89 | } |
yuhki50 | 0:70c8e56bac45 | 90 | |
yuhki50 | 0:70c8e56bac45 | 91 | // Success |
yuhki50 | 0:70c8e56bac45 | 92 | results->bits = NEC_BITS; |
yuhki50 | 0:70c8e56bac45 | 93 | results->value = data; |
yuhki50 | 0:70c8e56bac45 | 94 | results->decode_type = NEC; |
yuhki50 | 0:70c8e56bac45 | 95 | |
yuhki50 | 0:70c8e56bac45 | 96 | return true; |
yuhki50 | 0:70c8e56bac45 | 97 | } |
yuhki50 | 0:70c8e56bac45 | 98 | #endif |