emori/emori.ino

102 lines
2.2 KiB
C++

// https://code.google.com/p/rc-switch/
#include <RCSwitch.h>
int led_stripe = 9;
int fade_value = 0;
String command = "";
boolean command_complete = false;
RCSwitch rc_switch = RCSwitch();
void setup() {
pinMode(led_stripe, OUTPUT);
Serial.begin(9600);
command.reserve(200);
rc_switch.enableTransmit(10);
}
void process_command(String command, String param) {
// LED Stripes
if (command == "fade" || command == "f") fade_to(param.toInt());
if (command == "led" || command == "l") toggle_stripes(param);
// Radio switches
if (command == "a" ||
command == "b" ||
command == "c" ||
command == "d") switch_230(param, command);
// Turn all on or off
if (command == "off" || command == "on") {
switch_230(command, "a");
switch_230(command, "b");
switch_230(command, "c");
switch_230(command, "d");
toggle_stripes(command);
}
Serial.println("Ok");
}
void switch_230(String state, String number) {
char* code = "10011";
char* device = "10000";
if (number == "b") device = "01000";
if (number == "c") device = "00100";
if (number == "d") device = "00010";
if (number == "e") device = "00001";
if (state == "on" || state == "1") {
rc_switch.switchOn(code, device);
} else {
rc_switch.switchOff(code, device);
}
}
void toggle_stripes(String on_or_off) {
if (on_or_off == "on" || on_or_off == "1") {
fade_to(255);
digitalWrite(led_stripe, HIGH);
} else {
fade_to(0);
digitalWrite(led_stripe, LOW);
}
}
void fade_to(int value) {
int direction = -1;
if (value > fade_value) direction = 1;
for (fade_value; fade_value != value; fade_value += direction) {
analogWrite(led_stripe, fade_value);
delay(5);
}
}
void loop() {
// Process command?
if (command_complete) {
// Split the command and the params
int pos = command.indexOf(' ');
String cmd = command.substring(0, pos);
String param = command.substring(pos+1);
process_command(cmd, param);
command = "";
command_complete = false;
}
}
void serialEvent() {
while (Serial.available()) {
char rx_char = (char)Serial.read();
// Command complete?
if (rx_char == '\n') {
command_complete = true;
} else {
command += rx_char;
}
}
}