Created
February 24, 2017 22:48
-
-
Save gjbagrowski/25193247c9b41e62fdf9f90683b826a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "RCSwitch/RCSwitch.h" | |
RCSwitch mySwitch = RCSwitch(); | |
int ledPin = D7; | |
int inputPin = D3; | |
int outputPin = D2; | |
int flagsCount = 6; | |
bool lightFlags[6]; | |
char* onCodes[] = { | |
"F0FFFFFF0101", | |
"F0FFFFFF1001", | |
"F0FFFFF10001", | |
// second set | |
"FF0FFFFF0101", | |
"FF0FFFFF1001", | |
"FF0FFFF10001", | |
}; | |
char* offCodes[] = { | |
"F0FFFFFF0110", | |
"F0FFFFFF1010", | |
"F0FFFFF10010", | |
// second set | |
"FF0FFFFF0110", | |
"FF0FFFFF1010", | |
"FF0FFFF10010", | |
}; | |
static char *bin2tristate(char *bin) { | |
char returnValue[50]; | |
for (int i=0; i<50; i++) { | |
returnValue[i] = '\0'; | |
} | |
int pos = 0; | |
int pos2 = 0; | |
while (bin[pos]!='\0' && bin[pos+1]!='\0') { | |
if (bin[pos]=='0' && bin[pos+1]=='0') { | |
returnValue[pos2] = '0'; | |
} else if (bin[pos]=='1' && bin[pos+1]=='1') { | |
returnValue[pos2] = '1'; | |
} else if (bin[pos]=='0' && bin[pos+1]=='1') { | |
returnValue[pos2] = 'F'; | |
} else { | |
return "not applicable"; | |
} | |
pos = pos+2; | |
pos2++; | |
} | |
returnValue[pos2] = '\0'; | |
return returnValue; | |
} | |
void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) { | |
if (decimal == 0) { | |
Serial.print("Unknown encoding."); | |
} else { | |
char* b = mySwitch.dec2binWzerofill(decimal, length); | |
char* tristate = bin2tristate(b); | |
Serial.print("Decimal: "); | |
Serial.print(decimal); | |
Serial.print(" ("); | |
Serial.print( length ); | |
Serial.print("Bit) Binary: "); | |
Serial.print( b ); | |
Serial.print(" Tri-State: "); | |
Serial.print( tristate ); | |
Serial.print(" PulseLength: "); | |
Serial.print(delay); | |
Serial.print(" microseconds"); | |
Serial.print(" Protocol: "); | |
Serial.println(protocol); | |
Spark.publish("tristate-received", String(delay) + " " + String(tristate)); | |
} | |
Serial.print("Raw data: "); | |
for (int i=0; i<= length*2; i++) { | |
Serial.print(raw[i]); | |
Serial.print(","); | |
} | |
Serial.println(); | |
Serial.println(); | |
} | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Listening"); | |
pinMode(inputPin, INPUT_PULLDOWN); | |
mySwitch.enableTransmit(outputPin); | |
// mySwitch.enableReceive(inputPin); | |
mySwitch.setPulseLength(180); | |
pinMode(ledPin, OUTPUT); | |
Particle.function("on", allOnHandler); | |
Particle.function("off", allOffHandler); | |
Particle.function("toggle", toggleHandler); | |
for (int i=0; i <= flagsCount; i++) { | |
Particle.variable(String("light-") + String(i), lightFlags[i]); | |
}; | |
} | |
void loop() { | |
// (mySwitch.available()) { | |
// output(mySwitch.getReceivhttps://build.particle.io/build/57b5ff016c1c21e51f00086c#flashedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(), mySwitch.getReceivedProtocol()); | |
// mySwitch.resetAvailable(); | |
// } | |
// setLight(0, true); | |
// delay(20); | |
// setLight(0, false); | |
// delay(20); | |
} | |
int allOnHandler(String args) { | |
for (int i=0; i <= flagsCount; i++) { | |
setLight(i, true); | |
//delay(20); | |
} | |
return 1; | |
} | |
int allOffHandler(String args) { | |
for (int i=0; i <= flagsCount; i++) { | |
setLight(i, false); | |
//delay(20); | |
} | |
return 1; | |
} | |
bool setLight(int lightIndex, bool value) { | |
lightFlags[lightIndex] = value; | |
// Particle.variable(String("light-") + String(lightIndex), value ? 1: 0); | |
char* code; | |
if (value) { | |
code = onCodes[lightIndex]; | |
} else { | |
code = offCodes[lightIndex]; | |
} | |
mySwitch.sendTriState(code); | |
return value; | |
} | |
int toggleHandler(String args) { | |
int val = args.toInt(); | |
if (setLight(val, !lightFlags[val])) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment