spejsiot/projection-screen/app/application.cpp

116 lines
3.2 KiB
C++

#include <SpejsNode.h>
#include <Libraries/RCSwitch/RCSwitch.h>
class ProjectionScreenEndpoint: public Endpoint {
private:
RCSwitch rc;
int codeStop = 0b000110011110110100001000;
int codeUp = 0b000110011110110100000010;
int codeDown = 0b000110011110110100000100;
public:
ProjectionScreenEndpoint() : Endpoint("rcswitch") {
rc.enableTransmit(5); // pin GPIO5 - transmit
rc.setRepeatTransmit(5);
}
EndpointResult onValue(String property, String value) {
if (property == "down") {
if (value == "1" || value == "true") {
rc.send(codeDown, 24);
delay(200);
rc.send(codeDown, 24);
notify("down", "true");
} else {
rc.send(codeUp, 24);
delay(200);
rc.send(codeUp, 24);
notify("down", "false");
}
return 200;
}
return 400;
}
};
class PanasonicProjectorEndpoint: public Endpoint {
private:
const char STARTBYTE = 0x02;
const char ENDBYTE = 0x03;
void send(String message) {
Serial.write(STARTBYTE);
Serial.print(message);
Serial.write(ENDBYTE);
}
enum {
WAITFORSTART,
WAITFOREND,
} serialState = WAITFORSTART;
String buf;
void serialCallback(Stream& stream, char arrivedChar, unsigned short availableCharsCount) {
while(stream.available()) {
char byte = stream.read();
if (serialState == WAITFORSTART) {
if (byte == STARTBYTE) {
serialState = WAITFOREND;
buf = "";
} else {
debugf("panasonic: Invalid byte: %d", byte);
}
} else {
if (byte == ENDBYTE) {
serialState = WAITFORSTART;
processMessage(buf);
} else {
buf += byte;
}
}
}
}
void processMessage(String& msg) {
debugf("got message: %s", msg.c_str());
if (msg == "PON") {
notify("power", "true");
} else if (msg == "POF") {
notify("power", "false");
} else if (msg.startsWith("IIS:")) {
notify("source", msg.substring(4));
}
}
public:
PanasonicProjectorEndpoint() : Endpoint("display") { }
void bind(String _name, SpejsNode* _parent) {
Endpoint::bind(_name, _parent);
Serial.setCallback(StreamDataReceivedDelegate(&PanasonicProjectorEndpoint::serialCallback, this));
}
EndpointResult onValue(String property, String value) {
if (property == "power") {
send(value == "true" ? "PON" : "POF");
return 200;
} else if (property == "source") {
send("IIS:" + value);
return 200;
}
return 400;
}
};
SpejsNode node("projection-screen");
void init() {
node.statusLED.config(2, LOW);
node.init();
Serial.begin(9600);
node.registerEndpoint("screen", new ProjectionScreenEndpoint());
node.registerEndpoint("projector", new PanasonicProjectorEndpoint());
}