Statistics
| Revision:

root / Ports / examples / ir_recv_irq / ir_recv_irq.pde

History | View | Annotate | Download (1.6 KB)

1
//>>> The latest version of this code can be found at https://github.com/jcw/ !!
2
3
// Infrared receiver using the InfraredPlug class (interrupt version)
4
// 2010-10-12 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
5
// $Id: ir_recv_irq.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
ISR(PCINT1_vect) {
14
    ir.poll();
15
}
16
17
void setup () {
18
    Serial.begin(57600);
19
    Serial.println("\n[ir_recv_irq]");
20
21
    // enable pin change interrupts on PC1
22
    PCMSK1 = bit(1);
23
    PCICR |= bit(PCIE1);
24
}
25
26
void loop () {
27
    if (Serial.available()) {
28
        char c = Serial.read();
29
        if ('0' <= c && c <= '9')
30
            value = 10 * value + c - '0';
31
        else {
32
            if (c == 's')
33
                ir.configure(value); // 20 ms end-of-data gap
34
            value = 0;
35
        }
36
    }
37
    
38
    byte count = ir.done();
39
    if (count > 0) {
40
        const byte* data = ir.buffer();
41
        Serial.print("IR ");
42
        switch (ir.decoder(count)) {
43
            case InfraredPlug::NEC:
44
                Serial.print("NEC");
45
                for (byte i = 0; i < 4; ++i) {
46
                    Serial.print(' ');
47
                    Serial.print(data[i], DEC);
48
                }
49
                break;
50
            case InfraredPlug::NEC_REP:
51
                Serial.print("NEC REPEAT");
52
                break;
53
            default:
54
                for (byte i = 0; i < count; ++i) {
55
                    byte nibble = (data[i/2] >> (i % 2) * 4) & 0x0F;
56
                    Serial.print(nibble, HEX);
57
                }
58
        }
59
        Serial.println();
60
    }
61
}