Code and the control script.

This commit is contained in:
Aaron Mueller 2014-06-11 22:40:23 +02:00
parent 6f13ebe1be
commit a87de80e2f
2 changed files with 96 additions and 0 deletions

8
ctrl.rb Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'serialport'
SerialPort.open('/dev/ttyUSB0', 9600, 8, 1, SerialPort::NONE) do |s|
s.puts(ARGV.join(' '))
end

88
emori.ino Normal file
View file

@ -0,0 +1,88 @@
// 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-to") fade_to(param.toInt());
if (command == "led-stripe") toggle_stripes(param);
if (command == "switch-on") switch_230("on", param);
if (command == "switch-off") 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;
}
}
}