From 9cbf8c2419f675de1952701d70c14df9e977549b Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Thu, 21 Jan 2016 17:43:29 +0100 Subject: [PATCH] add irRead functionality --- arduino/src/sketch.ino | 69 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/arduino/src/sketch.ino b/arduino/src/sketch.ino index 2e54e39..5fc5421 100644 --- a/arduino/src/sketch.ino +++ b/arduino/src/sketch.ino @@ -1,8 +1,16 @@ #include +#include + +// Necessary IR Globals +int RECV_PIN = 10; +IRrecv irrecv(RECV_PIN); +IRsend irsend; +decode_results results; uint8_t message[8]; int index = 0; boolean com = false; +int irReceive = 0; void setup() { @@ -11,16 +19,27 @@ void setup() void loop() { - while(Serial.available()) { - uint8_t x = Serial.read(); - if(index > 0 || (x >= 80 && x <= 95)) { - message[index++] = x; - if(index > 7){ - decodeMessage(); - index = 0; + + if (irReceive < millis() && irrecv.decode(&results)) { + + irRead(&results); + irrecv.resume(); + + } else { + + while(Serial.available()) { + uint8_t x = Serial.read(); + if(index > 0 || (x >= 80 && x <= 95)) { + message[index++] = x; + if(index > 7){ + decodeMessage(); + index = 0; + } } } + } + } void decodeMessage() { @@ -40,10 +59,12 @@ void decodeMessage() { //case 84: break; //case 85: break; //case 86: break; - case 87: sendRCTristate(val, pin); break; + case 87: sendRCTristate(val, pin); break; //case 88: break; - //case 89: break; - default: break; + case 89: irrecv.enableIRIn(); + irReceive = millis() + 10000; + break; + default: break; } } else if(cmd == 90) { @@ -90,8 +111,7 @@ void digitalR(int pin) { if(pin == -1) { Serial.println("badpin"); return; } pinMode(pin, INPUT); int digraw = digitalRead(pin); - String m = String(pin + '::' + digraw); - Serial.println(m); + Serial.println(String(pin + '::' + digraw)); } @@ -115,4 +135,29 @@ void sendRCTristate (uint8_t val[], int pin) { rc.sendTriState(triState); Serial.println("RC-Tristate send!"); +} + +void irRead(decode_results *results) { + + unsigned long codeValue; + int codeLen; + int codeType = results->decode_type; + + if (codeType == UNKNOWN) { + + Serial.println("Unknown IR Protocol!"); + + } else { + + if (codeType == NEC) { + if (results->value == REPEAT) { + return; + } + } + + codeValue = results->value; + codeLen = results->bits; + String m = String(codeType + "::" + codeValue); + Serial.println(String(m + "::" + codeLen)); + } } \ No newline at end of file