Statistics
| Revision:

root / Ports / examples / ir_recv / ir_recv.pde

History | View | Annotate | Download (1.5 KB)

1
//>>> The latest version of this code can be found at https://github.com/jcw/ !!
2
3
// Infrared receiver using the InfraredPlug class (polled version)
4
// 2010-10-12 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
5
// $Id: ir_recv.pde 7763 2011-12-11 01:28:16Z jcw $
6
7
#include <Ports.h>
8
#include <RF12.h> // needed to avoid a linker error :(
9
10
InfraredPlug ir (2);
11
byte value;
12
13
void setup () {
14
    Serial.begin(57600);
15
    Serial.println("\n[ir_recv]");
16
}
17
18
void loop () {
19
    if (Serial.available()) {
20
        char c = Serial.read();
21
        if ('0' <= c && c <= '9')
22
            value = 10 * value + c - '0';
23
        else {
24
            if (c == 's')
25
                ir.configure(value); // 20 ms end-of-data gap
26
            value = 0;
27
        }
28
    }
29
    
30
    ir.poll();
31
    
32
    byte count = ir.done();
33
    if (count > 0) {
34
        const byte* data = ir.buffer();
35
        Serial.print("IR ");
36
        switch (ir.decoder(count)) {
37
            case InfraredPlug::NEC:
38
                Serial.print("NEC");
39
                for (byte i = 0; i < 4; ++i) {
40
                    Serial.print(' ');
41
                    Serial.print(data[i], DEC);
42
                }
43
                break;
44
            case InfraredPlug::NEC_REP:
45
                Serial.print("NEC REPEAT");
46
                break;
47
            default:
48
                for (byte i = 0; i < count; ++i) {
49
                    byte nibble = (data[i/2] >> (i % 2) * 4) & 0x0F;
50
                    Serial.print(nibble, HEX);
51
                }
52
        }
53
        Serial.println();
54
    }
55
}