85 lines
2 KiB
C++
85 lines
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) {
|
|
if (command == "fade" || command == "f") fade_to(param.toInt());
|
|
if (command == "led" || command == "l") toggle_stripes(param);
|
|
if (command == "on" || command == "1") switch_230("on", param);
|
|
if (command == "off" || command == "0") switch_230("off", param);
|
|
|
|
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") rc_switch.switchOn(code, device);
|
|
if (state == "off") rc_switch.switchOff(code, device);
|
|
}
|
|
|
|
void toggle_stripes(String on_or_off) {
|
|
if (on_or_off == "on") {
|
|
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;
|
|
}
|
|
}
|
|
}
|