implement part of the protocol

This commit is contained in:
Shannon Sterz 2016-01-18 22:30:36 +01:00
parent ae943a451b
commit 41544ff508

View file

@ -2,7 +2,9 @@
RCSwitch mySwitch = RCSwitch(); RCSwitch mySwitch = RCSwitch();
int mode = 0; uint8_t message[8];
int index = 0;
boolean com = false;
boolean on = false; boolean on = false;
void setup() void setup()
@ -13,26 +15,72 @@ void setup()
void loop() void loop()
{ {
if(Serial.available()) { while(Serial.available()) {
mode = Serial.read(); if(index > 0 || (Serial.peek() >= 80 && Serial.peek() <= 95)) {
message[index++] = Serial.read();
if(index > 7){
decodeMessage();
index = 0;
}
}
}
}
switch (mode) { void decodeMessage() {
if(com) {
int cmd = message[0];
int pin = message[1] - 97;
int val[6];
memcpy(val, message + 2, 6);
case 126: switch (cmd) {
//case 80: break;
//case 81: break;
//case 82: break;
//case 83: break;
//case 84: break;
//case 85: break;
//case 86: break;
case 87: switchLight(val); break;
//case 88: break;
//case 89: break;
default: break;
}
} else if(message[0] == 90) {
com = true;
Serial.println("Ready!");
} else {
Serial.println("Arduino not configured for communication!");
}
}
void switchLight(int val[]) {
String triStateCode = "";
for(int i = 0; i < 6; i++)
{
String triStatePart = String(val[i], HEX);
triStatePart = (triStatePart.length() < 2) ? String("0" + triStatePart) : triStatePart;
triStateCode.concat(triStatePart);
}
char triState[triStateCode.length()];
triStateCode.toCharArray(triState, triStateCode.length());
if(on) { if(on) {
controlRCOutlets("0FFF0FFF0000"); controlRCOutlets(triState);
Serial.println("Light is off."); Serial.println("Light is off.");
on = false; on = false;
} else { } else {
controlRCOutlets("0FFF0FFF000F"); controlRCOutlets(triState);
Serial.println("Light is on."); Serial.println("Light is on.");
on = true; on = true;
} }
break;
}
}
} }
void controlRCOutlets(const char* sCodeWord) { void controlRCOutlets(const char* sCodeWord) {