summaryrefslogtreecommitdiffstats
path: root/onkyo_ri.ino
blob: b2152e57a0ab9d5f0c6a6b67dd02da7969d0aa07 (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
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
/*

RI Codes:

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

*/

#include "signals.h"
//#include <SoftwareSerial.h>

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

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

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

uint8_t raw[2];

uint64_t listenForIR(ir_t *proto);
int getPulseLength();
int getPulseLengthInRange(int expected);

//SoftwareSerial wdlive_serial(11, 12);

void sendToggle(int pin, int delayTime[]) {
        digitalWrite(pin, LOW);
        delayMicroseconds(delayTime[0] * 10);
        digitalWrite(pin, HIGH);
        delayMicroseconds(delayTime[1] * 10);
}

void send(ir_t *proto, uint64_t value, int pin, bool inverted) {
        bool low = inverted;
        int i;
        uint8_t bit;

        for (i = 0; i < ARRAY_LEN(proto->header); i++) {
                digitalWrite(pin, low ? LOW : HIGH);
                delayMicroseconds(proto->header[i] * 10);
                low = !low;
        }

        for (i = proto->prebits - 1; i >= 0; i--) {
                bit = (proto->pre >> i) & 1;
                sendToggle(pin, proto->values[bit]);
        }

        for (i = proto->bits - 1; i >= 0; i--) {
                bit = (value >> i) & 1;
                sendToggle(pin, proto->values[bit]);
        }

        for (i = 0; i < ARRAY_LEN(proto->trail); i++) {
                digitalWrite(pin, low ? LOW : HIGH);
                delayMicroseconds(proto->trail[i] * 10);
                low = !low;
        }

        digitalWrite(pin, low ? LOW : HIGH);
}

void setup(void) {
        Serial.begin(9600);
        Serial.println("Ready to decode IR!");

//	wdlive_serial.begin(115200);
//	wdlive_serial.println("irinject down");

        pinMode(8, OUTPUT);
        digitalWrite(8, HIGH);
}

void findKey(ir_map_t *ir_map, uint8_t count, uint64_t code) {
        uint8_t i;

        for (i = 0; i < count; i++) {
                if (ir_map[i].from == code) {
                        send(&pioneer, ir_map[i].to, 8, true);
                        return;
                }
        }
}

void loop(void) {
        uint64_t code = listenForIR(&onkyo);
        Serial.println((long unsigned int)code, HEX);

        findKey(ir_map, ARRAY_LEN(ir_map), code);
}

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

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

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

                check = IRread_PIN & (1 << IRread);
                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 (length <= 0 || !inRange(length, expected)) {
                return 0;
        }

        return length;
}

uint64_t listenForIR(ir_t *proto) {
        int length;
        uint64_t code;
        uint8_t i, j, k;
        bool found;

start:
        code = 0;

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

                if (length <= 0)
                        goto start;
        }

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

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

                for (k = 0; k < ARRAY_LEN(proto->values); k++) {
                        found = true;

                        for (i = 0; i < ARRAY_LEN(raw); i++) {
                                if (!inRange(raw[i], proto->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(proto->trail); i++) {
                length = getPulseLengthInRange(proto->trail[i]);

                if (length <= 0)
                        goto start;
        }

        return code;
}