IRremote
irRecv.cpp
Go to the documentation of this file.
1 #include "IRremote.h"
2 
3 //+=============================================================================
4 // Decodes the received IR message
5 // Returns 0 if no data ready, 1 if data ready.
6 // Results of decoding are stored in results
7 //
9  results->rawbuf = irparams.rawbuf;
10  results->rawlen = irparams.rawlen;
11 
12  results->overflow = irparams.overflow;
13 
15  return false;
16  }
17 
18 #if DECODE_NEC
19  DBG_PRINTLN("Attempting NEC decode");
20  if (decodeNEC(results)) {
21  return true;
22  }
23 #endif
24 
25 #if DECODE_SHARP
26  DBG_PRINTLN("Attempting Sharp decode");
27  if (decodeSharp(results)) {
28  return true;
29  }
30 #endif
31 
32 #if DECODE_SHARP_ALT
33  DBG_PRINTLN("Attempting SharpAlt decode");
34  if (decodeSharpAlt(results)) {
35  return true;
36  }
37 #endif
38 
39 #if DECODE_SONY
40  DBG_PRINTLN("Attempting Sony decode");
41  if (decodeSony(results)) {
42  return true;
43  }
44 #endif
45 
46 #if DECODE_SANYO
47  DBG_PRINTLN("Attempting Sanyo decode");
48  if (decodeSanyo(results)) {
49  return true;
50  }
51 #endif
52 
53 #if DECODE_MITSUBISHI
54  DBG_PRINTLN("Attempting Mitsubishi decode");
55  if (decodeMitsubishi(results)) {
56  return true;
57  }
58 #endif
59 
60 #if DECODE_RC5
61  DBG_PRINTLN("Attempting RC5 decode");
62  if (decodeRC5(results)) {
63  return true;
64  }
65 #endif
66 
67 #if DECODE_RC6
68  DBG_PRINTLN("Attempting RC6 decode");
69  if (decodeRC6(results)) {
70  return true;
71  }
72 #endif
73 
74 #if DECODE_PANASONIC
75  DBG_PRINTLN("Attempting Panasonic decode");
76  if (decodePanasonic(results)) {
77  return true;
78  }
79 #endif
80 
81 #if DECODE_LG
82  DBG_PRINTLN("Attempting LG decode");
83  if (decodeLG(results)) {
84  return true;
85  }
86 #endif
87 
88 #if DECODE_JVC
89  DBG_PRINTLN("Attempting JVC decode");
90  if (decodeJVC(results)) {
91  return true;
92  }
93 #endif
94 
95 #if DECODE_SAMSUNG
96  DBG_PRINTLN("Attempting SAMSUNG decode");
97  if (decodeSAMSUNG(results)) {
98  return true;
99  }
100 #endif
101 
102 #if DECODE_WHYNTER
103  DBG_PRINTLN("Attempting Whynter decode");
104  if (decodeWhynter(results)) {
105  return true;
106  }
107 #endif
108 
109 #if DECODE_AIWA_RC_T501
110  DBG_PRINTLN("Attempting Aiwa RC-T501 decode");
111  if (decodeAiwaRCT501(results)) {
112  return true;
113  }
114 #endif
115 
116 #if DECODE_DENON
117  DBG_PRINTLN("Attempting Denon decode");
118  if (decodeDenon(results)) {
119  return true;
120  }
121 #endif
122 
123 #if DECODE_LEGO_PF
124  DBG_PRINTLN("Attempting Lego Power Functions");
125  if (decodeLegoPowerFunctions(results)) {return true ;}
126 #endif
127 
128 #if DECODE_HASH
129  DBG_PRINTLN("Hash decode");
130  // decodeHash returns a hash on any input.
131  // Thus, it needs to be last in the list.
132  // If you add any decodes, add them before this.
133  if (decodeHash(results)) {
134  return true;
135  }
136 #endif
137 
138  // Throw away and start over
139  resume();
140  return false;
141 }
142 
143 //+=============================================================================
144 IRrecv::IRrecv(int recvpin) {
145  irparams.recvpin = recvpin;
146  irparams.blinkflag = 0;
147 }
148 
149 IRrecv::IRrecv(int recvpin, int blinkpin) {
150  irparams.recvpin = recvpin;
151  irparams.blinkpin = blinkpin;
152  pinMode(blinkpin, OUTPUT);
153  irparams.blinkflag = 0;
154 }
155 
156 //+=============================================================================
157 // initialization
158 //
159 #ifdef USE_DEFAULT_ENABLE_IR_IN
161 // Interrupt Service Routine - Fires every 50uS
162  cli();
163  // Setup pulse clock timer interrupt
164  // Prescale /8 (16M/8 = 0.5 microseconds per tick)
165  // Therefore, the timer interval can range from 0.5 to 128 microseconds
166  // Depending on the reset value (255 to 0)
167  TIMER_CONFIG_NORMAL();
168 
169  // Timer2 Overflow Interrupt Enable
170  TIMER_ENABLE_INTR;
171 
172  TIMER_RESET;
173 
174  sei();
175  // enable interrupts
176 
177  // Initialize state machine variables
179  irparams.rawlen = 0;
180 
181  // Set pin modes
182  pinMode(irparams.recvpin, INPUT);
183 }
184 
186  cli();
187  TIMER_DISABLE_INTR;
188  sei();
189  // enable interrupts
190 }
191 
192 #endif // USE_DEFAULT_ENABLE_IR_IN
193 
194 //+=============================================================================
195 // Enable/disable blinking of pin 13 on IR processing
196 //
197 void IRrecv::blink13(int blinkflag) {
198 #ifdef BLINKLED
199  irparams.blinkflag = blinkflag;
200  if (blinkflag) {
201  pinMode(BLINKLED, OUTPUT);
202  }
203 #endif
204 }
205 
206 //+=============================================================================
207 // Return if receiving new IR signals
208 //
210  return (irparams.rcvstate == IR_REC_STATE_IDLE || irparams.rcvstate == IR_REC_STATE_STOP) ? true : false;
211 }
212 //+=============================================================================
213 // Restart the ISR state machine
214 //
217  irparams.rawlen = 0;
218 }
219 
220 # if DECODE_HASH
221 //+=============================================================================
222 // hashdecode - decode an arbitrary IR code.
223 // Instead of decoding using a standard encoding scheme
224 // (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value.
225 //
226 // The algorithm: look at the sequence of MARK signals, and see if each one
227 // is shorter (0), the same length (1), or longer (2) than the previous.
228 // Do the same with the SPACE signals. Hash the resulting sequence of 0's,
229 // 1's, and 2's to a 32-bit value. This will give a unique value for each
230 // different code (probably), for most code systems.
231 //
232 // http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html
233 //
234 // Compare two tick values, returning 0 if newval is shorter,
235 // 1 if newval is equal, and 2 if newval is longer
236 // Use a tolerance of 20%
237 //
238 int IRrecv::compare(unsigned int oldval, unsigned int newval) {
239 // @formatter:off
240  if (newval * 10 < oldval * 8) return 0 ;
241  else if (oldval * 10 < newval * 8) return 2 ;
242  else return 1 ;
243 // @formatter:on
244 } //+=============================================================================
245 // Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param
246 // Converts the raw code values into a 32-bit hash code.
247 // Hopefully this code is unique for each button.
248 // This isn't a "real" decoding, just an arbitrary value.
249 //
250 #define FNV_PRIME_32 16777619
251 #define FNV_BASIS_32 2166136261
252 
253 long IRrecv::decodeHash(decode_results *results) {
254  long hash = FNV_BASIS_32;
255 
256  // Require at least 6 samples to prevent triggering on noise
257  if (results->rawlen < 6) {
258  return false;
259  }
260 
261  for (unsigned int i = 1; (i + 2) < results->rawlen; i++) {
262  int value = compare(results->rawbuf[i], results->rawbuf[i + 2]);
263  // Add value into the hash
264  hash = (hash * FNV_PRIME_32) ^ value;
265  }
266 
267  results->value = hash;
268  results->bits = 32;
269  results->decode_type = UNKNOWN;
270 
271  return true;
272 }
273 #endif // defined(DECODE_HASH)
UNKNOWN
@ UNKNOWN
Definition: IRremote.h:113
irparams_t::overflow
uint8_t overflow
Raw buffer overflow occurred.
Definition: IRremoteInt.h:48
IRrecv::resume
void resume()
Called to re-enable IR reception.
Definition: irRecv.cpp:215
IRrecv::disableIRIn
void disableIRIn()
Disable IR reception.
Definition: irRecv.cpp:185
IRrecv::enableIRIn
void enableIRIn()
Enable IR reception.
Definition: irRecv.cpp:160
decode_results
Results returned from the decoder.
Definition: IRremote.h:174
decode_results::bits
int bits
Number of bits in decoded value.
Definition: IRremote.h:179
DBG_PRINTLN
#define DBG_PRINTLN(...)
If DEBUG, print the arguments as a line, otherwise do nothing.
Definition: IRremote.h:155
FNV_PRIME_32
#define FNV_PRIME_32
Definition: irRecv.cpp:250
irparams_t::recvpin
uint8_t recvpin
Pin connected to IR data from detector.
Definition: IRremoteInt.h:42
decode_results::rawbuf
volatile unsigned int * rawbuf
Raw intervals in 50uS ticks.
Definition: IRremote.h:180
decode_results::decode_type
decode_type_t decode_type
UNKNOWN, NEC, SONY, RC5, ...
Definition: IRremote.h:176
irparams_t::rcvstate
uint8_t rcvstate
State Machine state.
Definition: IRremoteInt.h:41
irparams
volatile irparams_t irparams
Allow all parts of the code access to the ISR data NB.
IRrecv::decode
int decode(decode_results *results)
Attempt to decode the recently receive IR signal.
Definition: irRecv.cpp:8
IR_REC_STATE_STOP
#define IR_REC_STATE_STOP
Definition: IRremoteInt.h:55
irparams_t::blinkpin
uint8_t blinkpin
Definition: IRremoteInt.h:43
IRremote.h
Public API to the library.
IRrecv::IRrecv
IRrecv(int recvpin)
Instantiate the IRrecv class.
Definition: irRecv.cpp:144
FNV_BASIS_32
#define FNV_BASIS_32
Definition: irRecv.cpp:251
decode_results::value
unsigned long value
Decoded value [max 32-bits].
Definition: IRremote.h:178
IRrecv::blink13
void blink13(int blinkflag)
TODO: Why is this public???
Definition: irRecv.cpp:197
decode_results::overflow
int overflow
true if IR raw code too long
Definition: IRremote.h:182
irparams_t::rawbuf
unsigned int rawbuf[RAW_BUFFER_LENGTH]
raw data
Definition: IRremoteInt.h:47
BLINKLED
#define BLINKLED
If defined, denotes pin number of LED that should be blinked during IR reception.
Definition: IRremoteBoardDefs.h:74
irparams_t::blinkflag
uint8_t blinkflag
true -> enable blinking of pin on IR processing
Definition: IRremoteInt.h:44
decode_results::rawlen
unsigned int rawlen
Number of records in rawbuf.
Definition: IRremote.h:181
irparams_t::rawlen
unsigned int rawlen
counter of entries in rawbuf
Definition: IRremoteInt.h:45
IR_REC_STATE_IDLE
#define IR_REC_STATE_IDLE
Definition: IRremoteInt.h:52
IRrecv::isIdle
bool isIdle()
Returns status of reception.
Definition: irRecv.cpp:209