forked from adafruit/Raw-IR-decoder-for-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathrawirdecode.ino
627 lines (553 loc) · 17 KB
/
rawirdecode.ino
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#include <Arduino.h>
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP32) || defined(ESP8266)
#define MITSUBISHI_ELECTRIC
#define FUJITSU
#define MITSUBISHI_HEAVY
#define DAIKIN
#define SHARP_
#define CARRIER
#define PANASONIC_CKP
#define PANASONIC_CS
#define HYUNDAI
#define OLIMPIA // try model choice 3
#define GREE
#define GREE_YAC
#define FUEGO
#define TOSHIBA
#define NIBE
#define AIRWELL
#define HITACHI
#define SAMSUNG
#define BALLU
#define AUX
#define ZHLT01_REMOTE
#define ZHJG01_REMOTE
#define KY26_REMOTE
#else
// low-memory device,
// uncomment the define corresponding with your remote
//#define MITSUBISHI_ELECTRIC
//#define FUJITSU
//#define MITSUBISHI_HEAVY
//#define DAIKIN
//#define SHARP_
//#define CARRIER
//#define PANASONIC_CKP
//#define PANASONIC_CS
//#define HYUNDAI
//#define OLIMPIA
//#define GREE
//#define GREE_YAC
//#define FUEGO
//#define TOSHIBA
//#define NIBE
//#define AIRWELL
//#define HITACHI
//#define SAMSUNG
//#define BALLU
//#define AUX
//#define ZHLT01_REMOTE
//#define PHILCO
//#define KY26_REMOTE
#endif
#if !defined(MITSUBISHI_ELECTRIC)&&!defined(FUJITSU)&&!defined(MITSUBISHI_HEAVY)&&!defined(DAIKIN)&&!defined(SHARP_)&&!defined(CARRIER)&&!defined(PANASONIC_CKP)&&!defined(PANASONIC_CS)&&!defined(HYUNDAI)&&!defined(GREE)&&!defined(GREE_YAC)&&!defined(FUEGO)&&!defined(TOSHIBA)&&!defined(NIBE)&&!defined(AIRWELL)&&!defined(HITACHI)&&!defined(SAMSUNG)&&!defined(BALLU)&&!defined(AUX)&&!defined(ZHLT01_REMOTE)&&!defined(ZHJG01_REMOTE)&&!defined(PHILCO)&&!defined(KY26_REMOTE)
#error You must uncomment at least one brand define!!
#endif
#if defined(MITSUBISHI_ELECTRIC)
bool decodeMitsubishiElectric(byte *bytes, int byteCount);
#endif
#if defined(FUJITSU)
bool decodeFujitsu(byte *bytes, int byteCount);
#endif
#if defined(MITSUBISHI_HEAVY)
bool decodeMitsubishiHeavy(byte *bytes, int byteCount);
#endif
#if defined(DAIKIN)
bool decodeDaikin(byte *bytes, int byteCount);
#endif
#if defined(SHARP_)
bool decodeSharp(byte *bytes, int byteCount);
#endif
#if defined(CARRIER)
bool decodeCarrier(byte *bytes, int byteCount);
#endif
#if defined(PANASONIC_CKP)
bool decodePanasonicCKP(byte *bytes, int byteCount);
#endif
#if defined(PANASONIC_CS)
bool decodePanasonicCS(byte *bytes, int byteCount);
#endif
#if defined(HYUNDAI)
bool decodeHyundai(byte *bytes, int pulseCount);
#endif
#if defined(OLIMPIA)
bool decodeOlimpiaMaestro(byte *bytes, int pulseCount);
#endif
#if defined(GREE) or defined(GREE_YAC)
bool decodeGree(byte *bytes, int pulseCount);
bool decodeGree_YAC(byte *bytes, int pulseCount);
#endif
#if defined(FUEGO)
bool decodeFuego(byte *bytes, int byteCount);
#endif
#if defined(TOSHIBA)
bool decodeToshiba(byte *bytes, int byteCount);
#endif
#if defined(NIBE)
bool decodeNibe(byte *bytes, char* symbols, int bitCount);
#endif
#if defined(AIRWELL)
bool decodeAirwell(char* symbols, int bitCount);
#endif
#if defined(HITACHI)
bool decodeHitachi(byte *bytes, int byteCount);
#endif
#if defined(SAMSUNG)
bool decodeSamsung(byte *bytes, int byteCount);
#endif
#if defined(BALLU)
bool decodeBallu(byte *bytes, int byteCount);
#endif
#if defined(AUX)
bool decodeAUX(byte *bytes, int byteCount);
#endif
#if defined(ZHLT01_REMOTE)
bool decodeZHLT01remote(byte *bytes, int byteCount);
#endif
#if defined(ZHJG01_REMOTE)
bool decodeZHJG01remote(byte *bytes, int byteCount);
#endif
#if defined(PHILCO)
bool decodePhilco(byte *bytes, int byteCount);
#endif
#if defined(KY26_REMOTE)
bool decodeKY26Remote(byte *bytes, int byteCount);
#endif
/* Raw IR decoder sketch!
This sketch/program uses the Arduno and a PNA4602 to
decode IR received. This can be used to make a IR receiver
(by looking for a particular code)
or transmitter (by pulsing an IR LED at ~38KHz for the
durations detected
Code is public domain, check out www.ladyada.net and adafruit.com
for more tutorials!
*/
// We need to use the 'raw' pin reading methods
// because timing is very important here and the digitalRead()
// procedure is slower!
//uint8_t IRpin = 2;
// Digital pin #2 is the same as Pin D2 see
// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define IRpin_PIN PINE
#define IRpin 4
#elif defined(ESP32)
#define IRpin 25 // G25 on M5STACK ATOM LITE
#elif defined(ESP8266)
#define IRpin 2
#else
#define IRpin_PIN PIND
#define IRpin 2
#endif
// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 65000
// what our timing resolution should be, larger is better
// as its more 'precise' - but too large and you wont get
// accurate timing
uint16_t RESOLUTION=5;
// The thresholds for different symbols
uint16_t MARK_THRESHOLD_BIT_HEADER = 0; // Value between BIT MARK and HEADER MARK
uint16_t SPACE_THRESHOLD_ZERO_ONE = 0; // Value between ZERO SPACE and ONE SPACE
uint16_t SPACE_THRESHOLD_ONE_HEADER = 0; // Value between ONE SPACE and HEADER SPACE
uint16_t SPACE_THRESHOLD_HEADER_PAUSE = 0; // Value between HEADER SPACE and PAUSE SPACE (Panasonic/Midea only)
uint32_t mark_header_avg = 0;
uint16_t mark_header_cnt = 0;
uint32_t mark_bit_avg = 0;
uint16_t mark_bit_cnt = 0;
uint32_t space_zero_avg = 0;
uint16_t space_zero_cnt = 0;
uint32_t space_one_avg = 0;
uint16_t space_one_cnt = 0;
uint32_t space_header_avg = 0;
uint16_t space_header_cnt = 0;
uint32_t space_pause_avg = 0;
uint16_t space_pause_cnt = 0;
// we will store up to 1024 symbols
char symbols[1024]; // decoded symbols
uint16_t currentpulse = 0; // index for pulses we're storing
uint8_t modelChoice = 0;
// Decoded bytes
byte byteCount = 0;
byte bytes[128];
void setup(void) {
pinMode(IRpin, INPUT);
Serial.begin(9600);
delay(1000);
Serial.println(F("Select model to decode (this affects the IR signal timings detection):"));
Serial.println(F("* '1' for Panasonic DKE>, Mitsubishi Electric, Fujitsu etc. codes"));
Serial.println(F("* '2' for Panasonic CKP, Midea etc. codes"));
Serial.println(F("* '3' for Mitsubishi Heavy etc. codes"));
Serial.println(F("* '4' for Hyundai etc. codes"));
Serial.println(F("* '5' for Samsung etc. codes"));
Serial.println(F("* '6' for ZH/JG-01 codes"));
Serial.println(F("* '9' for entering the bit sequence on the serial monitor (instead of the IR receiver)"));
Serial.println();
Serial.print(F("Enter choice: "));
while (modelChoice == 0) {
#if defined(ESP8266)
yield();
#endif
int selection = Serial.read();
if ( selection != -1 ) {
Serial.print((char)selection);
switch ((char)selection) {
case '1':
modelChoice = 1;
break;
case '2':
modelChoice = 2;
break;
case '3':
modelChoice = 3;
break;
case '4':
modelChoice = 4;
break;
case '5':
modelChoice = 5;
break;
case '6':
modelChoice = 6;
break;
case '9':
modelChoice = 9;
break;
}
}
}
Serial.print(F("\n\nReady to decode IR for choice '"));
Serial.print(modelChoice);
Serial.println(F("'\n\n"));
if (modelChoice == 1) {
MARK_THRESHOLD_BIT_HEADER = 2000;
SPACE_THRESHOLD_ZERO_ONE = 800;
SPACE_THRESHOLD_ONE_HEADER = 1500;
SPACE_THRESHOLD_HEADER_PAUSE = 8000;
} else if (modelChoice == 2) {
MARK_THRESHOLD_BIT_HEADER = 2000;
SPACE_THRESHOLD_ZERO_ONE = 1800;
SPACE_THRESHOLD_ONE_HEADER = 3200;
SPACE_THRESHOLD_HEADER_PAUSE = 8000;
} else if (modelChoice == 3) {
MARK_THRESHOLD_BIT_HEADER = 2000;
SPACE_THRESHOLD_ZERO_ONE = 800;
SPACE_THRESHOLD_ONE_HEADER = 1400;
SPACE_THRESHOLD_HEADER_PAUSE = 8000;
} else if (modelChoice == 4) {
MARK_THRESHOLD_BIT_HEADER = 2000;
SPACE_THRESHOLD_ZERO_ONE = 800;
SPACE_THRESHOLD_ONE_HEADER = 2400;
SPACE_THRESHOLD_HEADER_PAUSE = 8000;
} else if (modelChoice == 5) {
MARK_THRESHOLD_BIT_HEADER = 2000;
SPACE_THRESHOLD_ZERO_ONE = 800;
SPACE_THRESHOLD_ONE_HEADER = 2400;
SPACE_THRESHOLD_HEADER_PAUSE = 10000;
} else if (modelChoice == 6) {
MARK_THRESHOLD_BIT_HEADER = 6500;
SPACE_THRESHOLD_ZERO_ONE = 2100;
SPACE_THRESHOLD_ONE_HEADER = 7750;
SPACE_THRESHOLD_HEADER_PAUSE = 9000;
}
}
void loop(void) {
memset(symbols, 0, sizeof(symbols));
memset(bytes, 0, sizeof(bytes));
currentpulse=0;
byteCount=0;
if (modelChoice != 9) {
receivePulses();
} else {
while ((currentpulse = Serial.readBytesUntil('\n', symbols+1, sizeof(symbols)-1)) == 0) {
#if defined(ESP8266)
yield();
#endif
}
currentpulse++;
}
// avoid false receiving positives
if (currentpulse > 1)
{
Serial.println("################# Start");
printPulses();
decodeProtocols();
Serial.println("################# End ");
Serial.println();
}
}
void receivePulses(void) {
uint16_t highpulse, lowpulse; // temporary storage timing
// Initialize the averages every time
mark_header_avg = 0;
mark_header_cnt = 0;
mark_bit_avg = 0;
mark_bit_cnt = 0;
space_zero_avg = 0;
space_zero_cnt = 0;
space_one_avg = 0;
space_one_cnt = 0;
space_header_avg = 0;
space_header_cnt = 0;
space_pause_avg = 0;
space_pause_cnt = 0;
// Only Panasonic seems to use the pause
space_pause_avg = 0;
space_pause_cnt = 0;
unsigned long start = micros();
unsigned long last = start;
while (currentpulse < sizeof(symbols))
{
#if defined(ESP8266)
yield();
#endif
highpulse = 0;
while (getPinState()) {
#if defined(ESP8266)
yield();
#endif
// pin is still HIGH
// count off another few microseconds
highpulse++;
delayMicroseconds(RESOLUTION);
// If the pulse is too long, we 'timed out' - either nothing
// was received or the code is finished, so print what
// we've grabbed so far, and then reset
if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
return;
}
}
// Instructions below here also take time, and need to be counted before the next high pulse.
unsigned long ts = micros();
highpulse = ts - last;
last = ts;
if (currentpulse > 0)
{
// this is a SPACE
if ( highpulse > SPACE_THRESHOLD_HEADER_PAUSE ) {
symbols[currentpulse] = 'W';
// Cumulative moving average, see http://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average
space_pause_avg = (highpulse + space_pause_cnt * space_pause_avg) / (space_pause_cnt + 1);
space_pause_cnt++;
} else if ( (currentpulse > 0 && symbols[currentpulse-1] == 'H') || highpulse > SPACE_THRESHOLD_ONE_HEADER ) {
symbols[currentpulse] = 'h';
// Cumulative moving average, see http://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average
if (highpulse > SPACE_THRESHOLD_ONE_HEADER)
space_header_avg = (highpulse + space_header_cnt * space_header_avg) / (space_header_cnt + 1);
space_header_cnt++;
} else if ( highpulse > SPACE_THRESHOLD_ZERO_ONE ) {
symbols[currentpulse] = '1';
space_one_avg = (highpulse + space_one_cnt * space_one_avg) / (space_one_cnt + 1);
space_one_cnt++;
} else {
symbols[currentpulse] = '0';
space_zero_avg = (highpulse + space_zero_cnt * space_zero_avg) / (space_zero_cnt + 1);
space_zero_cnt++;
}
}
currentpulse++;
// same as above
lowpulse = 0;
while (! getPinState()) {
#if defined(ESP8266)
yield();
#endif
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
return;
}
}
// this is a MARK
ts = micros();
lowpulse = ts - last;
last = ts;
if ( lowpulse > MARK_THRESHOLD_BIT_HEADER ) {
symbols[currentpulse] = 'H';
currentpulse++;
mark_header_avg = (lowpulse + mark_header_cnt * mark_header_avg) / (mark_header_cnt + 1);
mark_header_cnt++;
} else {
mark_bit_avg = (lowpulse + mark_bit_cnt * mark_bit_avg) / (mark_bit_cnt + 1);
mark_bit_cnt++;
}
// we read one high-low pulse successfully, continue!
}
}
bool getPinState() {
#if defined(ESP32)
return gpio_get_level(gpio_num_t(IRpin));
#elif defined(ESP8266)
return digitalRead(IRpin);
#else
return (IRpin_PIN & _BV(IRpin));
#endif
}
void printPulses(void) {
int bitCount = 0;
byte currentByte = 0;
Serial.print(F("\nNumber of symbols: "));
Serial.println(currentpulse);
// Print the symbols (0, 1, H, h, W)
Serial.println(F("Symbols:"));
//Serial.println("--1-------2-------3-------4-------5-------6-------7-------8-------9-------0-------1-------2-------3-------4-------5-------");
//Serial.println("--123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678");
Serial.println(symbols+1);
// Print the decoded bytes
Serial.println(F("Bytes:"));
// Decode the string of bits to a byte array from LSB first to MSB last for each byte
for (uint16_t i = 0; i < currentpulse; i++) {
if (symbols[i] == '0' || symbols[i] == '1') {
if (bitCount == 0) {
if (byteCount < 10)
Serial.print("0");
Serial.print(byteCount); Serial.print(": ");
}
currentByte >>= 1;
bitCount++;
if (symbols[i] == '1') {
currentByte |= 0x80;
}
Serial.print(symbols[i]);
if (bitCount == 4) {
Serial.print("|");
}
if (bitCount == 8) {
bytes[byteCount++] = currentByte;
bitCount = 0;
Serial.print(" | ");
printByte(currentByte);
Serial.print(" | ");
for (int mask = 0x80; mask > 0; mask >>= 1) {
Serial.print((currentByte & mask) ? "1" : "0");
}
Serial.println(" ");
}
} else { // Ignore bits which do not form octets
bitCount = 0;
currentByte = 0;
}
}
// Print the byte array
for (int i = 0; i < byteCount; i++) {
// if (bytes[i] < 0x10) {
// Serial.print(F("0"));
// }
// Serial.print(bytes[i],HEX);
printByte(bytes[i]);
if ( i < byteCount - 1 ) {
Serial.print(F(","));
}
}
Serial.println();
// Print the timing constants
Serial.println(F("Timings (in us): "));
Serial.print(F("PAUSE SPACE: "));
Serial.println(space_pause_avg);
Serial.print(F("HEADER MARK: "));
Serial.println(mark_header_avg);
Serial.print(F("HEADER SPACE: "));
Serial.println(space_header_avg);
Serial.print(F("BIT MARK: "));
Serial.println(mark_bit_avg);
Serial.print(F("ZERO SPACE: "));
Serial.println(space_zero_avg);
Serial.print(F("ONE SPACE: "));
Serial.println(space_one_avg);
}
void printByte(byte bytetoprint) {
if (bytetoprint < 0x10) {
Serial.print(F("0"));
}
Serial.print(bytetoprint, HEX);
}
void decodeProtocols()
{
Serial.println(F("Decoding known protocols..."));
bool knownProtocol;
#if defined(MITSUBISHI_ELECTRIC)
knownProtocol = decodeMitsubishiElectric(bytes, byteCount);
#endif
#if defined(FUJITSU)
knownProtocol = decodeFujitsu(bytes, byteCount);
#endif
#if defined(MITSUBISHI_HEAVY)
knownProtocol = decodeMitsubishiHeavy(bytes, byteCount);
#endif
#if defined(SHARP_)
knownProtocol = decodeSharp(bytes, byteCount);
#endif
#if defined(DAIKIN)
knownProtocol = decodeDaikin(bytes, byteCount);
#endif
#if defined(CARRIER)
knownProtocol = decodeCarrier(bytes, byteCount);
#endif
#if defined(PANASONIC_CKP)
knownProtocol = decodePanasonicCKP(bytes, byteCount);
#endif
#if defined(PANASONIC_CS)
knownProtocol = decodePanasonicCS(bytes, byteCount);
#endif
#if defined(HYUNDAI)
knownProtocol = decodeHyundai(bytes, currentpulse);
#endif
#if defined(OLIMPIA)
knownProtocol = decodeOlimpiaMaestro(bytes, byteCount);
#endif
#if defined(GREE) or defined(GREE_YAC)
knownProtocol = decodeGree(bytes, currentpulse) || decodeGree_YAC(bytes, currentpulse);
#endif
#if defined(FUEGO)
knownProtocol = decodeFuego(bytes, byteCount);
#endif
#if defined(TOSHIBA)
knownProtocol = decodeToshiba(bytes, byteCount);
#endif
#if defined(NIBE)
knownProtocol = decodeNibe(bytes, symbols, currentpulse);
#endif
#if defined(AIRWELL)
knownProtocol = decodeAirwell(symbols, currentpulse);
#endif
#if defined(HITACHI)
knownProtocol = decodeHitachi(bytes, byteCount);
#endif
#if defined(SAMSUNG)
knownProtocol = decodeSamsung(bytes, byteCount);
#endif
#if defined(BALLU)
knownProtocol = decodeBallu(bytes, byteCount);
#endif
#if defined(AUX)
knownProtocol = decodeAUX(bytes, byteCount);
#endif
#if defined(ZHLT01_REMOTE)
knownProtocol = decodeZHLT01remote(bytes, byteCount);
#endif
#if defined(ZHJG01_REMOTE)
knownProtocol = decodeZHJG01remote(bytes, byteCount);
#endif
#if defined(PHILCO)
knownProtocol = decodePhilco(bytes, byteCount);
#endif
#if defined(KY26_REMOTE)
knownProtocol = decodeKY26Remote(bytes, byteCount);
#endif
if (!knownProtocol)
{
Serial.println(F("Unknown protocol"));
Serial.print("Bytecount: ");
Serial.println(byteCount);
}
}