summaryrefslogblamecommitdiffstats
path: root/onkyo_ri.pde
blob: 3fa28086d9eb71e2d3bd256a6815b58464f749ff (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
















                                                                       

                            
 
                                                              




                     

               
                                  









                                              
                                            



                                  
                                               










                                              
                                                   





















                                                    
                                   

                      

                        



                 

                                                                 
 
                                


                                   
                                           


                                                      
                                        


                                           
                                                                


                                                              
                                                                            



















                                                      

                                                                
 
                                




                                   
/*

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 IRread_PIN      PINB
#define IRread          1

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

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

uint8_t raw[2];

uint64_t listenForIR(ir_t *proto);
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(&onkyo);
        Serial.println(code, HEX);
}

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 (!inRange(length, expected)) {
                length = 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;
}