diff --git a/examples/adjustable-baud.js b/examples/adjustable-baud.js index f897045..6e1b8f0 100644 --- a/examples/adjustable-baud.js +++ b/examples/adjustable-baud.js @@ -3,7 +3,7 @@ var arduino = require('../'); var board = new arduino.Board({ debug: true, baudrate: 9600 -}); +}).setup(); var led = new arduino.Led({ board: board, diff --git a/examples/adjustable-port.js b/examples/adjustable-port.js index 3815493..e6b64d3 100644 --- a/examples/adjustable-port.js +++ b/examples/adjustable-port.js @@ -3,7 +3,7 @@ var arduino = require('../'); var board = new arduino.Board({ debug: true, device: "ACM" -}); +}).setup(); var led = new arduino.Led({ board: board, @@ -12,4 +12,4 @@ var led = new arduino.Led({ board.on('ready', function(){ led.blink(); -}); \ No newline at end of file +}); diff --git a/examples/analogled.js b/examples/analogled.js index 66d891a..3c3836d 100644 --- a/examples/analogled.js +++ b/examples/analogled.js @@ -2,7 +2,7 @@ var arduino = require('../'); var board = new arduino.Board({ debug: true -}); +}).setup(); var aled = new arduino.Led({ board: board, diff --git a/examples/basic.js b/examples/basic.js index ca573c0..d0e148f 100644 --- a/examples/basic.js +++ b/examples/basic.js @@ -1,7 +1,7 @@ var arduino = require('../'); -var board = new arduino.Board(); +var board = new arduino.Board().setup(); board.on('connected', function(){ board.write('HELLO WORLD'); diff --git a/examples/button.js b/examples/button.js index 1ba9ad6..d42bc66 100644 --- a/examples/button.js +++ b/examples/button.js @@ -1,6 +1,6 @@ var arduino = require('../'); -var board = new arduino.Board(); +var board = new arduino.Board().setup(); var button = new arduino.Button({ board: board, diff --git a/examples/combination.js b/examples/combination.js index a5812e4..b088d4f 100644 --- a/examples/combination.js +++ b/examples/combination.js @@ -1,6 +1,6 @@ var arduino = require('../'); -var board = new arduino.Board(); +var board = new arduino.Board().setup(); var button = new arduino.Button({ board: board, diff --git a/examples/ir.js b/examples/ir.js index 2b75052..8e977ed 100644 --- a/examples/ir.js +++ b/examples/ir.js @@ -4,7 +4,7 @@ var arduino = require('../') var board = new arduino.Board({ debug: true -}); +}).setup(); var ir = new arduino.IR({ board: board diff --git a/examples/led.js b/examples/led.js index 1325284..8163f9f 100644 --- a/examples/led.js +++ b/examples/led.js @@ -2,11 +2,11 @@ var arduino = require('../'); var board = new arduino.Board({ debug: true -}); +}).setup(); var led = new arduino.Led({ board: board, - pin: "A0" + pin: "13" }); board.on('ready', function(){ diff --git a/examples/piezo.js b/examples/piezo.js index dd59278..f86bb16 100644 --- a/examples/piezo.js +++ b/examples/piezo.js @@ -2,7 +2,7 @@ var arduino = require('../'); var board = new arduino.Board({ debug: true -}); +}).setup(); var piezo = new arduino.Piezo({ board: board diff --git a/examples/ping.js b/examples/ping.js index c50499c..b7a4a63 100644 --- a/examples/ping.js +++ b/examples/ping.js @@ -3,7 +3,7 @@ var arduino = require('../'), board = new arduino.Board({ debug: false -}); +}).setup(); ping = new arduino.Ping({ board: board, diff --git a/examples/pir.js b/examples/pir.js index fd3e788..adcd2f7 100644 --- a/examples/pir.js +++ b/examples/pir.js @@ -3,7 +3,7 @@ var arduino = require('../'), board = new arduino.Board({ debug: false -}); +}).setup(); pir = new arduino.PIR({ board: board, diff --git a/examples/rc.js b/examples/rc.js index 85031ec..a535a73 100644 --- a/examples/rc.js +++ b/examples/rc.js @@ -3,7 +3,7 @@ var arduino = require('../') var board = new arduino.Board({ debug: true -}); +}).setup(); var rc = new arduino.RC({ board: board, diff --git a/examples/sensor-throttled.js b/examples/sensor-throttled.js index 1e4ed85..2abeea5 100644 --- a/examples/sensor-throttled.js +++ b/examples/sensor-throttled.js @@ -3,7 +3,7 @@ var arduino = require('../'), board = new arduino.Board({ debug: false -}); +}).setup(); sensor = new arduino.Sensor({ board: board, diff --git a/examples/sensor.js b/examples/sensor.js index b1a105a..94802d6 100644 --- a/examples/sensor.js +++ b/examples/sensor.js @@ -3,7 +3,7 @@ var arduino = require('../'), board = new arduino.Board({ debug: true -}); +}).setup(); sensor = new arduino.Sensor({ board: board, diff --git a/examples/servo.js b/examples/servo.js index eb732ab..c240b04 100644 --- a/examples/servo.js +++ b/examples/servo.js @@ -4,7 +4,7 @@ var arduino = require('../'), // Construct instances board = new arduino.Board({ debug: true -}); +}).setup(); led = new arduino.Led({ board: board, diff --git a/lib/board.js b/lib/board.js index 6f66e3c..68afc3b 100644 --- a/lib/board.js +++ b/lib/board.js @@ -1,4 +1,3 @@ - var events = require('events'), child = require('child_process'), util = require('util'), @@ -7,24 +6,37 @@ var events = require('events'), /* * The main Arduino constructor - * Connect to the serial port and bind + * + * [NEW] Does *not* open a serial connection. Add a `setup()` call to do so. + * + * This allows the user to add a listener for error events that occur + * during the serial connection attempts. */ var Board = function (options) { this.log('info', 'initializing'); - this.debug = options && options.debug || false; - this.device = options && options.device || 'usb|ttyACM*|ttyS0'; + this.debug = options && options.debug || false; + this.devregex = options && options.devregex || 'usb|ttyACM*|ttyS0'; this.baudrate = options && options.baudrate || 115200; this.writeBuffer = []; +} + +/* + * EventEmitter, I choose you! + */ +util.inherits(Board, events.EventEmitter); +/* + * Establish the serial connection + * + * Tries to open a serial connection with `connectSerial()`. + * If successful, the connection is initialized ("clearing bytes", debug mode). + * If unsuccessful, error event is emitted; if no listeners: error is thrown. + */ +Board.prototype.setup = function() { var self = this; - this.detect(function (err, serial) { - if (err) { - if(self.listeners('error').length) - self.emit('error', err); - else - throw new Error(err); - }else{ - self.serial = serial; + this.connectSerial(function(found) { + if (found) { + self.serial = found; self.emit('connected'); self.log('info', 'binding serial events'); @@ -56,52 +68,70 @@ var Board = function (options) { self.emit('ready'); }, 500); + } else { + msg = "Could not establish Serial connection to Arduino."; + if (self.listeners('error').length > 0) { + self.emit('error', msg); + } else { + throw new Error(msg); + } } }); + return this; } /* - * EventEmitter, I choose you! - */ -util.inherits(Board, events.EventEmitter); - -/* - * Detect an Arduino board - * Loop through all USB devices and try to connect - * This should really message the device and wait for a correct response + * (Tries to) Open serial connection with Arduino (formerly named `detect()`) + * + * Loops through devices matching `devregex` and naively tries to open a serial + * connection with each until one succeeds. + * This should really message the device and wait for a correct response to + * ensure that we're actually connected to an Arduino running `duino`. FIXME + * + * Returns false if no serial connection could be established. */ -Board.prototype.detect = function (callback) { +Board.prototype.connectSerial = function (callback) { this.log('info', 'attempting to find Arduino board'); var self = this; - child.exec('ls /dev | grep -E "'+ self.device +'"', function(err, stdout, stderr){ - var usb = stdout.slice(0, -1).split('\n'), - found = false, - err = null, - possible, temp; - - while ( usb.length ) { - possible = usb.pop(); - - if (possible.slice(0, 2) !== 'cu') { - try { - temp = new serial.SerialPort('/dev/' + possible, { - baudrate: self.baudrate, - parser: serial.parsers.readline('\n') - }); - } catch (e) { - err = e; - } - if (!err) { - found = temp; - self.log('info', 'found board at ' + temp.port); - break; - } else { - err = new Error('Could not find Arduino'); - } - } + child.exec('ls /dev | grep -E "'+ self.devregex +'"', function(err, stdout, stderr){ + + function skipUnwanted(e) { + return (e !== '') && (e.slice(0, 2) !== 'cu'); } - callback(err, found); + var devices = stdout.slice(0, -1).split('\n').filter(skipUnwanted), + device, + candidate; + + // loop over list of possible Arduinos + // do not stop (even/especially on error, that's the point!) until + // - we run out of options, or + // - a serial connection is established + var success = devices.some(function(device) { + try { + self.log('debug', 'attempting to open serial conn.: ' + device); + candidate = new serial.SerialPort('/dev/' + device, { + baudrate: self.baudrate, + parser: serial.parsers.readline('\n') + }); + + // connection succeeded, though we don't test whether it's an Arduino + self.log('info', 'found board at /dev/' + device); + return true; + + } catch (e) { + // ignore error and cont. + self.log('warning', 'error while establishing serial connection with' + + '/dev/' + device + '; trying next'); + return false; + } + }); + + if (success) { + callback(candidate); + } else { + callback(false); + } }); } @@ -137,18 +167,17 @@ Board.prototype.write = function (m) { } } -/* - * Add a 0 to the front of a single-digit pin number - */ +// Add a 0 to the front of a single-digit pin number Board.prototype.normalizePin = function (pin) { return this.lpad( 2, '0', pin ); } +// Left-pad values with 0s so it has three digits. Board.prototype.normalizeVal = function(val) { - return this.lpad( 3, '0', val ); + return this.lpad( 3, '0', val ); } -// +// Left-pad `str` with `chr` and return the last `len` digits Board.prototype.lpad = function(len, chr, str) { return (Array(len + 1).join(chr || ' ') + str).substr(-len); }; @@ -210,8 +239,8 @@ Board.prototype.analogRead = function (pin) { * Utility function to pause for a given time */ Board.prototype.delay = function (ms) { - ms += +new Date(); - while (+new Date() < ms) { } + ms += Date.now(); + while (Date.now() < ms) { } } /* diff --git a/package.json b/package.json index 7464b4d..c0dd4dc 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ ], "name": "duino", "description": "Arduino framework for mad scientists", - "version": "0.0.11", + "version": "0.1.0", "keywords": [ "arduino", "serial", diff --git a/src/du.ino b/src/du.ino deleted file mode 100644 index 93cfed9..0000000 --- a/src/du.ino +++ /dev/null @@ -1,288 +0,0 @@ -#include -#include - -bool debug = false; - -int index = 0; - -char messageBuffer[24]; -char cmd[3]; -char pin[3]; -char val[13]; -char aux[4]; -char type[2]; -char addr[5]; - -Servo servo; - -void setup() { - Serial.begin(115200); -} - -void loop() { - while(Serial.available() > 0) { - char x = Serial.read(); - if (x == '!') index = 0; // start - else if (x == '.') process(); // end - else messageBuffer[index++] = x; - } -} - -/* - * Deal with a full message and determine function to call - */ -void process() { - index = 0; - - strncpy(cmd, messageBuffer, 2); - cmd[2] = '\0'; - strncpy(pin, messageBuffer + 2, 2); - pin[2] = '\0'; - - if (debug) { - Serial.println(messageBuffer); - } - int cmdid = atoi(cmd); - - if (cmdid == 95) { - strncpy(type, messageBuffer + 4, 1); - type[1] = '\0'; - strncpy(val, messageBuffer + 5, 9); - val[8] = '\0'; - strncpy(addr, messageBuffer + 14, 4); - addr[4] = '\0'; - } else if (cmdid == 96 || cmdid == 94) { - strncpy(val, messageBuffer + 4, 12); - val[12] = '\0'; - } else if (cmdid > 96) { - strncpy(val, messageBuffer + 4, 2); - val[2] = '\0'; - strncpy(aux, messageBuffer + 6, 3); - aux[3] = '\0'; - } else { - strncpy(val, messageBuffer + 4, 3); - val[4] = '\0'; - strncpy(aux, messageBuffer + 7, 3); - aux[4] = '\0'; - } - - // Serial.println(cmd); - // Serial.println(pin); - // Serial.println(val); - // Serial.println(aux); - - switch(cmdid) { - case 0: sm(pin,val); break; - case 1: dw(pin,val); break; - case 2: dr(pin,val); break; - case 3: aw(pin,val); break; - case 4: ar(pin,val); break; - case 94: handleRCDecimal(pin, val); break; - case 96: handleRCTriState(pin, val); break; - case 97: handlePing(pin,val,aux); break; - case 98: handleServo(pin,val,aux); break; - case 99: toggleDebug(val); break; - default: break; - } -} - -/* - * Toggle debug mode - */ -void toggleDebug(char *val) { - if (atoi(val) == 0) { - debug = false; - Serial.println("goodbye"); - } else { - debug = true; - Serial.println("hello"); - } -} - -/* - * Set pin mode - */ -void sm(char *pin, char *val) { - if (debug) Serial.println("sm"); - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - if (atoi(val) == 0) { - pinMode(p, OUTPUT); - } else { - pinMode(p, INPUT); - } -} - -/* - * Digital write - */ -void dw(char *pin, char *val) { - if (debug) Serial.println("dw"); - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - pinMode(p, OUTPUT); - if (atoi(val) == 0) { - digitalWrite(p, LOW); - } else { - digitalWrite(p, HIGH); - } -} - -/* - * Digital read - */ -void dr(char *pin, char *val) { - if (debug) Serial.println("dr"); - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - pinMode(p, INPUT); - int oraw = digitalRead(p); - char m[7]; - sprintf(m, "%02d::%02d", p,oraw); - Serial.println(m); -} - -/* - * Analog read - */ -void ar(char *pin, char *val) { - if(debug) Serial.println("ar"); - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - pinMode(p, INPUT); // don't want to sw - int rval = analogRead(p); - char m[8]; - sprintf(m, "%s::%03d", pin, rval); - Serial.println(m); -} - -void aw(char *pin, char *val) { - if(debug) Serial.println("aw"); - int p = getPin(pin); - pinMode(p, OUTPUT); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - analogWrite(p,atoi(val)); -} - -int getPin(char *pin) { //Converts to A0-A5, and returns -1 on error - int ret = -1; - if(pin[0] == 'A' || pin[0] == 'a') { - switch(pin[1]) { - case '0': ret = A0; break; - case '1': ret = A1; break; - case '2': ret = A2; break; - case '3': ret = A3; break; - case '4': ret = A4; break; - case '5': ret = A5; break; - default: break; - } - } else { - ret = atoi(pin); - if(ret == 0 && (pin[0] != '0' || pin[1] != '0')) { - ret = -1; - } - } - return ret; -} - -/* - * Handle Ping commands - * fire, read - */ -void handlePing(char *pin, char *val, char *aux) { - if (debug) Serial.println("ss"); - int p = getPin(pin); - - if(p == -1) { if(debug) Serial.println("badpin"); return; } - Serial.println("got signal"); - - // 01(1) Fire and Read - if (atoi(val) == 1) { - char m[16]; - - pinMode(p, OUTPUT); - digitalWrite(p, LOW); - delayMicroseconds(2); - digitalWrite(p, HIGH); - delayMicroseconds(5); - digitalWrite(p, LOW); - - Serial.println("ping fired"); - - pinMode(p, INPUT); - sprintf(m, "%s::read::%08d", pin, pulseIn(p, HIGH)); - Serial.println(m); - - delay(50); - } -} - -/* - * Handle Servo commands - * attach, detach, write, read, writeMicroseconds, attached - */ -void handleServo(char *pin, char *val, char *aux) { - if (debug) Serial.println("ss"); - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - Serial.println("signal: servo"); - - // 00(0) Detach - if (atoi(val) == 0) { - servo.detach(); - char m[12]; - sprintf(m, "%s::detached", pin); - Serial.println(m); - - // 01(1) Attach - } else if (atoi(val) == 1) { - // servo.attach(p, 750, 2250); - servo.attach(p); - char m[12]; - sprintf(m, "%s::attached", pin); - Serial.println(m); - - // 02(2) Write - } else if (atoi(val) == 2) { - Serial.println("writing to servo"); - Serial.println(atoi(aux)); - // Write to servo - servo.write(atoi(aux)); - delay(15); - - // 03(3) Read - } else if (atoi(val) == 3) { - Serial.println("reading servo"); - int sval = servo.read(); - char m[13]; - sprintf(m, "%s::read::%03d", pin, sval); - Serial.println(m); - } -} - -/* - * Handle RC commands - * handleRCTriState("10", "0FFF0FFFFF0F") - */ -void handleRCTriState(char *pin, char *val) { - int p = getPin(pin); - if(p == -1) { if(debug) Serial.println("badpin"); return; } - if (debug) Serial.println("RC"); - RCSwitch rc = RCSwitch(); - rc.enableTransmit(p); - rc.sendTriState(val); -} - -/* - * Handle RC commands via decimal code - * For those sockets that don't use tri-state. - * handleRCDecimal("10", "5522351") - */ -void handleRCDecimal(char *pin, char *val) { - int p = getPin(pin); - if (p == -1) { if (debug) Serial.println("badpin"); return; } - if (debug) Serial.println("RCdec" + atol(val)); - RCSwitch rc = RCSwitch(); - rc.enableTransmit(p); - rc.send(atol(val), 24); -} diff --git a/src/du.ino b/src/du.ino new file mode 120000 index 0000000..1362fb3 --- /dev/null +++ b/src/du.ino @@ -0,0 +1 @@ +duino/duino.ino \ No newline at end of file diff --git a/src/duino/duino.ino b/src/duino/duino.ino new file mode 100644 index 0000000..3da144c --- /dev/null +++ b/src/duino/duino.ino @@ -0,0 +1,324 @@ +#include +#include +#include + +bool debug = false; + +int index = 0; + +char messageBuffer[24]; +char cmd[3]; +char pin[3]; +char val[13]; +char aux[4]; +char type[2]; +char addr[5]; + +Servo servo; + +IRsend irsend; + +void setup() { + Serial.begin(115200); +} + +void loop() { + while(Serial.available() > 0) { + char x = Serial.read(); + if (x == '!') index = 0; // start + else if (x == '.') process(); // end + else messageBuffer[index++] = x; + } +} + +/* + * Deal with a full message and determine function to call + */ +void process() { + index = 0; + + strncpy(cmd, messageBuffer, 2); + cmd[2] = '\0'; + strncpy(pin, messageBuffer + 2, 2); + pin[2] = '\0'; + + if (debug) { + Serial.println(messageBuffer); + } + int cmdid = atoi(cmd); + + if (cmdid == 95) { + strncpy(type, messageBuffer + 4, 1); + type[1] = '\0'; + strncpy(val, messageBuffer + 5, 9); + val[8] = '\0'; + strncpy(addr, messageBuffer + 14, 4); + addr[4] = '\0'; + } else if (cmdid == 96) { + strncpy(val, messageBuffer + 4, 12); + val[12] = '\0'; + } else if (cmdid > 96) { + strncpy(val, messageBuffer + 4, 2); + val[2] = '\0'; + strncpy(aux, messageBuffer + 6, 3); + aux[3] = '\0'; + } else { + strncpy(val, messageBuffer + 4, 3); + val[4] = '\0'; + strncpy(aux, messageBuffer + 7, 3); + aux[4] = '\0'; + } + + // Serial.println(cmd); + // Serial.println(pin); + // Serial.println(val); + // Serial.println(aux); + + switch(cmdid) { + case 0: sm(pin,val); break; + case 1: dw(pin,val); break; + case 2: dr(pin,val); break; + case 3: aw(pin,val); break; + case 4: ar(pin,val); break; + case 95: handleIRsend(type, val, addr); break; + case 96: handleRCTriState(pin, val); break; + case 97: handlePing(pin,val,aux); break; + case 98: handleServo(pin,val,aux); break; + case 99: toggleDebug(val); break; + default: break; + } +} + +/* + * Toggle debug mode + */ +void toggleDebug(char *val) { + if (atoi(val) == 0) { + debug = false; + Serial.println("goodbye"); + } else { + debug = true; + Serial.println("hello"); + } +} + +/* + * Set pin mode + */ +void sm(char *pin, char *val) { + if (debug) Serial.println("sm"); + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + if (atoi(val) == 0) { + pinMode(p, OUTPUT); + } else { + pinMode(p, INPUT); + } +} + +/* + * Digital write + */ +void dw(char *pin, char *val) { + if (debug) Serial.println("dw"); + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + pinMode(p, OUTPUT); + if (atoi(val) == 0) { + digitalWrite(p, LOW); + } else { + digitalWrite(p, HIGH); + } +} + +/* + * Digital read + */ +void dr(char *pin, char *val) { + if (debug) Serial.println("dr"); + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + pinMode(p, INPUT); + int oraw = digitalRead(p); + char m[7]; + sprintf(m, "%02d::%02d", p,oraw); + Serial.println(m); +} + +/* + * Analog read + */ +void ar(char *pin, char *val) { + if(debug) Serial.println("ar"); + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + pinMode(p, INPUT); // don't want to sw + int rval = analogRead(p); + char m[8]; + sprintf(m, "%s::%03d", pin, rval); + Serial.println(m); +} + +void aw(char *pin, char *val) { + if(debug) Serial.println("aw"); + int p = getPin(pin); + pinMode(p, OUTPUT); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + analogWrite(p,atoi(val)); +} + +int getPin(char *pin) { //Converts to A0-A5, and returns -1 on error + int ret = -1; + if(pin[0] == 'A' || pin[0] == 'a') { + switch(pin[1]) { + case '0': ret = A0; break; + case '1': ret = A1; break; + case '2': ret = A2; break; + case '3': ret = A3; break; + case '4': ret = A4; break; + case '5': ret = A5; break; + default: break; + } + } else { + ret = atoi(pin); + if(ret == 0 && (pin[0] != '0' || pin[1] != '0')) { + ret = -1; + } + } + return ret; +} + +/* + * Handle Ping commands + * fire, read + */ +void handlePing(char *pin, char *val, char *aux) { + if (debug) Serial.println("ss"); + int p = getPin(pin); + + if(p == -1) { if(debug) Serial.println("badpin"); return; } + Serial.println("got signal"); + + // 01(1) Fire and Read + if (atoi(val) == 1) { + char m[16]; + + pinMode(p, OUTPUT); + digitalWrite(p, LOW); + delayMicroseconds(2); + digitalWrite(p, HIGH); + delayMicroseconds(5); + digitalWrite(p, LOW); + + Serial.println("ping fired"); + + pinMode(p, INPUT); + sprintf(m, "%s::read::%08d", pin, pulseIn(p, HIGH)); + Serial.println(m); + + delay(50); + } +} + +/* + * Handle Servo commands + * attach, detach, write, read, writeMicroseconds, attached + */ +void handleServo(char *pin, char *val, char *aux) { + if (debug) Serial.println("ss"); + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + Serial.println("signal: servo"); + + // 00(0) Detach + if (atoi(val) == 0) { + servo.detach(); + char m[12]; + sprintf(m, "%s::detached", pin); + Serial.println(m); + + // 01(1) Attach + } else if (atoi(val) == 1) { + // servo.attach(p, 750, 2250); + servo.attach(p); + char m[12]; + sprintf(m, "%s::attached", pin); + Serial.println(m); + + // 02(2) Write + } else if (atoi(val) == 2) { + Serial.println("writing to servo"); + Serial.println(atoi(aux)); + // Write to servo + servo.write(atoi(aux)); + delay(15); + + // 03(3) Read + } else if (atoi(val) == 3) { + Serial.println("reading servo"); + int sval = servo.read(); + char m[13]; + sprintf(m, "%s::read::%03d", pin, sval); + Serial.println(m); + } +} + +/* + * Handle RC commands + * handleRCTriState("10", "0FFF0FFFFF0F") + */ +void handleRCTriState(char *pin, char *val) { + int p = getPin(pin); + if(p == -1) { if(debug) Serial.println("badpin"); return; } + if (debug) Serial.println("RC"); + RCSwitch rc = RCSwitch(); + rc.enableTransmit(p); + rc.sendTriState(val); +} + +/* + * Handle RC commands via decimal code + * For those sockets that don't use tri-state. + * handleRCDecimal("10", "5522351") + */ +void handleRCDecimal(char *pin, char *val) { + int p = getPin(pin); + if (p == -1) { if (debug) Serial.println("badpin"); return; } + if (debug) Serial.println("RCdec" + atol(val)); + RCSwitch rc = RCSwitch(); + rc.enableTransmit(p); + rc.send(atol(val), 24); +} + +/* + * Handle IR commands + */ +void handleIRsend(char *type, char *val, char *addr) { + if (debug) Serial.println("IR"); + switch (atoi(type)) { + case 1: + irsend.sendRC5(strtol(val, (char **)0, 16), atoi(addr)); + break; + case 2: + irsend.sendRC6(strtol(val, (char **)0, 16), atoi(addr)); + break; + case 3: + irsend.sendNEC(strtol(val, (char **)0, 16), atoi(addr)); + break; + case 4: + irsend.sendSony(strtol(val, (char **)0, 16), atoi(addr)); + break; + case 5: + irsend.sendDISH(strtol(val, (char **)0, 16), atoi(addr)); + break; + case 6: + irsend.sendSharp(strtol(val, (char **)0, 16), atoi(addr)); + break; + case 7: + irsend.sendPanasonic(strtol(addr, (char **)0, 16), strtol(val, (char **)0, 16)); + break; + case 8: + irsend.sendJVC(atoi(val), atoi(addr), 1); + break; + } +} diff --git a/src/RCSwitch.cpp b/src/libs/RCSwitch/RCSwitch.cpp similarity index 100% rename from src/RCSwitch.cpp rename to src/libs/RCSwitch/RCSwitch.cpp diff --git a/src/RCSwitch.h b/src/libs/RCSwitch/RCSwitch.h similarity index 100% rename from src/RCSwitch.h rename to src/libs/RCSwitch/RCSwitch.h