diff --git a/docs/accelerometer-pan-tilt.md b/docs/accelerometer-pan-tilt.md index 69afe0d67..26af41e53 100644 --- a/docs/accelerometer-pan-tilt.md +++ b/docs/accelerometer-pan-tilt.md @@ -8,7 +8,7 @@ node eg/accelerometer-pan-tilt.js ```javascript var five = require("johnny-five"), - board; + board; board = new five.Board(); @@ -16,7 +16,7 @@ board.on("ready", function() { var range, pan, tilt, accel; - range = [ 0, 170 ]; + range = [0, 170]; // Servo to control panning pan = new five.Servo({ @@ -32,18 +32,18 @@ board.on("ready", function() { // Accelerometer to control pan/tilt accel = new five.Accelerometer({ - pins: [ "A3", "A4", "A5" ], + pins: ["A3", "A4", "A5"], freq: 250 }); // Center all servos (five.Servos()).center(); - accel.on("acceleration", function( err, timestamp ) { + accel.on("acceleration", function(err, timestamp) { // console.log( "acceleration", this.axis ); - tilt.move( Math.abs( Math.ceil(170 * this.pitch.toFixed(2)) - 180 ) ); - pan.move( Math.ceil(170 * this.roll.toFixed(2)) ); + tilt.move(Math.abs(Math.ceil(170 * this.pitch.toFixed(2)) - 180)); + pan.move(Math.ceil(170 * this.roll.toFixed(2))); // TODO: Math.abs(v - 180) as inversion function ? }); diff --git a/docs/accelerometer.md b/docs/accelerometer.md index f01828bde..4a91331ac 100644 --- a/docs/accelerometer.md +++ b/docs/accelerometer.md @@ -8,7 +8,7 @@ node eg/accelerometer.js ```javascript var five = require("johnny-five"), - board, accel; + board, accel; board = new five.Board(); @@ -25,7 +25,7 @@ board.on("ready", function() { // accel = new five.Accelerometer({ - pins: [ "A3", "A4", "A5" ], + pins: ["A3", "A4", "A5"], freq: 100, threshold: 0.2 }); @@ -37,18 +37,18 @@ board.on("ready", function() { // Fires once every N ms, equal to value of freg // Defaults to 500ms // - accel.on("acceleration", function( err, data ) { + accel.on("acceleration", function(err, data) { - console.log( "acceleration", data.smooth ); + console.log("acceleration", data.smooth); }); // "axischange" // // Fires only when X, Y or Z has changed // - accel.on("axischange", function( err, timestamp ) { + accel.on("axischange", function(err, timestamp) { - console.log( "axischange", this.raw ); + console.log("axischange", this.raw); }); }); diff --git a/docs/board-multi.md b/docs/board-multi.md index 69ebd4491..32e44bd67 100644 --- a/docs/board-multi.md +++ b/docs/board-multi.md @@ -10,7 +10,7 @@ node eg/board-multi.js var five = require("johnny-five"); // Create 2 board instances with IDs "A" & "B" -new five.Boards([ "A", "B" ]).on("ready", function() { +new five.Boards(["A", "B"]).on("ready", function() { // Both "A" and "B" are initialized // (connected and available for communication) @@ -21,7 +21,10 @@ new five.Boards([ "A", "B" ]).on("ready", function() { // Initialize an Led instance on pin 13 of // each initialized board and strobe it. - new five.Led({ pin: 13, board: board }).strobe(); + new five.Led({ + pin: 13, + board: board + }).strobe(); }); }); diff --git a/docs/board-with-port.md b/docs/board-with-port.md index efbac98e7..dd0e22836 100644 --- a/docs/board-with-port.md +++ b/docs/board-with-port.md @@ -8,12 +8,14 @@ node eg/board-with-port.js ```javascript var five = require("johnny-five"), - board; + board; // Johnny-Five will try its hardest to detect the port for you, // however you may also explicitly specify the port by passing // it as an optional property to the Board constructor: -board = new five.Board({ port: "/dev/cu.usbmodem411" }); +board = new five.Board({ + port: "/dev/cu.usbmodem411" +}); // The board's pins will not be accessible until // the board has reported that it is ready @@ -21,7 +23,7 @@ board.on("ready", function() { var val = 0; // Set pin 13 to OUTPUT mode - this.pinMode( 13, 1 ); + this.pinMode(13, 1); // Mode Table // INPUT: 0 @@ -31,8 +33,8 @@ board.on("ready", function() { // SERVO: 4 // Create a loop to "flash/blink/strobe" an led - this.loop( 50, function() { - this.digitalWrite( 13, (val = val ? 0 : 1) ); + this.loop(50, function() { + this.digitalWrite(13, (val = val ? 0 : 1)); }); }); diff --git a/docs/board.md b/docs/board.md index d83998814..1c1bc7423 100644 --- a/docs/board.md +++ b/docs/board.md @@ -15,11 +15,11 @@ five.Board().on("ready", function() { var val = 0; // Set pin 13 to OUTPUT mode - this.pinMode( 13, 1 ); + this.pinMode(13, 1); // Create a loop to "flash/blink/strobe" an led - this.loop( 100, function() { - this.digitalWrite( 13, (val = val ? 0 : 1) ); + this.loop(100, function() { + this.digitalWrite(13, (val = val ? 0 : 1)); }); }); diff --git a/docs/bug.md b/docs/bug.md index 8a6f70fe2..92c2cfcfd 100644 --- a/docs/bug.md +++ b/docs/bug.md @@ -9,13 +9,13 @@ node eg/bug.js ```javascript var five = require("johnny-five"); -function Bug( servos ) { +function Bug(servos) { var part, k; this.isMoving = false; // Initialize list of body parts - this.parts = [ "front", "left", "right", "rear" ]; + this.parts = ["front", "left", "right", "rear"]; // Servo positions this.history = []; @@ -28,10 +28,10 @@ function Bug( servos ) { k = -1; // Initialize Servo properties from "servos" argument - while ( ++k < this.parts.length ) { - part = this.parts[ k ]; + while (++k < this.parts.length) { + part = this.parts[k]; - this[ part ] = servos[ part ] || null; + this[part] = servos[part] || null; } // Unwind and wait ~10ms, center servos @@ -43,12 +43,12 @@ function Bug( servos ) { Bug.STEP_MAP = { // steps fwd: [ - [ 10, -30 ], - [ -15, 30 ] + [10, -30], + [-15, 30] ], rev: [ - [ -15, 30 ], - [ 15, -30 ] + [-15, 30], + [15, -30] ] }; @@ -60,13 +60,13 @@ Bug.prototype.idle = function() { // If the bug is actually in motion, // stop the servo intervals - if ( this.isMoving ) { + if (this.isMoving) { this.stop(); } // set to center position° - this.front.move( 90 ); - this.rear.move( 90 ); + this.front.move(90); + this.rear.move(90); //return this; }; @@ -79,7 +79,7 @@ Bug.prototype.idle = function() { * dir {String} * @return {Bug} */ -Bug.prototype.step = function( opts ) { +Bug.prototype.step = function(opts) { var dir, move, last, front, rear, step; opts = opts || {}; @@ -87,14 +87,15 @@ Bug.prototype.step = function( opts ) { // Get last move from history if any history exists // Provide a "fake" history if needed (first call) last = this.history.length ? - this.history[ this.history.length - 1 ] : - { step: 1 }; + this.history[this.history.length - 1] : { + step: 1 + }; // increment the last step step = last.step + 1; // If step is too high, step back to 0 - if ( step > 1 ) { + if (step > 1) { step = 0; } @@ -103,20 +104,20 @@ Bug.prototype.step = function( opts ) { dir = opts.dir || "fwd"; // Derive position° for next move - move = Bug.STEP_MAP[ dir ][ step ]; + move = Bug.STEP_MAP[dir][step]; // Assign position° from center front = 90 + move[0]; rear = 90 + move[1]; // Write position° to servos - this.front.move( front ); - this.rear.move( rear ); + this.front.move(front); + this.rear.move(rear); // Allow half step or full if provided, // defaults to full // enum(false|null|undefined) - if ( !opts.half ) { + if (!opts.half) { // Wait one second and move servos back to // center idling position, 90° setTimeout(function() { @@ -129,7 +130,12 @@ Bug.prototype.step = function( opts ) { this.history.push( // NOTE: this is a great use case example for // ES.next concise object initializers - { dir: dir, step: step, front: front, rear: rear } + { + dir: dir, + step: step, + front: front, + rear: rear + } ); }; @@ -138,9 +144,9 @@ Bug.prototype.step = function( opts ) { * @return {Bug} */ Bug.prototype.stop = function() { - Object.keys( this.intervals ).forEach(function( key ) { - if ( this.intervals[ key ] ) { - clearInterval( this.intervals[ key ] ); + Object.keys(this.intervals).forEach(function(key) { + if (this.intervals[key]) { + clearInterval(this.intervals[key]); } }, this); //return this; @@ -159,15 +165,18 @@ Bug.prototype.stop = function() { */ "rev" -].forEach(function( dir, k ) { +].forEach(function(dir, k) { - Bug.prototype[ dir ] = function() { + Bug.prototype[dir] = function() { this.isMoving = true; - this.intervals[ dir ] = setInterval(function() { - this.step({ dir: dir, half: true }); - }.bind(this), 750 ); + this.intervals[dir] = setInterval(function() { + this.step({ + dir: dir, + half: true + }); + }.bind(this), 750); // //return this; }; diff --git a/docs/button-bumper.md b/docs/button-bumper.md index a56f2a40c..aa4b1127e 100644 --- a/docs/button-bumper.md +++ b/docs/button-bumper.md @@ -8,7 +8,7 @@ node eg/button-bumper.js ```javascript var five = require("johnny-five"), - bumper, led; + bumper, led; five.Board().on("ready", function() { diff --git a/docs/button-options.md b/docs/button-options.md index 0c726cc69..91c6d2925 100644 --- a/docs/button-options.md +++ b/docs/button-options.md @@ -8,7 +8,7 @@ node eg/button-options.js ```javascript var five = require("johnny-five"), - board, button; + board, button; board = new five.Board(); diff --git a/docs/button-pullup.md b/docs/button-pullup.md index b67c48483..444292390 100644 --- a/docs/button-pullup.md +++ b/docs/button-pullup.md @@ -20,7 +20,7 @@ node eg/button-pullup.js // https://learn.sparkfun.com/tutorials/pull-up-resistors var five = require("../lib/johnny-five"), - button, led; + button, led; five.Board().on("ready", function() { @@ -31,11 +31,11 @@ five.Board().on("ready", function() { led = new five.Led(13); - button.on("down", function(value){ + button.on("down", function(value) { led.on(); }); - button.on("up", function(){ + button.on("up", function() { led.off(); }); diff --git a/docs/button.md b/docs/button.md index 71793875e..c8eb034da 100644 --- a/docs/button.md +++ b/docs/button.md @@ -8,7 +8,7 @@ node eg/button.js ```javascript var five = require("johnny-five"), - board, button; + board, button; board = new five.Board(); diff --git a/docs/classic-controller.md b/docs/classic-controller.md index 2602d6e26..0ffcfd42e 100644 --- a/docs/classic-controller.md +++ b/docs/classic-controller.md @@ -8,7 +8,7 @@ node eg/classic-controller.js ```javascript var five = require("johnny-five"), - board, nunchuk; + board, nunchuk; board = new five.Board(); @@ -47,18 +47,18 @@ board.on("ready", function() { // Fired when the joystick detects a change in // axis position. // - nunchuk.joystick.left.on( "change", function( err, event ) { + nunchuk.joystick.left.on("change", function(err, event) { console.log( "Left joystick " + event.axis, - event.target[ event.axis ], + event.target[event.axis], event.axis, event.direction ); }); - nunchuk.joystick.right.on( "change", function( err, event ) { + nunchuk.joystick.right.on("change", function(err, event) { console.log( "Right joystick " + event.axis, - event.target[ event.axis ], + event.target[event.axis], event.axis, event.direction ); }); @@ -85,13 +85,14 @@ board.on("ready", function() { // - [ "down", "up", "hold" ].forEach(function( type ) { + ["down", "up", "hold"].forEach(function(type) { - nunchuk.on( type, function( err, event ) { + nunchuk.on(type, function(err, event) { console.log( event.target.which + " is " + type, - { isUp: event.target.isUp, + { + isUp: event.target.isUp, isDown: event.target.isDown } ); @@ -100,9 +101,9 @@ board.on("ready", function() { }); -// Further reading -// http://media.pragprog.com/titles/msard/tinker.pdf -// http://lizarum.com/assignments/physical_computing/2008/wii_nunchuck.html + // Further reading + // http://media.pragprog.com/titles/msard/tinker.pdf + // http://lizarum.com/assignments/physical_computing/2008/wii_nunchuck.html }); ``` diff --git a/docs/claw.md b/docs/claw.md index 899a9cd36..66acc8d70 100644 --- a/docs/claw.md +++ b/docs/claw.md @@ -8,36 +8,40 @@ node eg/claw.js ```javascript var five = require("johnny-five"), - board; + board; board = new five.Board(); board.on("ready", function() { - var claw = new five.Servo({ pin: 9 }), - arm = five.Servo({ pin: 10 }), - degrees = 10, - incrementer = 10, - last; + var claw = new five.Servo({ + pin: 9 + }), + arm = five.Servo({ + pin: 10 + }), + degrees = 10, + incrementer = 10, + last; - this.loop( 25, function() { + this.loop(25, function() { - if ( degrees >= 180 || degrees === 0 ) { + if (degrees >= 180 || degrees === 0) { incrementer *= -1; } degrees += incrementer; - if ( degrees === 180 ) { - if ( !last || last === 90 ) { + if (degrees === 180) { + if (!last || last === 90) { last = 180; } else { last = 90; } - arm.move( last ); + arm.move(last); } - claw.move( degrees ); + claw.move(degrees); }); }); diff --git a/docs/continuous-clock.md b/docs/continuous-clock.md index 274451f10..97ffc050e 100644 --- a/docs/continuous-clock.md +++ b/docs/continuous-clock.md @@ -8,19 +8,19 @@ node eg/continuous-clock.js ```javascript var five = require("johnny-five"), - board, servo; + board, servo; board = new five.Board(); board.on("ready", function() { [ - [ 91, "ccw" ], - [ 89, "cw" ] + [91, "ccw"], + [89, "cw"] - ].forEach(function( def ) { - five.Servo.prototype[ def[1] ] = function() { - this.move( def[0] ); + ].forEach(function(def) { + five.Servo.prototype[def[1]] = function() { + this.move(def[0]); }; }); @@ -46,7 +46,7 @@ board.on("ready", function() { // move( speed ) // Set the speed at which the continuous rotation // servo will rotate at. - servo.move( 90 ); + servo.move(90); }); diff --git a/docs/continuous.md b/docs/continuous.md index f79977bd1..71d3771c3 100644 --- a/docs/continuous.md +++ b/docs/continuous.md @@ -8,7 +8,7 @@ node eg/continuous.js ```javascript var five = require("johnny-five"), - board, servo; + board, servo; board = new five.Board(); @@ -35,7 +35,7 @@ board.on("ready", function() { // move( speed ) // Set the speed at which the continuous rotation // servo will rotate at. - servo.move( 90 ); + servo.move(90); }); diff --git a/docs/ed.md b/docs/ed.md index cdca069fe..71e0a7458 100644 --- a/docs/ed.md +++ b/docs/ed.md @@ -8,10 +8,10 @@ node eg/ed.js ```javascript var five = require("johnny-five"), - compulsive = require("compulsive"); + compulsive = require("compulsive"); var ED, - priv = new WeakMap(); + priv = new WeakMap(); /** @@ -33,7 +33,8 @@ var ED, * * @param {Object} opts Optional properties object */ -function ED( opts ) { + +function ED(opts) { opts = opts || {}; @@ -64,12 +65,12 @@ function ED( opts ) { // TODO: Support pre-initialized servo instances this.servos = { right: { - hip: opts.right.hip && new five.Servo( opts.right.hip ), - foot: opts.right.foot && new five.Servo( opts.right.foot ) + hip: opts.right.hip && new five.Servo(opts.right.hip), + foot: opts.right.foot && new five.Servo(opts.right.foot) }, left: { - hip: opts.left.hip && new five.Servo( opts.left.hip ), - foot: opts.left.foot && new five.Servo( opts.left.foot ) + hip: opts.left.hip && new five.Servo(opts.left.hip), + foot: opts.left.foot && new five.Servo(opts.left.foot) } }; @@ -82,37 +83,37 @@ function ED( opts ) { // .left { .foot, .hip } // .right { .foot, .hip } // - [ "right", "left" ].forEach(function( key ) { + ["right", "left"].forEach(function(key) { var descriptor = {}; - [ "foot", "hip" ].forEach(function( part ) { - descriptor[ part ] = { + ["foot", "hip"].forEach(function(part) { + descriptor[part] = { get: function() { - var history = this.servos[ key ][ part ].history, - last = history[ history.length - 1 ]; + var history = this.servos[key][part].history, + last = history[history.length - 1]; return last && last.degrees || 90; }.bind(this) }; }, this); - this.degrees[ key ] = {}; + this.degrees[key] = {}; // And finally, create properties with the generated descriptor - Object.defineProperties( this.degrees[ key ], descriptor ); - }, this ); + Object.defineProperties(this.degrees[key], descriptor); + }, this); - Object.defineProperty( this, "isCentered", { + Object.defineProperty(this, "isCentered", { get: function() { var right, left; right = this.degrees.right; left = this.degrees.left; - if ( (right.foot === 90 && right.hip === 90) && - (left.foot === 90 && left.foot === 90) ) { + if ((right.foot === 90 && right.hip === 90) && + (left.foot === 90 && left.foot === 90)) { return true; } return false; @@ -124,12 +125,18 @@ function ED( opts ) { this.history = [{ timestamp: Date.now(), side: "right", - right: { hip: 0, foot: 0 }, - left: { hip: 0, foot: 0 } + right: { + hip: 0, + foot: 0 + }, + left: { + hip: 0, + foot: 0 + } }]; // Create an entry in the private data store. - priv.set( this, { + priv.set(this, { // `isWalking` is used in: // ED.prototype.(attn|stop) // ED.prototype.(forward|fwd;reverse|rev) @@ -145,17 +152,17 @@ function ED( opts ) { * @return {Object} this */ //ED.prototype.attn = ED.prototype.stop = function() { -ED.prototype.attn = function( options ) { +ED.prototype.attn = function(options) { options = options || {}; - if ( !options.isWalking ) { + if (!options.isWalking) { - if ( this.sequence ) { + if (this.sequence) { this.sequence.stop(); this.sequence = null; } - priv.set( this, { + priv.set(this, { isWalking: false }); } @@ -180,21 +187,20 @@ ED.prototype.attn = function( options ) { * one of: (fwd, rev, left, right) * */ -ED.prototype.step = function( direct ) { +ED.prototype.step = function(direct) { var isLeft, isFwd, opposing, direction, state; state = priv.get(this); - if ( /fwd|rev/.test(direct) ) { + if (/fwd|rev/.test(direct)) { direction = direct; direct = undefined; - } - else { + } else { direction = "fwd"; } // Derive which side to step on; based on last step or explicit step - this.side = direct || ( this.side !== "right" ? "right" : "left" ); + this.side = direct || (this.side !== "right" ? "right" : "left"); // Update the value of the current direction this.direction = direction; @@ -227,15 +233,15 @@ ED.prototype.step = function( direct ) { // Lift the currently stepping foot, while // leaning on the currently opposing foot. - instruct[ stepping ] = { + instruct[stepping] = { foot: isLeft ? 40 : 140 }; - instruct[ opposing ] = { + instruct[opposing] = { foot: isLeft ? 70 : 110 }; // Swing currently stepping hips - this.move( instruct ); + this.move(instruct); }.bind(this) }, @@ -244,8 +250,8 @@ ED.prototype.step = function( direct ) { wait: 500, task: function() { var degrees = isLeft ? - ( isFwd ? 120 : 60 ) : - ( isFwd ? 60 : 120 ); + (isFwd ? 120 : 60) : + (isFwd ? 60 : 120); // Swing currently stepping hips this.move({ @@ -298,9 +304,9 @@ ED.prototype.step = function( direct ) { abbr: "rev" } -].forEach(function( dir ) { +].forEach(function(dir) { - ED.prototype[ dir.name ] = ED.prototype[ dir.abbr ] = function() { + ED.prototype[dir.name] = ED.prototype[dir.abbr] = function() { var startAt, stepper, state; startAt = 10; @@ -308,13 +314,13 @@ ED.prototype.step = function( direct ) { // If ED is already walking in this direction, return immediately; // This prevents multiple movement loops from being scheduled. - if ( this.direction === dir.abbr && state.isWalking ) { + if (this.direction === dir.abbr && state.isWalking) { return; } // If a sequence reference exists, kill it. This will // clear all pending queue repeaters. - if ( this.sequence ) { + if (this.sequence) { this.sequence.stop(); this.sequence = null; } @@ -334,39 +340,38 @@ ED.prototype.step = function( direct ) { isWalking: true }); - stepper = function( loop ) { + stepper = function(loop) { // Capture of sequence queue reference - if ( this.sequence === null ) { + if (this.sequence === null) { this.sequence = loop; } - this.step( dir.abbr ); + this.step(dir.abbr); - if ( !priv.get(this).isWalking ) { + if (!priv.get(this).isWalking) { loop.stop(); } }.bind(this); // If the bot is not centered, ie. all servos at 90degrees, // bring the bot to attention before proceeding. - if ( !this.isCentered ) { - this.attn({ isWalking: true }); + if (!this.isCentered) { + this.attn({ + isWalking: true + }); // Offset the amount ms required for attn() to complete startAt = 750; } - this.queue([ - { - wait: startAt, - task: function() { - this.step( dir.abbr ); - }.bind(this) - }, - { - loop: 1500, - task: stepper - } - ]); + this.queue([{ + wait: startAt, + task: function() { + this.step(dir.abbr); + }.bind(this) + }, { + loop: 1500, + task: stepper + }]); }; }); @@ -382,7 +387,7 @@ ED.prototype.dance = function() { this.attn(); - if ( typeof this.moves === "undefined" ) { + if (typeof this.moves === "undefined") { this.moves = 0; } @@ -393,7 +398,7 @@ ED.prototype.dance = function() { task: function() { var degrees = isLeft ? 120 : 60; - if ( this.moves % 2 === 0 ) { + if (this.moves % 2 === 0) { this.move({ type: "attn", right: { @@ -491,77 +496,79 @@ ED.prototype.dance = function() { * @param {Object} positions left/right hip/foot positions * */ -ED.prototype.move = function( positions ) { +ED.prototype.move = function(positions) { var start, type; - if ( this.history.length ) { - start = this.history[ this.history.length - 1 ]; + if (this.history.length) { + start = this.history[this.history.length - 1]; } type = positions.type || "step"; - [ "foot", "hip" ].forEach(function( section ) { - [ "right", "left" ].forEach(function( side ) { + ["foot", "hip"].forEach(function(section) { + ["right", "left"].forEach(function(side) { var interval, endAt, startAt, servo, step, s; - if ( typeof positions[ side ] === "undefined" ) { + if (typeof positions[side] === "undefined") { return; } - endAt = positions[ side ][ section ]; - servo = this.servos[ side ][ section ]; - startAt = this.degrees[ side ][ section ]; + endAt = positions[side][section]; + servo = this.servos[side][section]; + startAt = this.degrees[side][section]; // Degrees per step step = 2; s = Date.now(); - if ( !endAt || endAt === startAt ) { + if (!endAt || endAt === startAt) { return; } - if ( start ) { + if (start) { // Determine degree step direction - if ( endAt < startAt ) { + if (endAt < startAt) { step *= -1; } // Repeat each step for required number of steps to move // servo into new position. Each step is ~20ms duration - this.repeat( Math.abs( endAt - startAt ) / 2, 10, function() { + this.repeat(Math.abs(endAt - startAt) / 2, 10, function() { // console.log( startAt ); - servo.move( startAt += step ); + servo.move(startAt += step); - if ( startAt === endAt ) { - this.times[ type ] = (this.times[ type ] + (Date.now() - s)) / 2; + if (startAt === endAt) { + this.times[type] = (this.times[type] + (Date.now() - s)) / 2; } }.bind(this)); } else { // TODO: Stop doing this - servo.move( endAt ); + servo.move(endAt); five.Fn.sleep(500); } - }, this ); - }, this ); + }, this); + }, this); // Push a record object into the stepping history this.history.push({ timestamp: Date.now(), side: this.side, - right: five.Fn.extend( - { hip: 0, foot: 0 }, this.degrees.right, positions.right - ), - left: five.Fn.extend( - { hip: 0, foot: 0 }, this.degrees.left, positions.left - ) + right: five.Fn.extend({ + hip: 0, + foot: 0 + }, this.degrees.right, positions.right), + left: five.Fn.extend({ + hip: 0, + foot: 0 + }, this.degrees.left, positions.left) }); }; // Borrow API from Compulsive -[ "wait", "loop", "queue", "repeat" ].forEach(function( api ) { - ED.prototype[ api ] = compulsive[ api ]; +["wait", "loop", "queue", "repeat"].forEach(function(api) { + ED.prototype[api] = compulsive[api]; }); // Begin program when the board, serial and @@ -573,10 +580,12 @@ ED.prototype.move = function( positions ) { // assign servos biped = new ED({ right: { - hip: 9, foot: 11 + hip: 9, + foot: 11 }, left: { - hip: 10, foot: 12 + hip: 10, + foot: 12 } }); diff --git a/docs/gripper.md b/docs/gripper.md index 698044cae..d39d874f5 100644 --- a/docs/gripper.md +++ b/docs/gripper.md @@ -8,74 +8,75 @@ node eg/gripper.js ```javascript var five = require("johnny-five"), - compulsive = require("compulsive"), - wrist, gripper, motion, repeater; + compulsive = require("compulsive"), + wrist, gripper, motion, repeater; (new five.Board()).on("ready", function() { - // Create a new `gripper` hardware instance. - // This example allows the gripper module to - // create a completely default instance + // Create a new `gripper` hardware instance. + // This example allows the gripper module to + // create a completely default instance - wrist = new five.Servo(9); - gripper = new five.Gripper(10); + wrist = new five.Servo(9); + gripper = new five.Gripper(10); - function slice() { - wrist.move(100); + function slice() { + wrist.move(100); - compulsive.wait(100, function() { - wrist.move(120); - }); - } - function chop() { - compulsive.loop(200, function( loop ) { - if ( !repeater ) { - repeater = loop; - } - slice(); + compulsive.wait(100, function() { + wrist.move(120); + }); + } + + function chop() { + compulsive.loop(200, function(loop) { + if (!repeater) { + repeater = loop; + } + slice(); + }); + } + + // Inject the `gripper` hardware into + // the Repl instance's context; + // allows direct command line access + this.repl.inject({ + w: wrist, + g: gripper, + chop: chop, + slice: slice }); - } - - // Inject the `gripper` hardware into - // the Repl instance's context; - // allows direct command line access - this.repl.inject({ - w: wrist, - g: gripper, - chop: chop, - slice: slice - }); - motion = new five.IR.Motion(7); - - // gripper.open() - // - // gripper.close() - // - // gripper.set([0-10]) - // - // - // g.*() from REPL - // - // - motion.on("motionstart", function( err, ts ) { - chop(); - }); + motion = new five.IR.Motion(7); + + // gripper.open() + // + // gripper.close() + // + // gripper.set([0-10]) + // + // + // g.*() from REPL + // + // + motion.on("motionstart", function(err, ts) { + chop(); + }); - // "motionstart" events are fired following a "motionstart event - // when no movement has occurred in X ms - motion.on("motionend", function( err, ts ) { - if ( repeater ) { - repeater.stop(); - repeater = null; - } - }); + // "motionstart" events are fired following a "motionstart event + // when no movement has occurred in X ms + motion.on("motionend", function(err, ts) { + if (repeater) { + repeater.stop(); + repeater = null; + } + }); -}); + }); ``` diff --git a/docs/ir-motion.md b/docs/ir-motion.md index afb7d8d6b..bbbc4b30e 100644 --- a/docs/ir-motion.md +++ b/docs/ir-motion.md @@ -8,7 +8,7 @@ node eg/ir-motion.js ```javascript var five = require("johnny-five"), - board, motion; + board, motion; board = new five.Board(); @@ -27,20 +27,20 @@ board.on("ready", function() { // Pir Event API // "calibrated" occurs once, at the beginning of a session, - motion.on("calibrated", function( err, ts ) { - console.log( "calibrated", ts ); + motion.on("calibrated", function(err, ts) { + console.log("calibrated", ts); }); // "motionstart" events are fired when the "calibrated" // proximal area is disrupted, generally by some form of movement - motion.on("motionstart", function( err, ts ) { - console.log( "motionstart", ts ); + motion.on("motionstart", function(err, ts) { + console.log("motionstart", ts); }); // "motionstart" events are fired following a "motionstart event // when no movement has occurred in X ms - motion.on("motionend", function( err, ts ) { - console.log( "motionend", ts ); + motion.on("motionend", function(err, ts) { + console.log("motionend", ts); }); }); diff --git a/docs/ir-proximity.md b/docs/ir-proximity.md index d4de4f94e..ed34096b4 100644 --- a/docs/ir-proximity.md +++ b/docs/ir-proximity.md @@ -34,7 +34,7 @@ five.Board().on("ready", function() { ir.on("motionstart", function() { - console.log( "motionstart" ); + console.log("motionstart"); }); @@ -45,7 +45,7 @@ five.Board().on("ready", function() { ir.on("motionend", function() { - console.log( "motionend" ); + console.log("motionend"); }); @@ -53,7 +53,7 @@ five.Board().on("ready", function() { // // Fires continuously, every 66ms. // - ir.on("data", function( err, timestamp ) { + ir.on("data", function(err, timestamp) { // console.log( "data" ); }); }); diff --git a/docs/ir-reflect.md b/docs/ir-reflect.md index 007cbb347..8e85a47b7 100644 --- a/docs/ir-reflect.md +++ b/docs/ir-reflect.md @@ -28,8 +28,8 @@ five.Board().on("ready", function() { // // Fires continuously, every 66ms. // - ir.on("data", function( err, timestamp ) { - console.log( "data" ); + ir.on("data", function(err, timestamp) { + console.log("data"); }); }); diff --git a/docs/joystick-claw.md b/docs/joystick-claw.md index 4bd39726a..f30342315 100644 --- a/docs/joystick-claw.md +++ b/docs/joystick-claw.md @@ -8,30 +8,30 @@ node eg/joystick-claw.js ```javascript var five = require("johnny-five"), - board, claw, joystick; + board, claw, joystick; board = new five.Board(); board.on("ready", function() { var claw = new five.Servo({ - pin: 9, - range: [ 0, 170 ] - }), - joystick = new five.Joystick({ - pins: [ "A0", "A1" ], - freq: 250 - }); + pin: 9, + range: [0, 170] + }), + joystick = new five.Joystick({ + pins: ["A0", "A1"], + freq: 250 + }); // Set the claw degrees to half way // (the joystick deadzone) - claw.move( 90 ); + claw.move(90); joystick.on("axismove", function() { // Open/close the claw by setting degrees according // to Y position of joystick. // limit to 170 on medium servos (ei. the servo used on the claw) - claw.move( Math.ceil(170 * this.fixed.y) ); + claw.move(Math.ceil(170 * this.fixed.y)); }); }); diff --git a/docs/joystick-laser.md b/docs/joystick-laser.md index 3d9578427..2e8cd1189 100644 --- a/docs/joystick-laser.md +++ b/docs/joystick-laser.md @@ -8,14 +8,14 @@ node eg/joystick-laser.js ```javascript var five = require("johnny-five"), - board = new five.Board({ - debug: true - }); + board = new five.Board({ + debug: true + }); board.on("ready", function() { var range, pan, tilt, joystick; - range = [ 0, 170 ]; + range = [0, 170]; // Servo to control panning pan = new five.Servo({ @@ -33,7 +33,7 @@ board.on("ready", function() { // Read Analog 0, 1 // Limit events to every 50ms joystick = new five.Joystick({ - pins: [ "A0", "A1" ], + pins: ["A0", "A1"], freq: 100 }); @@ -42,8 +42,8 @@ board.on("ready", function() { joystick.on("axismove", function() { - tilt.move( Math.ceil(170 * this.fixed.y) ); - pan.move( Math.ceil(170 * this.fixed.x) ); + tilt.move(Math.ceil(170 * this.fixed.y)); + pan.move(Math.ceil(170 * this.fixed.x)); }); }); diff --git a/docs/joystick-motor-led.md b/docs/joystick-motor-led.md index 519417dc8..a173e6dad 100644 --- a/docs/joystick-motor-led.md +++ b/docs/joystick-motor-led.md @@ -8,7 +8,7 @@ node eg/joystick-motor-led.js ```javascript var five = require("johnny-five"), - board, joystick, motor, led; + board, joystick, motor, led; board = new five.Board(); @@ -20,7 +20,7 @@ board.on("ready", function() { // Pin orders: // [ up, down, left, right ] // [ ud, lr ] - pins: [ "A0", "A1" ], + pins: ["A0", "A1"], freq: 25 }); @@ -46,13 +46,13 @@ board.on("ready", function() { // Pushing the joystick to up position should start the motor, // releasing it will turn the motor off. - joystick.on("axismove", function( err, timestamp ) { + joystick.on("axismove", function(err, timestamp) { - if ( !motor.isOn && this.axis.y > 0.51 ) { + if (!motor.isOn && this.axis.y > 0.51) { motor.start(); } - if ( motor.isOn && this.axis.y < 0.51 ) { + if (motor.isOn && this.axis.y < 0.51) { motor.stop(); } }); @@ -60,7 +60,7 @@ board.on("ready", function() { // While the motor is on, blink the led motor.on("start", function() { // 250ms - led.strobe( 250 ); + led.strobe(250); }); motor.on("stop", function() { diff --git a/docs/joystick.md b/docs/joystick.md index bdb915683..6e711eaf8 100644 --- a/docs/joystick.md +++ b/docs/joystick.md @@ -8,7 +8,7 @@ node eg/joystick.js ```javascript var five = require("johnny-five"), - board, joystick; + board, joystick; board = new five.Board(); @@ -20,7 +20,7 @@ board.on("ready", function() { // Pin orders: // [ up, down, left, right ] // [ ud, lr ] - pins: [ "A0", "A1" ], + pins: ["A0", "A1"], freq: 500 }); @@ -33,7 +33,7 @@ board.on("ready", function() { // Joystick Event API - joystick.on("axismove", function( err, timestamp ) { + joystick.on("axismove", function(err, timestamp) { // Axis data is available on: // this.axis @@ -49,9 +49,9 @@ board.on("ready", function() { // console.log( "UD:", this.axis.y, this.normalized.y ); // console.log( "MAG:", this.magnitude ); - console.log( "LR:", this.fixed.x ); - console.log( "UD:", this.fixed.y ); - console.log( "MAG:", this.magnitude ); + console.log("LR:", this.fixed.x); + console.log("UD:", this.fixed.y); + console.log("MAG:", this.magnitude); }); }); diff --git a/docs/laser.md b/docs/laser.md index 308e08a96..0f0b24058 100644 --- a/docs/laser.md +++ b/docs/laser.md @@ -8,7 +8,7 @@ node eg/laser.js ```javascript var five = require("johnny-five"), - board, laser; + board, laser; board = new five.Board(); diff --git a/docs/lcd-enumeratechars.md b/docs/lcd-enumeratechars.md index 3fc8b1533..7c0a56895 100644 --- a/docs/lcd-enumeratechars.md +++ b/docs/lcd-enumeratechars.md @@ -8,7 +8,7 @@ node eg/lcd-enumeratechars.js ```javascript var five = require("../lib/johnny-five"), - board, lcd; + board, lcd; board = new five.Board(); @@ -17,7 +17,7 @@ board.on("ready", function() { lcd = new five.LCD({ // LCD pin name RS EN DB4 DB5 DB6 DB7 // Arduino pin # 7 8 9 10 11 12 - pins: [ 7, 8, 9, 10, 11, 12 ], + pins: [7, 8, 9, 10, 11, 12], rows: 4, cols: 20 }); @@ -32,27 +32,27 @@ board.on("ready", function() { length = keys.length; eights = []; - while ( i < length ) { - eights.push( keys.slice(i, i + 8) ); + while (i < length) { + eights.push(keys.slice(i, i + 8)); i += 8; } - console.log( "Wait 5 seconds..." ); + console.log("Wait 5 seconds..."); - board.loop( 2000, function() { - var charset = eights[ k ], - display = ""; + board.loop(2000, function() { + var charset = eights[k], + display = ""; lcd.clear(); - if ( k < eights.length ) { + if (k < eights.length) { - charset.forEach(function( char, index ) { + charset.forEach(function(char, index) { lcd.useChar(char); display += ":" + char + ":"; }); - lcd.clear().cursor(0, 0).print( display ); + lcd.clear().cursor(0, 0).print(display); k++; } diff --git a/docs/lcd-runner-20x4.md b/docs/lcd-runner-20x4.md index eb3f1dce4..8b74cfbd3 100644 --- a/docs/lcd-runner-20x4.md +++ b/docs/lcd-runner-20x4.md @@ -8,7 +8,7 @@ node eg/lcd-runner-20x4.js ```javascript var five = require("../lib/johnny-five"), - board, lcd; + board, lcd; board = new five.Board(); @@ -17,28 +17,30 @@ board.on("ready", function() { lcd = new five.LCD({ // LCD pin name RS EN DB4 DB5 DB6 DB7 // Arduino pin # 7 8 9 10 11 12 - pins: [ 7, 8, 9, 10, 11, 12 ], + pins: [7, 8, 9, 10, 11, 12], rows: 4, cols: 20 }); lcd.on("ready", function() { - var frame = 1, col = 0, row = 0; + var frame = 1, + col = 0, + row = 0; lcd.useChar("runninga"); lcd.useChar("runningb"); - board.loop( 300, function() { + board.loop(300, function() { - lcd.clear().cursor( row, col ).print( + lcd.clear().cursor(row, col).print( ":running" + (++frame % 2 === 0 ? "a" : "b") + ":" ); - if ( ++col === lcd.cols ) { + if (++col === lcd.cols) { col = 0; - if ( ++row === lcd.rows ) { + if (++row === lcd.rows) { row = 0; } } diff --git a/docs/lcd-runner.md b/docs/lcd-runner.md index 6ccf03670..86afef3ba 100644 --- a/docs/lcd-runner.md +++ b/docs/lcd-runner.md @@ -8,7 +8,7 @@ node eg/lcd-runner.js ```javascript var five = require("../lib/johnny-five"), - board, lcd; + board, lcd; board = new five.Board(); @@ -17,28 +17,30 @@ board.on("ready", function() { lcd = new five.LCD({ // LCD pin name RS EN DB4 DB5 DB6 DB7 // Arduino pin # 7 8 9 10 11 12 - pins: [ 7, 8, 9, 10, 11, 12 ], + pins: [7, 8, 9, 10, 11, 12], rows: 2, cols: 16 }); lcd.on("ready", function() { - var frame = 1, col = 0, row = 0; + var frame = 1, + col = 0, + row = 0; lcd.useChar("runninga"); lcd.useChar("runningb"); - board.loop( 300, function() { + board.loop(300, function() { - lcd.clear().cursor( row, col ).print( + lcd.clear().cursor(row, col).print( ":running" + (++frame % 2 === 0 ? "a" : "b") + ":" ); - if ( ++col === lcd.cols ) { + if (++col === lcd.cols) { col = 0; - if ( ++row === lcd.rows ) { + if (++row === lcd.rows) { row = 0; } } diff --git a/docs/lcd.md b/docs/lcd.md index 5cf76dcb0..2cdf4033c 100644 --- a/docs/lcd.md +++ b/docs/lcd.md @@ -8,7 +8,7 @@ node eg/lcd.js ```javascript var five = require("../lib/johnny-five"), - board, lcd; + board, lcd; board = new five.Board(); @@ -17,7 +17,7 @@ board.on("ready", function() { lcd = new five.LCD({ // LCD pin name RS EN DB4 DB5 DB6 DB7 // Arduino pin # 7 8 9 10 11 12 - pins: [ 7, 8, 9, 10, 11, 12 ], + pins: [7, 8, 9, 10, 11, 12], // Options: // bitMode: 4 or 8, defaults to 4 diff --git a/docs/led-fade.md b/docs/led-fade.md index 24f63c613..b485092c1 100644 --- a/docs/led-fade.md +++ b/docs/led-fade.md @@ -8,7 +8,7 @@ node eg/led-fade.js ```javascript var five = require("johnny-five"), - board, led; + board, led; board = new five.Board(); @@ -37,7 +37,7 @@ board.on("ready", function() { // Toggle the led after 10 seconds (shown in ms) - this.wait( 5000, function() { + this.wait(5000, function() { led.fadeOut(); diff --git a/docs/led-matrix.md b/docs/led-matrix.md index a671fa613..84d1f8d3a 100644 --- a/docs/led-matrix.md +++ b/docs/led-matrix.md @@ -8,7 +8,7 @@ node eg/led-matrix.js ```javascript var five = require("../lib/johnny-five"), - board, lc; + board, lc; board = new five.Board(); @@ -38,26 +38,30 @@ board.on("ready", function() { }); function queue(fn) { - process.nextTick( fn ); + process.nextTick(fn); } lc.heart = function() { heart.forEach(function(row, rowIndex) { - queue( function() { lc.row( 0, rowIndex, parseInt( row, 2 ) ); } ); + queue(function() { + lc.row(0, rowIndex, parseInt(row, 2)); + }); }); }; - lc.on( 0 ); + lc.on(0); var msg = "johnny-five"; var idx = 0; function next() { - var c = msg[ idx ]; - lc.char( 0, c ); + var c = msg[idx]; + lc.char(0, c); idx++; - if ( idx === msg.length ) { return; } - setTimeout( next, 800 ); + if (idx === msg.length) { + return; + } + setTimeout(next, 800); } next(); diff --git a/docs/led-on-off.md b/docs/led-on-off.md index 338906488..35e9081c7 100644 --- a/docs/led-on-off.md +++ b/docs/led-on-off.md @@ -8,7 +8,7 @@ node eg/led-on-off.js ```javascript var five = require("johnny-five"), - board, led; + board, led; board = new five.Board(); @@ -26,7 +26,7 @@ board.on("ready", function() { led.off(); // Turn the led back on after 3 seconds (shown in ms) - this.wait( 3000, function() { + this.wait(3000, function() { led.on(); diff --git a/docs/led-pulse.md b/docs/led-pulse.md index e8ca967f8..914f98d5c 100644 --- a/docs/led-pulse.md +++ b/docs/led-pulse.md @@ -8,7 +8,7 @@ node eg/led-pulse.js ```javascript var five = require("johnny-five"), - board, led; + board, led; board = new five.Board(); @@ -36,7 +36,7 @@ board.on("ready", function() { // Turn off the led pulse loop after 10 seconds (shown in ms) - this.wait( 10000, function() { + this.wait(10000, function() { led.stop().off(); diff --git a/docs/led-rainbow.md b/docs/led-rainbow.md index 77b8c712a..7eafc1455 100644 --- a/docs/led-rainbow.md +++ b/docs/led-rainbow.md @@ -12,15 +12,15 @@ var five = require("johnny-five"); five.Board().on("ready", function() { var rgb, rainbow, index; - rgb = new five.Led.RGB([ 3, 5, 6 ]); - rainbow = [ "FF000", "FF7F00", "00FF00", "FFFF00", "0000FF", "4B0082", "8F00FF" ]; + rgb = new five.Led.RGB([3, 5, 6]); + rainbow = ["FF000", "FF7F00", "00FF00", "FFFF00", "0000FF", "4B0082", "8F00FF"]; index = 0; setInterval(function() { - if ( index + 1 === rainbow.length ) { + if (index + 1 === rainbow.length) { index = 0; } - rgb.color( rainbow[ index++ ] ); + rgb.color(rainbow[index++]); }, 500); }); diff --git a/docs/led-rgb.md b/docs/led-rgb.md index 266cd9921..8b3769d05 100644 --- a/docs/led-rgb.md +++ b/docs/led-rgb.md @@ -10,7 +10,7 @@ node eg/led-rgb.js var five = require("johnny-five"); five.Board().on("ready", function() { - var a = new five.Led.RGB([ 9, 10, 11 ]); + var a = new five.Led.RGB([9, 10, 11]); var b = new five.Led.RGB({ pins: { diff --git a/docs/led-strobe.md b/docs/led-strobe.md index 8e3a162ce..bbdd29363 100644 --- a/docs/led-strobe.md +++ b/docs/led-strobe.md @@ -8,7 +8,7 @@ node eg/led-strobe.js ```javascript var five = require("johnny-five"), - board, led; + board, led; board = new five.Board(); @@ -18,7 +18,7 @@ board.on("ready", function() { led = new five.Led(13); // "strobe" the led in 100ms on-off phases - led.strobe( 100 ); + led.strobe(100); }); ``` diff --git a/docs/magnetometer-log.md b/docs/magnetometer-log.md index c51c764de..ca5926dbc 100644 --- a/docs/magnetometer-log.md +++ b/docs/magnetometer-log.md @@ -8,84 +8,84 @@ node eg/magnetometer-log.js ```javascript var color = require("colors"), - five = require("johnny-five"), - board, colors, servo, mag, count, dirs, lock; + five = require("johnny-five"), + board, colors, servo, mag, count, dirs, lock; (board = new five.Board()).on("ready", function() { - count = -1; - dirs = [ "cw", "ccw" ]; - lock = false; + count = -1; + dirs = ["cw", "ccw"]; + lock = false; - [ - // Medium Speed Counter Clock Wise - [ 92, "ccw" ], - // Medium Speed Clock Wise - [ 88, "cw" ] + [ + // Medium Speed Counter Clock Wise + [92, "ccw"], + // Medium Speed Clock Wise + [88, "cw"] - ].forEach(function( def ) { + ].forEach(function(def) { - // Define a directional method and default speed - five.Servo.prototype[ def[1] ] = function( speed ) { - speed = speed || def[0]; + // Define a directional method and default speed + five.Servo.prototype[def[1]] = function(speed) { + speed = speed || def[0]; - this.move( speed ); - }; - }); + this.move(speed); + }; + }); - // Create a new `servo` hardware instance. - servo = new five.Servo({ - pin: 9, - // `type` defaults to standard servo. - // For continuous rotation servos, override the default - // by setting the `type` here - type: "continuous" - }); + // Create a new `servo` hardware instance. + servo = new five.Servo({ + pin: 9, + // `type` defaults to standard servo. + // For continuous rotation servos, override the default + // by setting the `type` here + type: "continuous" + }); - // Create an I2C `Magnetometer` instance - mag = new five.Magnetometer(); + // Create an I2C `Magnetometer` instance + mag = new five.Magnetometer(); - // Inject the servo and magnometer into the REPL - this.repl.inject({ - servo: servo, - mag: mag - }); + // Inject the servo and magnometer into the REPL + this.repl.inject({ + servo: servo, + mag: mag + }); - // set the continuous servo to stopped - servo.move( 90 ); + // set the continuous servo to stopped + servo.move(90); - // As the heading changes, log heading value - mag.on("headingchange", function() { - var log; + // As the heading changes, log heading value + mag.on("headingchange", function() { + var log; - log = ( this.bearing.name + " " + Math.floor(this.heading) + "°" ); + log = (this.bearing.name + " " + Math.floor(this.heading) + "°"); - console.log( - log[ colors[ this.bearing.abbr ] ] - ); + console.log( + log[colors[this.bearing.abbr]] + ); - if ( !lock && this.bearing.name === "North" ) { - // Set redirection lock - lock = true; + if (!lock && this.bearing.name === "North") { + // Set redirection lock + lock = true; - // Redirect - servo[ dirs[ ++count % 2 ] ](); + // Redirect + servo[dirs[++count % 2]](); - // Release redirection lock - board.wait( 2000, function() { - lock = false; - }); - } - }); + // Release redirection lock + board.wait(2000, function() { + lock = false; + }); + } + }); - this.wait( 2000, function() { - servo[ dirs[ ++count % 2 ] ](); + this.wait(2000, function() { + servo[dirs[++count % 2]](); + }); }); -}); colors = { N: "red", diff --git a/docs/magnetometer-north.md b/docs/magnetometer-north.md index 639761238..151a75989 100644 --- a/docs/magnetometer-north.md +++ b/docs/magnetometer-north.md @@ -8,25 +8,25 @@ node eg/magnetometer-north.js ```javascript var color = require("colors"), - five = require("johnny-five"), - board, colors, servo, mag, count, dirs, isNorth, isSeeking, last; + five = require("johnny-five"), + board, colors, servo, mag, count, dirs, isNorth, isSeeking, last; board = new five.Board(); board.on("ready", function() { count = -1; - dirs = [ "cw", "ccw" ]; + dirs = ["cw", "ccw"]; isNorth = false; isSeeking = false; [ - [ 95, "ccw" ], - [ 85, "cw" ] + [95, "ccw"], + [85, "cw"] - ].forEach(function( def ) { - five.Servo.prototype[ def[1] ] = function() { - this.move( def[0] ); + ].forEach(function(def) { + five.Servo.prototype[def[1]] = function() { + this.move(def[0]); }; }); @@ -50,16 +50,16 @@ board.on("ready", function() { }); // set the continuous servo to stopped - servo.move( 90 ); + servo.move(90); // As the heading changes, log heading value mag.on("data", function() { - var heading = Math.floor( this.heading ); + var heading = Math.floor(this.heading); - if ( heading > 345 || heading < 15 ) { + if (heading > 345 || heading < 15) { - if ( !isNorth ) { - console.log( "FOUND north!".yellow ); + if (!isNorth) { + console.log("FOUND north!".yellow); isSeeking = false; } @@ -69,13 +69,13 @@ board.on("ready", function() { } else { isNorth = false; - if ( !isSeeking ) { - console.log( "find north!".red, heading ); + if (!isSeeking) { + console.log("find north!".red, heading); isSeeking = true; } - if ( heading < 270 ) { + if (heading < 270) { servo.ccw(); } else { servo.cw(); diff --git a/docs/magnetometer.md b/docs/magnetometer.md index 9e5e8fb9c..1d56514d1 100644 --- a/docs/magnetometer.md +++ b/docs/magnetometer.md @@ -8,7 +8,7 @@ node eg/magnetometer.js ```javascript var five = require("johnny-five"), - mag; + mag; five.Board().on("ready", function() { @@ -61,8 +61,8 @@ five.Board().on("ready", function() { // mag.on("headingchange", function() { - console.log( "heading", Math.floor(this.heading) ); - console.log( "bearing", this.bearing ); + console.log("heading", Math.floor(this.heading)); + console.log("bearing", this.bearing); }); @@ -70,7 +70,7 @@ five.Board().on("ready", function() { // // Fires continuously, every 66ms. // - mag.on("read", function( err, timestamp ) { + mag.on("read", function(err, timestamp) { // console.log( "read", this.axis ); }); }); diff --git a/docs/motobot.md b/docs/motobot.md index 8dbf78074..13e7e315b 100644 --- a/docs/motobot.md +++ b/docs/motobot.md @@ -8,8 +8,8 @@ node eg/motobot.js ```javascript var five = require("johnny-five"), - keypress = require("keypress"), - board = new five.Board(); + keypress = require("keypress"), + board = new five.Board(); board.on("ready", function() { @@ -18,41 +18,41 @@ board.on("ready", function() { speed = 100; commands = null; motors = { - a: new five.Motor([ 3, 12 ]), - b: new five.Motor([ 11, 13 ]) + a: new five.Motor([3, 12]), + b: new five.Motor([11, 13]) }; this.repl.inject({ motors: motors }); - function controller( ch, key ) { - if ( key ) { - if ( key.name === "space" ) { + function controller(ch, key) { + if (key) { + if (key.name === "space") { motors.a.stop(); motors.b.stop(); } - if ( key.name === "up" ) { + if (key.name === "up") { motors.a.rev(speed); motors.b.fwd(speed); } - if ( key.name === "down" ) { + if (key.name === "down") { motors.a.fwd(speed); motors.b.rev(speed); } - if ( key.name === "right" ) { + if (key.name === "right") { motors.a.fwd(speed * 0.75); motors.b.fwd(speed * 0.75); } - if ( key.name === "left" ) { + if (key.name === "left") { motors.a.rev(speed * 0.75); motors.b.rev(speed * 0.75); } commands = [].slice.call(arguments); } else { - if ( ch >= 1 && ch <= 9 ) { - speed = five.Fn.scale( ch, 1, 9, 0, 255 ); + if (ch >= 1 && ch <= 9) { + speed = five.Fn.scale(ch, 1, 9, 0, 255); controller.apply(null, commands); } } diff --git a/docs/motor-directional.md b/docs/motor-directional.md index 4ed4fc56e..a17accb0b 100644 --- a/docs/motor-directional.md +++ b/docs/motor-directional.md @@ -8,7 +8,7 @@ node eg/motor-directional.js ```javascript var five = require("johnny-five"), - board = new five.Board(); + board = new five.Board(); board.on("ready", function() { var motor; diff --git a/docs/motor.md b/docs/motor.md index e4b006b4e..6da828b97 100644 --- a/docs/motor.md +++ b/docs/motor.md @@ -8,7 +8,7 @@ node eg/motor.js ```javascript var five = require("johnny-five"), - board, motor, led; + board, motor, led; board = new five.Board(); @@ -28,18 +28,18 @@ board.on("ready", function() { // Motor Event API // "start" events fire when the motor is started. - motor.on("start", function( err, timestamp ) { - console.log( "start", timestamp ); + motor.on("start", function(err, timestamp) { + console.log("start", timestamp); // Demonstrate motor stop in 2 seconds - board.wait( 2000, function() { + board.wait(2000, function() { motor.stop(); }); }); // "stop" events fire when the motor is started. - motor.on("stop", function( err, timestamp ) { - console.log( "stop", timestamp ); + motor.on("stop", function(err, timestamp) { + console.log("stop", timestamp); }); // Motor API diff --git a/docs/navigator.md b/docs/navigator.md index 2a8c10c19..5ee1304cf 100644 --- a/docs/navigator.md +++ b/docs/navigator.md @@ -8,9 +8,9 @@ node eg/navigator.js ```javascript var five = require("johnny-five"), - __ = require("../lib/fn.js"), - board, Navigator, navigator, servos, - pivotExpansion, directionMap, scale; + __ = require("../lib/fn.js"), + board, Navigator, navigator, servos, + pivotExpansion, directionMap, scale; directionMap = { @@ -20,22 +20,19 @@ directionMap = { fwd: "rev", rev: "fwd" }, - translations: [ - { - f: "forward", - r: "reverse", - fwd: "forward", - rev: "reverse" - }, - { - r: "right", - l: "left" - } - ] + translations: [{ + f: "forward", + r: "reverse", + fwd: "forward", + rev: "reverse" + }, { + r: "right", + l: "left" + }] }; -scale = function( speed, low, high ) { - return Math.floor( five.Fn.map( speed, 0, 5, low, high ) ); +scale = function(speed, low, high) { + return Math.floor(five.Fn.map(speed, 0, 5, low, high)); }; @@ -43,15 +40,22 @@ scale = function( speed, low, high ) { * Navigator * @param {Object} opts Optional properties object */ -function Navigator( opts ) { + +function Navigator(opts) { // Boe Navigator continuous are calibrated to stop at 90° this.center = opts.center || 90; // Initialize the right and left cooperative servos this.servos = { - right: new five.Servo({ pin: opts.right, type: "continuous" }), - left: new five.Servo({ pin: opts.left, type: "continuous" }) + right: new five.Servo({ + pin: opts.right, + type: "continuous" + }), + left: new five.Servo({ + pin: opts.left, + type: "continuous" + }) }; // Set the initial servo cooperative direction @@ -92,11 +96,11 @@ Navigator.DIR_MAP = directionMap; * @param {Number} left Speed/Direction of left servo * @return {Object} this */ -Navigator.prototype.move = function( right, left ) { +Navigator.prototype.move = function(right, left) { // Quietly ignore duplicate instructions - if ( this.direction.right === right && - this.direction.left === left ) { + if (this.direction.right === right && + this.direction.left === left) { return this; } @@ -104,8 +108,8 @@ Navigator.prototype.move = function( right, left ) { // Servos are mounted opposite of each other, // the values for left and right will be in // opposing directions. - this.servos.right.move( right ); - this.servos.left.move( left ); + this.servos.right.move(right); + this.servos.left.move(left); // Push a record object into the history this.history.push({ @@ -133,8 +137,8 @@ Navigator.prototype.move = function( right, left ) { { name: "forward", abbr: "fwd", - args: function( center, val ) { - return [ center - (val - center), val ]; + args: function(center, val) { + return [center - (val - center), val]; } }, @@ -148,26 +152,26 @@ Navigator.prototype.move = function( right, left ) { { name: "reverse", abbr: "rev", - args: function( center, val ) { - return [ val, center - (val - center) ]; + args: function(center, val) { + return [val, center - (val - center)]; } } -].forEach(function( dir ) { +].forEach(function(dir) { - var method = function( speed ) { + var method = function(speed) { // Set default direction method speed = speed === undefined ? 1 : speed; this.speed = speed; this.which = dir.name; - return this.move.apply( this, - dir.args( this.center, scale( speed, this.center, 110 ) ) + return this.move.apply(this, + dir.args(this.center, scale(speed, this.center, 110)) ); }; - Navigator.prototype[ dir.name ] = Navigator.prototype[ dir.abbr ] = method; + Navigator.prototype[dir.name] = Navigator.prototype[dir.abbr] = method; }); /** @@ -178,7 +182,7 @@ Navigator.prototype.stop = function() { this.speed = this.center; this.which = "stop"; - return this.move( this.center, this.center ); + return this.move(this.center, this.center); }; @@ -195,53 +199,53 @@ Navigator.prototype.stop = function() { */ "left" -].forEach(function( dir ) { - Navigator.prototype[ dir ] = function( time ) { +].forEach(function(dir) { + Navigator.prototype[dir] = function(time) { // Use direction value and reverse direction map to // derive the direction values for moving the // cooperative servos - var actual = this.direction[ directionMap.reverse[ dir ] ]; + var actual = this.direction[directionMap.reverse[dir]]; time = time || 500; - if ( !this.isTurning ) { + if (!this.isTurning) { // Set turning lock this.isTurning = true; // Send turning command - this.move( actual, actual ); + this.move(actual, actual); // Cap turning time setTimeout(function() { // Restore direction after turn - this[ this.which ]( this.speed || 2 ); + this[this.which](this.speed || 2); // Release turning lock this.isTurning = false; - }.bind(this), time ); + }.bind(this), time); } return this; }; }); -pivotExpansion = function( which ) { +pivotExpansion = function(which) { var parts; - if ( which.length === 2 ) { - parts = [ which[0], which[1] ]; + if (which.length === 2) { + parts = [which[0], which[1]]; } - if ( /\-/.test(which) ) { + if (/\-/.test(which)) { parts = which.split("-"); } - return parts.map(function( val, i ) { - console.log( val ); - return directionMap.translations[ i ][ val ]; + return parts.map(function(val, i) { + console.log(val); + return directionMap.translations[i][val]; }).join("-"); }; @@ -257,35 +261,35 @@ pivotExpansion = function( which ) { * * @return {Object} this */ -Navigator.prototype.pivot = function( which, time ) { +Navigator.prototype.pivot = function(which, time) { var actual, directions, scaled; - scaled = scale( this.speed, this.center, 110 ); + scaled = scale(this.speed, this.center, 110); directions = { "forward-right": function() { - this.move( this.center, scaled ); + this.move(this.center, scaled); }, "forward-left": function() { - this.move( this.center - (scaled - this.center), this.center ); + this.move(this.center - (scaled - this.center), this.center); }, "reverse-right": function() { - this.move( scaled, this.center ); + this.move(scaled, this.center); }, "reverse-left": function() { - this.move( this.center, this.center - (scaled - this.center) ); + this.move(this.center, this.center - (scaled - this.center)); } }; - which = directions[ which ] || directions[ pivotExpansion( which ) ]; + which = directions[which] || directions[pivotExpansion(which)]; - which.call( this, this.speed ); + which.call(this, this.speed); setTimeout(function() { - this[ this.which ]( this.speed ); + this[this.which](this.speed); - }.bind(this), time || 1000 ); + }.bind(this), time || 1000); return this; }; @@ -301,7 +305,7 @@ Navigator.prototype.pivot = function( which, time ) { // TODO: Refactor into modular program code var center, collideAt, degrees, step, facing, - range, laser, look, isScanning, scanner, gripper, isGripping, sonar, gripAt, ping, mag, bearing; + range, laser, look, isScanning, scanner, gripper, isGripping, sonar, gripAt, ping, mag, bearing; // Collision distance (inches) collideAt = 6; @@ -315,10 +319,10 @@ Navigator.prototype.pivot = function( which, time ) { facing = ""; // Scanning range (degrees) - range = [ 10, 170 ]; + range = [10, 170]; // Servo center point (degrees) - center = ( (range[1] - range[0]) / 2 ) + range[0]; + center = ((range[1] - range[0]) / 2) + range[0]; // Starting scanner scanning position (degrees) degrees = center; @@ -343,9 +347,9 @@ Navigator.prototype.pivot = function( which, time ) { gripper = new five.Gripper({ servo: { pin: 13, - range: [ 20, 160 ] + range: [20, 160] }, - scale: [ 0, 10 ] + scale: [0, 10] }); // New base navigator @@ -387,13 +391,13 @@ Navigator.prototype.pivot = function( which, time ) { scanner.center(); // Wait 1000ms, then initialize forward movement - this.wait( 1000, function() { + this.wait(1000, function() { // navigator.fwd(3); }); // Scanner/Panning loop - this.loop( 50, function() { + this.loop(50, function() { var bounds; bounds = { @@ -403,9 +407,9 @@ Navigator.prototype.pivot = function( which, time ) { // During course change, scanning is paused to avoid // overeager redirect instructions[1] - if ( isScanning ) { + if (isScanning) { // Calculate the next step position - if ( degrees >= scanner.range[1] || degrees <= scanner.range[0] ) { + if (degrees >= scanner.range[1] || degrees <= scanner.range[0]) { step *= -1; } @@ -415,21 +419,21 @@ Navigator.prototype.pivot = function( which, time ) { // The following three conditions will help determine // which way the navigator should turn if a potential collideAt // may occur in the ping "change" event handler[2] - if ( degrees > bounds.left ) { + if (degrees > bounds.left) { facing = "left"; } - if ( degrees < bounds.right ) { + if (degrees < bounds.right) { facing = "right"; } // if ( degrees > bounds.right && degrees < bounds.left ) { - if ( __.range( bounds.right, bounds.left ).indexOf( degrees ) > -1 ) { + if (__.range(bounds.right, bounds.left).indexOf(degrees) > -1) { facing = "fwd"; } - scanner.move( degrees ); + scanner.move(degrees); } }); @@ -463,18 +467,18 @@ Navigator.prototype.pivot = function( which, time ) { // distance reading has changed since the previous reading // // TODO: Avoid false positives? - ping.on("data", function( err ) { + ping.on("data", function(err) { var release = 750, - distance = Math.abs(this.inches), - isReverse = false, - turnTo; + distance = Math.abs(this.inches), + isReverse = false, + turnTo; - if ( navigator.isTurning ) { + if (navigator.isTurning) { return; } // If distance value is null or NaN - if ( distance === null || isNaN(distance) ) { + if (distance === null || isNaN(distance)) { return; } @@ -482,7 +486,7 @@ Navigator.prototype.pivot = function( which, time ) { // Detect collideAt // && isScanning - if ( distance <= collideAt && isScanning ) { + if (distance <= collideAt && isScanning) { laser.strobe(); @@ -491,37 +495,37 @@ Navigator.prototype.pivot = function( which, time ) { isScanning = false; // Determine direction to turn - turnTo = Navigator.DIR_MAP.reverse[ facing ]; + turnTo = Navigator.DIR_MAP.reverse[facing]; // Set reversal flag. isReverse = turnTo === "rev"; // Log collideAt detection to REPL console.log( - [ Date.now(), + [Date.now(), "\tCollision detected " + this.inches + " inches away.", "\tTurning " + turnTo.toUpperCase() + " to avoid" ].join("\n") ); // Turn the navigator - navigator[ turnTo ]( navigator.speed ); + navigator[turnTo](navigator.speed); - if ( isReverse ) { + if (isReverse) { release = 1500; } // [1] Allow Nms to pass and release the scanning lock // by setting isScanning state to true. - board.wait( release, function() { - console.log( "Release Scanner Lock" ); + board.wait(release, function() { + console.log("Release Scanner Lock"); degrees = 89; scanner.center(); - if ( isReverse ) { + if (isReverse) { // navigator.fwd( navigator.speed ); navigator.pivot("reverse-right"); navigator.which = "fwd"; diff --git a/docs/nodeconf-compass.md b/docs/nodeconf-compass.md index ea99c14a6..e458cd070 100644 --- a/docs/nodeconf-compass.md +++ b/docs/nodeconf-compass.md @@ -8,8 +8,8 @@ node eg/nodeconf-compass.js ```javascript var color = require("colors"), - five = require("johnny-five"), - colors, mag; + five = require("johnny-five"), + colors, mag; five.Board().on("ready", function() { @@ -20,10 +20,10 @@ five.Board().on("ready", function() { mag.on("headingchange", function() { var log; - log = ( this.bearing.name + " " + Math.floor(this.heading) + "°" ); + log = (this.bearing.name + " " + Math.floor(this.heading) + "°"); console.log( - log[ colors[ this.bearing.abbr ] ] + log[colors[this.bearing.abbr]] ); }); }); diff --git a/docs/nodeconf-navigator.md b/docs/nodeconf-navigator.md index f79f809ea..5108210d8 100644 --- a/docs/nodeconf-navigator.md +++ b/docs/nodeconf-navigator.md @@ -8,9 +8,9 @@ node eg/nodeconf-navigator.js ```javascript var five = require("johnny-five"), - __ = require("../lib/fn.js"), - board, Navigator, navigator, servos, - expandedWhich, directionMap, scale; + __ = require("../lib/fn.js"), + board, Navigator, navigator, servos, + expandedWhich, directionMap, scale; directionMap = { @@ -20,22 +20,19 @@ directionMap = { fwd: "rev", rev: "fwd" }, - translations: [ - { - f: "forward", - r: "reverse", - fwd: "forward", - rev: "reverse" - }, - { - r: "right", - l: "left" - } - ] + translations: [{ + f: "forward", + r: "reverse", + fwd: "forward", + rev: "reverse" + }, { + r: "right", + l: "left" + }] }; -scale = function( speed, low, high ) { - return Math.floor( five.Fn.map( speed, 0, 5, low, high ) ); +scale = function(speed, low, high) { + return Math.floor(five.Fn.map(speed, 0, 5, low, high)); }; @@ -43,15 +40,22 @@ scale = function( speed, low, high ) { * Navigator * @param {Object} opts Optional properties object */ -function Navigator( opts ) { + +function Navigator(opts) { // Boe Navigator continuous are calibrated to stop at 90° this.center = opts.center || 90; // Initialize the right and left cooperative servos this.servos = { - right: new five.Servo({ pin: opts.right, type: "continuous" }), - left: new five.Servo({ pin: opts.left, type: "continuous" }) + right: new five.Servo({ + pin: opts.right, + type: "continuous" + }), + left: new five.Servo({ + pin: opts.left, + type: "continuous" + }) }; // Set the initial servo cooperative direction @@ -89,11 +93,11 @@ Navigator.DIR_MAP = directionMap; * @param {Number} left Speed/Direction of left servo * @return {Object} this */ -Navigator.prototype.move = function( right, left ) { +Navigator.prototype.move = function(right, left) { // Quietly ignore duplicate instructions - if ( this.direction.right === right && - this.direction.left === left ) { + if (this.direction.right === right && + this.direction.left === left) { return this; } @@ -101,8 +105,8 @@ Navigator.prototype.move = function( right, left ) { // Servos are mounted opposite of each other, // the values for left and right will be in // opposing directions. - this.servos.right.move( right ); - this.servos.left.move( left ); + this.servos.right.move(right); + this.servos.left.move(left); // Push a record object into the history this.history.push({ @@ -130,8 +134,8 @@ Navigator.prototype.move = function( right, left ) { { name: "forward", abbr: "fwd", - args: function( center, val ) { - return [ center - (val - center), val ]; + args: function(center, val) { + return [center - (val - center), val]; } }, @@ -145,26 +149,26 @@ Navigator.prototype.move = function( right, left ) { { name: "reverse", abbr: "rev", - args: function( center, val ) { - return [ val, center - (val - center) ]; + args: function(center, val) { + return [val, center - (val - center)]; } } -].forEach(function( dir ) { +].forEach(function(dir) { - var method = function( speed ) { + var method = function(speed) { // Set default direction method speed = speed === undefined ? 1 : speed; this.speed = speed; this.which = dir.name; - return this.move.apply( this, - dir.args( this.center, scale( speed, this.center, 110 ) ) + return this.move.apply(this, + dir.args(this.center, scale(speed, this.center, 110)) ); }; - Navigator.prototype[ dir.name ] = Navigator.prototype[ dir.abbr ] = method; + Navigator.prototype[dir.name] = Navigator.prototype[dir.abbr] = method; }); /** @@ -175,68 +179,68 @@ Navigator.prototype.stop = function() { this.speed = this.center; this.which = "stop"; - return this.move( this.center, this.center ); + return this.move(this.center, this.center); }; [ -/** - * left Turn the bot left - * @return {Object} this - */ -"left", + /** + * left Turn the bot left + * @return {Object} this + */ + "left", -/** - * right Turn the bot right - * @return {Object} this - */ -"right" + /** + * right Turn the bot right + * @return {Object} this + */ + "right" -].forEach(function( dir ) { - Navigator.prototype[ dir ] = function() { +].forEach(function(dir) { + Navigator.prototype[dir] = function() { // Use direction value and reverse direction map to // derive the direction values for moving the // cooperative servos - var actual = this.direction[ directionMap.reverse[ dir ] ]; + var actual = this.direction[directionMap.reverse[dir]]; - if ( !this.isTurning ) { + if (!this.isTurning) { // Set turning lock this.isTurning = true; // Send turning command - this.move( actual, actual ); + this.move(actual, actual); // Cap turning time setTimeout(function() { // Restore direction after turn - this[ this.which ]( this.speed ); + this[this.which](this.speed); // Release turning lock this.isTurning = false; - }.bind(this), 500 ); + }.bind(this), 500); } return this; }; }); -expandedWhich = function( which ) { +expandedWhich = function(which) { var parts; - if ( which.length === 2 ) { - parts = [ which[0], which[1] ]; + if (which.length === 2) { + parts = [which[0], which[1]]; } - if ( /\-/.test(which) ) { + if (/\-/.test(which)) { parts = which.split("-"); } - return parts.map(function( val, i ) { - console.log( val ); - return directionMap.translations[ i ][ val ]; + return parts.map(function(val, i) { + console.log(val); + return directionMap.translations[i][val]; }).join("-"); }; @@ -252,35 +256,35 @@ expandedWhich = function( which ) { * * @return {Object} this */ -Navigator.prototype.pivot = function( which, time ) { +Navigator.prototype.pivot = function(which, time) { var actual, directions, scaled; - scaled = scale( this.speed, this.center, 110 ); + scaled = scale(this.speed, this.center, 110); directions = { "forward-right": function() { - this.move( this.center, scaled ); + this.move(this.center, scaled); }, "forward-left": function() { - this.move( this.center - (scaled - this.center), this.center ); + this.move(this.center - (scaled - this.center), this.center); }, "reverse-right": function() { - this.move( scaled, this.center ); + this.move(scaled, this.center); }, "reverse-left": function() { - this.move( this.center, this.center - (scaled - this.center) ); + this.move(this.center, this.center - (scaled - this.center)); } }; - which = directions[ which ] || directions[ expandedWhich( which ) ]; + which = directions[which] || directions[expandedWhich(which)]; - which.call( this, this.speed ); + which.call(this, this.speed); setTimeout(function() { - this[ this.which ]( this.speed ); + this[this.which](this.speed); - }.bind(this), time || 1000 ); + }.bind(this), time || 1000); return this; }; @@ -297,7 +301,7 @@ board.on("ready", function() { // TODO: Refactor into modular program code var center, collideAt, degrees, step, facing, - range, laser, look, isScanning, scanner, ping, mag, bearing; + range, laser, look, isScanning, scanner, ping, mag, bearing; // Collision distance (inches) collideAt = 6; @@ -309,10 +313,10 @@ board.on("ready", function() { facing = ""; // Scanning range (degrees) - range = [ 10, 170 ]; + range = [10, 170]; // Servo center point (degrees) - center = ( (range[1] - range[0]) / 2 ) + range[0]; + center = ((range[1] - range[0]) / 2) + range[0]; // Starting scanner scanning position (degrees) degrees = center; @@ -360,13 +364,13 @@ board.on("ready", function() { scanner.center(); // Wait 1000ms, then initialize forward movement - this.wait( 1000, function() { + this.wait(1000, function() { navigator.fwd(3); }); // Scanner/Panning loop - this.loop( 50, function() { + this.loop(50, function() { var bounds; bounds = { @@ -376,9 +380,9 @@ board.on("ready", function() { // During course change, scanning is paused to avoid // overeager redirect instructions[1] - if ( isScanning ) { + if (isScanning) { // Calculate the next step position - if ( degrees >= scanner.range[1] || degrees <= scanner.range[0] ) { + if (degrees >= scanner.range[1] || degrees <= scanner.range[0]) { step *= -1; } @@ -388,21 +392,21 @@ board.on("ready", function() { // The following three conditions will help determine // which way the navigator should turn if a potential collideAt // may occur in the ping "change" event handler[2] - if ( degrees > bounds.left ) { + if (degrees > bounds.left) { facing = "left"; } - if ( degrees < bounds.right ) { + if (degrees < bounds.right) { facing = "right"; } // if ( degrees > bounds.right && degrees < bounds.left ) { - if ( __.range( bounds.right, bounds.left ).indexOf( degrees ) > -1 ) { + if (__.range(bounds.right, bounds.left).indexOf(degrees) > -1) { facing = "fwd"; } - scanner.move( degrees ); + scanner.move(degrees); } }); @@ -421,24 +425,24 @@ board.on("ready", function() { // distance reading has changed since the previous reading // // TODO: Avoid false positives? - ping.on("data", function( err ) { + ping.on("data", function(err) { var release = 750, - distance = Math.abs(this.inches), - isReverse = false, - turnTo; + distance = Math.abs(this.inches), + isReverse = false, + turnTo; - if ( navigator.isTurning ) { + if (navigator.isTurning) { return; } // If distance value is null or NaN - if ( !distance ) { + if (!distance) { return; } // Detect collideAt // && isScanning - if ( distance <= collideAt && isScanning ) { + if (distance <= collideAt && isScanning) { laser.strobe(); @@ -447,37 +451,37 @@ board.on("ready", function() { isScanning = false; // Determine direction to turn - turnTo = Navigator.DIR_MAP.reverse[ facing ]; + turnTo = Navigator.DIR_MAP.reverse[facing]; // Set reversal flag. isReverse = turnTo === "rev"; // Log collideAt detection to REPL console.log( - [ Date.now(), + [Date.now(), "\tCollision detected " + this.inches + " inches away.", "\tTurning " + turnTo.toUpperCase() + " to avoid" ].join("\n") ); // Turn the navigator - navigator[ turnTo ]( navigator.speed ); + navigator[turnTo](navigator.speed); - if ( isReverse ) { + if (isReverse) { release = 1500; } // [1] Allow Nms to pass and release the scanning lock // by setting isScanning state to true. - board.wait( release, function() { - console.log( "Release Scanner Lock" ); + board.wait(release, function() { + console.log("Release Scanner Lock"); degrees = 89; scanner.center(); - if ( isReverse ) { + if (isReverse) { // navigator.fwd( navigator.speed ); navigator.pivot("reverse-right"); navigator.which = "fwd"; diff --git a/docs/nodeconf-radar.md b/docs/nodeconf-radar.md index af8ce9103..666a52374 100644 --- a/docs/nodeconf-radar.md +++ b/docs/nodeconf-radar.md @@ -8,37 +8,37 @@ node eg/nodeconf-radar.js ```javascript var five = require("johnny-five"), - child = require("child_process"), - http = require("http"), - socket = require("socket.io"), - fs = require("fs"), - app, board, io; + child = require("child_process"), + http = require("http"), + socket = require("socket.io"), + fs = require("fs"), + app, board, io; -function handler( req, res ) { +function handler(req, res) { var path = __dirname; - if ( req.url === "/" ) { + if (req.url === "/") { path += "/radar.html"; } else { path += req.url; } - fs.readFile( path, function( err, data ) { - if ( err ) { - res.writeHead( 500 ); - return res.end( "Error loading " + path ); + fs.readFile(path, function(err, data) { + if (err) { + res.writeHead(500); + return res.end("Error loading " + path); } - res.writeHead( 200 ); - res.end( data ); + res.writeHead(200); + res.end(data); }); } -app = http.createServer( handler ); -app.listen( 8080 ); +app = http.createServer(handler); +app.listen(8080); -io = socket.listen( app ); -io.set( "log level", 1 ); +io = socket.listen(app); +io.set("log level", 1); board = new five.Board(); @@ -47,7 +47,7 @@ board.on("ready", function() { // Open Radar view - child.exec( "open http://localhost:8080/" ); + child.exec("open http://localhost:8080/"); // Starting scanner scanning position (degrees) degrees = 1; @@ -61,10 +61,10 @@ board.on("ready", function() { last = 0; // Scanning range (degrees) - range = [ 0, 170 ]; + range = [0, 170]; // Servo center point (degrees) - center = range[ 1 ] / 2; + center = range[1] / 2; // ping instance (distance detection) ping = new five.Ping(7); @@ -83,7 +83,7 @@ board.on("ready", function() { scanner.min(); // Scanner/Panning loop - this.loop( 100, function() { + this.loop(100, function() { var bounds, isOver, isUnder; bounds = { @@ -95,8 +95,8 @@ board.on("ready", function() { isUnder = degrees <= scanner.range[0]; // Calculate the next step position - if ( isOver || isUnder ) { - if ( isOver ) { + if (isOver || isUnder) { + if (isOver) { io.sockets.emit("reset"); degrees = 0; step = 1; @@ -110,18 +110,18 @@ board.on("ready", function() { degrees += step; // Update servo position - scanner.move( degrees ); + scanner.move(degrees); }); - io.sockets.on( "connection", function( socket ) { - console.log( "Socket Connected" ); + io.sockets.on("connection", function(socket) { + console.log("Socket Connected"); soi = socket; ping.on("read", function() { - if ( last !== degrees ) { - io.sockets.emit( "ping", { + if (last !== degrees) { + io.sockets.emit("ping", { degrees: degrees, distance: this.cm }); diff --git a/docs/nodeconf-slider.md b/docs/nodeconf-slider.md index a4265506e..f96222997 100644 --- a/docs/nodeconf-slider.md +++ b/docs/nodeconf-slider.md @@ -8,7 +8,7 @@ node eg/nodeconf-slider.js ```javascript var five = require("johnny-five"), - board, slider, servo, scalingRange; + board, slider, servo, scalingRange; board = new five.Board(); @@ -20,11 +20,11 @@ board.on("ready", function() { }); // log out the slider values to the console. - slider.on("slide", function( err, value ) { - if ( err ) { - console.log( "error: ", err ); + slider.on("slide", function(err, value) { + if (err) { + console.log("error: ", err); } else { - console.log( Math.floor(this.value) ); + console.log(Math.floor(this.value)); } }); }); diff --git a/docs/nunchuk.md b/docs/nunchuk.md index 54765e06b..19f0ed158 100644 --- a/docs/nunchuk.md +++ b/docs/nunchuk.md @@ -8,7 +8,7 @@ node eg/nunchuk.js ```javascript var five = require("johnny-five"), - board, nunchuk; + board, nunchuk; board = new five.Board(); @@ -40,10 +40,10 @@ board.on("ready", function() { // Fired when the joystick detects a change in // axis position. // - nunchuk.joystick.on( "change", function( err, event ) { + nunchuk.joystick.on("change", function(err, event) { console.log( "joystick " + event.axis, - event.target[ event.axis ], + event.target[event.axis], event.axis, event.direction ); }); @@ -53,10 +53,10 @@ board.on("ready", function() { // Fired when the accelerometer detects a change in // axis position. // - nunchuk.accelerometer.on( "change", function( err, event ) { + nunchuk.accelerometer.on("change", function(err, event) { console.log( "accelerometer " + event.axis, - event.target[ event.axis ], + event.target[event.axis], event.axis, event.direction ); }); @@ -83,13 +83,14 @@ board.on("ready", function() { // - [ "down", "up", "hold" ].forEach(function( type ) { + ["down", "up", "hold"].forEach(function(type) { - nunchuk.on( type, function( err, event ) { + nunchuk.on(type, function(err, event) { console.log( event.target.which + " is " + type, - { isUp: event.target.isUp, + { + isUp: event.target.isUp, isDown: event.target.isDown } ); @@ -98,9 +99,9 @@ board.on("ready", function() { }); -// Further reading -// http://media.pragprog.com/titles/msard/tinker.pdf -// http://lizarum.com/assignments/physical_computing/2008/wii_nunchuck.html + // Further reading + // http://media.pragprog.com/titles/msard/tinker.pdf + // http://lizarum.com/assignments/physical_computing/2008/wii_nunchuck.html }); ``` diff --git a/docs/photoresistor-servo.md b/docs/photoresistor-servo.md index 84d2f8eb6..09e551f03 100644 --- a/docs/photoresistor-servo.md +++ b/docs/photoresistor-servo.md @@ -8,7 +8,7 @@ node eg/photoresistor-servo.js ```javascript var five = require("johnny-five"), - board, photoresistor, servo, range; + board, photoresistor, servo, range; board = new five.Board(); @@ -17,7 +17,7 @@ board.on("ready", function() { //range for the servo (degrees) //CAUTION: don't set the numbers past what you servo can handle // it could potentially damage the servo - range = [ 0, 159 ]; + range = [0, 159]; // Create a new `photoresistor` hardware instance. photoresistor = new five.Sensor({ @@ -46,12 +46,12 @@ board.on("ready", function() { //equate the photoresistor values to the servo scale //do something when the a value is read from the photoresistor - photoresistor.scale( range ).on("data", function() { + photoresistor.scale(range).on("data", function() { //outputs to screen the values being used from the photoresistor //console.log( "Normalized value: " + this.normalized + " Scaled Value: " + this.scaled ); //tell the servo arm to move the scaled value. - servo.move( Math.floor(this.scaled)); + servo.move(Math.floor(this.scaled)); }); }); diff --git a/docs/photoresistor.md b/docs/photoresistor.md index ca7998a6b..062969b17 100644 --- a/docs/photoresistor.md +++ b/docs/photoresistor.md @@ -8,7 +8,7 @@ node eg/photoresistor.js ```javascript var five = require("johnny-five"), - board, photoresistor; + board, photoresistor; board = new five.Board(); @@ -29,7 +29,7 @@ board.on("ready", function() { // "data" get the current reading from the photoresistor photoresistor.on("data", function() { - console.log( this.value ); + console.log(this.value); }); }); diff --git a/docs/pin-circuit-event.md b/docs/pin-circuit-event.md index 38a172136..020ebefea 100644 --- a/docs/pin-circuit-event.md +++ b/docs/pin-circuit-event.md @@ -13,9 +13,9 @@ new five.Board().on("ready", function() { var pin = new five.Pin(5); // Event tests - [ "high", "low" ].forEach(function( type ) { - pin.on( type, function() { - console.log( "Circuit Event: ", type ); + ["high", "low"].forEach(function(type) { + pin.on(type, function() { + console.log("Circuit Event: ", type); }); }); }); diff --git a/docs/pin.md b/docs/pin.md index 095781e26..c8471a1ea 100644 --- a/docs/pin.md +++ b/docs/pin.md @@ -8,31 +8,31 @@ node eg/pin.js ```javascript var five = require("johnny-five"), - temporal = require("temporal"); + temporal = require("temporal"); (new five.Board()).on("ready", function() { - var events, strobe; + var events, strobe; - events = []; - strobe = new five.Pin({ - addr: 13 - }); + events = []; + strobe = new five.Pin({ + addr: 13 + }); - temporal.loop(500, function( loop ) { - strobe[ loop.called % 2 === 0 ? "high" : "low" ](); - }); + temporal.loop(500, function(loop) { + strobe[loop.called % 2 === 0 ? "high" : "low"](); + }); - // Event tests - [ "high", "low" ].forEach(function( state ) { - strobe.on( state, function() { - if ( events.indexOf(state) === -1 ) { - console.log( "Event emitted for:", state, "on", this.addr ); - events.push( state ); - } + // Event tests + ["high", "low"].forEach(function(state) { + strobe.on(state, function() { + if (events.indexOf(state) === -1) { + console.log("Event emitted for:", state, "on", this.addr); + events.push(state); + } + }); }); }); -}); ``` diff --git a/docs/ping.md b/docs/ping.md index ead24fd6c..7284f0940 100644 --- a/docs/ping.md +++ b/docs/ping.md @@ -8,7 +8,7 @@ node eg/ping.js ```javascript var five = require("johnny-five"), - board, ping; + board, ping; board = new five.Board(); @@ -38,14 +38,14 @@ board.on("ready", function() { // Ping Event API // "data" get the current reading from the ping - ping.on("data", function( err, value ) { - console.log( "data", value ); + ping.on("data", function(err, value) { + console.log("data", value); }); - ping.on("change", function( err, value ) { + ping.on("change", function(err, value) { - console.log( typeof this.inches ); - console.log( "Object is " + this.inches + "inches away" ); + console.log(typeof this.inches); + console.log("Object is " + this.inches + "inches away"); }); }); diff --git a/docs/potentiometer.md b/docs/potentiometer.md index 31ba0feaa..288ee0654 100644 --- a/docs/potentiometer.md +++ b/docs/potentiometer.md @@ -8,7 +8,7 @@ node eg/potentiometer.js ```javascript var five = require("johnny-five"), - board, potentiometer; + board, potentiometer; board = new five.Board(); @@ -29,7 +29,7 @@ board.on("ready", function() { // "data" get the current reading from the potentiometer potentiometer.on("data", function() { - console.log( this.value, this.raw ); + console.log(this.value, this.raw); }); }); diff --git a/docs/radar.md b/docs/radar.md index c29f7dc8a..c4022eb6b 100644 --- a/docs/radar.md +++ b/docs/radar.md @@ -8,37 +8,37 @@ node eg/radar.js ```javascript var five = require("johnny-five"), - child = require("child_process"), - http = require("http"), - socket = require("socket.io"), - fs = require("fs"), - app, board, io; + child = require("child_process"), + http = require("http"), + socket = require("socket.io"), + fs = require("fs"), + app, board, io; -function handler( req, res ) { +function handler(req, res) { var path = __dirname; - if ( req.url === "/" ) { + if (req.url === "/") { path += "/radar.html"; } else { path += req.url; } - fs.readFile( path, function( err, data ) { - if ( err ) { - res.writeHead( 500 ); - return res.end( "Error loading " + path ); + fs.readFile(path, function(err, data) { + if (err) { + res.writeHead(500); + return res.end("Error loading " + path); } - res.writeHead( 200 ); - res.end( data ); + res.writeHead(200); + res.end(data); }); } -app = http.createServer( handler ); -app.listen( 8080 ); +app = http.createServer(handler); +app.listen(8080); -io = socket.listen( app ); -io.set( "log level", 1 ); +io = socket.listen(app); +io.set("log level", 1); board = new five.Board(); @@ -47,7 +47,7 @@ board.on("ready", function() { // Open Radar view - child.exec( "open http://localhost:8080/" ); + child.exec("open http://localhost:8080/"); // Starting scanner scanning position (degrees) degrees = 1; @@ -61,10 +61,10 @@ board.on("ready", function() { last = 0; // Scanning range (degrees) - range = [ 0, 170 ]; + range = [0, 170]; // Servo center point (degrees) - center = range[ 1 ] / 2; + center = range[1] / 2; // ping instance (distance detection) ping = new five.Ping(7); @@ -83,7 +83,7 @@ board.on("ready", function() { scanner.min(); // Scanner/Panning loop - this.loop( 100, function() { + this.loop(100, function() { var bounds, isOver, isUnder; bounds = { @@ -95,8 +95,8 @@ board.on("ready", function() { isUnder = degrees <= scanner.range[0]; // Calculate the next step position - if ( isOver || isUnder ) { - if ( isOver ) { + if (isOver || isUnder) { + if (isOver) { io.sockets.emit("reset"); degrees = 0; step = 1; @@ -110,18 +110,18 @@ board.on("ready", function() { degrees += step; // Update servo position - scanner.move( degrees ); + scanner.move(degrees); }); - io.sockets.on( "connection", function( socket ) { - console.log( "Socket Connected" ); + io.sockets.on("connection", function(socket) { + console.log("Socket Connected"); soi = socket; ping.on("data", function() { - if ( last !== degrees ) { - io.sockets.emit( "ping", { + if (last !== degrees) { + io.sockets.emit("ping", { degrees: degrees, distance: this.cm }); diff --git a/docs/repl.md b/docs/repl.md index fd5d579df..39b1661ea 100644 --- a/docs/repl.md +++ b/docs/repl.md @@ -8,12 +8,12 @@ node eg/repl.js ```javascript var five = require("johnny-five"), - board; + board; board = new five.Board(); board.on("ready", function() { - console.log( "Ready event. Repl instance auto-initialized" ); + console.log("Ready event. Repl instance auto-initialized"); this.repl.inject({ test: "foo" diff --git a/docs/sensor-fsr-servo.md b/docs/sensor-fsr-servo.md index b433c7e54..705f5b44b 100644 --- a/docs/sensor-fsr-servo.md +++ b/docs/sensor-fsr-servo.md @@ -8,7 +8,7 @@ node eg/sensor-fsr-servo.js ```javascript var five = require("johnny-five"), - board, fsr, servo; + board, fsr, servo; board = new five.Board(); @@ -22,9 +22,9 @@ board.on("ready", function() { servo = new five.Servo(10); - fsr.scale([ 0, 180 ]).on("data", function() { + fsr.scale([0, 180]).on("data", function() { - servo.move( this.value ); + servo.move(this.value); }); }); diff --git a/docs/sensor-fsr.md b/docs/sensor-fsr.md index cc25b4731..9ff90bf94 100644 --- a/docs/sensor-fsr.md +++ b/docs/sensor-fsr.md @@ -8,27 +8,27 @@ node eg/sensor-fsr.js ```javascript var five = require("johnny-five"), - fsr, led; + fsr, led; (new five.Board()).on("ready", function() { - // Create a new `fsr` hardware instance. - fsr = new five.Sensor({ - pin: "A0", - freq: 25 - }); + // Create a new `fsr` hardware instance. + fsr = new five.Sensor({ + pin: "A0", + freq: 25 + }); - led = new five.Led(9); + led = new five.Led(9); - // Scale the sensor's value to the LED's brightness range - fsr.scale([ 0, 255 ]).on("data", function() { + // Scale the sensor's value to the LED's brightness range + fsr.scale([0, 255]).on("data", function() { - // set the led's brightness based on force - // applied to force sensitive resistor + // set the led's brightness based on force + // applied to force sensitive resistor - led.brightness( this.value ); + led.brightness(this.value); + }); }); -}); ``` diff --git a/docs/sensor-ir-led-receiver.md b/docs/sensor-ir-led-receiver.md index 9329e89b0..3f59e0a78 100644 --- a/docs/sensor-ir-led-receiver.md +++ b/docs/sensor-ir-led-receiver.md @@ -8,7 +8,7 @@ node eg/sensor-ir-led-receiver.js ```javascript var five = require("johnny-five"), - board, ir; + board, ir; board = new five.Board(); board.on("ready", function() { @@ -22,7 +22,7 @@ board.on("ready", function() { }) }; - ir.receive.scale([ 0, 100 ]).on("data", function() { + ir.receive.scale([0, 100]).on("data", function() { // console.log( this.value ); diff --git a/docs/sensor-slider.md b/docs/sensor-slider.md index 0944931ef..032dcfc4f 100644 --- a/docs/sensor-slider.md +++ b/docs/sensor-slider.md @@ -8,7 +8,7 @@ node eg/sensor-slider.js ```javascript var five = require("johnny-five"), - board, slider; + board, slider; board = new five.Board(); @@ -29,9 +29,9 @@ board.on("ready", function() { // // Fires when value of sensor changes // - slider.scale([ 0, 100 ]).on("slide", function() { + slider.scale([0, 100]).on("slide", function() { - console.log( "slide", this.value ); + console.log("slide", this.value); }); }); diff --git a/docs/sensor-temperature.md b/docs/sensor-temperature.md index e0a66753c..cf92b8d09 100644 --- a/docs/sensor-temperature.md +++ b/docs/sensor-temperature.md @@ -9,13 +9,13 @@ node eg/sensor-temperature.js ```javascript var five = require("johnny-five"); -five.Board().on("ready", function(){ +five.Board().on("ready", function() { var sensor = new five.Sensor("A0"); - sensor.on("data", function(){ + sensor.on("data", function() { var voltage = this.value * 0.004882814; var celsius = (voltage - 0.5) * 100; - var fahrenheit = celsius * (9/5) + 32; + var fahrenheit = celsius * (9 / 5) + 32; console.log(celsius + "°C", fahrenheit + "°F"); }); diff --git a/docs/sensor.md b/docs/sensor.md index 65048cbf7..4328a5d64 100644 --- a/docs/sensor.md +++ b/docs/sensor.md @@ -8,7 +8,7 @@ node eg/sensor.js ```javascript var five = require("johnny-five"), - board, sensor; + board, sensor; board = new five.Board(); @@ -45,8 +45,8 @@ board.on("ready", function() { // // Fires when the pin is read for a value // - sensor.scale([ 0, 100 ]).on("data", function() { - console.log( this.value, this.raw ); + sensor.scale([0, 100]).on("data", function() { + console.log(this.value, this.raw); }); // "change" diff --git a/docs/servo-array.md b/docs/servo-array.md index c4a1462a4..f5c4cfe3d 100644 --- a/docs/servo-array.md +++ b/docs/servo-array.md @@ -8,7 +8,7 @@ node eg/servo-array.js ```javascript var five = require("johnny-five"), - board, array; + board, array; board = new five.Board(); @@ -17,7 +17,7 @@ board.on("ready", function() { five.Servo({ pin: 9, // Limit this servo to 170° - range: [ 0, 170 ] + range: [0, 170] }); five.Servo(10); diff --git a/docs/servo-digital.md b/docs/servo-digital.md index c56d68791..74abb54b1 100644 --- a/docs/servo-digital.md +++ b/docs/servo-digital.md @@ -8,7 +8,7 @@ node eg/servo-digital.js ```javascript var five = require("johnny-five"), - board, slider, servo, range; + board, slider, servo, range; board = new five.Board(); @@ -17,7 +17,7 @@ board.on("ready", function() { // The Servo class defaults to the standard 180°, // This example program is intended to run with a // 90° digital servo. - range = [ 0, 90 ]; + range = [0, 90]; // Create a new servo instance on PWM 10, w/ a 0-90° range servo = new five.Servo({ @@ -35,9 +35,9 @@ board.on("ready", function() { // Scale the slider's value to fit in the servo's // movement range. When the slider position changes // update the servo's position - slider.scale( range ).on( "slide", function() { + slider.scale(range).on("slide", function() { - servo.move( Math.floor(this.value) ); + servo.move(Math.floor(this.value)); }); }); diff --git a/docs/servo-dual.md b/docs/servo-dual.md index b84eb810a..0eb286dcc 100644 --- a/docs/servo-dual.md +++ b/docs/servo-dual.md @@ -8,7 +8,7 @@ node eg/servo-dual.js ```javascript var five = require("johnny-five"), - board, servos; + board, servos; board = new five.Board(); @@ -17,7 +17,7 @@ board.on("ready", function() { servos = { claw: five.Servo({ pin: 9, - range: [ 0, 170 ] + range: [0, 170] }), arm: five.Servo(10) }; @@ -31,7 +31,7 @@ board.on("ready", function() { servos.claw.min(); - this.wait( 1000, function() { + this.wait(1000, function() { servos.claw.sweep(); }); }); diff --git a/docs/servo-options.md b/docs/servo-options.md index 91df4ce97..7600baf10 100644 --- a/docs/servo-options.md +++ b/docs/servo-options.md @@ -8,14 +8,14 @@ node eg/servo-options.js ```javascript var five = require("johnny-five"), - board, servo; + board, servo; board = new five.Board(); board.on("ready", function() { servo = new five.Servo({ pin: 10, - range: [ 0, 180 ], // Default: 0-180 + range: [0, 180], // Default: 0-180 type: "standard", // Default: "standard". Use "continuous" for continuous rotation servos startAt: 90, // if you would like the servo to immediately move to a degree center: false // overrides startAt if true and moves the servo to the center of the range diff --git a/docs/servo-tutorial.md b/docs/servo-tutorial.md index 34f03ad30..660715e32 100644 --- a/docs/servo-tutorial.md +++ b/docs/servo-tutorial.md @@ -8,7 +8,7 @@ node eg/servo-tutorial.js ```javascript var five = require("johnny-five"), - board, servo; + board, servo; board = new five.Board(); @@ -17,7 +17,7 @@ board.on("ready", function() { five.Servo({ pin: 9, // Limit this servo to 170° - range: [ 45, 135 ] + range: [45, 135] }); five.Servo(10); diff --git a/docs/servo.md b/docs/servo.md index 98fba79e2..2bc5df673 100644 --- a/docs/servo.md +++ b/docs/servo.md @@ -8,7 +8,7 @@ node eg/servo.js ```javascript var five = require("johnny-five"), - board, servo; + board, servo; board = new five.Board(); diff --git a/docs/seven-segment.md b/docs/seven-segment.md index a04b1ff86..95c234564 100644 --- a/docs/seven-segment.md +++ b/docs/seven-segment.md @@ -12,7 +12,7 @@ node eg/seven-segment.js * 74HC595 shift register. See docs/breadboard/seven-segment.png for wiring. */ var five = require("johnny-five"), - board; + board; board = new five.Board(); @@ -37,27 +37,27 @@ board.on("ready", function() { digits = []; // .GFEDCBA - digits[0] = parseInt( "00111111", 2 ); // 0 - digits[1] = parseInt( "00000110", 2 ); // 1 - digits[2] = parseInt( "01011011", 2 ); // 2 - digits[3] = parseInt( "01001111", 2 ); // 3 - digits[4] = parseInt( "01100110", 2 ); // 4 - digits[5] = parseInt( "01101101", 2 ); // 5 - digits[6] = parseInt( "01111101", 2 ); // 6 - digits[7] = parseInt( "00000111", 2 ); // 7 - digits[8] = parseInt( "01111111", 2 ); // 8 - digits[9] = parseInt( "01101111", 2 ); // 9 + digits[0] = parseInt("00111111", 2); // 0 + digits[1] = parseInt("00000110", 2); // 1 + digits[2] = parseInt("01011011", 2); // 2 + digits[3] = parseInt("01001111", 2); // 3 + digits[4] = parseInt("01100110", 2); // 4 + digits[5] = parseInt("01101101", 2); // 5 + digits[6] = parseInt("01111101", 2); // 6 + digits[7] = parseInt("00000111", 2); // 7 + digits[8] = parseInt("01111111", 2); // 8 + digits[9] = parseInt("01101111", 2); // 9 segments = { // .GFEDCBA - a : parseInt( "00000001", 2 ), - b : parseInt( "00000010", 2 ), - c : parseInt( "00000100", 2 ), - d : parseInt( "00001000", 2 ), - e : parseInt( "00010000", 2 ), - f : parseInt( "00100000", 2 ), - g : parseInt( "01000000", 2 ), - dp : parseInt( "10000000", 2 ) + a: parseInt("00000001", 2), + b: parseInt("00000010", 2), + c: parseInt("00000100", 2), + d: parseInt("00001000", 2), + e: parseInt("00010000", 2), + f: parseInt("00100000", 2), + g: parseInt("01000000", 2), + dp: parseInt("10000000", 2) }; sr = new five.ShiftRegister({ @@ -70,11 +70,11 @@ board.on("ready", function() { led = new five.Led(5); - function invert( num ) { + function invert(num) { return ((~num << 24) >> 24) & 255; } - sr.digit = function( num ) { + sr.digit = function(num) { sr.clear(); sr.send( @@ -82,7 +82,7 @@ board.on("ready", function() { ); }; - sr.segment = function( s ) { + sr.segment = function(s) { sr.clear(); sr.send( @@ -100,14 +100,14 @@ board.on("ready", function() { function next() { led.stop(); - sr.digit( i-- ); + sr.digit(i--); - if ( i < 0 ) { + if (i < 0) { i = 9; led.strobe(50); - setTimeout( next, 2000 ); + setTimeout(next, 2000); } else { - setTimeout( next, 1000 ); + setTimeout(next, 1000); } } diff --git a/docs/shiftregister.md b/docs/shiftregister.md index 6403f5173..8faefc141 100644 --- a/docs/shiftregister.md +++ b/docs/shiftregister.md @@ -8,7 +8,7 @@ node eg/shiftregister.js ```javascript var five = require("../lib/johnny-five"), - board, shiftRegister; + board, shiftRegister; board = new five.Board(); @@ -29,7 +29,7 @@ board.on("ready", function() { function next() { value = value > 0x11 ? value >> 1 : 0x88; - shiftRegister.send( value ); + shiftRegister.send(value); setTimeout(next, 200); } diff --git a/docs/slider-log.md b/docs/slider-log.md index 47bc9bb51..8a0931eca 100644 --- a/docs/slider-log.md +++ b/docs/slider-log.md @@ -8,7 +8,7 @@ node eg/slider-log.js ```javascript var five = require("johnny-five"), - board, slider, servo, scalingRange; + board, slider, servo, scalingRange; board = new five.Board(); @@ -20,11 +20,11 @@ board.on("ready", function() { }); // log out the slider values to the console. - slider.scale( 0, 100 ).on("slide", function( err, value ) { - if ( err ) { - console.log( "error: ", err ); + slider.scale(0, 100).on("slide", function(err, value) { + if (err) { + console.log("error: ", err); } else { - console.log( Math.floor(this.value) ); + console.log(Math.floor(this.value)); } }); }); diff --git a/docs/slider-pan.md b/docs/slider-pan.md index 8e3a6dc75..e05ee793b 100644 --- a/docs/slider-pan.md +++ b/docs/slider-pan.md @@ -8,13 +8,13 @@ node eg/slider-pan.js ```javascript var five = require("johnny-five"), - board, slider, tilt, scalingRange; + board, slider, tilt, scalingRange; board = new five.Board(); board.on("ready", function() { - scalingRange = [ 0, 170 ]; + scalingRange = [0, 170]; slider = new five.Sensor({ pin: "A0", @@ -26,11 +26,11 @@ board.on("ready", function() { range: scalingRange }); - slider.scale( scalingRange ).on("slide", function( err, value ) { + slider.scale(scalingRange).on("slide", function(err, value) { // The slider's value will be scaled to match the tilt servo range - tilt.move( Math.floor(this.value) ); + tilt.move(Math.floor(this.value)); }); }); diff --git a/docs/slider-servo-control.md b/docs/slider-servo-control.md index 3bd34990c..31c8e7ae6 100644 --- a/docs/slider-servo-control.md +++ b/docs/slider-servo-control.md @@ -8,13 +8,13 @@ node eg/slider-servo-control.js ```javascript var five = require("johnny-five"), - board, slider, servo, scalingRange; + board, slider, servo, scalingRange; board = new five.Board(); board.on("ready", function() { - scalingRange = [ 0, 170 ]; + scalingRange = [0, 170]; slider = new five.Sensor({ pin: "A0", @@ -29,9 +29,9 @@ board.on("ready", function() { // The slider's value will be scaled to match the servo's movement range - slider.scale( scalingRange ).on("slide", function( err, value ) { + slider.scale(scalingRange).on("slide", function(err, value) { - servo.move( Math.floor(this.value) ); + servo.move(Math.floor(this.value)); }); }); diff --git a/docs/sonar-scan.md b/docs/sonar-scan.md index 11fad4ba0..f2b72c563 100644 --- a/docs/sonar-scan.md +++ b/docs/sonar-scan.md @@ -8,13 +8,13 @@ node eg/sonar-scan.js ```javascript var five = require("johnny-five"), - board; + board; board = new five.Board(); board.on("ready", function() { var center, collision, degrees, step, facing, - range, redirect, look, isScanning, scanner, sonar, servos; + range, redirect, look, isScanning, scanner, sonar, servos; // Collision distance (inches) collision = 6; @@ -29,10 +29,10 @@ board.on("ready", function() { facing = ""; // Scanning range (degrees) - range = [ 0, 170 ]; + range = [0, 170]; // Servo center point (degrees) - center = range[ 1 ] / 2; + center = range[1] / 2; // Redirection map redirect = { @@ -59,8 +59,14 @@ board.on("ready", function() { }); servos = { - right: new five.Servo({ pin: 10, type: "continuous" }), - left: new five.Servo({ pin: 11, type: "continuous" }) + right: new five.Servo({ + pin: 10, + type: "continuous" + }), + left: new five.Servo({ + pin: 11, + type: "continuous" + }) }; // Initialize the scanner at it's center point @@ -72,7 +78,7 @@ board.on("ready", function() { servos.left.move(90); // Scanner/Panning loop - this.loop( 100, function() { + this.loop(100, function() { var bounds; bounds = { @@ -82,9 +88,9 @@ board.on("ready", function() { // During course change, scanning is paused to avoid // overeager redirect instructions[1] - if ( isScanning ) { + if (isScanning) { // Calculate the next step position - if ( degrees >= scanner.range[1] || degrees === scanner.range[0] ){ + if (degrees >= scanner.range[1] || degrees === scanner.range[0]) { step *= -1; } @@ -94,38 +100,38 @@ board.on("ready", function() { // The following three conditions will help determine // which way the bot should turn if a potential collision // may occur in the sonar "change" event handler[2] - if ( degrees > bounds.left ) { + if (degrees > bounds.left) { facing = "left"; } - if ( degrees < bounds.right ) { + if (degrees < bounds.right) { facing = "right"; } - if ( degrees > bounds.right && degrees < bounds.left ) { + if (degrees > bounds.right && degrees < bounds.left) { facing = "forward"; } - scanner.move( degrees ); + scanner.move(degrees); } }); // [2] Sonar "change" events are emitted when the value of a // distance reading has changed since the previous reading // - sonar.on("change", function( err ) { + sonar.on("change", function(err) { var turnTo; // Detect collision - if ( Math.abs(this.inches) < collision && isScanning ) { + if (Math.abs(this.inches) < collision && isScanning) { // Scanning lock will prevent multiple collision detections // of the same obstacle isScanning = false; - turnTo = redirect[ facing ] || Object.keys( redirect )[ Date.now() % 2 ]; + turnTo = redirect[facing] || Object.keys(redirect)[Date.now() % 2]; // Log collision detection to REPL console.log( - [ Date.now(), + [Date.now(), "Collision detected " + this.inches + " inches away.", "Turning " + turnTo.toUpperCase() + " to avoid" ].join("\n") @@ -136,8 +142,8 @@ board.on("ready", function() { // [1] Allow 1000ms to pass and release the scanning lock // by setting isScanning state to true. - board.wait( 1500, function() { - console.log( "Release Scanner Lock" ); + board.wait(1500, function() { + console.log("Release Scanner Lock"); isScanning = true; }); } diff --git a/docs/sonar.md b/docs/sonar.md index 96ba0e3bb..40ce0524d 100644 --- a/docs/sonar.md +++ b/docs/sonar.md @@ -8,7 +8,7 @@ node eg/sonar.js ```javascript var five = require("johnny-five"), - board, sonar; + board, sonar; board = new five.Board(); @@ -47,14 +47,14 @@ board.on("ready", function() { this.cm - calculated distance, centimeters */ - console.log( "data", "Object is " + this.inches + "inches away" ); + console.log("data", "Object is " + this.inches + "inches away"); }); // // "change" fired when distance reading changes // sonar.on("change", function() { - console.log( "change", "Object is " + this.inches + "inches away" ); + console.log("change", "Object is " + this.inches + "inches away"); }); }); diff --git a/docs/stepper-driver.md b/docs/stepper-driver.md index 25e62e0d2..af32745af 100644 --- a/docs/stepper-driver.md +++ b/docs/stepper-driver.md @@ -22,12 +22,12 @@ board.on("ready", function() { * dir: 12 * } * }); - */ + */ var stepper = new five.Stepper({ type: five.Stepper.TYPE.DRIVER, stepsPerRev: 200, - pins: [ 11, 12 ] + pins: [11, 12] }); // Make 10 full revolutions counter-clockwise at 180 rpm with acceleration and deceleration @@ -37,7 +37,10 @@ board.on("ready", function() { // once first movement is done, make 10 revolutions clockwise at previously // defined speed, accel, and decel by passing an object into stepper.step - stepper.step({ steps: 2000, direction: five.Stepper.DIRECTION.CW }, function() { + stepper.step({ + steps: 2000, + direction: five.Stepper.DIRECTION.CW + }, function() { console.log("Done moving CW"); }); }); diff --git a/docs/stepper-sweep.md b/docs/stepper-sweep.md index a3844a2ea..43347e9b4 100644 --- a/docs/stepper-sweep.md +++ b/docs/stepper-sweep.md @@ -15,12 +15,12 @@ five.Board().on("ready", function() { stepper = new five.Stepper({ type: five.Stepper.TYPE.DRIVER, stepsPerRev: 200, - pins: [ 11, 12 ] + pins: [11, 12] }); function sweep() { // 200 stepsPerRev / 2 = 100 (180degree sweeps) - stepper[ ++k % 2 === 0 ? "ccw" : "cw" ]().step(100, function() { + stepper[++k % 2 === 0 ? "ccw" : "cw"]().step(100, function() { sweep(); }); } diff --git a/docs/tinkerkit-button.md b/docs/tinkerkit-button.md index 5cb4ced35..96e7421d3 100644 --- a/docs/tinkerkit-button.md +++ b/docs/tinkerkit-button.md @@ -15,9 +15,9 @@ new five.Board().on("ready", function() { // the button to an I* pin. var button = new five.Button("O5"); - [ "down", "up", "hold" ].forEach(function( type ) { - button.on( type, function() { - console.log( type ); + ["down", "up", "hold"].forEach(function(type) { + button.on(type, function() { + console.log(type); }); }); }); diff --git a/docs/tinkerkit-combo.md b/docs/tinkerkit-combo.md index 7cc978a4c..d10c9d759 100644 --- a/docs/tinkerkit-combo.md +++ b/docs/tinkerkit-combo.md @@ -14,7 +14,7 @@ new five.Board().on("ready", function() { accel = new five.Accelerometer({ id: "accelerometer", - pins: [ "I0", "I1" ] + pins: ["I0", "I1"] }); slider = new five.Sensor({ @@ -35,8 +35,8 @@ new five.Board().on("ready", function() { servos = new five.Servo.Array(); - slider.scale( 0, 180 ).on("change", function() { - servos.move( this.value ); + slider.scale(0, 180).on("change", function() { + servos.move(this.value); }); accel.on("acceleration", function() { diff --git a/docs/tinkerkit-continuous-servo.md b/docs/tinkerkit-continuous-servo.md index fb651c4c0..66749dc85 100644 --- a/docs/tinkerkit-continuous-servo.md +++ b/docs/tinkerkit-continuous-servo.md @@ -16,7 +16,7 @@ new five.Board().on("ready", function() { }); new five.Sensor("I0").scale(0, 1).on("change", function() { - servo.cw( this.value ); + servo.cw(this.value); }); }); diff --git a/docs/tinkerkit-gyroscope.md b/docs/tinkerkit-gyroscope.md index 053b779cb..4c4592af5 100644 --- a/docs/tinkerkit-gyroscope.md +++ b/docs/tinkerkit-gyroscope.md @@ -17,15 +17,16 @@ board.on("ready", function() { // Create a new `Gyroscope` hardware instance. gyro = new five.Gyroscope({ - pins: [ "I0", "I1" ], + pins: ["I0", "I1"], freq: 200, extent: 4 }); - gyro.on("acceleration", function( err, data ) { + gyro.on("acceleration", function(err, data) { console.log(data.position); }); }); + ``` diff --git a/docs/tinkerkit-joystick.md b/docs/tinkerkit-joystick.md index df0e4e610..567e8d6b8 100644 --- a/docs/tinkerkit-joystick.md +++ b/docs/tinkerkit-joystick.md @@ -8,7 +8,7 @@ node eg/tinkerkit-joystick.js ```javascript var five = require("johnny-five"), - Change = require("../eg/change.js"); + Change = require("../eg/change.js"); new five.Board().on("ready", function() { // var servo = new five.Servo("O0"); @@ -39,16 +39,16 @@ new five.Board().on("ready", function() { }; - [ "x", "y" ].forEach(function( axis ) { - joystick[ axis ].scale(1, 3).on("change", function() { - var round = Math.round( this.value ); + ["x", "y"].forEach(function(axis) { + joystick[axis].scale(1, 3).on("change", function() { + var round = Math.round(this.value); - if ( round !== 2 && changes[ axis ].isNoticeable( round ) ) { + if (round !== 2 && changes[axis].isNoticeable(round)) { console.log( - "%s changed noticeably (%d): %s", axis, round, dirs[ axis ][ round ] + "%s changed noticeably (%d): %s", axis, round, dirs[axis][round] ); } else { - changes[ axis ].last = round; + changes[axis].last = round; } }); }); diff --git a/docs/tinkerkit-linear-pot.md b/docs/tinkerkit-linear-pot.md index 18d967a34..89ff17eb3 100644 --- a/docs/tinkerkit-linear-pot.md +++ b/docs/tinkerkit-linear-pot.md @@ -11,7 +11,7 @@ var five = require("johnny-five"); new five.Board().on("ready", function() { new five.Sensor("I0").scale(0, 255).on("data", function() { - console.log( Math.round(this.value) ); + console.log(Math.round(this.value)); }); }); diff --git a/docs/tinkerkit-rotary.md b/docs/tinkerkit-rotary.md index 2e5f22167..8ba9901c9 100644 --- a/docs/tinkerkit-rotary.md +++ b/docs/tinkerkit-rotary.md @@ -13,7 +13,7 @@ new five.Board().on("ready", function() { var servo = new five.Servo("O0"); new five.Sensor("I1").scale(0, 180).on("change", function() { - servo.to( this.value ); + servo.to(this.value); }); }); diff --git a/docs/tinkerkit-thermistor.md b/docs/tinkerkit-thermistor.md index ee9a4e91b..b464cc200 100644 --- a/docs/tinkerkit-thermistor.md +++ b/docs/tinkerkit-thermistor.md @@ -7,7 +7,8 @@ node eg/tinkerkit-thermistor.js ```javascript -var five = require("johnny-five"), Thermistor; +var five = require("johnny-five"), + Thermistor; (function() { var adcres, beta, kelvin, rb, ginf; @@ -23,24 +24,24 @@ var five = require("johnny-five"), Thermistor; ginf = 120.6685; Thermistor = { - c: function( raw ) { + c: function(raw) { var rthermistor, tempc; rthermistor = rb * (adcres / raw - 1); - tempc = beta / ( Math.log( rthermistor * ginf ) ); + tempc = beta / (Math.log(rthermistor * ginf)); return tempc - kelvin; }, - f: function( raw ) { - return ( this.c(raw) * 9 ) / 5 + 32; + f: function(raw) { + return (this.c(raw) * 9) / 5 + 32; } }; }()); new five.Board().on("ready", function() { new five.Sensor("I0").on("change", function() { - console.log( "F: ", Thermistor.f(this.value) ); - console.log( "C: ", Thermistor.c(this.value) ); + console.log("F: ", Thermistor.f(this.value)); + console.log("C: ", Thermistor.c(this.value)); }); }); diff --git a/docs/tinkerkit-tilt.md b/docs/tinkerkit-tilt.md index acfd316dc..f110a988c 100644 --- a/docs/tinkerkit-tilt.md +++ b/docs/tinkerkit-tilt.md @@ -13,7 +13,7 @@ new five.Board().on("ready", function() { // var servo = new five.Servo("O0"); new five.Sensor("I2").on("change", function() { - console.log( this.boolean ); + console.log(this.boolean); }); }); diff --git a/docs/tinkerkit-touch.md b/docs/tinkerkit-touch.md index ff2e4fb9e..0a60c9548 100644 --- a/docs/tinkerkit-touch.md +++ b/docs/tinkerkit-touch.md @@ -18,9 +18,9 @@ new five.Board().on("ready", function() { // instance, like this: var touch = new five.Button("O5"); - [ "down", "up", "hold" ].forEach(function( type ) { - touch.on( type, function() { - console.log( type ); + ["down", "up", "hold"].forEach(function(type) { + touch.on(type, function() { + console.log(type); }); }); }); diff --git a/docs/whisker.md b/docs/whisker.md index 97ad9a9e0..23c9fcbee 100644 --- a/docs/whisker.md +++ b/docs/whisker.md @@ -12,7 +12,7 @@ var Change, five; Change = require("../eg/change.js"); five = require("johnny-five"); -new five.Boards([ "control", "nodebot" ]).on("ready", function(boards) { +new five.Boards(["control", "nodebot"]).on("ready", function(boards) { var controllers, changes, nodebot, whiskers, opposing, directions, speed; controllers = { @@ -69,9 +69,9 @@ new five.Boards([ "control", "nodebot" ]).on("ready", function(boards) { } }; - [ "left", "right" ].forEach(function( impact ) { - whiskers[ impact ].on("high", function() { - var turn = opposing[ impact ]; + ["left", "right"].forEach(function(impact) { + whiskers[impact].on("high", function() { + var turn = opposing[impact]; console.log( "%s impact, turning %s", @@ -79,39 +79,39 @@ new five.Boards([ "control", "nodebot" ]).on("ready", function(boards) { turn.toUpperCase() ); - nodebot.stop()[ turn ]( 500 ); + nodebot.stop()[turn](500); }); }); - [ "x", "y" ].forEach(function( axis ) { - controllers[ axis ].scale(1, 3).on("change", function() { - var round = Math.round( this.value ); + ["x", "y"].forEach(function(axis) { + controllers[axis].scale(1, 3).on("change", function() { + var round = Math.round(this.value); - if ( changes[ axis ].isNoticeable( round ) ) { - if ( round === 2 ) { + if (changes[axis].isNoticeable(round)) { + if (round === 2) { nodebot.stop(); } else { // console.log( axis, round, directions[ axis ][ round ] ); - nodebot[ directions[ axis ][ round ] ](); + nodebot[directions[axis][round]](); } } else { - changes[ axis ].last = round; + changes[axis].last = round; } }); }); controllers.speed.scale(0, 6).on("change", function() { - var value = Math.round( this.value ); + var value = Math.round(this.value); - if ( changes.speed.isNoticeable( value ) ) { + if (changes.speed.isNoticeable(value)) { // console.log( "update nodebot.speed: %d", value ); // console.log( nodebot.motion ); nodebot.speed = value; - if ( nodebot.motion !== "stop" ) { - nodebot[ nodebot.motion ](); + if (nodebot.motion !== "stop") { + nodebot[nodebot.motion](); } } });