forked from crankyoldgit/IRremoteESP8266
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirRecv.cpp.old
535 lines (494 loc) · 15.7 KB
/
irRecv.cpp.old
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/***************************************************
* IRremote for ESP8266
*
* Based on the IRremote library for Arduino by Ken Shirriff
* Version 0.11 August, 2009
* Copyright 2009 Ken Shirriff
* For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
*
* Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers
* Modified by Mitra Ardron <mitra@mitra.biz>
* Added Sanyo and Mitsubishi controllers
* Modified Sony to spot the repeat codes that some Sony's send
*
* Interrupt code based on NECIRrcv by Joe Knapp
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
* Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
*
* JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
* LG added by Darryl Smith (based on the JVC protocol)
* Whynter A/C ARC-110WD added by Francesco Meschia
* Global Cache IR format sender added by Hisham Khalifa (http://www.hishamkhalifa.com)
* Coolix A/C / heatpump added by bakrus
*
* Updated by markszabo (/~https://github.com/markszabo/IRremoteESP8266) for sending IR code on ESP8266
* Updated by Sebastien Warin (http://sebastien.warin.fr) for receiving IR code on ESP8266
* Nov2016 updated bu marcosamarinho to fix times and Sony and NEC improvements to reduce UNKNOWN
* Nov 2016 marcosamarinho split ir types on spared files and merged the changes from IRRemote see 2.0.0 changelog.md
* GPL license, all text above must be included in any redistribution
****************************************************/
#include "IRremote.h"
#include "IRremoteInt.h"
#include <string>
using std::string;
void id2protocol (int id,String& protocol) {
switch (id) {
default:
case UNKNOWN: protocol="UNKNOWN"; break;
case RC5: protocol="RC5"; break;
case RC6: protocol="RC6"; break;
case NEC: protocol="NEC"; break;
case SONY: protocol="SONY"; break;
case PANASONIC: protocol="PANASONIC"; break;
case JVC: protocol="JVC"; break;
case SAMSUNG: protocol="SAMSUNG"; break;
case WHYNTER: protocol="WHYNTER"; break;
case AIWA_RC_T501: protocol="AIWA_RC_T501";break;
case LG: protocol="LG"; break;
case SANYO: protocol="SANYO"; break;
case MITSUBISHI: protocol="MITSUBISHI"; break;
case DISH: protocol="DISH"; break;
case SHARP: protocol="SHARP"; break;
case DENON: protocol="DENON"; break;
case PRONTO: protocol="PRONTO"; break;
case LEGO_PF: protocol="LEGO_PF"; break;
case COOLIX: protocol="COOLIX"; break;
case DAIKIN: protocol="DAIKIN"; break;
case GC: protocol="GC"; break;
}
}
int IRrecv::match_mark_nolog(int measured_ticks, int desired) {
return measured_ticks >= TICKS_LOW(desired) && measured_ticks <= TICKS_HIGH(desired+MARK_EXCESS);
}
int IRrecv::match_space_nolog(int measured_ticks, int desired) {
return measured_ticks >= TICKS_LOW(desired-MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired);
}
#ifdef DEBUG
int IRrecv::MATCH(int measured, int desired) {
bool ret = measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);
if (!ret) {
Serial.print("NOT Match (");
Serial.print(TICKS_LOW(desired), DEC);
Serial.print(" <= ");
Serial.print(measured, DEC);
Serial.print(" <= ");
Serial.print(TICKS_HIGH(desired), DEC);
Serial.print(") Expected : ");
Serial.println(desired, DEC);
}
return ret;
}
int IRrecv::MATCH_MARK(int measured_ticks, int desired) {
bool ret = match_mark_nolog(measured_ticks,desired);
if (!ret) {
Serial.print("MARK NOT Match (");
Serial.print(TICKS_LOW(desired ), DEC);
Serial.print(" <= ");
Serial.print(measured_ticks, DEC);
Serial.print(" <= ");
Serial.print(TICKS_HIGH(desired), DEC);
Serial.print(") Expected : ");
Serial.println(desired, DEC);
}
return ret;
}
int IRrecv::MATCH_SPACE(int measured_ticks, int desired) {
bool ret = match_space_nolog(measured_ticks,desired);
if (!ret) {
Serial.print("SPACE NOT Match (");
Serial.print(TICKS_LOW(desired ), DEC);
Serial.print(" <= ");
Serial.print(measured_ticks, DEC);
Serial.print(" <= ");
Serial.print(TICKS_HIGH(desired ), DEC);
Serial.print(") Expected : ");
Serial.println(desired, DEC);
}
return ret;
}
#else
int IRrecv::MATCH(int measured, int desired) {return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);}
int IRrecv::MATCH_MARK( int measured_ticks, int desired) {return match_mark_nolog( measured_ticks, desired );}
int IRrecv::MATCH_SPACE(int measured_ticks, int desired) {return match_space_nolog(measured_ticks, desired );}
#endif
void printDecoded(decode_results *results) {
Serial.print("IR DECODED Type:") ;
Serial.print(results->decode_type);
Serial.print(" Name:") ;
String sir;
Serial.print(results->protocol);
Serial.print(" RawData:") ;
//Print long long
int start =false ;
for ( int i=15; i>-1; i--) {
int val = results->value>>(4*i) & 0xF;
if ( val == 0 ) {
if (start) Serial.print("0");
} else {
start =true ;
Serial.print(val,HEX) ;
}
}
if (results->address >-1) {
Serial.print(" Address:");
Serial.print(results->address ,HEX);
}
if (results->command >-1) {
Serial.print(" Command:");
Serial.print(results->command,HEX) ;
}
Serial.print(" Bits:");
Serial.print(results->bits); Serial.print(" ");
Serial.println();
}
#ifdef DEBUG
void dump(decode_results *results)
{
Serial.println("IR Received dump.");
Serial.print("Results:");
Serial.print(results->rawlen-OFFSET_START, DEC);
Serial.println(" Timing in us ");
for (int i = OFFSET_START; i < results->rawlen; i++) {
unsigned long x = results->rawbuf[i] * USECPERTICK;
if (x < 10000) Serial.print(" ") ;
if (x < 1000) Serial.print(" ") ;
if (x < 100) Serial.print(" ") ;
Serial.print(" ");
if (!(i & 1)) { // even
Serial.print("-");
Serial.print(x, DEC);
} else { // odd
Serial.print(" +");
Serial.print(x, DEC);
}
if (!(i % 8)) Serial.println("");
}
Serial.println("");
}
#endif
void IRrecv::addBit(unsigned long long &data,bool bit) {
if (bit) {
data = (unsigned long long ) (data << 1) | 1;
//Serial.print("1");
} else {
data <<= 1;
//Serial.print("0");
}
}
void IRrecv::addBit(unsigned long &data,bool bit) {
if (bit) {
data = (unsigned long) (data << 1) | 1;
//Serial.print("1");
} else {
data <<= 1;
//Serial.print("0");
}
}
void IRrecv::addBit(unsigned int &data,bool bit) {
if (bit) {
data = (data << 1) | 1;
} else {
data <<= 1;
}
}
// No log if not matches to clen up debuggind
bool IRrecv::mark_decode(unsigned long &data,int val, int timeOne, int timeZero) {
if (match_mark_nolog(val, timeOne)) {
addBit(data,1) ;
} else if (match_mark_nolog(val , timeZero)) {
addBit(data,0) ;
} else {
#ifdef DEBUG
Serial.print("Wrong MARK_DECODE :") ;Serial.print(val) ;
Serial.print(" TimeOne :" ) ;Serial.print(timeOne) ;
Serial.print(" TimeZero :" ) ;Serial.println(timeZero) ;
#endif
return false;
}
return true;
}
bool IRrecv::space_decode(unsigned long &data,int val, int timeOne, int timeZero) {
if (match_space_nolog(val, timeOne)) {
addBit(data,1) ;
} else if (match_space_nolog(val, timeZero)) {
addBit(data,0) ;
} else {
#ifdef DEBUG
Serial.print("Wrong SPACE_DECODE :") ;Serial.print(val) ;
Serial.print(" TimeOne :" ) ;Serial.print(timeOne) ;
Serial.print(" TimeZero :" ) ;Serial.println(timeZero) ;
#endif
return false;
}
return true;
}
bool IRrecv::mark_decode(unsigned long long &data,int val, int timeOne, int timeZero) {
if (match_mark_nolog(val, timeOne)) {
addBit(data,1) ;
} else if (match_mark_nolog(val , timeZero)) {
addBit(data,0) ;
} else {
#ifdef DEBUG
Serial.print("Wrong MARK_DECODE :") ;Serial.print(val) ;
Serial.print(" TimeOne :" ) ;Serial.print(timeOne) ;
Serial.print(" TimeZero :" ) ;Serial.println(timeZero) ;
#endif
return false;
}
return true;
}
bool IRrecv::space_decode(unsigned long long &data,int val, int timeOne, int timeZero) {
if (match_space_nolog(val, timeOne)) {
addBit(data,1) ;
} else if (match_space_nolog(val, timeZero)) {
addBit(data,0) ;
} else {
#ifdef DEBUG
Serial.print("Wrong SPACE_DECODE :") ;Serial.print(val) ;
Serial.print(" TimeOne :" ) ;Serial.print(timeOne) ;
Serial.print(" TimeZero :" ) ;Serial.println(timeZero) ;
#endif
return false;
}
return true;
}
extern "C" {
#include "user_interface.h"
#include "gpio.h"
}
static ETSTimer timer;
volatile irparams_t irparams;
static void ICACHE_FLASH_ATTR read_timeout(void *arg) {
os_intr_lock();
if (irparams.rawlen) {
irparams.rcvstate = STATE_STOP;
}
os_intr_unlock();
}
// resume
void IRrecv::resume() {
irparams.rcvstate = STATE_IDLE;
irparams.rawlen = 0;
}
bool IRrecv::isIdle() {
return irparams.rcvstate == STATE_IDLE;
}
IRrecv::IRrecv(int recvpin) {
irparams.recvpin = recvpin;
}
static void ICACHE_FLASH_ATTR gpio_intr() {
uint32_t gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status);
if (irparams.rcvstate == STATE_STOP) {
return;
}
static uint32_t start = 0;
uint32_t now = system_get_time();
if (irparams.rcvstate == STATE_IDLE) {
irparams.rcvstate = STATE_MARK;
// if OFFSET_START==1 Include an fake initial space to keep compatible with IR-Remote .
if ( OFFSET_START==1) irparams.rawbuf[irparams.rawlen++] = 20 ;
} else {
if (irparams.rawlen < RAWBUF) {
irparams.rawbuf[irparams.rawlen++] = (now-start)/USECPERTICK ;
} else {
irparams.overflow= true;
}
}
start = now;
os_timer_disarm(&timer);
// This time need to be larger tham the larger repetition time, to allow full REPEAT decode .
os_timer_arm(&timer, 80, 0);
}
// initialization
void IRrecv::enableIRIn() {
// initialize state machine variables
irparams.rcvstate = STATE_IDLE;
irparams.rawlen = 0;
irparams.overflow = false;
// Initialize timer
os_timer_disarm(&timer);
os_timer_setfn(&timer, (os_timer_func_t *)read_timeout, &timer);
// Attach Interrupt
attachInterrupt(irparams.recvpin, gpio_intr, CHANGE);
}
void IRrecv::disableIRIn() {
//irReadTimer.stop();
//os_timer_disarm(&irReadTimer);
ETS_INTR_LOCK();
ETS_GPIO_INTR_DISABLE();
}
int IRrecv::decodeESP8266(decode_results *results) {
#if DECODE_NEC
#ifdef DEBUG
Serial.println("Attempting NEC decode");
#endif
if (decodeNEC(results)) return true;
#endif
#if DECODE_SONY
#ifdef DEBUG
Serial.println("Attempting Sony decode");
#endif
if (decodeSony(results)) return true;
#endif
#if DECODE_SANYO
#ifdef DEBUG
Serial.println("Attempting Sanyo decode");
#endif
if (decodeSanyo(results)) return true;
#endif
#if DECODE_MITSUBISHI
#ifdef DEBUG
Serial.println("Attempting Mitsubishi decode");
#endif
if (decodeMitsubishi(results)) return true;
#endif
#if DECODE_RC5
#ifdef DEBUG
Serial.println("Attempting RC5 decode");
#endif
if (decodeRC5(results)) return true;
#endif
#if DECODE_RC6
#ifdef DEBUG
Serial.println("Attempting RC6 decode");
#endif
if (decodeRC6(results)) return true;
#endif
#if DECODE_PANASONIC
#ifdef DEBUG
Serial.println("Attempting Panasonic decode");
#endif
if (decodePanasonic(results)) return true;
#endif
#if DECODE_LG_32
#ifdef DEBUG
Serial.println("Attempting LG 32 bits decode");
#endif
if (decodeLG_32(results)) return true;
#endif
#if DECODE_LG
#ifdef DEBUG
Serial.println("Attempting LG 28 bits decode");
#endif
if (decodeLG(results)) return true;
#endif
#if DECODE_JVC
#ifdef DEBUG
Serial.println("Attempting JVC decode");
#endif
if (decodeJVC(results)) return true;
#endif
#if DECODE_SAMSUNG
#ifdef DEBUG
Serial.println("Attempting SAMSUNG decode");
#endif
if (decodeSAMSUNG(results)) return true;
#endif
#if DECODE_SHARP
#ifdef DEBUG
Serial.println("Attempting SHARP decode");
#endif
if (decodeSharp(results)) return true;
#endif
#if DECODE_WHYNTER
#ifdef DEBUG
Serial.println("Attempting Whynter decode");
#endif
if (decodeWhynter(results)) return true;
#endif
#if DECODE_AIWA_RC_T501
#ifdef DEBUG
Serial.println("Attempting Aiwa RC-T501 decode");
#endif
if (decodeAiwaRCT501(results)) return true ;
#endif
#if DECODE_DENON
#ifdef DEBUG
Serial.println("Attempting Denon decode");
#endif
if (decodeDenon(results)) return true ;
#endif
#if DECODE_LEGO_PF
#ifdef DEBUG
Serial.println("Attempting Lego Power Functions");
#endif
if (decodeLegoPowerFunctions(results)) return true ;
#endif
// decodeHash returns a hash on any input.
// Thus, it needs to be last in the list.
// If you add any decodes, add them before this.
#if DECODE_HASH
if (decodeHash(results)) return true;
#endif
return false;
}
// Decodes the received IR message
// Returns 0 if no data ready, 1 if data ready.
// Results of decoding are stored in results
int IRrecv::decode(decode_results *results) {
if (irparams.rcvstate != STATE_STOP) return false;
results->rawbuf = irparams.rawbuf;
results->rawlen = irparams.rawlen;
results->overflow = irparams.overflow;
results->command = -1;
results->address = -1;
#ifdef DEBUG
dump(results) ;
#endif
if ( decodeESP8266(results) && (results->bits!=0)) {
#ifdef DEBUG
printDecoded(results) ;
#endif
id2protocol(results->decode_type, results->protocol) ;
return true ;
}
resume();
return false;
}
/* -----------------------------------------------------------------------
* hashdecode - decode an arbitrary IR code.
* Instead of decoding using a standard encoding scheme
* (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value.
*
* The algorithm: look at the sequence of MARK signals, and see if each one
* is shorter (0), the same length (1), or longer (2) than the previous.
* Do the same with the SPACE signals. Hszh the resulting sequence of 0's,
* 1's, and 2's to a 32-bit value. This will give a unique value for each
* different code (probably), for most code systems.
*
* http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html
*/
// Compare two tick values, returning 0 if newval is shorter,
// 1 if newval is equal, and 2 if newval is longer
// Use a tolerance of 20%
int IRrecv::compare(unsigned int oldval, unsigned int newval) {
if (newval < oldval * .8) {
return 0;
} else if ( oldval < newval * .8) {
return 2;
} else {
return 1;
}
}
// Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param
#define FNV_PRIME_32 16777619
#define FNV_BASIS_32 2166136261
/* Converts the raw code values into a 32-bit hash code.
* Hopefully this code is unique for each button.
* This isn't a "real" decoding, just an arbitrary value.
*/
bool IRrecv::decodeHash(decode_results *results) {
// take the dirt out when it is a noise
if (results->rawlen < 20+ OFFSET_START) return false;
long hash = FNV_BASIS_32;
for (int i = OFFSET_START; i+2 < results->rawlen; i++) {
int value = compare(results->rawbuf[i], results->rawbuf[i+2]);
// Add value into the hash
hash = (hash * FNV_PRIME_32) ^ value;
}
results->value = hash;
results->bits = 32;
results->decode_type = UNKNOWN;
return true;
}