From d0ad0bd2d70612a5d7039e6d63f1f18e00c08e0d Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Sun, 25 Mar 2012 23:35:39 -0400 Subject: [PATCH 01/14] GPS Module support; locate event; Signed-off-by: Rick Waldron waldron.rick@gmail.com --- examples/gps.js | 36 +++++++++++++++++ index.js | 3 +- lib/board.js | 5 ++- lib/gps.js | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ src/du.ino | 46 ++++++++++++++++++++++ 5 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 examples/gps.js create mode 100644 lib/gps.js diff --git a/examples/gps.js b/examples/gps.js new file mode 100644 index 0000000..717484f --- /dev/null +++ b/examples/gps.js @@ -0,0 +1,36 @@ +var arduino = require('../'), + board, gps; + +board = new arduino.Board({ + debug: true +}); + +gps = new arduino.GPS({ + board: board, + pin: 2 +}); + +// watchLocation +gps.on('locate', function( err, data ) { + + console.log( data ); + +/* + +Looks like: + +{ + time: '033355.000', + latitude: '4221.1902', + longitude: '07108.7709', + hemisphere: { latitude: 'N', longitude: 'W' }, + quality: '2', + satellites: 6, + hdop: 1.6, + altitude: '8.0M' +} + +*/ + + +}); diff --git a/index.js b/index.js index a30f8f9..54f2709 100644 --- a/index.js +++ b/index.js @@ -7,5 +7,6 @@ module.exports = { Servo: require('./lib/servo'), Sensor: require('./lib/sensor'), Ping: require('./lib/ping'), - PIR: require('./lib/pir') + PIR: require('./lib/pir'), + GPS: require('./lib/gps') }; diff --git a/lib/board.js b/lib/board.js index af30801..aa1a419 100644 --- a/lib/board.js +++ b/lib/board.js @@ -21,7 +21,10 @@ var Board = function (options) { self.emit('connected'); self.log('info', 'binding serial events'); - self.serial.on('data', function(data){ + self.serial.on('data', function(data) { + if ( !data.trim() ) { + return; + } self.log('receive', data.toString().red); self.emit('data', data); }); diff --git a/lib/gps.js b/lib/gps.js new file mode 100644 index 0000000..0529801 --- /dev/null +++ b/lib/gps.js @@ -0,0 +1,101 @@ +var events = require('events'), + util = require('util'); + +/* + * Main GPS constructor + * Process options + * Tell the board to set it up + */ + +function ToDecimalDegrees() { +} + +var GPS = function (options) { + 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.init(); + }.bind(this)); + + this.board.on('data', function (message) { + var m = message.slice(0, -1), //m = message.slice(0, -1).split('::'), + err = null, + data = { + time: null, + latitude: null, + longitude: null, + hemisphere: { + latitude: null, + longitude: null, + }, + quality: null, + satellites: null, + hdop: null, + altitude: null + }, + pin, fields; + + if (!m.length) { + return; + } + + // pin = m[0]; + + // Refer to http://aprs.gids.nl/nmea/#gga + + + if ( m.indexOf('$GPGGA') === 0 ) { + // console.log( "valid" ); + // console.log( message ); + + fields = m.split(','); + + // TODO: make time less shitty. + data.time = fields[1]; + data.latitude = fields[2]; + data.longitude = fields[4]; + + data.hemisphere = { + latitude: fields[3], + longitude: fields[5], + }; + + data.quality = fields[6]; + data.satellites = +fields[7]; + data.hdop = +fields[8]; + data.altitude = fields[9] + fields[10]; + + // 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); + } + }.bind(this)); +}; + +util.inherits(GPS, events.EventEmitter); + +GPS.prototype.command = function () { + var msg = '96' + this.pin + ([].slice.call(arguments).join('')); + + // this.board.log( 'info', 'command', msg ); + this.board.write(msg); +}; + +GPS.prototype.init = function () { + this.command('01'); +}; + +GPS.prototype.setPosition = function () { + // Reserved 02 code + // this.command('02'); +}; + +GPS.prototype.getPosition = function () { + this.command('03'); +}; + + +module.exports = GPS; diff --git a/src/du.ino b/src/du.ino index 2427435..cab5b7f 100644 --- a/src/du.ino +++ b/src/du.ino @@ -1,9 +1,12 @@ #include +#include bool debug = false; +bool gps = false; int index = 0; +int gindex = 0; char messageBuffer[12]; char cmd[3]; @@ -11,7 +14,12 @@ char pin[3]; char val[4]; char aux[4]; +char buffer[100]; +char response[100]; + Servo servo; +SoftwareSerial GPS = SoftwareSerial(2, 3); + void setup() { Serial.begin(115200); @@ -24,6 +32,24 @@ void loop() { else if (x == '.') process(); // end else messageBuffer[index++] = x; } + + // If gps boolean flag has been set to true + // via API call to gps.init(); + if (gps) { + while(GPS.available()) { + char g = GPS.read(); + // At the beginning of a GPS sentence + Serial.println(" "); + // Serial.println("ho"); + if (g == '$') { + gindex = 0; // start + // sprintf(response, "%s::%s", pin, buffer); + // Serial.println(response); + Serial.println(buffer); + } + buffer[gindex++] = g; + } + } } /* @@ -65,6 +91,7 @@ void process() { case 2: dr(pin,val); break; case 3: aw(pin,val); break; case 4: ar(pin,val); break; + case 96: handleGPS(pin,val,aux); break; case 97: handlePing(pin,val,aux); break; case 98: handleServo(pin,val,aux); break; case 99: toggleDebug(val); break; @@ -171,6 +198,25 @@ int getPin(char *pin) { //Converts to A0-A5, and returns -1 on error return ret; } +void handleGPS(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("Initializing GPS"); + + // 01(1) Connect + if (atoi(val) == 1) { + GPS.begin(4800); + + // Set the global gps flag to true + // this will tell the main event loop that + // this program expects gps readings + gps = true; + + Serial.println("GPS Connected"); + } +} /* * Handle Ping commands * fire, read From 53b7f4f60088dda743e772d06f5e78da4de77904 Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Mon, 26 Mar 2012 00:43:11 -0400 Subject: [PATCH 02/14] Fix incorrect pins Signed-off-by: Rick Waldron waldron.rick@gmail.com --- examples/analogled.js | 2 +- examples/basic.js | 4 +++- examples/led.js | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/analogled.js b/examples/analogled.js index 66d891a..9664215 100644 --- a/examples/analogled.js +++ b/examples/analogled.js @@ -6,7 +6,7 @@ var board = new arduino.Board({ var aled = new arduino.Led({ board: board, - pin: 9 + pin: 'A0' }); board.on('ready', function(){ diff --git a/examples/basic.js b/examples/basic.js index ca573c0..aa1af8d 100644 --- a/examples/basic.js +++ b/examples/basic.js @@ -1,7 +1,9 @@ var arduino = require('../'); -var board = new arduino.Board(); +var board = new arduino.Board({ + debug: true +}); board.on('connected', function(){ board.write('HELLO WORLD'); diff --git a/examples/led.js b/examples/led.js index 1325284..98381bc 100644 --- a/examples/led.js +++ b/examples/led.js @@ -6,7 +6,7 @@ var board = new arduino.Board({ var led = new arduino.Led({ board: board, - pin: "A0" + pin: 9 }); board.on('ready', function(){ From 93adfb60d4c66a46820d63088158d533f52ddf4c Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Mon, 26 Mar 2012 12:52:46 -0400 Subject: [PATCH 03/14] Move reference url to correct module Signed-off-by: Rick Waldron waldron.rick@gmail.com --- examples/servo.js | 2 -- lib/pir.js | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/servo.js b/examples/servo.js index eb732ab..b7f10a1 100644 --- a/examples/servo.js +++ b/examples/servo.js @@ -52,5 +52,3 @@ servo.on('attached', function(err) { // To test, use the following: // http://arduino.cc/en/uploads/Tutorial/sweep_BB.png // -// More information: -// http://www.ladyada.net/learn/sensors/pir.html diff --git a/lib/pir.js b/lib/pir.js index cef77dc..70092b7 100644 --- a/lib/pir.js +++ b/lib/pir.js @@ -67,3 +67,7 @@ var PIR = function (options) { util.inherits(PIR, events.EventEmitter); module.exports = PIR; + + +// More information: +// http://www.ladyada.net/learn/sensors/pir.html From 826ded134f8f11b94ee4a926f439065f5485e571 Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Mon, 26 Mar 2012 12:58:45 -0400 Subject: [PATCH 04/14] Adds additional nmea support/reference url Signed-off-by: Rick Waldron waldron.rick@gmail.com --- lib/gps.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/gps.js b/lib/gps.js index 0529801..cc5f5be 100644 --- a/lib/gps.js +++ b/lib/gps.js @@ -44,7 +44,9 @@ var GPS = function (options) { // pin = m[0]; - // Refer to http://aprs.gids.nl/nmea/#gga + // Refer to + // http://aprs.gids.nl/nmea/#gga + // http://www.gpsinformation.org/dale/nmea.htm#position if ( m.indexOf('$GPGGA') === 0 ) { From 416abdda9fae5efeaa391254c797cb9aa80b3809 Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Mon, 26 Mar 2012 13:05:12 -0400 Subject: [PATCH 05/14] Adds grunt for project maintenance Signed-off-by: Rick Waldron waldron.rick@gmail.com --- LICENSE-MIT | 22 ++++++++++++++++++++ README.md | 1 - grunt.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 37 +++++++++++++++++++++++--------- 4 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 LICENSE-MIT create mode 100644 grunt.js diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..747725c --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,22 @@ +Copyright (c) 2012 Cam Pedersen + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index f3d84d2..158f5f7 100644 --- a/README.md +++ b/README.md @@ -236,4 +236,3 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/grunt.js b/grunt.js new file mode 100644 index 0000000..a9a298e --- /dev/null +++ b/grunt.js @@ -0,0 +1,59 @@ +module.exports = function(grunt) { + + // Project configuration. + grunt.initConfig({ + pkg: '', + 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: ['', '.js>'], + dest: 'dist/<%= pkg.name %>.js' + } + }, + min: { + dist: { + src: ['', ''], + dest: 'dist/<%= pkg.name %>.min.js' + } + }, + test: { + files: ['test/**/*.js'] + }, + lint: { + files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js'] + }, + watch: { + files: '', + tasks: 'lint test' + }, + jshint: { + options: { + curly: true, + eqeqeq: true, + immed: true, + latedef: true, + newcap: true, + noarg: true, + sub: true, + undef: true, + boss: true, + eqnull: true + }, + globals: { + exports: true, + module: false + } + }, + uglify: {} + }); + + // Default task. + grunt.registerTask('default', 'lint test concat min'); + +}; diff --git a/package.json b/package.json index 0f606f2..cfd95f0 100644 --- a/package.json +++ b/package.json @@ -1,19 +1,36 @@ { - "author": "Cam Pedersen (http://campedersen.com/)", "name": "duino", - "description": "Arduino framework for mad scientists", - "version": "0.0.5", + "description": "A framework for working with Arduinos in node.js", + "version": "0.1.0", + "homepage": "https://github.com/ecto/duino", + "author": { + "name": "Cam Pedersen", + "email": "diffference@gmail.com", + "url": "none" + }, "repository": { "type": "git", "url": "git://github.com/ecto/duino.git" }, - "main": "index.js", - "engines": { - "node": "*" + "bugs": { + "url": "https://github.com/ecto/duino/issues" }, - "dependencies": { - "serialport": "*", - "colors": "*" + "licenses": [ + { + "type": "MIT", + "url": "https://github.com/ecto/duino/blob/master/LICENSE-MIT" + } + ], + "dependencies": {}, + "devDependencies": { + "grunt": "~0.3.0" }, - "devDependencies": {} + "keywords": [], + "engines": { + "node": ">= 0.6.13" + }, + "main": "index.js", + "scripts": { + "test": "grunt test" + } } From 8978a8dd2b35229a7b1d1102e03df747a7e93c63 Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Mon, 26 Mar 2012 13:28:09 -0400 Subject: [PATCH 06/14] Lint fixes, adds empty test dir. Signed-off-by: Rick Waldron waldron.rick@gmail.com --- examples/analogled.js | 6 +- examples/basic.js | 8 +-- examples/button.js | 10 ++-- examples/gps.js | 2 +- examples/led.js | 4 +- examples/piezo.js | 8 +-- examples/pir.js | 20 +++---- examples/sensor.js | 6 +- grunt.js | 40 ++++--------- index.js | 18 +++--- lib/board.js | 136 ++++++++++++++++++++++-------------------- lib/button.js | 42 +++++++------ lib/gps.js | 36 +++++------ lib/led.js | 61 +++++++++++-------- lib/ping.js | 18 +++--- lib/pir.js | 28 ++++----- lib/sensor.js | 20 ++++--- lib/servo.js | 81 +++++++++++++------------ package.json | 2 +- test/duino_test.js | 14 +++++ 20 files changed, 291 insertions(+), 269 deletions(-) create mode 100644 test/duino_test.js diff --git a/examples/analogled.js b/examples/analogled.js index 9664215..f9a5590 100644 --- a/examples/analogled.js +++ b/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(); }); diff --git a/examples/basic.js b/examples/basic.js index aa1af8d..75fc8d9 100644 --- a/examples/basic.js +++ b/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); }); diff --git a/examples/button.js b/examples/button.js index 1ba9ad6..0819f22 100644 --- a/examples/button.js +++ b/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"); }); diff --git a/examples/gps.js b/examples/gps.js index 717484f..9750cf4 100644 --- a/examples/gps.js +++ b/examples/gps.js @@ -1,4 +1,4 @@ -var arduino = require('../'), +" arduino = require('../'), board, gps; board = new arduino.Board({ diff --git a/examples/led.js b/examples/led.js index 98381bc..a7461a4 100644 --- a/examples/led.js +++ b/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(); }); diff --git a/examples/piezo.js b/examples/piezo.js index dd59278..df3272e 100644 --- a/examples/piezo.js +++ b/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); }); diff --git a/examples/pir.js b/examples/pir.js index fd3e788..8d23596 100644 --- a/examples/pir.js +++ b/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); }); }); diff --git a/examples/sensor.js b/examples/sensor.js index b1a105a..5063c77 100644 --- a/examples/sensor.js +++ b/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 ); diff --git a/grunt.js b/grunt.js index a9a298e..7f033d9 100644 --- a/grunt.js +++ b/grunt.js @@ -2,35 +2,16 @@ module.exports = function(grunt) { // Project configuration. grunt.initConfig({ - pkg: '', - 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: ['', '.js>'], - dest: 'dist/<%= pkg.name %>.js' - } - }, - min: { - dist: { - src: ['', ''], - dest: 'dist/<%= pkg.name %>.min.js' - } - }, + pkg: "", test: { - files: ['test/**/*.js'] + files: ["test/**/*.js"] }, lint: { - files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js'] + files: ["grunt.js", "lib/**/*.js", "test/**/*.js"] }, watch: { - files: '', - tasks: 'lint test' + 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"); }; diff --git a/index.js b/index.js index 54f2709..6046d61 100644 --- a/index.js +++ b/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") }; diff --git a/lib/board.js b/lib/board.js index aa1a419..bd363ea 100644 --- a/lib/board.js +++ b/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; diff --git a/lib/button.js b/lib/button.js index 636a132..7e3dd55 100644 --- a/lib/button.js +++ b/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! diff --git a/lib/gps.js b/lib/gps.js index cc5f5be..6301140 100644 --- a/lib/gps.js +++ b/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"); }; diff --git a/lib/led.js b/lib/led.js index 6017587..0d1d25e 100644 --- a/lib/led.js +++ b/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; diff --git a/lib/ping.js b/lib/ping.js index 9eac71b..ead5828 100644 --- a/lib/ping.js +++ b/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; diff --git a/lib/pir.js b/lib/pir.js index 70092b7..6b12ace 100644 --- a/lib/pir.js +++ b/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)); diff --git a/lib/sensor.js b/lib/sensor.js index a1883fc..66cfd54 100644 --- a/lib/sensor.js +++ b/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)); }; diff --git a/lib/servo.js b/lib/servo.js index 2a9b1d4..002eefd 100644 --- a/lib/servo.js +++ b/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; diff --git a/package.json b/package.json index cfd95f0..16d6493 100644 --- a/package.json +++ b/package.json @@ -33,4 +33,4 @@ "scripts": { "test": "grunt test" } -} +} \ No newline at end of file diff --git a/test/duino_test.js b/test/duino_test.js new file mode 100644 index 0000000..37d9f0e --- /dev/null +++ b/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(); +// } +// }; From d31a6d92a14f70ab64888a8a873b433201c9ccde Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Mon, 26 Mar 2012 13:29:18 -0400 Subject: [PATCH 07/14] Tweaks to lib/piezo.js (aborting process.nextTick experiment) Signed-off-by: Rick Waldron waldron.rick@gmail.com --- lib/piezo.js | 80 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/lib/piezo.js b/lib/piezo.js index a3a0dbb..80401f6 100644 --- a/lib/piezo.js +++ b/lib/piezo.js @@ -1,43 +1,65 @@ /* - * Main Pieze constructor + * Main Piezo constructor * Process options * Tell the board to set pin mode */ var Piezo = function (options) { - if (!options || !options.board) throw new Error('Must supply required options to Piezo'); + if (!options || !options.board) { + throw new Error("Must supply required options to Piezo"); + } + this.board = options.board; - this.pin = options.pin || 13; + this.pin = this.board.normalizePin(options.pin || 11); + + // Set pinMode for OUTPUT + this.board.pinMode(this.pin, "out"); + + // Module specific instance properties + // Piezo buzzer brightness this.bright = false; - this.board.pinMode(this.pin, 'out'); -} + + this.playing = false; + // Locked? This piezo"s current lock state, + // this allows us to avoid potentially making way too + // may serial writes + this.isLocked = false; + // Stores data about this piezo"s state + // used to determine lock state + this.state = { + tone: null, + note: null, + lapse: null + }; +}; /* * Send a square wave to the speaker for a given duration */ -Piezo.prototype.tone = function (tone, duration) { - this.board.log('info', 'starting tone ' + tone.toString().green + ' for ' + duration.toString().green + ' milliseconds'); +Piezo.prototype.tone = function(tone, duration) { + this.board.log("info", "starting tone " + tone.toString().green + " for " + duration.toString().green + " milliseconds"); - var self = this, - tone = Math.floor(tone / 1000); // timeHigh is in microseconds and our delay() function is in milliseconds + // timeHigh is in microseconds and our delay() function is in milliseconds + var micro = Math.floor(tone / 1000), i = 0; setInterval(function(){ - if (self.bright) { - self.board.digitalWrite(self.pin, self.board.LOW); - self.bright = false; + if (this.bright) { + this.board.digitalWrite(this.pin, this.board.LOW); + this.bright = false; } else { - self.board.digitalWrite(self.pin, self.board.HIGH); - self.bright = true; + this.board.digitalWrite(this.pin, this.board.HIGH); + this.bright = true; } if (i++ >= duration) { - self.board.log('info', 'tone end'); clearInterval(this); + + this.board.log("info", "tone end"); } - }, tone); + }.bind(this), micro); -} +}; /* * Play a tone for a given duration to create a note @@ -55,18 +77,16 @@ Piezo.prototype.tone = function (tone, duration) { * c 523hz 1912 956 */ Piezo.prototype.note = function (note, duration) { - this.board.log('info', 'playing note ' + note.green + ' for ' + duration.toString().green + ' milliseconds'); - var notes = { - 'c': 1915, - 'd': 1700, - 'e': 1519, - 'f': 1432, - 'g': 1275, - 'a': 1136, - 'b': 1014, - 'c': 956 - }; - this.tone(notes[note], duration); -} + this.board.log("info", "playing note " + note.green + " for " + duration.toString().green + " milliseconds"); + this.tone({ + "c": 1915, + "d": 1700, + "e": 1519, + "f": 1432, + "g": 1275, + "a": 1136, + "b": 1014 + }[ note ], duration); +}; module.exports = Piezo; From b1d368b6df2da434bf4432994353bf9567b57014 Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Mon, 26 Mar 2012 13:30:57 -0400 Subject: [PATCH 08/14] Grunt instructions Signed-off-by: Rick Waldron waldron.rick@gmail.com --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 158f5f7..61cdd6e 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,22 @@ Pins can be sent as an integer or a string(`1`, `2`, `"3"`, `"A0"`) * `board.HIGH`(`255`) * integer/string from `0`-`255` for PWM pins + +## Development + +# Requires grunt for code linting & test running. + +```bash +npm install grunt -g +``` + +Run with: + +``` +grunt +``` + + # license (The MIT License) From 76e4188aac034a4d87b34b6a0e9e67e7b4847bfa Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Mon, 26 Mar 2012 13:31:34 -0400 Subject: [PATCH 09/14] Fixes typo in lib/gps.js Signed-off-by: Rick Waldron waldron.rick@gmail.com --- examples/gps.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gps.js b/examples/gps.js index 9750cf4..717484f 100644 --- a/examples/gps.js +++ b/examples/gps.js @@ -1,4 +1,4 @@ -" arduino = require('../'), +var arduino = require('../'), board, gps; board = new arduino.Board({ From 2a3c78e03f33c36730ede4bd337b6a3d00fd4b30 Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Tue, 27 Mar 2012 10:58:39 -0400 Subject: [PATCH 10/14] Consistent quoting Signed-off-by: Rick Waldron waldron.rick@gmail.com --- examples/analogled.js | 6 +-- examples/basic.js | 8 ++-- examples/button.js | 10 ++--- examples/led.js | 4 +- examples/piezo.js | 8 ++-- examples/pir.js | 20 ++++----- examples/sensor.js | 6 +-- examples/servo.js | 6 +-- grunt.js | 12 ++--- index.js | 18 ++++---- lib/board.js | 100 +++++++++++++++++++++--------------------- lib/button.js | 16 +++---- lib/gps.js | 32 +++++++------- lib/led.js | 4 +- lib/piezo.js | 28 ++++++------ lib/ping.js | 16 +++---- lib/pir.js | 26 +++++------ lib/sensor.js | 16 +++---- lib/servo.js | 46 +++++++++---------- 19 files changed, 191 insertions(+), 191 deletions(-) diff --git a/examples/analogled.js b/examples/analogled.js index f9a5590..9664215 100644 --- a/examples/analogled.js +++ b/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(); }); diff --git a/examples/basic.js b/examples/basic.js index 75fc8d9..aa1af8d 100644 --- a/examples/basic.js +++ b/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); }); diff --git a/examples/button.js b/examples/button.js index 0819f22..1ba9ad6 100644 --- a/examples/button.js +++ b/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'); }); diff --git a/examples/led.js b/examples/led.js index a7461a4..98381bc 100644 --- a/examples/led.js +++ b/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(); }); diff --git a/examples/piezo.js b/examples/piezo.js index df3272e..dd59278 100644 --- a/examples/piezo.js +++ b/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); }); diff --git a/examples/pir.js b/examples/pir.js index 8d23596..fd3e788 100644 --- a/examples/pir.js +++ b/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); }); }); diff --git a/examples/sensor.js b/examples/sensor.js index 5063c77..b1a105a 100644 --- a/examples/sensor.js +++ b/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 ); diff --git a/examples/servo.js b/examples/servo.js index b7f10a1..0c2b44c 100644 --- a/examples/servo.js +++ b/examples/servo.js @@ -17,13 +17,13 @@ servo = new arduino.Servo({ }); // Once servo is attached: -// - "read" +// - 'read' // - log position -// - "aftersweep" +// - 'aftersweep' // - blink the led // - read the position // - detach the servo -// - "detached" +// - 'detached' // - log detach message // // - execute full sweep diff --git a/grunt.js b/grunt.js index 7f033d9..ff4feb7 100644 --- a/grunt.js +++ b/grunt.js @@ -2,16 +2,16 @@ module.exports = function(grunt) { // Project configuration. grunt.initConfig({ - pkg: "", + pkg: '', test: { - files: ["test/**/*.js"] + files: ['test/**/*.js'] }, lint: { - files: ["grunt.js", "lib/**/*.js", "test/**/*.js"] + files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js'] }, watch: { - files: "", - tasks: "default" + files: '', + tasks: 'default' }, jshint: { options: { @@ -34,6 +34,6 @@ module.exports = function(grunt) { }); // Default task. - grunt.registerTask("default", "lint test"); + grunt.registerTask('default', 'lint test'); }; diff --git a/index.js b/index.js index 6046d61..54f2709 100644 --- a/index.js +++ b/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') }; diff --git a/lib/board.js b/lib/board.js index bd363ea..718e3a7 100644 --- a/lib/board.js +++ b/lib/board.js @@ -1,16 +1,16 @@ -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 = []; @@ -21,27 +21,27 @@ var Board = function (options) { 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(); @@ -53,7 +53,7 @@ var Board = function (options) { self.processWriteBuffer(); } - self.emit("ready"); + self.emit('ready'); }, 500); }); }; @@ -69,32 +69,32 @@ 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){ + child.exec('ls /dev | grep usb', function(err, stdout, stderr){ err = err || null; - var usb = stdout.slice(0, -1).split("\n"), + var usb = stdout.slice(0, -1).split('\n'), found = false, 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'); } } } @@ -108,16 +108,16 @@ Board.prototype.detect = function (callback) { * 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()); } }; @@ -127,10 +127,10 @@ Board.prototype.processWriteBuffer = function () { */ 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); } }; @@ -139,38 +139,38 @@ Board.prototype.write = function (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 ((new 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); }; /* @@ -179,8 +179,8 @@ 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); }; /* @@ -188,21 +188,21 @@ Board.prototype.digitalWrite = function (pin, val) { */ 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)); }; /* @@ -219,7 +219,7 @@ 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(', ')); } }; diff --git a/lib/button.js b/lib/button.js index 7e3dd55..cd0237c 100644 --- a/lib/button.js +++ b/lib/button.js @@ -1,5 +1,5 @@ -var events = require("events"), - util = require("util"); +var events = require('events'), + util = require('util'); /* * Main Button constructor @@ -8,11 +8,11 @@ var events = require("events"), */ var Button = function (options) { if (!options || !options.board) { - throw new Error("Must supply required options to Button"); + 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.board.pinMode(this.pin, 'in'); this.down = false; @@ -20,8 +20,8 @@ var Button = function (options) { this.board.digitalRead(this.pin); }.bind(this), 50); - this.board.on("data", function (m) { - m = m.slice(0, -1).split("::"); + this.board.on('data', function (m) { + m = m.slice(0, -1).split('::'); var err = null; @@ -30,11 +30,11 @@ var Button = function (options) { // 1 is down if (+m[1] === 0 && this.down) { this.down = false; - this.emit("up", err); + this.emit('up', err); } if (+m[1] === 1 && !this.down) { this.down = true; - this.emit("down", err); + this.emit('down', err); } } }.bind(this)); diff --git a/lib/gps.js b/lib/gps.js index 6301140..409b283 100644 --- a/lib/gps.js +++ b/lib/gps.js @@ -1,5 +1,5 @@ -var events = require("events"), - util = require("util"); +var events = require('events'), + util = require('util'); /* * Main GPS constructor @@ -12,18 +12,18 @@ function ToDecimalDegrees() { var GPS = function (options) { if (!options || !options.board) { - throw new Error("Must supply required options to GPS"); + 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, @@ -51,11 +51,11 @@ var GPS = function (options) { // http://www.gpsinformation.org/dale/nmea.htm#position - if ( m.indexOf("$GPGGA") === 0 ) { - // console.log( "valid" ); + 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]; @@ -74,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)); }; @@ -82,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'); }; diff --git a/lib/led.js b/lib/led.js index 0d1d25e..c061623 100644 --- a/lib/led.js +++ b/lib/led.js @@ -6,11 +6,11 @@ */ var Led = function(options) { if (!options || !options.board) { - throw new Error("Must supply required options to LED"); + 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.board.pinMode(this.pin, 'out'); this.bright = 0; this.direction = -1; diff --git a/lib/piezo.js b/lib/piezo.js index 80401f6..4fad6ec 100644 --- a/lib/piezo.js +++ b/lib/piezo.js @@ -6,25 +6,25 @@ */ var Piezo = function (options) { if (!options || !options.board) { - throw new Error("Must supply required options to Piezo"); + throw new Error('Must supply required options to Piezo'); } this.board = options.board; this.pin = this.board.normalizePin(options.pin || 11); // Set pinMode for OUTPUT - this.board.pinMode(this.pin, "out"); + this.board.pinMode(this.pin, 'out'); // Module specific instance properties // Piezo buzzer brightness this.bright = false; this.playing = false; - // Locked? This piezo"s current lock state, + // Locked? This piezo's current lock state, // this allows us to avoid potentially making way too // may serial writes this.isLocked = false; - // Stores data about this piezo"s state + // Stores data about this piezo's state // used to determine lock state this.state = { tone: null, @@ -37,7 +37,7 @@ var Piezo = function (options) { * Send a square wave to the speaker for a given duration */ Piezo.prototype.tone = function(tone, duration) { - this.board.log("info", "starting tone " + tone.toString().green + " for " + duration.toString().green + " milliseconds"); + this.board.log('info', 'starting tone ' + tone.toString().green + ' for ' + duration.toString().green + ' milliseconds'); // timeHigh is in microseconds and our delay() function is in milliseconds var micro = Math.floor(tone / 1000), @@ -55,7 +55,7 @@ Piezo.prototype.tone = function(tone, duration) { if (i++ >= duration) { clearInterval(this); - this.board.log("info", "tone end"); + this.board.log('info', 'tone end'); } }.bind(this), micro); @@ -77,15 +77,15 @@ Piezo.prototype.tone = function(tone, duration) { * c 523hz 1912 956 */ Piezo.prototype.note = function (note, duration) { - this.board.log("info", "playing note " + note.green + " for " + duration.toString().green + " milliseconds"); + this.board.log('info', 'playing note ' + note.green + ' for ' + duration.toString().green + ' milliseconds'); this.tone({ - "c": 1915, - "d": 1700, - "e": 1519, - "f": 1432, - "g": 1275, - "a": 1136, - "b": 1014 + 'c': 1915, + 'd': 1700, + 'e': 1519, + 'f': 1432, + 'g': 1275, + 'a': 1136, + 'b': 1014 }[ note ], duration); }; diff --git a/lib/ping.js b/lib/ping.js index ead5828..16ba202 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -1,5 +1,5 @@ -var events = require("events"), - util = require("util"); +var events = require('events'), + util = require('util'); /* * Main Ping constructor @@ -8,7 +8,7 @@ var events = require("events"), */ var Ping = function (options) { if (!options || !options.board) { - throw new Error("Must supply required options to Ping"); + throw new Error('Must supply required options to Ping'); } this.board = options.board; this.pin = this.board.normalizePin(options.pin || 9); @@ -27,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; @@ -42,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. @@ -61,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; diff --git a/lib/pir.js b/lib/pir.js index 6b12ace..42b15b2 100644 --- a/lib/pir.js +++ b/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; @@ -40,25 +40,25 @@ var PIR = function (options) { // 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)); diff --git a/lib/sensor.js b/lib/sensor.js index 66cfd54..4f76446 100644 --- a/lib/sensor.js +++ b/lib/sensor.js @@ -1,5 +1,5 @@ -var events = require("events"), - util = require("util"); +var events = require('events'), + util = require('util'); /* * Main Sensor constructor @@ -8,11 +8,11 @@ var events = require("events"), */ var Sensor = function (options) { if (!options || !options.board) { - throw new Error("Must supply required options to Sensor"); + 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 () { @@ -21,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; @@ -34,7 +34,7 @@ var Sensor = function (options) { data = m.length === 2 ? m[1] : null; if (pin === this.pin) { - this.emit("read", err, data); + this.emit('read', err, data); } }.bind(this)); }; diff --git a/lib/servo.js b/lib/servo.js index 002eefd..983151c 100644 --- a/lib/servo.js +++ b/lib/servo.js @@ -1,5 +1,5 @@ -var events = require("events"), - util = require("util"); +var events = require('events'), + util = require('util'); /* * Main Servo constructor @@ -8,7 +8,7 @@ var events = require("events"), */ var Servo = function (options) { if (!options || !options.board) { - throw new Error("Must supply required options to Servo"); + throw new Error('Must supply required options to Servo'); } this.board = options.board; this.pin = this.board.normalizePin(options.pin || 9); @@ -20,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; @@ -47,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 () {}; @@ -91,17 +91,17 @@ Servo.prototype.sweep = function (options) { 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; @@ -118,25 +118,25 @@ Servo.prototype.sweep = function (options) { }, loop = function() { // Read the current position, will trigger - // "read" event with position data; + // 'read' event with position data; if (moves < 2) { this.read(); timeout.inner = setTimeout(loop.bind(this), lapse); } else { - // this.board.log("info", "info", "clearing"); + // this.board.log('info', 'info', 'clearing'); clearTimeout(timeout.inner); clearTimeout(timeout.outer); loop = null; - this.removeListener("read", doSweep); - this.emit.call(this, "aftersweep"); + this.removeListener('read', doSweep); + this.emit.call(this, 'aftersweep'); } }.bind(this); - this.on("read", doSweep); + this.on('read', doSweep); // Initialize sweep; wait for for stack unwind. timeout.outer = setTimeout(loop, 0); From 16aacb8c0444aacbbef192812c93d6c9df666f6f Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Tue, 27 Mar 2012 13:12:47 -0400 Subject: [PATCH 11/14] Minor ws tweaks; pin normalize calls Signed-off-by: Rick Waldron waldron.rick@gmail.com --- examples/analogled.js | 2 +- examples/basic.js | 2 +- examples/button.js | 4 ++-- examples/combination.js | 4 ++-- examples/led.js | 2 +- examples/piezo.js | 2 +- lib/board.js | 28 ++++++++++++++-------------- lib/button.js | 15 ++++++++------- lib/gps.js | 14 +++++++------- lib/piezo.js | 4 ++-- lib/ping.js | 11 ++++++----- lib/pir.js | 6 +++--- lib/sensor.js | 9 +++++---- 13 files changed, 53 insertions(+), 50 deletions(-) diff --git a/examples/analogled.js b/examples/analogled.js index 9664215..a4d5c3d 100644 --- a/examples/analogled.js +++ b/examples/analogled.js @@ -9,6 +9,6 @@ var aled = new arduino.Led({ pin: 'A0' }); -board.on('ready', function(){ +board.on('ready', function() { aled.fade(); }); diff --git a/examples/basic.js b/examples/basic.js index aa1af8d..12676d2 100644 --- a/examples/basic.js +++ b/examples/basic.js @@ -5,7 +5,7 @@ var board = new arduino.Board({ debug: true }); -board.on('connected', function(){ +board.on('connected', function() { board.write('HELLO WORLD'); }); diff --git a/examples/button.js b/examples/button.js index 1ba9ad6..faf128f 100644 --- a/examples/button.js +++ b/examples/button.js @@ -7,10 +7,10 @@ var button = new arduino.Button({ pin: 2 }); -button.on('down', function(){ +button.on('down', function() { console.log('DOWN'); }); -button.on('up', function(){ +button.on('up', function() { console.log('UP'); }); diff --git a/examples/combination.js b/examples/combination.js index a5812e4..8ff85c6 100644 --- a/examples/combination.js +++ b/examples/combination.js @@ -12,10 +12,10 @@ var led = new arduino.Led({ pin: 13 }); -button.on('down', function(){ +button.on('down', function() { led.on(); }); -button.on('up', function(){ +button.on('up', function() { led.off(); }); diff --git a/examples/led.js b/examples/led.js index 98381bc..4d2030d 100644 --- a/examples/led.js +++ b/examples/led.js @@ -9,6 +9,6 @@ var led = new arduino.Led({ pin: 9 }); -board.on('ready', function(){ +board.on('ready', function() { led.blink(); }); diff --git a/examples/piezo.js b/examples/piezo.js index dd59278..c1ecd12 100644 --- a/examples/piezo.js +++ b/examples/piezo.js @@ -8,7 +8,7 @@ var piezo = new arduino.Piezo({ board: board }); -board.on('ready', function(){ +board.on('ready', function() { piezo.note('a', 1000); setTimeout(function(){ diff --git a/lib/board.js b/lib/board.js index 718e3a7..4073186 100644 --- a/lib/board.js +++ b/lib/board.js @@ -9,14 +9,14 @@ var events = require('events'), * The main Arduino constructor * Connect to the serial port and bind */ -var Board = function (options) { +var Board = function(options) { this.log('info', 'initializing'); this.debug = options && options.debug || false; this.writeBuffer = []; var self = this; - this.detect(function (err, serial) { + this.detect(function(err, serial) { if (err) { throw err; } @@ -68,7 +68,7 @@ util.inherits(Board, events.EventEmitter); * Loop through all USB devices and try to connect * This should really message the device and wait for a correct response */ -Board.prototype.detect = function (callback) { +Board.prototype.detect = function(callback) { this.log('info', 'attempting to find Arduino board'); var self = this; child.exec('ls /dev | grep usb', function(err, stdout, stderr){ @@ -107,14 +107,14 @@ Board.prototype.detect = function (callback) { * The board will eat the first 4 bytes of the session * So we give it crap to eat */ -Board.prototype.sendClearingBytes = function () { +Board.prototype.sendClearingBytes = function() { this.serial.write('00000000'); }; /* * Process the writeBuffer (messages attempted before serial was ready) */ -Board.prototype.processWriteBuffer = function () { +Board.prototype.processWriteBuffer = function() { this.log('info', 'processing buffered messages'); while (this.writeBuffer.length > 0) { this.log('info', 'writing buffered message'); @@ -125,7 +125,7 @@ Board.prototype.processWriteBuffer = function () { /* * Low-level serial write */ -Board.prototype.write = function (m) { +Board.prototype.write = function(m) { if (this.serial) { this.log('write', m); this.serial.write('!' + m + '.'); @@ -138,7 +138,7 @@ Board.prototype.write = function (m) { /* * Add a 0 to the front of a single-digit pin number */ -Board.prototype.normalizePin = function (pin) { +Board.prototype.normalizePin = function(pin) { return this.lpad( 2, '0', pin ); }; @@ -162,7 +162,7 @@ Board.prototype.LOW = '000'; * val == out = 001 * val == in = 000 */ -Board.prototype.pinMode = function (pin, val) { +Board.prototype.pinMode = function(pin, val) { pin = this.normalizePin(pin); this.log('info', 'set pin ' + pin + ' mode to ' + val); val = ( @@ -176,7 +176,7 @@ Board.prototype.pinMode = function (pin, val) { /* * Tell the board to write to a digital pin */ -Board.prototype.digitalWrite = 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); @@ -186,20 +186,20 @@ Board.prototype.digitalWrite = function (pin, val) { /* * Tell the board to extract data from a pin */ -Board.prototype.digitalRead = function (pin) { +Board.prototype.digitalRead = function(pin) { pin = this.normalizePin(pin); this.log('info', 'digitalRead from pin ' + pin); this.write('02' + pin + this.normalizeVal(0)); }; -Board.prototype.analogWrite = function (pin, val) { +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); }; -Board.prototype.analogRead = function (pin) { +Board.prototype.analogRead = function(pin) { pin = this.normalizePin(pin); this.log('info', 'analogRead from pin ' + pin); this.write('04' + pin + this.normalizeVal(0)); @@ -208,7 +208,7 @@ Board.prototype.analogRead = function (pin) { /* * Utility function to pause for a given time */ -Board.prototype.delay = function (ms) { +Board.prototype.delay = function(ms) { ms += +new Date(); while (+new Date() < ms) { } }; @@ -216,7 +216,7 @@ Board.prototype.delay = function (ms) { /* * Logger utility function */ -Board.prototype.log = function (/*level, message*/) { +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(', ')); diff --git a/lib/button.js b/lib/button.js index cd0237c..ed65272 100644 --- a/lib/button.js +++ b/lib/button.js @@ -6,12 +6,12 @@ var events = require('events'), * Process options * Tell the board to set it up */ -var Button = function (options) { +var Button = function(options) { if (!options || !options.board) { throw new Error('Must supply required options to Button'); } this.board = options.board; - this.pin = options.pin || 13; + this.pin = this.board.normalizePin(options.pin || 13); this.board.pinMode(this.pin, 'in'); this.down = false; @@ -20,7 +20,7 @@ var Button = function (options) { this.board.digitalRead(this.pin); }.bind(this), 50); - this.board.on('data', function (m) { + this.board.on('data', function(m) { m = m.slice(0, -1).split('::'); var err = null; @@ -31,10 +31,11 @@ var Button = function (options) { if (+m[1] === 0 && this.down) { this.down = false; this.emit('up', err); - } - if (+m[1] === 1 && !this.down) { - this.down = true; - this.emit('down', err); + } else { + if (+m[1] === 1 && !this.down) { + this.down = true; + this.emit('down', err); + } } } }.bind(this)); diff --git a/lib/gps.js b/lib/gps.js index 409b283..0098a44 100644 --- a/lib/gps.js +++ b/lib/gps.js @@ -10,19 +10,19 @@ var events = require('events'), function ToDecimalDegrees() { } -var GPS = function (options) { +var GPS = function(options) { 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.on('ready', function() { this.board.log('info', 'initializing gps'); this.init(); }.bind(this)); - this.board.on('data', function (message) { + this.board.on('data', function(message) { var m = message.slice(0, -1), //m = message.slice(0, -1).split('::'), err = null, data = { @@ -81,23 +81,23 @@ var GPS = function (options) { util.inherits(GPS, events.EventEmitter); -GPS.prototype.command = function () { +GPS.prototype.command = function() { var msg = '96' + this.pin + ([].slice.call(arguments).join('')); // this.board.log( 'info', 'command', msg ); this.board.write(msg); }; -GPS.prototype.init = function () { +GPS.prototype.init = function() { this.command('01'); }; -GPS.prototype.setPosition = function () { +GPS.prototype.setPosition = function() { // Reserved 02 code // this.command('02'); }; -GPS.prototype.getPosition = function () { +GPS.prototype.getPosition = function() { this.command('03'); }; diff --git a/lib/piezo.js b/lib/piezo.js index 4fad6ec..cee881d 100644 --- a/lib/piezo.js +++ b/lib/piezo.js @@ -4,7 +4,7 @@ * Process options * Tell the board to set pin mode */ -var Piezo = function (options) { +var Piezo = function(options) { if (!options || !options.board) { throw new Error('Must supply required options to Piezo'); } @@ -76,7 +76,7 @@ Piezo.prototype.tone = function(tone, duration) { * b 493hz 2028 1014 * c 523hz 1912 956 */ -Piezo.prototype.note = function (note, duration) { +Piezo.prototype.note = function(note, duration) { this.board.log('info', 'playing note ' + note.green + ' for ' + duration.toString().green + ' milliseconds'); this.tone({ 'c': 1915, diff --git a/lib/ping.js b/lib/ping.js index 16ba202..48eb21f 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -6,7 +6,7 @@ var events = require('events'), * Process options * Tell the board to set it up */ -var Ping = function (options) { +var Ping = function(options) { if (!options || !options.board) { throw new Error('Must supply required options to Ping'); } @@ -23,11 +23,11 @@ var Ping = function (options) { }; // Loop and trigger fire-read sequences - setInterval(function () { + setInterval(function() { this.fire(); }.bind(this), 50); - this.board.on('data', function (message) { + this.board.on('data', function(message) { var m = message.slice(0, -1).split('::'), err = null, pin, type, data; @@ -41,6 +41,7 @@ var Ping = function (options) { data = m.length === 3 ? m[2] : null; if (pin === this.pin && types[type]) { + this.microseconds = +data; // See: http://arduino.cc/en/Tutorial/Ping // According to Parallax's datasheet for the PING))), there are // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per @@ -60,13 +61,13 @@ var Ping = function (options) { util.inherits(Ping, events.EventEmitter); -Ping.prototype.command = function () { +Ping.prototype.command = function() { var msg = '97' + this.pin + ([].slice.call(arguments).join('')); this.board.write(msg); }; -Ping.prototype.fire = function () { +Ping.prototype.fire = function() { this.command('01'); }; diff --git a/lib/pir.js b/lib/pir.js index 42b15b2..21e7029 100644 --- a/lib/pir.js +++ b/lib/pir.js @@ -6,7 +6,7 @@ var events = require('events'), * Process options * Tell the board to set it up */ -var PIR = function (options) { +var PIR = function(options) { if (!options || !options.board) { throw new Error('Must supply required options to PIR'); } @@ -15,11 +15,11 @@ var PIR = function (options) { this.state = null; this.calibrated = false; - setInterval(function () { + setInterval(function() { this.board.digitalRead(this.pin); }.bind(this), 50); - this.board.on('data', function (message) { + this.board.on('data', function(message) { var m = message.slice(0, -1).split('::'), timestamp = new Date(), err = null, diff --git a/lib/sensor.js b/lib/sensor.js index 4f76446..9066e55 100644 --- a/lib/sensor.js +++ b/lib/sensor.js @@ -6,22 +6,23 @@ var events = require('events'), * Process options * Tell the board to set it up */ -var Sensor = function (options) { +var Sensor = function(options) { if (!options || !options.board) { throw new Error('Must supply required options to Sensor'); } this.board = options.board; - this.pin = options.pin || 'A0'; + this.pin = this.board.normalizePin(options.pin || 'A0'); this.board.pinMode(this.pin, 'in'); // Poll for sensor readings - setInterval(function () { + setInterval(function() { + // TODO: make this dependant on the pin type this.board.analogRead(this.pin); }.bind(this), options.throttle || 50); // When data is received, parse inbound message // match pin to instance pin value - this.board.on('data', function (message) { + this.board.on('data', function(message) { var m = message.slice(0, -1).split('::'), err = null, pin, data; From 6d185579ef9b5cec1b07616e2ffd43516605b165 Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Tue, 27 Mar 2012 13:13:06 -0400 Subject: [PATCH 12/14] Adds tests. Run with nodeunit test/file.js Signed-off-by: Rick Waldron waldron.rick@gmail.com --- test/board.js | 26 ++++++++++++++++++++++++++ test/button.js | 30 ++++++++++++++++++++++++++++++ test/button_test.js | 30 ++++++++++++++++++++++++++++++ test/duino_test.js | 14 -------------- test/gps.js | 43 +++++++++++++++++++++++++++++++++++++++++++ test/led.js | 31 +++++++++++++++++++++++++++++++ test/piezo.js | 31 +++++++++++++++++++++++++++++++ test/ping.js | 33 +++++++++++++++++++++++++++++++++ test/pir.js | 38 ++++++++++++++++++++++++++++++++++++++ test/sensor.js | 30 ++++++++++++++++++++++++++++++ 10 files changed, 292 insertions(+), 14 deletions(-) create mode 100644 test/board.js create mode 100644 test/button.js create mode 100644 test/button_test.js delete mode 100644 test/duino_test.js create mode 100644 test/gps.js create mode 100644 test/led.js create mode 100644 test/piezo.js create mode 100644 test/ping.js create mode 100644 test/pir.js create mode 100644 test/sensor.js diff --git a/test/board.js b/test/board.js new file mode 100644 index 0000000..01892db --- /dev/null +++ b/test/board.js @@ -0,0 +1,26 @@ +var arduino = require('../'), + board; + +board = new arduino.Board({ + debug: true +}); + +exports['board'] = { + 'connected & message': function(test) { + test.expect(3); + + board.on('connected', function() { + test.ok(true, 'connected'); + board.write('foo'); + }); + + board.on('ready', function() { + test.ok(true, 'ready'); + }); + + board.on('data', function(data) { + test.ok(true, 'message'); + test.done(); + }); + } +}; diff --git a/test/button.js b/test/button.js new file mode 100644 index 0000000..86f7200 --- /dev/null +++ b/test/button.js @@ -0,0 +1,30 @@ +var arduino = require('../'); + + +// console.log( board ); +exports['button'] = function(test) { + test.expect(2); + + var board = new arduino.Board({ + debug: true + }); + + var button = new arduino.Button({ + board: board, + pin: 9 + }); + + button.on('down', function() { + test.ok(true, 'down'); + test.ok(button.down, 'state'); + test.done(); + + process.nextTick(function() { + process.exit(); + }); + }); + + board.on('ready', function() { + board.serial.emit('data', '09::01\r'); + }); +}; diff --git a/test/button_test.js b/test/button_test.js new file mode 100644 index 0000000..86f7200 --- /dev/null +++ b/test/button_test.js @@ -0,0 +1,30 @@ +var arduino = require('../'); + + +// console.log( board ); +exports['button'] = function(test) { + test.expect(2); + + var board = new arduino.Board({ + debug: true + }); + + var button = new arduino.Button({ + board: board, + pin: 9 + }); + + button.on('down', function() { + test.ok(true, 'down'); + test.ok(button.down, 'state'); + test.done(); + + process.nextTick(function() { + process.exit(); + }); + }); + + board.on('ready', function() { + board.serial.emit('data', '09::01\r'); + }); +}; diff --git a/test/duino_test.js b/test/duino_test.js deleted file mode 100644 index 37d9f0e..0000000 --- a/test/duino_test.js +++ /dev/null @@ -1,14 +0,0 @@ -// 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(); -// } -// }; diff --git a/test/gps.js b/test/gps.js new file mode 100644 index 0000000..7fd82c0 --- /dev/null +++ b/test/gps.js @@ -0,0 +1,43 @@ +var arduino = require('../'); + + +// console.log( board ); +exports['gps'] = function(test) { + test.expect(1); + + var board = new arduino.Board({ + debug: true + }); + + var gps = new arduino.GPS({ + board: board, + pin: 9 + }); + + var expecting = { + time: '024322.000', + latitude: '4221.1928', + longitude: '07108.7770', + hemisphere: { latitude: 'N', longitude: 'W' }, + quality: '2', + satellites: 7, + hdop: 1.2, + altitude: '6.2M' + }; + + gps.on('locate', function(err, data) { + test.deepEqual( data, expecting, "data object matches expected "); + + console.log( data, expecting ); + + test.done(); + + process.nextTick(function() { + process.exit(); + }); + }); + + board.on('ready', function() { + board.serial.emit('data', '$GPGGA,024322.000,4221.1928,N,07108.7770,W,2,07,1.2,6.2,M,-33.7,M,1.8,0000*4B\r'); + }); +}; diff --git a/test/led.js b/test/led.js new file mode 100644 index 0000000..d0dd431 --- /dev/null +++ b/test/led.js @@ -0,0 +1,31 @@ +var arduino = require('../'); + + +// console.log( board ); +exports['led'] = function(test) { + test.expect(4); + + var board = new arduino.Board({ + debug: true + }); + + var led = new arduino.Led({ + board: board, + pin: 9 + }); + + var api = [ 'on', 'off', 'blink', 'fade' ]; + + board.on('ready', function() { + + api.forEach(function(method) { + test.ok(method in led, method); + }); + + test.done(); + + process.nextTick(function() { + process.exit(); + }); + }); +}; diff --git a/test/piezo.js b/test/piezo.js new file mode 100644 index 0000000..e072716 --- /dev/null +++ b/test/piezo.js @@ -0,0 +1,31 @@ +var arduino = require('../'); + + +// console.log( board ); +exports['piezo'] = function(test) { + test.expect(2); + + var board = new arduino.Board({ + debug: true + }); + + var piezo = new arduino.Piezo({ + board: board, + pin: 9 + }); + + var api = [ 'tone', 'note' ]; + + board.on('ready', function() { + + api.forEach(function(method) { + test.ok(method in piezo, method); + }); + + test.done(); + + process.nextTick(function() { + process.exit(); + }); + }); +}; diff --git a/test/ping.js b/test/ping.js new file mode 100644 index 0000000..acae076 --- /dev/null +++ b/test/ping.js @@ -0,0 +1,33 @@ +var arduino = require('../'); + + +// console.log( board ); +exports['ping'] = function(test) { + test.expect(3); + + var board = new arduino.Board({ + debug: true + }); + + var ping = new arduino.Ping({ + board: board, + pin: 9 + }); + + ping.on('read', function(err, data) { + + test.equal(this.microseconds, 300, "microseconds"); + test.equal(this.inches, 2.027027027027027, "inches"); + test.equal(this.centimeters, 5.172413793103448, "centimeters"); + + test.done(); + + process.nextTick(function() { + process.exit(); + }); + }); + + board.on('ready', function() { + board.serial.emit('data', '09::read::300\r'); + }); +}; diff --git a/test/pir.js b/test/pir.js new file mode 100644 index 0000000..869ba9b --- /dev/null +++ b/test/pir.js @@ -0,0 +1,38 @@ +var arduino = require('../'); + + +// console.log( board ); +exports['pir'] = function(test) { + test.expect(3); + + var board = new arduino.Board({ + debug: true + }); + + var pir = new arduino.PIR({ + board: board, + pin: 9 + }); + + pir.on('calibrated', function(err, data) { + test.ok(true, 'calibrated'); + + pir.on('motionstart', function(err, data) { + test.ok(true, data); + + board.serial.emit('data', '09::00\r'); + }); + + pir.on('motionend', function(err, data) { + + test.ok(true, data); + test.done(); + + process.nextTick(function() { + process.exit(); + }); + }); + + board.serial.emit('data', '09::01\r'); + }); +}; diff --git a/test/sensor.js b/test/sensor.js new file mode 100644 index 0000000..aef4865 --- /dev/null +++ b/test/sensor.js @@ -0,0 +1,30 @@ +var arduino = require('../'); + + +// console.log( board ); +exports['sensor'] = function(test) { + test.expect(1); + + var board = new arduino.Board({ + debug: true + }); + + var sensor = new arduino.Sensor({ + board: board, + pin: 'A0' + }); + + sensor.on('read', function(err, data) { + + test.ok(!!data, "mock data"); + test.done(); + + process.nextTick(function() { + process.exit(); + }); + }); + + board.on('ready', function() { + board.serial.emit('data', 'A0::300\r'); + }); +}; From 646056a602a0650119e3747c077c25de71dc9566 Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Tue, 27 Mar 2012 13:51:12 -0400 Subject: [PATCH 13/14] WS nit Signed-off-by: Rick Waldron waldron.rick@gmail.com --- lib/board.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/board.js b/lib/board.js index 4073186..9f05d2d 100644 --- a/lib/board.js +++ b/lib/board.js @@ -71,7 +71,7 @@ util.inherits(Board, events.EventEmitter); Board.prototype.detect = function(callback) { this.log('info', 'attempting to find Arduino board'); var self = this; - child.exec('ls /dev | grep usb', function(err, stdout, stderr){ + child.exec('ls /dev | grep usb', function(err, stdout, stderr) { err = err || null; var usb = stdout.slice(0, -1).split('\n'), found = false, From fc1178e562537bd15016160bad81821579c1686d Mon Sep 17 00:00:00 2001 From: "Rick Waldron waldron.rick@gmail.com" Date: Tue, 27 Mar 2012 17:21:12 -0400 Subject: [PATCH 14/14] Additional TODO notes; block default grunt task, favor nodeunit file.x... Signed-off-by: Rick Waldron waldron.rick@gmail.com --- grunt.js | 9 ++++----- src/du.ino | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/grunt.js b/grunt.js index ff4feb7..79359ee 100644 --- a/grunt.js +++ b/grunt.js @@ -3,9 +3,9 @@ module.exports = function(grunt) { // Project configuration. grunt.initConfig({ pkg: '', - test: { - files: ['test/**/*.js'] - }, + // test: { + // files: ['test/**/*.js'] + // }, lint: { files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js'] }, @@ -34,6 +34,5 @@ module.exports = function(grunt) { }); // Default task. - grunt.registerTask('default', 'lint test'); - + grunt.registerTask('default', 'lint'); }; diff --git a/src/du.ino b/src/du.ino index cab5b7f..bf46749 100644 --- a/src/du.ino +++ b/src/du.ino @@ -35,6 +35,10 @@ void loop() { // If gps boolean flag has been set to true // via API call to gps.init(); + // + // TODO: Abstract the process of creating new + // SoftwareSerial objects to allow for + // arbitrary module connections if (gps) { while(GPS.available()) { char g = GPS.read();