IRremote
ir_NEC.cpp
Go to the documentation of this file.
1 #include "IRremote.h"
2 
3 //==============================================================================
4 // N N EEEEE CCCC
5 // NN N E C
6 // N N N EEE C
7 // N NN E C
8 // N N EEEEE CCCC
9 //==============================================================================
10 
11 #define NEC_BITS 32
12 #define NEC_HDR_MARK 9000
13 #define NEC_HDR_SPACE 4500
14 #define NEC_BIT_MARK 560
15 #define NEC_ONE_SPACE 1690
16 #define NEC_ZERO_SPACE 560
17 #define NEC_RPT_SPACE 2250
18 
19 //+=============================================================================
20 #if SEND_NEC
21 void IRsend::sendNEC (unsigned long data, int nbits)
22 {
23  // Set IR carrier frequency
24  enableIROut(38);
25 
26  // Header
29 
30  // Data
31  for (unsigned long mask = 1UL << (nbits - 1); mask; mask >>= 1) {
32  if (data & mask) {
35  } else {
38  }
39  }
40 
41  // Footer
43  space(0); // Always end with the LED off
44 }
45 #endif
46 
47 //+=============================================================================
48 // NECs have a repeat only 4 items long
49 //
50 #if DECODE_NEC
51 bool IRrecv::decodeNEC (decode_results *results)
52 {
53  long data = 0; // We decode in to here; Start with nothing
54  int offset = 1; // Index in to results; Skip first entry!?
55 
56  // Check header "mark"
57  if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) return false ;
58  offset++;
59 
60  // Check for repeat
61  if ( (irparams.rawlen == 4)
62  && MATCH_SPACE(results->rawbuf[offset ], NEC_RPT_SPACE)
63  && MATCH_MARK (results->rawbuf[offset+1], NEC_BIT_MARK )
64  ) {
65  results->bits = 0;
66  results->value = REPEAT;
67  results->decode_type = NEC;
68  return true;
69  }
70 
71  // Check we have enough data
72  if (irparams.rawlen < (2 * NEC_BITS) + 4) return false ;
73 
74  // Check header "space"
75  if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) return false ;
76  offset++;
77 
78  // Build the data
79  for (int i = 0; i < NEC_BITS; i++) {
80  // Check data "mark"
81  if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) return false ;
82  offset++;
83  // Suppend this bit
84  if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE )) data = (data << 1) | 1 ;
85  else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) data = (data << 1) | 0 ;
86  else return false ;
87  offset++;
88  }
89 
90  // Success
91  results->bits = NEC_BITS;
92  results->value = data;
93  results->decode_type = NEC;
94 
95  return true;
96 }
97 #endif
NEC_BIT_MARK
#define NEC_BIT_MARK
Definition: ir_NEC.cpp:14
NEC_ONE_SPACE
#define NEC_ONE_SPACE
Definition: ir_NEC.cpp:15
decode_results
Results returned from the decoder.
Definition: IRremote.h:174
NEC_BITS
#define NEC_BITS
Definition: ir_NEC.cpp:11
decode_results::bits
int bits
Number of bits in decoded value.
Definition: IRremote.h:179
IRsend::sendNEC
void sendNEC(unsigned long data, int nbits)
Definition: ir_NEC.cpp:21
decode_results::rawbuf
volatile unsigned int * rawbuf
Raw intervals in 50uS ticks.
Definition: IRremote.h:180
IRsend::mark
void mark(unsigned int usec)
Definition: irSend.cpp:69
IRsend::enableIROut
void enableIROut(int khz)
Definition: irSend.cpp:127
decode_results::decode_type
decode_type_t decode_type
UNKNOWN, NEC, SONY, RC5, ...
Definition: IRremote.h:176
MATCH_SPACE
int MATCH_SPACE(int measured_ticks, int desired_us)
Definition: IRremote.cpp:89
irparams
volatile irparams_t irparams
Allow all parts of the code access to the ISR data NB.
NEC_HDR_MARK
#define NEC_HDR_MARK
Definition: ir_NEC.cpp:12
IRremote.h
Public API to the library.
IRsend::space
void space(unsigned int usec)
Definition: irSend.cpp:103
REPEAT
#define REPEAT
Decoded value for NEC when a repeat code is received.
Definition: IRremote.h:188
decode_results::value
unsigned long value
Decoded value [max 32-bits].
Definition: IRremote.h:178
NEC_HDR_SPACE
#define NEC_HDR_SPACE
Definition: ir_NEC.cpp:13
MATCH_MARK
int MATCH_MARK(int measured_ticks, int desired_us)
Definition: IRremote.cpp:63
NEC_RPT_SPACE
#define NEC_RPT_SPACE
Definition: ir_NEC.cpp:17
NEC_ZERO_SPACE
#define NEC_ZERO_SPACE
Definition: ir_NEC.cpp:16
NEC
@ NEC
Definition: IRremote.h:117
irparams_t::rawlen
unsigned int rawlen
counter of entries in rawbuf
Definition: IRremoteInt.h:45