add irRead functionality

This commit is contained in:
Shannon Sterz 2016-01-21 17:43:29 +01:00
parent 569c6d96ac
commit 9cbf8c2419

View file

@ -1,8 +1,16 @@
#include <RCSwitch.h> #include <RCSwitch.h>
#include <IRremote.h>
// Necessary IR Globals
int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;
uint8_t message[8]; uint8_t message[8];
int index = 0; int index = 0;
boolean com = false; boolean com = false;
int irReceive = 0;
void setup() void setup()
{ {
@ -11,16 +19,27 @@ void setup()
void loop() void loop()
{ {
while(Serial.available()) {
uint8_t x = Serial.read(); if (irReceive < millis() && irrecv.decode(&results)) {
if(index > 0 || (x >= 80 && x <= 95)) {
message[index++] = x; irRead(&results);
if(index > 7){ irrecv.resume();
decodeMessage();
index = 0; } 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() { void decodeMessage() {
@ -40,10 +59,12 @@ void decodeMessage() {
//case 84: break; //case 84: break;
//case 85: break; //case 85: break;
//case 86: break; //case 86: break;
case 87: sendRCTristate(val, pin); break; case 87: sendRCTristate(val, pin); break;
//case 88: break; //case 88: break;
//case 89: break; case 89: irrecv.enableIRIn();
default: break; irReceive = millis() + 10000;
break;
default: break;
} }
} else if(cmd == 90) { } else if(cmd == 90) {
@ -90,8 +111,7 @@ void digitalR(int pin) {
if(pin == -1) { Serial.println("badpin"); return; } if(pin == -1) { Serial.println("badpin"); return; }
pinMode(pin, INPUT); pinMode(pin, INPUT);
int digraw = digitalRead(pin); int digraw = digitalRead(pin);
String m = String(pin + '::' + digraw); Serial.println(String(pin + '::' + digraw));
Serial.println(m);
} }
@ -116,3 +136,28 @@ void sendRCTristate (uint8_t val[], int pin) {
rc.sendTriState(triState); rc.sendTriState(triState);
Serial.println("RC-Tristate send!"); 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));
}
}