forked from ToniA/Raw-IR-decoder-for-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawirdecode.ino
398 lines (338 loc) · 11.8 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
#include <Arduino.h>
bool decodeMitsubishiElectric(byte *bytes, int byteCount);
bool decodeFujitsu(byte *bytes, int byteCount);
bool decodeMitsubishiHeavy(byte *bytes, int byteCount);
bool decodeDaikin(byte *bytes, int byteCount);
bool decodeSharp(byte *bytes, int byteCount);
bool decodeCarrier(byte *bytes, int byteCount);
bool decodePanasonicCKP(byte *bytes, int byteCount);
bool decodePanasonicCS(byte *bytes, int byteCount);
bool decodeHyundai(byte *bytes, int pulseCount);
bool decodeGree(byte *bytes, int pulseCount);
bool decodeGree_YAC(byte *bytes, int pulseCount);
bool decodeFuego(byte *bytes, int byteCount);
bool decodeToshiba(byte *bytes, int byteCount);
bool decodeNibe(byte *bytes, char* symbols, int bitCount);
bool decodeHitachi(byte *bytes, int byteCount);
bool decodeSamsung(byte *bytes, int byteCount);
bool decodeBallu(byte *bytes, int byteCount);
bool decodeAUX(byte *bytes, int byteCount);
bool decodeZHLT01remote(byte *bytes, int byteCount);
/* 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
#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=20;
// 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) {
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("* '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) {
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 '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;
}
}
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) {}
currentpulse++;
}
printPulses();
decodeProtocols();
}
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;
while (currentpulse < sizeof(symbols))
{
highpulse = 0;
while (IRpin_PIN & (1 << IRpin)) {
// 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;
}
}
highpulse = highpulse * RESOLUTION;
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;
} 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
space_header_avg = (highpulse + space_header_cnt * space_header_avg) / ++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;
} else {
symbols[currentpulse] = '0';
space_zero_avg = (highpulse + space_zero_cnt * space_zero_avg) / ++space_zero_cnt;
}
}
currentpulse++;
// same as above
lowpulse = 0;
while (! (IRpin_PIN & _BV(IRpin))) {
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
return;
}
}
// this is a MARK
lowpulse = lowpulse * RESOLUTION;
if ( lowpulse > MARK_THRESHOLD_BIT_HEADER ) {
symbols[currentpulse] = 'H';
currentpulse++;
mark_header_avg = (lowpulse + mark_header_cnt * mark_header_avg) / ++mark_header_cnt;
} else {
mark_bit_avg = (lowpulse + mark_bit_cnt * mark_bit_avg) / ++mark_bit_cnt;
}
// we read one high-low pulse successfully, continue!
}
}
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
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..."));
if ( ! (decodeMitsubishiElectric(bytes, byteCount) ||
decodeFujitsu(bytes, byteCount) ||
decodeMitsubishiHeavy(bytes, byteCount) ||
decodeSharp(bytes, byteCount) ||
decodeDaikin(bytes, byteCount) ||
decodeCarrier(bytes, byteCount) ||
decodeCarrier(bytes, byteCount) ||
decodePanasonicCKP(bytes, byteCount) ||
decodePanasonicCS(bytes, byteCount) ||
decodeHyundai(bytes, currentpulse) ||
decodeGree(bytes, currentpulse) ||
decodeGree_YAC(bytes, currentpulse) ||
decodeFuego(bytes, byteCount) ||
decodeToshiba(bytes, byteCount) ||
decodeNibe(bytes, symbols, currentpulse) ||
decodeHitachi(bytes, byteCount) ||
decodeSamsung(bytes, byteCount) ||
decodeBallu(bytes, byteCount) ||
decodeAUX(bytes, byteCount) ||
decodeZHLT01remote(bytes, byteCount)
))
{
Serial.println(F("Unknown protocol"));
Serial.print("Bytecount: ");
Serial.println(byteCount);
}
}