summaryrefslogtreecommitdiffstats
path: root/onkyo_ri.pde
blob: 89adcfc69bfd9b376ac4a3c06a769434b2f7ad68 (plain) (blame)
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
/*

RI Codes:

TAPE = 42157
DVD = 31612
CD = 71327
PORT = 81993
CDR = 71322
MD = 71808

*/

#include "signals.h"

// Digital pin #2 is the same as Pin D2 see
// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping
#define IRpin_PIN      PINB
#define IRpin          0

#define ARRAY_LEN(array)    (sizeof(array)/sizeof(array[0]))

#define MAXPULSE 500
#define RESOLUTION 20
#define FUZZINESS 20

int header[] = {300, 100};
int values[2][2] = { {100, 100}, {100, 200} };
int trail[] = { 100 };
uint8_t bits = 12;

uint8_t raw[2];

uint64_t listenForIR(void);
int getPulseLength();
int getPulseLengthInRange(int expected);

void setup(void) {
        Serial.begin(9600);
        Serial.println("Ready to decode IR!");
        pinMode(13, OUTPUT);
}

void loop(void) {
        uint64_t code = listenForIR();
        Serial.println(code, HEX);
}

int getPulseLength() {
        bool high = IRpin_PIN & (1 << IRpin);
        bool check = true;
        int length = 0;

        while (check) {
                length++;
                delayMicroseconds(RESOLUTION);

                if (length >= MAXPULSE) {
                        return -1;
                }

                check = IRpin_PIN & (1 << IRpin);
                if (! high) {
                        check = ! check;
                }
        }

        return length * RESOLUTION / 10;
}

bool inRange(int value, int expected) {
        return (abs(value - expected) <= FUZZINESS);
}

int getPulseLengthInRange(int expected) {
        int length = getPulseLength();

        if (!inRange(length, expected)) {
                length = 0;
        }

        return length;
}

uint64_t listenForIR(void) {
        int length;
        uint64_t code;
        uint8_t i;

start:
        code = 0;

        for (i = 0; i < ARRAY_LEN(header); i++) {
                length = getPulseLengthInRange(header[i]);

                if ((length == 0) || (length < 0))
                        goto start;
        }

        for (uint8_t j = 0; j < bits; j++) {
                for (i = 0; i < ARRAY_LEN(raw); i++) {
                        raw[i] = getPulseLength();

                        if ((raw[i] == 0) || (raw[i] < 0))
                                goto start;
                }

                bool found;

                for (uint8_t k = 0; k < ARRAY_LEN(values); k++) {
                        found = true;

                        for (i = 0; i < ARRAY_LEN(raw); i++) {
                                if (!inRange(raw[i], values[k][i])) {
                                        found = false;
                                        break;
                                }
                        }

                        if (found) {
                                code <<= 1;
                                code += k;

                                break;
                        }
                }

                if (!found) {
                        Serial.println(raw[0]);
                        Serial.println(raw[1]);
                        goto start;
                }
        }

        for (i = 0; i < ARRAY_LEN(trail); i++) {
                length = getPulseLengthInRange(trail[i]);

                if ((length == 0) || (length < 0))
                        goto start;
        }

        return code;
}