/* 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; }