Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Lint fixes, adds empty test dir.

Signed-off-by: Rick Waldron waldron.rick@gmail.com <waldron.rick@gmail.com>
  • Loading branch information...
commit 8978a8dd2b35229a7b1d1102e03df747a7e93c63 1 parent 416abdd
@rwaldron authored
View
6 examples/analogled.js
@@ -1,4 +1,4 @@
-var arduino = require('../');
+var arduino = require("../");
var board = new arduino.Board({
debug: true
@@ -6,9 +6,9 @@ var board = new arduino.Board({
var aled = new arduino.Led({
board: board,
- pin: 'A0'
+ pin: "A0"
});
-board.on('ready', function(){
+board.on("ready", function(){
aled.fade();
});
View
8 examples/basic.js
@@ -1,14 +1,14 @@
-var arduino = require('../');
+var arduino = require("../");
var board = new arduino.Board({
debug: true
});
-board.on('connected', function(){
- board.write('HELLO WORLD');
+board.on("connected", function(){
+ board.write("HELLO WORLD");
});
-board.on('message', function(data) {
+board.on("message", function(data) {
console.log(data);
});
View
10 examples/button.js
@@ -1,4 +1,4 @@
-var arduino = require('../');
+var arduino = require("../");
var board = new arduino.Board();
@@ -7,10 +7,10 @@ var button = new arduino.Button({
pin: 2
});
-button.on('down', function(){
- console.log('DOWN');
+button.on("down", function(){
+ console.log("DOWN");
});
-button.on('up', function(){
- console.log('UP');
+button.on("up", function(){
+ console.log("UP");
});
View
2  examples/gps.js
@@ -1,4 +1,4 @@
-var arduino = require('../'),
+" arduino = require('../'),
board, gps;
board = new arduino.Board({
View
4 examples/led.js
@@ -1,4 +1,4 @@
-var arduino = require('../');
+var arduino = require("../");
var board = new arduino.Board({
debug: true
@@ -9,6 +9,6 @@ var led = new arduino.Led({
pin: 9
});
-board.on('ready', function(){
+board.on("ready", function(){
led.blink();
});
View
8 examples/piezo.js
@@ -1,4 +1,4 @@
-var arduino = require('../');
+var arduino = require("../");
var board = new arduino.Board({
debug: true
@@ -8,11 +8,11 @@ var piezo = new arduino.Piezo({
board: board
});
-board.on('ready', function(){
- piezo.note('a', 1000);
+board.on("ready", function(){
+ piezo.note("a", 1000);
setTimeout(function(){
- piezo.note('b', 1100);
+ piezo.note("b", 1100);
}, 1000);
});
View
20 examples/pir.js
@@ -1,4 +1,4 @@
-var arduino = require('../'),
+var arduino = require("../"),
board, pir;
board = new arduino.Board({
@@ -10,13 +10,13 @@ pir = new arduino.PIR({
pin: 7
});
-// 'calibrated' event fired when PIR sensor is
+// "calibrated" event fired when PIR sensor is
// ready to detect movement/motion in observable range
//
// All events receive error and date arguments
-pir.on('calibrated', function(err, date) {
+pir.on("calibrated", function(err, date) {
- console.log('calibrated');
+ console.log("calibrated");
// Current sensor data stored in properties
// of this PIR instance:
@@ -25,20 +25,20 @@ pir.on('calibrated', function(err, date) {
// 0 No motion currently detected
// 1 Motion currently detected
- // 'motionstart' event fired when motion occurs
+ // "motionstart" event fired when motion occurs
// within the observable range of the PIR sensor
- this.on('motionstart', function(err, date) {
+ this.on("motionstart", function(err, date) {
- console.log('motionstart', this.state);
+ console.log("motionstart", this.state);
console.log( date );
});
- // 'motionend' event fired when motion has ceased
+ // "motionend" event fired when motion has ceased
// within the observable range of the PIR sensor
- this.on('motionend', function(err, date) {
+ this.on("motionend", function(err, date) {
- console.log('motionend', this.state);
+ console.log("motionend", this.state);
});
});
View
6 examples/sensor.js
@@ -1,4 +1,4 @@
-var arduino = require('../'),
+var arduino = require("../"),
board, sensor;
board = new arduino.Board({
@@ -7,10 +7,10 @@ board = new arduino.Board({
sensor = new arduino.Sensor({
board: board,
- pin: 'A0'
+ pin: "A0"
});
-sensor.on('read', function(err, value) {
+sensor.on("read", function(err, value) {
value = +value;
// |value| is the raw sensor output
console.log( value );
View
40 grunt.js
@@ -2,35 +2,16 @@ module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
- pkg: '<json:package.json>',
- meta: {
- banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
- '<%= grunt.template.today("m/d/yyyy") %>\n' +
- '<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' +
- '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
- ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
- },
- concat: {
- dist: {
- src: ['<banner>', '<file_strip_banner:lib/<%= pkg.name %>.js>'],
- dest: 'dist/<%= pkg.name %>.js'
- }
- },
- min: {
- dist: {
- src: ['<banner>', '<config:concat.dist.dest>'],
- dest: 'dist/<%= pkg.name %>.min.js'
- }
- },
+ pkg: "<json:package.json>",
test: {
- files: ['test/**/*.js']
+ files: ["test/**/*.js"]
},
lint: {
- files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
+ files: ["grunt.js", "lib/**/*.js", "test/**/*.js"]
},
watch: {
- files: '<config:lint.files>',
- tasks: 'lint test'
+ files: "<config:lint.files>",
+ tasks: "default"
},
jshint: {
options: {
@@ -43,17 +24,16 @@ module.exports = function(grunt) {
sub: true,
undef: true,
boss: true,
- eqnull: true
+ eqnull: true,
+ node: true
},
globals: {
- exports: true,
- module: false
+ exports: true
}
- },
- uglify: {}
+ }
});
// Default task.
- grunt.registerTask('default', 'lint test concat min');
+ grunt.registerTask("default", "lint test");
};
View
18 index.js
@@ -1,12 +1,12 @@
module.exports = {
- Board: require('./lib/board'),
- Led: require('./lib/led'),
- Piezo: require('./lib/piezo'),
- Button: require('./lib/button'),
- Servo: require('./lib/servo'),
- Sensor: require('./lib/sensor'),
- Ping: require('./lib/ping'),
- PIR: require('./lib/pir'),
- GPS: require('./lib/gps')
+ Board: require("./lib/board"),
+ Led: require("./lib/led"),
+ Piezo: require("./lib/piezo"),
+ Button: require("./lib/button"),
+ Servo: require("./lib/servo"),
+ Sensor: require("./lib/sensor"),
+ Ping: require("./lib/ping"),
+ PIR: require("./lib/pir"),
+ GPS: require("./lib/gps")
};
View
136 lib/board.js
@@ -1,44 +1,47 @@
-var events = require('events'),
- child = require('child_process'),
- util = require('util'),
- colors = require('colors'),
- serial = require('serialport');
+var events = require("events"),
+ child = require("child_process"),
+ util = require("util"),
+ colors = require("colors"),
+ serial = require("serialport");
/*
* The main Arduino constructor
* Connect to the serial port and bind
*/
var Board = function (options) {
- this.log('info', 'initializing');
+ this.log("info", "initializing");
this.debug = options && options.debug || false;
this.writeBuffer = [];
var self = this;
+
this.detect(function (err, serial) {
- if (err) throw err;
+ if (err) {
+ throw err;
+ }
self.serial = serial;
- self.emit('connected');
+ self.emit("connected");
- self.log('info', 'binding serial events');
- self.serial.on('data', function(data) {
+ self.log("info", "binding serial events");
+ self.serial.on("data", function(data) {
if ( !data.trim() ) {
return;
}
- self.log('receive', data.toString().red);
- self.emit('data', data);
+ self.log("receive", data.toString().red);
+ self.emit("data", data);
});
setTimeout(function(){
- self.log('info', 'board ready');
+ self.log("info", "board ready");
self.sendClearingBytes();
if (self.debug) {
- self.log('info', 'sending debug mode toggle on to board');
- self.write('99' + self.normalizePin(0) + self.normalizeVal(1));
- process.on('SIGINT', function(){
- self.log('info', 'sending debug mode toggle off to board');
- self.write('99' + self.normalizePin(0) + self.normalizeVal(0));
+ self.log("info", "sending debug mode toggle on to board");
+ self.write("99" + self.normalizePin(0) + self.normalizeVal(1));
+ process.on("SIGINT", function(){
+ self.log("info", "sending debug mode toggle off to board");
+ self.write("99" + self.normalizePin(0) + self.normalizeVal(0));
delete self.serial;
setTimeout(function(){
process.exit();
@@ -50,10 +53,10 @@ var Board = function (options) {
self.processWriteBuffer();
}
- self.emit('ready');
+ self.emit("ready");
}, 500);
});
-}
+};
/*
* EventEmitter, I choose you!
@@ -66,109 +69,109 @@ util.inherits(Board, events.EventEmitter);
* This should really message the device and wait for a correct response
*/
Board.prototype.detect = function (callback) {
- this.log('info', 'attempting to find Arduino board');
+ this.log("info", "attempting to find Arduino board");
var self = this;
- child.exec('ls /dev | grep usb', function(err, stdout, stderr){
- var usb = stdout.slice(0, -1).split('\n'),
+ child.exec("ls /dev | grep usb", function(err, stdout, stderr){
+ err = err || null;
+ 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') {
+ if (possible.slice(0, 2) !== "cu") {
try {
- temp = new serial.SerialPort('/dev/' + possible, {
+ temp = new serial.SerialPort("/dev/" + possible, {
baudrate: 115200,
- parser: serial.parsers.readline('\n')
+ parser: serial.parsers.readline("\n")
});
} catch (e) {
err = e;
}
if (!err) {
found = temp;
- self.log('info', 'found board at ' + temp.port);
+ self.log("info", "found board at " + temp.port);
break;
} else {
- err = new Error('Could not find Arduino');
+ err = new Error("Could not find Arduino");
}
}
}
callback(err, found);
});
-}
+};
/*
* The board will eat the first 4 bytes of the session
* So we give it crap to eat
*/
Board.prototype.sendClearingBytes = function () {
- this.serial.write('00000000');
-}
+ this.serial.write("00000000");
+};
/*
* Process the writeBuffer (messages attempted before serial was ready)
*/
Board.prototype.processWriteBuffer = function () {
- this.log('info', 'processing buffered messages');
+ this.log("info", "processing buffered messages");
while (this.writeBuffer.length > 0) {
- this.log('info', 'writing buffered message');
+ this.log("info", "writing buffered message");
this.write(this.writeBuffer.shift());
}
-}
+};
/*
* Low-level serial write
*/
Board.prototype.write = function (m) {
if (this.serial) {
- this.log('write', m);
- this.serial.write('!' + m + '.');
+ this.log("write", m);
+ this.serial.write("!" + m + ".");
} else {
- this.log('info', 'serial not ready, buffering message: ' + m.red);
+ this.log("info", "serial not ready, buffering message: " + m.red);
this.writeBuffer.push(m);
}
-}
+};
/*
* Add a 0 to the front of a single-digit pin number
*/
Board.prototype.normalizePin = function (pin) {
- return this.lpad( 2, '0', pin );
-}
+ return this.lpad( 2, "0", pin );
+};
Board.prototype.normalizeVal = function(val) {
- return this.lpad( 3, '0', val );
-}
+ return this.lpad( 3, "0", val );
+};
//
Board.prototype.lpad = function(len, chr, str) {
- return (Array(len + 1).join(chr || ' ') + str).substr(-len);
+ return ((new Array(len + 1)).join(chr || " ") + str).substr(-len);
};
/*
* Define constants
*/
-Board.prototype.HIGH = '255';
-Board.prototype.LOW = '000';
+Board.prototype.HIGH = "255";
+Board.prototype.LOW = "000";
/*
- * Set a pin's mode
+ * Set a pin"s mode
* val == out = 001
* val == in = 000
*/
Board.prototype.pinMode = function (pin, val) {
pin = this.normalizePin(pin);
- this.log('info', 'set pin ' + pin + ' mode to ' + val);
+ this.log("info", "set pin " + pin + " mode to " + val);
val = (
- val == 'out' ?
+ val === "out" ?
this.normalizeVal(1) :
this.normalizeVal(0)
);
- this.write('00' + pin + val);
-}
+ this.write("00" + pin + val);
+};
/*
* Tell the board to write to a digital pin
@@ -176,30 +179,31 @@ Board.prototype.pinMode = function (pin, val) {
Board.prototype.digitalWrite = function (pin, val) {
pin = this.normalizePin(pin);
val = this.normalizeVal(val);
- this.log('info', 'digitalWrite to pin ' + pin + ': ' + val.green);
- this.write('01' + pin + val);
-}
+ this.log("info", "digitalWrite to pin " + pin + ": " + val.green);
+ this.write("01" + pin + val);
+};
/*
* Tell the board to extract data from a pin
*/
Board.prototype.digitalRead = function (pin) {
pin = this.normalizePin(pin);
- this.log('info', 'digitalRead from pin ' + pin);
- this.write('02' + pin + this.normalizeVal(0));
-}
+ this.log("info", "digitalRead from pin " + pin);
+ this.write("02" + pin + this.normalizeVal(0));
+};
Board.prototype.analogWrite = function (pin, val) {
pin = this.normalizePin(pin);
val = this.normalizeVal(val);
- this.log('info', 'analogWrite to pin ' + pin + ': ' + val.green);
- this.write('03' + pin + val);
-}
+ this.log("info", "analogWrite to pin " + pin + ": " + val.green);
+ this.write("03" + pin + val);
+};
+
Board.prototype.analogRead = function (pin) {
pin = this.normalizePin(pin);
- this.log('info', 'analogRead from pin ' + pin);
- this.write('04' + pin + this.normalizeVal(0));
-}
+ this.log("info", "analogRead from pin " + pin);
+ this.write("04" + pin + this.normalizeVal(0));
+};
/*
* Utility function to pause for a given time
@@ -207,7 +211,7 @@ Board.prototype.analogRead = function (pin) {
Board.prototype.delay = function (ms) {
ms += +new Date();
while (+new Date() < ms) { }
-}
+};
/*
* Logger utility function
@@ -215,8 +219,8 @@ Board.prototype.delay = function (ms) {
Board.prototype.log = function (/*level, message*/) {
var args = [].slice.call(arguments);
if (this.debug) {
- console.log(String(+new Date()).grey + ' duino '.blue + args.shift().magenta + ' ' + args.join(', '));
+ console.log(String(+new Date()).grey + " duino ".blue + args.shift().magenta + " " + args.join(", "));
}
-}
+};
module.exports = Board;
View
42 lib/button.js
@@ -1,5 +1,5 @@
-var events = require('events'),
- util = require('util');
+var events = require("events"),
+ util = require("util");
/*
* Main Button constructor
@@ -7,34 +7,38 @@ var events = require('events'),
* Tell the board to set it up
*/
var Button = function (options) {
- if (!options || !options.board) throw new Error('Must supply required options to Button');
+ if (!options || !options.board) {
+ throw new Error("Must supply required options to Button");
+ }
this.board = options.board;
this.pin = options.pin || 13;
+ this.board.pinMode(this.pin, "in");
+
this.down = false;
- this.board.pinMode(this.pin, 'in');
- var self = this;
- setInterval(function () {
- self.board.digitalRead(self.pin);
- }, 50);
- this.board.on('data', function (m) {
- m = m.slice(0, -1).split('::');
+
+ setInterval(function() {
+ this.board.digitalRead(this.pin);
+ }.bind(this), 50);
+
+ this.board.on("data", function (m) {
+ m = m.slice(0, -1).split("::");
var err = null;
- if (m.length > 1 && m[0] == self.pin) {
+ if (m.length > 1 && m[0] === this.pin) {
// 0 is up
// 1 is down
- if (m[1] == 0 && self.down) {
- self.down = false;
- self.emit('up', err);
+ if (+m[1] === 0 && this.down) {
+ this.down = false;
+ this.emit("up", err);
}
- if (m[1] == 1 && !self.down) {
- self.down = true;
- self.emit('down', err);
+ if (+m[1] === 1 && !this.down) {
+ this.down = true;
+ this.emit("down", err);
}
}
- });
-}
+ }.bind(this));
+};
/*
* EventEmitter, I choose you!
View
36 lib/gps.js
@@ -1,5 +1,5 @@
-var events = require('events'),
- util = require('util');
+var events = require("events"),
+ util = require("util");
/*
* Main GPS constructor
@@ -11,17 +11,19 @@ function ToDecimalDegrees() {
}
var GPS = function (options) {
- if (!options || !options.board) throw new Error('Must supply required options to GPS');
+ if (!options || !options.board) {
+ throw new Error("Must supply required options to GPS");
+ }
this.board = options.board;
this.pin = this.board.normalizePin(options.pin || 2);
- this.board.on('ready', function () {
- this.board.log('info', 'initializing gps');
+ this.board.on("ready", function () {
+ this.board.log("info", "initializing gps");
this.init();
}.bind(this));
- this.board.on('data', function (message) {
- var m = message.slice(0, -1), //m = message.slice(0, -1).split('::'),
+ this.board.on("data", function (message) {
+ var m = message.slice(0, -1), //m = message.slice(0, -1).split("::"),
err = null,
data = {
time: null,
@@ -29,7 +31,7 @@ var GPS = function (options) {
longitude: null,
hemisphere: {
latitude: null,
- longitude: null,
+ longitude: null
},
quality: null,
satellites: null,
@@ -49,11 +51,11 @@ var GPS = function (options) {
// http://www.gpsinformation.org/dale/nmea.htm#position
- if ( m.indexOf('$GPGGA') === 0 ) {
+ if ( m.indexOf("$GPGGA") === 0 ) {
// console.log( "valid" );
// console.log( message );
- fields = m.split(',');
+ fields = m.split(",");
// TODO: make time less shitty.
data.time = fields[1];
@@ -62,7 +64,7 @@ var GPS = function (options) {
data.hemisphere = {
latitude: fields[3],
- longitude: fields[5],
+ longitude: fields[5]
};
data.quality = fields[6];
@@ -72,7 +74,7 @@ var GPS = function (options) {
// TODO: Do something usefull with err
//$GPGGA,024322.000,4221.1928,N,07108.7770,W,2,07,1.2,6.2,M,-33.7,M,1.8,0000*4B
- this.emit('locate', err, data);
+ this.emit("locate", err, data);
}
}.bind(this));
};
@@ -80,23 +82,23 @@ var GPS = function (options) {
util.inherits(GPS, events.EventEmitter);
GPS.prototype.command = function () {
- var msg = '96' + this.pin + ([].slice.call(arguments).join(''));
+ var msg = "96" + this.pin + ([].slice.call(arguments).join(""));
- // this.board.log( 'info', 'command', msg );
+ // this.board.log( "info", "command", msg );
this.board.write(msg);
};
GPS.prototype.init = function () {
- this.command('01');
+ this.command("01");
};
GPS.prototype.setPosition = function () {
// Reserved 02 code
- // this.command('02');
+ // this.command("02");
};
GPS.prototype.getPosition = function () {
- this.command('03');
+ this.command("03");
};
View
61 lib/led.js
@@ -4,60 +4,71 @@
* Process options
* Tell the board to set it up
*/
-var Led = function (options) {
- if (!options || !options.board) throw new Error('Must supply required options to LED');
+var Led = function(options) {
+ if (!options || !options.board) {
+ throw new Error("Must supply required options to LED");
+ }
this.board = options.board;
this.pin = options.pin || 13;
+ this.board.pinMode(this.pin, "out");
+
this.bright = 0;
- this.board.pinMode(this.pin, 'out');
this.direction = -1;
-}
+};
/*
* Turn the LED on
*/
-Led.prototype.on = function () {
+Led.prototype.on = function() {
this.board.digitalWrite(this.pin, this.board.HIGH);
this.bright = 255;
-}
+};
-/*
+/*
* Turn the LED off
*/
-Led.prototype.off = function () {
+Led.prototype.off = function() {
this.board.digitalWrite(this.pin, this.board.LOW);
this.bright = 0;
-}
+};
Led.prototype.brightLevel = function(val) {
this.board.analogWrite(this.pin, this.bright = val);
-}
+};
Led.prototype.fade = function(interval) {
- to = (interval||5000)/(255*2);
- var self = this;
+ var to = (interval||5000)/(255*2),
+ direction;
+
setInterval(function() {
- if(!self.board.serial) return; //Interval too fast for debug messages on ^c
- if(self.bright==0) direction = 1;
- if(self.bright==255) direction = -1;
- self.brightLevel(self.bright+direction);
- },to);
-}
+ if (!this.board.serial) {
+ //Interval too fast for debug messages on ^c
+ return;
+ }
+ if (+this.bright === 0) {
+ direction = 1;
+ }
+ if (+this.bright === 255) {
+ direction = -1;
+ }
+ this.brightLevel(this.bright + direction);
+ }.bind(this), to);
+};
/*
* Start a bariable blinking pattern
*/
-Led.prototype.blink = function (interval) {
+Led.prototype.blink = function(interval) {
interval = interval || 1000;
- var self = this;
+
setInterval(function(){
- if (self.bright) {
- self.off()
+ if (this.bright) {
+ this.off();
} else {
- self.on();
+ this.on();
}
- }, interval);
-}
+ }.bind(this), interval);
+};
module.exports = Led;
View
18 lib/ping.js
@@ -1,5 +1,5 @@
-var events = require('events'),
- util = require('util');
+var events = require("events"),
+ util = require("util");
/*
* Main Ping constructor
@@ -7,7 +7,9 @@ var events = require('events'),
* Tell the board to set it up
*/
var Ping = function (options) {
- if (!options || !options.board) throw new Error('Must supply required options to Ping');
+ if (!options || !options.board) {
+ throw new Error("Must supply required options to Ping");
+ }
this.board = options.board;
this.pin = this.board.normalizePin(options.pin || 9);
@@ -25,8 +27,8 @@ var Ping = function (options) {
this.fire();
}.bind(this), 50);
- this.board.on('data', function (message) {
- var m = message.slice(0, -1).split('::'),
+ this.board.on("data", function (message) {
+ var m = message.slice(0, -1).split("::"),
err = null,
pin, type, data;
@@ -40,7 +42,7 @@ var Ping = function (options) {
if (pin === this.pin && types[type]) {
// See: http://arduino.cc/en/Tutorial/Ping
- // According to Parallax's datasheet for the PING))), there are
+ // According to Parallax"s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
@@ -59,13 +61,13 @@ var Ping = function (options) {
util.inherits(Ping, events.EventEmitter);
Ping.prototype.command = function () {
- var msg = '97' + this.pin + ([].slice.call(arguments).join(''));
+ var msg = "97" + this.pin + ([].slice.call(arguments).join(""));
this.board.write(msg);
};
Ping.prototype.fire = function () {
- this.command('01');
+ this.command("01");
};
module.exports = Ping;
View
28 lib/pir.js
@@ -1,5 +1,5 @@
-var events = require('events'),
- util = require('util');
+var events = require("events"),
+ util = require("util");
/*
* Main PIR constructor
@@ -8,7 +8,7 @@ var events = require('events'),
*/
var PIR = function (options) {
if (!options || !options.board) {
- throw new Error('Must supply required options to PIR');
+ throw new Error("Must supply required options to PIR");
}
this.board = options.board;
this.pin = this.board.normalizePin(options.pin || 9);
@@ -19,8 +19,8 @@ var PIR = function (options) {
this.board.digitalRead(this.pin);
}.bind(this), 50);
- this.board.on('data', function (message) {
- var m = message.slice(0, -1).split('::'),
+ this.board.on("data", function (message) {
+ var m = message.slice(0, -1).split("::"),
timestamp = new Date(),
err = null,
pin, data;
@@ -35,30 +35,30 @@ var PIR = function (options) {
if (pin === this.pin) {
// If this is not a calibration event
- if (this.state != null && this.state != +data) {
+ if (this.state != null && this.state !== +data) {
// Update current state of PIR instance
this.state = +data;
- // 'motionstart' event fired when motion occurs
+ // "motionstart" event fired when motion occurs
// within the observable range of the PIR sensor
- if (data === '01') {
- this.emit('motionstart', err, timestamp);
+ if (data === "01") {
+ this.emit("motionstart", err, timestamp);
}
- // 'motionend' event fired when motion has ceased
+ // "motionend" event fired when motion has ceased
// within the observable range of the PIR sensor
- if (data === '00') {
- this.emit('motionend', err, timestamp);
+ if (data === "00") {
+ this.emit("motionend", err, timestamp);
}
}
- // 'calibrated' event fired when PIR sensor is
+ // "calibrated" event fired when PIR sensor is
// ready to detect movement/motion in observable range
if (!this.calibrated) {
this.calibrated = true;
this.state = +data;
- this.emit('calibrated', err, timestamp);
+ this.emit("calibrated", err, timestamp);
}
}
}.bind(this));
View
20 lib/sensor.js
@@ -1,5 +1,5 @@
-var events = require('events'),
- util = require('util');
+var events = require("events"),
+ util = require("util");
/*
* Main Sensor constructor
@@ -7,10 +7,12 @@ var events = require('events'),
* Tell the board to set it up
*/
var Sensor = function (options) {
- if (!options || !options.board) throw new Error('Must supply required options to Sensor');
+ if (!options || !options.board) {
+ throw new Error("Must supply required options to Sensor");
+ }
this.board = options.board;
- this.pin = options.pin || 'A0';
- this.board.pinMode(this.pin, 'in');
+ this.pin = options.pin || "A0";
+ this.board.pinMode(this.pin, "in");
// Poll for sensor readings
setInterval(function () {
@@ -19,8 +21,8 @@ var Sensor = function (options) {
// When data is received, parse inbound message
// match pin to instance pin value
- this.board.on('data', function (message) {
- var m = message.slice(0, -1).split('::'),
+ this.board.on("data", function (message) {
+ var m = message.slice(0, -1).split("::"),
err = null,
pin, data;
@@ -28,11 +30,11 @@ var Sensor = function (options) {
return;
}
- pin = m[0]
+ pin = m[0];
data = m.length === 2 ? m[1] : null;
if (pin === this.pin) {
- this.emit('read', err, data);
+ this.emit("read", err, data);
}
}.bind(this));
};
View
81 lib/servo.js
@@ -1,5 +1,5 @@
-var events = require('events'),
- util = require('util');
+var events = require("events"),
+ util = require("util");
/*
* Main Servo constructor
@@ -7,7 +7,9 @@ var events = require('events'),
* Tell the board to set it up
*/
var Servo = function (options) {
- if (!options || !options.board) throw new Error('Must supply required options to Servo');
+ if (!options || !options.board) {
+ throw new Error("Must supply required options to Servo");
+ }
this.board = options.board;
this.pin = this.board.normalizePin(options.pin || 9);
@@ -18,13 +20,13 @@ var Servo = function (options) {
moved: true
};
- this.board.on('ready', function () {
- console.log('board ready, attaching servo', this);
+ this.board.on("ready", function () {
+ console.log("board ready, attaching servo", this);
this.attach();
}.bind(this));
- this.board.on('data', function (message) {
- var m = message.slice(0, -1).split('::'),
+ this.board.on("data", function (message) {
+ var m = message.slice(0, -1).split("::"),
err = null,
pin, type, data;
@@ -32,7 +34,7 @@ var Servo = function (options) {
return;
}
- pin = m[0]
+ pin = m[0];
type = m[1];
data = m.length === 3 ? m[2] : null;
@@ -45,28 +47,28 @@ var Servo = function (options) {
util.inherits(Servo, events.EventEmitter);
Servo.prototype.command = function () {
- var msg = '98' + this.pin + ([].slice.call(arguments).join(''));
+ var msg = "98" + this.pin + ([].slice.call(arguments).join(""));
- // this.board.log( 'info', 'command', msg );
+ // this.board.log( "info", "command", msg );
this.board.write(msg);
};
Servo.prototype.detach = function () {
- this.command('00');
+ this.command("00");
};
Servo.prototype.attach = function () {
- this.command('01');
+ this.command("01");
};
Servo.prototype.write = function (pos) {
- pos = this.board.lpad(3, '0', pos);
- this.board.log('info', 'moving to: ' + pos);
- this.command('02' + pos);
+ pos = this.board.lpad(3, "0", pos);
+ this.board.log("info", "moving to: " + pos);
+ this.command("02" + pos);
};
Servo.prototype.read = function () {
- this.command('03');
+ this.command("03");
};
// Servo.prototype.writeMilliseconds = function () {};
@@ -86,24 +88,24 @@ Servo.prototype.sweep = function (options) {
// sweep settings
lapse = options.lapse || 2000,
to = options.to || 180,
- from = options.from || 1;
+ from = options.from || 1,
// sweep handlers
doSweep = function doSweep(pos) {
- // this.board.log('info', 'current position: ', pos);
+ // this.board.log("info", "current position: ", pos);
var moveTo,
posint = +pos;
- // this.board.log('info', 'current pos int: ', posint);
+ // this.board.log("info", "current pos int: ", posint);
if (posint === 93) {
moveTo = 1;
} else {
- // this.board.log('info', 'posint not 93.....');
+ // this.board.log("info", "posint not 93.....");
if (posint < to) {
moveTo = to;
- } else if (posint == to) {
+ } else if (posint === to) {
moveTo = 90;
} else {
moveTo = from;
@@ -113,30 +115,31 @@ Servo.prototype.sweep = function (options) {
this.write(moveTo);
moves++;
- };
+ },
+ loop = function() {
+ // Read the current position, will trigger
+ // "read" event with position data;
+ if (moves < 2) {
+ this.read();
- this.on('read', doSweep);
+ timeout.inner = setTimeout(loop.bind(this), lapse);
+ } else {
+ // this.board.log("info", "info", "clearing");
- // Initialize sweep; wait for for stack unwind.
- timeout.outer = setTimeout(function loop() {
- // Read the current position, will trigger
- // 'read' event with position data;
- if (moves < 2) {
- this.read();
+ clearTimeout(timeout.inner);
+ clearTimeout(timeout.outer);
- timeout.inner = setTimeout(loop.bind(this), lapse);
- } else {
- // this.board.log('info', 'info', 'clearing');
+ loop = null;
- clearTimeout(timeout.inner);
- clearTimeout(timeout.outer);
+ this.removeListener("read", doSweep);
+ this.emit.call(this, "aftersweep");
+ }
+ }.bind(this);
- loop = null;
+ this.on("read", doSweep);
- this.removeListener('read', doSweep);
- this.emit.call(this, 'aftersweep');
- }
- }.bind(this), 0);
+ // Initialize sweep; wait for for stack unwind.
+ timeout.outer = setTimeout(loop, 0);
};
module.exports = Servo;
View
2  package.json
@@ -33,4 +33,4 @@
"scripts": {
"test": "grunt test"
}
-}
+}
View
14 test/duino_test.js
@@ -0,0 +1,14 @@
+// var duino = require('../');
+
+// exports['awesome'] = {
+// setUp: function(done) {
+// // setup here
+// done();
+// },
+// 'no args': function(test) {
+// test.expect(1);
+// // tests here
+// test.equal(duino.awesome(), 'awesome', 'should be awesome.');
+// test.done();
+// }
+// };
Please sign in to comment.
Something went wrong with that request. Please try again.