2016-01-12 20:05:43 +01:00
|
|
|
#include <RCSwitch.h>
|
2016-01-21 17:43:29 +01:00
|
|
|
#include <IRremote.h>
|
|
|
|
|
|
|
|
// Necessary IR Globals
|
|
|
|
int RECV_PIN = 10;
|
|
|
|
IRrecv irrecv(RECV_PIN);
|
|
|
|
IRsend irsend;
|
|
|
|
decode_results results;
|
2016-01-12 20:05:43 +01:00
|
|
|
|
2016-01-18 22:30:36 +01:00
|
|
|
uint8_t message[8];
|
|
|
|
int index = 0;
|
|
|
|
boolean com = false;
|
2016-01-30 18:10:23 +01:00
|
|
|
boolean debug = false;
|
2016-01-21 17:43:29 +01:00
|
|
|
int irReceive = 0;
|
2016-01-11 21:09:53 +01:00
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
2016-01-12 20:05:43 +01:00
|
|
|
Serial.begin(9600);
|
2016-01-11 21:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2016-01-21 17:43:29 +01:00
|
|
|
|
2016-01-21 22:49:04 +01:00
|
|
|
if (irReceive > millis() && irrecv.decode(&results)) {
|
2016-01-21 17:43:29 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2016-01-18 22:30:36 +01:00
|
|
|
}
|
2016-01-12 20:05:43 +01:00
|
|
|
}
|
2016-01-21 17:43:29 +01:00
|
|
|
|
2016-01-12 20:05:43 +01:00
|
|
|
}
|
2016-01-21 17:43:29 +01:00
|
|
|
|
2016-01-12 20:05:43 +01:00
|
|
|
}
|
|
|
|
|
2016-01-18 22:30:36 +01:00
|
|
|
void decodeMessage() {
|
2016-01-20 16:46:11 +01:00
|
|
|
|
|
|
|
int cmd = message[0];
|
2016-01-21 22:49:04 +01:00
|
|
|
int pin = (message[1] - 97 < 0 || message[1] - 97 > 19)? -1 : message[1] - 97 ;
|
|
|
|
|
2016-01-18 22:30:36 +01:00
|
|
|
if(com) {
|
2016-01-18 23:17:34 +01:00
|
|
|
uint8_t val[6];
|
2016-01-18 22:30:36 +01:00
|
|
|
memcpy(val, message + 2, 6);
|
|
|
|
|
|
|
|
switch (cmd) {
|
2016-01-30 18:10:23 +01:00
|
|
|
case 80: debug = debug ? false : true; break;
|
2016-01-26 16:39:58 +01:00
|
|
|
case 81: setMode(val[0], pin); break;
|
|
|
|
case 82: digitalW(val[0], pin); break;
|
|
|
|
case 83: digitalR(pin); break;
|
|
|
|
case 84: analogW(val, pin); break;
|
|
|
|
case 85: analogR(pin); break;
|
|
|
|
case 86: handleRCDecimal(val, pin); break;
|
|
|
|
case 87: sendRCTristate(val, pin); break;
|
|
|
|
case 88: irSend(val[0], val[1], lastFourToLong(val)); break;
|
2016-01-21 17:43:29 +01:00
|
|
|
case 89: irrecv.enableIRIn();
|
|
|
|
irReceive = millis() + 10000;
|
|
|
|
break;
|
2016-01-26 16:39:58 +01:00
|
|
|
default: break;
|
2016-01-18 22:30:36 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 19:40:28 +01:00
|
|
|
} else if(cmd == 90) {
|
2016-01-18 22:30:36 +01:00
|
|
|
|
|
|
|
com = true;
|
2016-01-30 19:06:14 +01:00
|
|
|
setMode(OUTPUT, pin);
|
2016-01-20 16:46:11 +01:00
|
|
|
digitalWrite(pin, HIGH);
|
2016-01-18 22:30:36 +01:00
|
|
|
Serial.println("Ready!");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Serial.println("Arduino not configured for communication!");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 16:46:11 +01:00
|
|
|
void setMode(uint8_t val, int pin) {
|
|
|
|
|
2016-01-30 18:10:23 +01:00
|
|
|
if(pin == -1) { if(debug) Serial.println("badpin"); return; }
|
2016-01-20 16:46:11 +01:00
|
|
|
|
|
|
|
if (val == 0) {
|
|
|
|
pinMode(pin, OUTPUT);
|
2016-01-30 18:10:23 +01:00
|
|
|
if(debug) {Serial.print('Set OUTPUT on pin:');Serial.println(pin);}
|
2016-01-20 16:46:11 +01:00
|
|
|
} else {
|
|
|
|
pinMode(pin, INPUT);
|
2016-01-30 18:10:23 +01:00
|
|
|
if(debug) {Serial.print('Set INPUT on pin:');Serial.println(pin);}
|
2016-01-20 16:46:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:00:46 +01:00
|
|
|
void digitalW(uint8_t val, int pin) {
|
|
|
|
|
2016-01-30 18:10:23 +01:00
|
|
|
if(pin == -1) { if(debug) Serial.println("badpin"); return; }
|
2016-01-20 17:00:46 +01:00
|
|
|
pinMode(pin, OUTPUT);
|
|
|
|
|
|
|
|
if(val == 0) {
|
|
|
|
digitalWrite(pin, LOW);
|
|
|
|
} else {
|
|
|
|
digitalWrite(pin, HIGH);
|
|
|
|
}
|
2016-01-30 18:10:23 +01:00
|
|
|
if(debug) {
|
|
|
|
Serial.print(pin);
|
|
|
|
Serial.print(' wrote ');
|
|
|
|
Serial.println(val);
|
|
|
|
}
|
2016-01-20 17:00:46 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 19:40:28 +01:00
|
|
|
void digitalR(int pin) {
|
|
|
|
|
2016-01-30 18:10:23 +01:00
|
|
|
if(pin == -1) { if(debug) Serial.println("badpin"); return; }
|
2016-01-20 19:40:28 +01:00
|
|
|
pinMode(pin, INPUT);
|
|
|
|
int digraw = digitalRead(pin);
|
2016-01-28 19:32:36 +01:00
|
|
|
Serial.print(pin);
|
|
|
|
Serial.print('::');
|
|
|
|
Serial.println(digraw);
|
2016-01-20 19:40:28 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-25 22:02:42 +01:00
|
|
|
void analogW(uint8_t val[], int pin) {
|
|
|
|
|
2016-01-30 18:10:23 +01:00
|
|
|
if(pin == -1) { if(debug) Serial.println("badpin"); return; }
|
2016-01-26 16:39:58 +01:00
|
|
|
pinMode(pin, OUTPUT);
|
|
|
|
analogWrite(pin,firsTwoToInt(val));
|
2016-01-25 22:02:42 +01:00
|
|
|
|
2016-01-30 18:10:23 +01:00
|
|
|
if(debug) {
|
|
|
|
Serial.print(pin);
|
|
|
|
Serial.print(' wrote ');
|
|
|
|
Serial.println((int)val);
|
|
|
|
}
|
2016-01-25 22:02:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void analogR(int pin) {
|
|
|
|
|
2016-01-30 18:10:23 +01:00
|
|
|
if(pin == -1) { if(debug) Serial.println("badpin"); return; }
|
2016-01-26 16:39:58 +01:00
|
|
|
pinMode(pin, INPUT);
|
|
|
|
int rval = analogRead(pin);
|
2016-01-28 19:15:56 +01:00
|
|
|
Serial.print(pin);
|
|
|
|
Serial.print("::");
|
|
|
|
Serial.println(rval);
|
2016-01-28 19:32:36 +01:00
|
|
|
|
2016-01-25 22:02:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleRCDecimal(uint8_t val[], int pin) {
|
|
|
|
|
2016-01-30 18:10:23 +01:00
|
|
|
if (pin == -1) { if(debug) Serial.println("badpin"); return; }
|
2016-01-25 23:29:21 +01:00
|
|
|
unsigned int length = firsTwoToInt(val);
|
|
|
|
unsigned long value = lastFourToLong(val);
|
2016-01-25 22:02:42 +01:00
|
|
|
RCSwitch rc = RCSwitch();
|
2016-01-26 16:39:58 +01:00
|
|
|
rc.enableTransmit(pin);
|
2016-01-25 22:02:42 +01:00
|
|
|
rc.send(value, length);
|
2016-01-30 18:10:23 +01:00
|
|
|
if(debug) {
|
|
|
|
Serial.print('RC Decimal: ');
|
|
|
|
Serial.println(value);
|
|
|
|
}
|
2016-01-25 22:02:42 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 16:46:11 +01:00
|
|
|
void sendRCTristate (uint8_t val[], int pin) {
|
|
|
|
|
2016-01-30 18:10:23 +01:00
|
|
|
if(pin == -1) { if(debug) Serial.println("badpin"); return; }
|
2016-01-18 22:30:36 +01:00
|
|
|
|
|
|
|
String triStateCode = "";
|
2016-01-20 16:46:11 +01:00
|
|
|
RCSwitch rc = RCSwitch();
|
2016-01-20 19:40:28 +01:00
|
|
|
rc.enableTransmit(pin);
|
2016-01-18 22:30:36 +01:00
|
|
|
|
|
|
|
for(int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
String triStatePart = String(val[i], HEX);
|
|
|
|
triStatePart = (triStatePart.length() < 2) ? String("0" + triStatePart) : triStatePart;
|
|
|
|
triStateCode.concat(triStatePart);
|
|
|
|
}
|
2016-01-18 23:17:34 +01:00
|
|
|
char triState[triStateCode.length() + 1];
|
|
|
|
triStateCode.toUpperCase();
|
|
|
|
triStateCode.toCharArray(triState, triStateCode.length() + 1);
|
2016-01-18 22:30:36 +01:00
|
|
|
|
2016-01-20 19:40:28 +01:00
|
|
|
rc.sendTriState(triState);
|
2016-01-21 22:49:04 +01:00
|
|
|
delay(500);
|
2016-01-30 18:10:23 +01:00
|
|
|
if(debug) {
|
|
|
|
Serial.print("RC-Tristate send: ");
|
|
|
|
Serial.println(triState);
|
|
|
|
}
|
2016-01-21 17:43:29 +01:00
|
|
|
}
|
|
|
|
|
2016-01-25 23:29:21 +01:00
|
|
|
void irSend(int type, int length, unsigned long val) {
|
2016-01-28 19:32:36 +01:00
|
|
|
|
2016-01-25 23:29:21 +01:00
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
switch (type) {
|
|
|
|
case RC5: irsend.sendRC5(val, length); break;
|
|
|
|
case RC6: irsend.sendRC6(val, length); break;
|
|
|
|
case NEC: if (i) { irsend.sendNEC(REPEAT, length);
|
|
|
|
} else { irsend.sendNEC(val, length);} break;
|
|
|
|
case SONY: irsend.sendSony(val, length); break;
|
|
|
|
case PANASONIC: irsend.sendPanasonic(length, val); break;
|
|
|
|
case JVC: irsend.sendJVC(val, length, i); break;
|
|
|
|
case SAMSUNG: irsend.sendSAMSUNG(val, length); break;
|
|
|
|
case WHYNTER: irsend.sendWhynter(val, length); break;
|
|
|
|
case LG: irsend.sendLG(val, length); break;
|
|
|
|
case DISH: irsend.sendDISH(val, length); break;
|
|
|
|
case DENON: irsend.sendDenon(val, length); break;
|
|
|
|
}
|
|
|
|
delay(10);
|
|
|
|
}
|
2016-01-30 18:10:23 +01:00
|
|
|
if(debug) {Serial.println('IR send!');}
|
2016-01-25 23:29:21 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 17:43:29 +01:00
|
|
|
void irRead(decode_results *results) {
|
|
|
|
|
|
|
|
int codeType = results->decode_type;
|
|
|
|
|
|
|
|
if (codeType == UNKNOWN) {
|
|
|
|
|
2016-01-30 18:10:23 +01:00
|
|
|
if(debug) {Serial.println("Unknown IR Protocol!"); }
|
2016-01-21 17:43:29 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (codeType == NEC) {
|
|
|
|
if (results->value == REPEAT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 22:49:04 +01:00
|
|
|
unsigned long codeValue = results->value;
|
|
|
|
int codeLen = results->bits;
|
2016-01-28 19:32:36 +01:00
|
|
|
Serial.print(codeType);
|
|
|
|
Serial.print('::');
|
|
|
|
Serial.print(codeLen);
|
|
|
|
Serial.print('::');
|
|
|
|
Serial.println(codeValue);
|
2016-01-21 17:43:29 +01:00
|
|
|
}
|
2016-01-21 22:49:04 +01:00
|
|
|
}
|
2016-01-25 23:29:21 +01:00
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
|
|
|
unsigned int firsTwoToInt(uint8_t val[]) {
|
|
|
|
return val[0] * 256 + val[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long lastFourToLong(uint8_t val[]) {
|
|
|
|
return val[2]*16777216 + val[3]*65536 + val[4]*256 + val[5];
|
|
|
|
}
|