From 2ce050520fec966a09dd60a49f1d99368820be74 Mon Sep 17 00:00:00 2001 From: Donovan Buck Date: Tue, 20 Aug 2019 19:15:34 -0500 Subject: [PATCH] Examples: Update Servo Syntax --- docs/servo-PCA9685.md | 24 +++++++++--------- docs/servo-animation-leg.md | 41 ++++++++++++++----------------- docs/servo-animation.md | 14 +++++------ docs/servo-array.md | 14 +++++------ docs/servo-continuous.md | 12 ++++----- docs/servo-drive.md | 26 ++++++++++---------- docs/servo-multi-turn.md | 12 ++++----- docs/servo-prompt.md | 18 +++++++------- docs/servo-slider.md | 16 ++++++------ docs/servo-sweep.md | 20 +++++++-------- docs/servo-tessel-servo-module.md | 10 ++++---- docs/servo.md | 12 ++++----- eg/servo-PCA9685.js | 24 +++++++++--------- eg/servo-animation-leg.js | 41 ++++++++++++++----------------- eg/servo-animation.js | 14 +++++------ eg/servo-array.js | 14 +++++------ eg/servo-continuous.js | 12 ++++----- eg/servo-diagnostic.js | 36 ++++++++++++--------------- eg/servo-drive.js | 26 ++++++++++---------- eg/servo-multi-turn.js | 12 ++++----- eg/servo-prompt.js | 18 +++++++------- eg/servo-slider.js | 16 ++++++------ eg/servo-sweep.js | 20 +++++++-------- eg/servo-tessel-servo-module.js | 10 ++++---- eg/servo.js | 12 ++++----- 25 files changed, 226 insertions(+), 248 deletions(-) diff --git a/docs/servo-PCA9685.md b/docs/servo-PCA9685.md index 5e5f11619..a5afd7b59 100644 --- a/docs/servo-PCA9685.md +++ b/docs/servo-PCA9685.md @@ -29,30 +29,28 @@ node eg/servo-PCA9685.js ```javascript -var five = require("johnny-five"); -var board = new five.Board(); +const {Board, Servo} = require("johnny-five"); +const board = new Board(); +const controller = "PCA9685"; -board.on("ready", function() { +board.on("ready", () => { console.log("Connected"); // Initialize the servo instance - var a = new five.Servo({ - address: 0x40, - controller: "PCA9685", + const a = new Servo({ + controller, pin: 0, }); - var b = new five.Servo({ - address: 0x40, - controller: "PCA9685", + const b = new Servo({ + controller, range: [0, 180], pin: 1, }); - var degrees = 0; - - a.to(degrees); - b.to(degrees); + a.to(0); + b.to(0); + }); ``` diff --git a/docs/servo-animation-leg.md b/docs/servo-animation-leg.md index 2987b1ae2..24ada94a4 100644 --- a/docs/servo-animation-leg.md +++ b/docs/servo-animation-leg.md @@ -31,12 +31,14 @@ node eg/servo-animation-leg.js ```javascript -var five = require("johnny-five"), - ph = { - state: "sleep" - }; +const {Animation, Board, Servo, Servos} = require("johnny-five"); +let ph = { + state: "sleep" +}; + +const board = new Board(); -var board = new five.Board().on("ready", function() { +board.on("ready", () => { /** * This animation controls three servos @@ -44,21 +46,21 @@ var board = new five.Board().on("ready", function() { * leg on a hexapod. A full hexapod might need 18 * servo instances (assuming 3 degrees of freedom) */ - ph.coxa = new five.Servo({ + ph.coxa = new Servo({ pin: 9, startAt: 45 }); - ph.femur = new five.Servo({ + ph.femur = new Servo({ pin: 10, startAt: 180 }); - ph.tibia = new five.Servo({ + ph.tibia = new Servo({ pin: 11, startAt: 180 }); // Create a Servos instance for those leg parts - ph.leg = new five.Servos([ph.coxa, ph.femur, ph.tibia]); + ph.leg = new Servos([ph.coxa, ph.femur, ph.tibia]); /** * Create an Animation(target) object. A newly initialized @@ -66,7 +68,7 @@ var board = new five.Board().on("ready", function() { * animation segments that will run asynchronously. * @param {target} A Servo or Servos instance to be animated */ - var legAnimation = new five.Animation(ph.leg); + const legAnimation = new Animation(ph.leg); /** * This object describes an animation segment and is passed into @@ -74,10 +76,10 @@ var board = new five.Board().on("ready", function() { * property is keyFrames. See the Animation wiki page for a full * list of available properties */ - var sleep = { + const sleep = { duration: 500, cuePoints: [0, 0.5, 1.0], - oncomplete: function() { + oncomplete() { ph.state = "sleep"; }, keyFrames: [ @@ -90,11 +92,11 @@ var board = new five.Board().on("ready", function() { /** * Another animation segment */ - var stand = { + const stand = { duration: 500, loop: false, cuePoints: [0, 0.1, 0.3, 0.7, 1.0], - oncomplete: function() { + oncomplete() { ph.state = "stand"; }, keyFrames: [ @@ -105,19 +107,14 @@ var board = new five.Board().on("ready", function() { }; // Functions we can call from the REPL - ph.sleep = function() { - legAnimation.enqueue(sleep); - }; - - ph.stand = function() { - legAnimation.enqueue(stand); - }; + ph.sleep = () => legAnimation.enqueue(sleep); + ph.stand = () => legAnimation.enqueue(stand); // Inject the `servo` hardware into; // the Repl instance's context; // allows direct command line access board.repl.inject({ - ph: ph + ph }); console.log("Try running ph.stand() or ph.sleep()"); diff --git a/docs/servo-animation.md b/docs/servo-animation.md index 3393882a6..35855ed2a 100644 --- a/docs/servo-animation.md +++ b/docs/servo-animation.md @@ -31,16 +31,16 @@ node eg/servo-animation.js ```javascript -var five = require("johnny-five"); -var board = new five.Board(); +const {Animation, Board, Servo} = require("johnny-five"); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { // Create a new `servo` hardware instance. - var servo = new five.Servo(10); + const servo = new Servo(10); // Create a new `animation` instance. - var animation = new five.Animation(servo); + const animation = new Animation(servo); // Enqueue an animation segment with options param // See Animation example and docs for details @@ -54,8 +54,8 @@ board.on("ready", function() { // the Repl instance's context; // allows direct command line access board.repl.inject({ - servo: servo, - animation: animation + servo, + animation }); }); diff --git a/docs/servo-array.md b/docs/servo-array.md index a99ae04e1..50495dfbb 100644 --- a/docs/servo-array.md +++ b/docs/servo-array.md @@ -31,23 +31,21 @@ node eg/servo-array.js ```javascript -var five = require("johnny-five"); -var board = new five.Board(); +const {Board, Servos} = require("johnny-five"); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { // Initialize a Servo collection - var servos = new five.Servos([9, 10]); - + const servos = new Servos([9, 10]); servos.center(); - // Inject the `servo` hardware into // the Repl instance's context; // allows direct command line access - this.repl.inject({ - servos: servos + board.repl.inject({ + servos }); diff --git a/docs/servo-continuous.md b/docs/servo-continuous.md index 66fdd75b1..f4da3d081 100644 --- a/docs/servo-continuous.md +++ b/docs/servo-continuous.md @@ -31,24 +31,24 @@ node eg/servo-continuous.js ```javascript -var five = require("johnny-five"); -var keypress = require("keypress"); +const {Board, Servo} = require("johnny-five"); +const keypress = require("keypress"); keypress(process.stdin); -var board = new five.Board(); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { console.log("Use Up and Down arrows for CW and CCW respectively. Space to stop."); - var servo = new five.Servo.Continuous(10); + const servo = new Servo.Continuous(10); process.stdin.resume(); process.stdin.setEncoding("utf8"); process.stdin.setRawMode(true); - process.stdin.on("keypress", function(ch, key) { + process.stdin.on("keypress", (ch, key) => { if (!key) { return; diff --git a/docs/servo-drive.md b/docs/servo-drive.md index 6328b8128..5c42b0758 100644 --- a/docs/servo-drive.md +++ b/docs/servo-drive.md @@ -31,17 +31,16 @@ node eg/servo-drive.js ```javascript -var five = require("johnny-five"), - board, wheels; +const {Board, Servo, Servos} = require("johnny-five"); -board = new five.Board(); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { - wheels = {}; + let wheels = {}; // Create two servos as our wheels - wheels.left = new five.Servo({ + wheels.left = new Servo({ pin: 9, // `type` defaults to standard servo. // For continuous rotation servos, override the default @@ -50,7 +49,7 @@ board.on("ready", function() { }); - wheels.right = new five.Servo({ + wheels.right = new Servo({ pin: 10, // `type` defaults to standard servo. // For continuous rotation servos, override the default @@ -59,11 +58,14 @@ board.on("ready", function() { invert: true // one wheel mounted inverted of the other }); - wheels.both = new five.Servos().stop(); // reference both together + // reference both together + wheels.both = new Servos([wheels.left, wheels.right]); + + wheels.stop(); // Add servos to REPL (optional) - this.repl.inject({ - wheels: wheels + board.repl.inject({ + wheels }); // Drive forwards @@ -72,9 +74,7 @@ board.on("ready", function() { wheels.both.cw(); // Stop driving after 3 seconds - this.wait(3000, function() { - wheels.both.stop(); - }); + board.wait(3000, wheels.both.stop); }); diff --git a/docs/servo-multi-turn.md b/docs/servo-multi-turn.md index 27d06d0eb..81d7a542c 100644 --- a/docs/servo-multi-turn.md +++ b/docs/servo-multi-turn.md @@ -18,11 +18,11 @@ node eg/servo-multi-turn.js ```javascript -var five = require("johnny-five"); -var board = new five.Board(); +const {Board, Servo} = require("johnny-five"); +const board = new Board(); -board.on("ready", function() { - var servo = new five.Servo({ +board.on("ready", () => { + const servo = new Servo({ pin: 10, // Cheapo servo has limited PWM range pwmRange: [600, 2370], @@ -31,8 +31,8 @@ board.on("ready", function() { }); // Add servo to REPL (optional) - this.repl.inject({ - servo: servo + board.repl.inject({ + servo }); diff --git a/docs/servo-prompt.md b/docs/servo-prompt.md index 2340465e4..b9dd23f33 100644 --- a/docs/servo-prompt.md +++ b/docs/servo-prompt.md @@ -31,26 +31,26 @@ node eg/servo-prompt.js ```javascript -var five = require("johnny-five"); -var readline = require("readline"); +const {Board, Servo} = require("johnny-five"); +const readline = require("readline"); -var rl = readline.createInterface({ +const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); -five.Board().on("ready", function() { - var servo = new five.Servo(10); +const board = new Board(); + +board.on("ready", () => { + const servo = new Servo(10); rl.setPrompt("SERVO TEST (0-180)> "); rl.prompt(); - rl.on("line", function(line) { + rl.on("line", (line) => { servo.to(+line.trim()); rl.prompt(); - }).on("close", function() { - process.exit(0); - }); + }).on("close", () => process.exit(0)); }); ``` diff --git a/docs/servo-slider.md b/docs/servo-slider.md index c584abc08..e3d8fbabd 100644 --- a/docs/servo-slider.md +++ b/docs/servo-slider.md @@ -29,18 +29,16 @@ node eg/servo-slider.js ```javascript -var five = require("johnny-five"); -var board = new five.Board(); +const {Board, Sensor, Servo} = require("johnny-five"); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { - var slider = new five.Sensor("A0"); - var tilt = new five.Servo(9); + const slider = new Sensor("A0"); + const tilt = new Servo(9); - slider.scale([0, 180]).on("slide", function() { - - // The slider's value will be scaled to match the tilt servo range - tilt.to(this.value); + slider.on("slide", () => { + tilt.to(slider.scaleTo(0, 180)); }); }); diff --git a/docs/servo-sweep.md b/docs/servo-sweep.md index a7dd41a2f..473c9a747 100644 --- a/docs/servo-sweep.md +++ b/docs/servo-sweep.md @@ -31,35 +31,35 @@ node eg/servo-sweep.js ```javascript -var five = require("johnny-five"), - board = new five.Board(); +const {Board, Servo} = require("johnny-five"); +const board = new Board(); -board.on("ready", function() { - var servo = new five.Servo({ +board.on("ready", () => { + const servo = new Servo({ pin: 10, startAt: 90 }); - var lap = 0; + let lap = 0; - servo.sweep().on("sweep:full", function() { - console.log("lap", ++lap); + servo.sweep().on("sweep:full", () => { + console.log(`lap ${++lap}`); if (lap === 1) { - this.sweep({ + servo.sweep({ range: [40, 140], step: 10 }); } if (lap === 2) { - this.sweep({ + servo.sweep({ range: [60, 120], step: 5 }); } if (lap === 3) { - this.sweep({ + servo.sweep({ range: [80, 100], step: 1 }); diff --git a/docs/servo-tessel-servo-module.md b/docs/servo-tessel-servo-module.md index 11b0ddf63..dd132fad8 100644 --- a/docs/servo-tessel-servo-module.md +++ b/docs/servo-tessel-servo-module.md @@ -18,18 +18,18 @@ node eg/servo-tessel-servo-module.js ```javascript -var five = require("johnny-five"); -var Tessel = require("tessel-io"); +const {Board, Servo} = require("johnny-five"); +const Tessel = require("tessel-io"); -var board = new five.Board({ +const board = new Board({ io: new Tessel() }); -board.on("ready", function() { +board.on("ready", () => { console.log("Connected"); // Initialize the servo instance - var servo = new five.Servo({ + const servo = new Servo({ controller: "PCA9685", port: "A", address: 0x73, diff --git a/docs/servo.md b/docs/servo.md index 136147903..dbe2f3457 100644 --- a/docs/servo.md +++ b/docs/servo.md @@ -29,11 +29,11 @@ node eg/servo.js ```javascript -var five = require("johnny-five"); -var board = new five.Board(); +const {Board, Servo} = require("johnny-five"); +const board = new Board(); -board.on("ready", function() { - var servo = new five.Servo(10); +board.on("ready", () => { + const servo = new Servo(10); // Servo alternate constructor with options /* @@ -50,8 +50,8 @@ board.on("ready", function() { */ // Add servo to REPL (optional) - this.repl.inject({ - servo: servo + board.repl.inject({ + servo }); diff --git a/eg/servo-PCA9685.js b/eg/servo-PCA9685.js index c41bf4ac4..02bef76fc 100644 --- a/eg/servo-PCA9685.js +++ b/eg/servo-PCA9685.js @@ -1,25 +1,23 @@ -var five = require("../lib/johnny-five.js"); -var board = new five.Board(); +const {Board, Servo} = require("../lib/johnny-five.js"); +const board = new Board(); +const controller = "PCA9685"; -board.on("ready", function() { +board.on("ready", () => { console.log("Connected"); // Initialize the servo instance - var a = new five.Servo({ - address: 0x40, - controller: "PCA9685", + const a = new Servo({ + controller, pin: 0, }); - var b = new five.Servo({ - address: 0x40, - controller: "PCA9685", + const b = new Servo({ + controller, range: [0, 180], pin: 1, }); - var degrees = 0; - - a.to(degrees); - b.to(degrees); + a.to(0); + b.to(0); + }); diff --git a/eg/servo-animation-leg.js b/eg/servo-animation-leg.js index cd4b4f136..4a3c0b526 100644 --- a/eg/servo-animation-leg.js +++ b/eg/servo-animation-leg.js @@ -1,9 +1,11 @@ -var five = require("../lib/johnny-five.js"), - ph = { - state: "sleep" - }; +const {Animation, Board, Servo, Servos} = require("../lib/johnny-five.js"); +let ph = { + state: "sleep" +}; + +const board = new Board(); -var board = new five.Board().on("ready", function() { +board.on("ready", () => { /** * This animation controls three servos @@ -11,21 +13,21 @@ var board = new five.Board().on("ready", function() { * leg on a hexapod. A full hexapod might need 18 * servo instances (assuming 3 degrees of freedom) */ - ph.coxa = new five.Servo({ + ph.coxa = new Servo({ pin: 9, startAt: 45 }); - ph.femur = new five.Servo({ + ph.femur = new Servo({ pin: 10, startAt: 180 }); - ph.tibia = new five.Servo({ + ph.tibia = new Servo({ pin: 11, startAt: 180 }); // Create a Servos instance for those leg parts - ph.leg = new five.Servos([ph.coxa, ph.femur, ph.tibia]); + ph.leg = new Servos([ph.coxa, ph.femur, ph.tibia]); /** * Create an Animation(target) object. A newly initialized @@ -33,7 +35,7 @@ var board = new five.Board().on("ready", function() { * animation segments that will run asynchronously. * @param {target} A Servo or Servos instance to be animated */ - var legAnimation = new five.Animation(ph.leg); + const legAnimation = new Animation(ph.leg); /** * This object describes an animation segment and is passed into @@ -41,10 +43,10 @@ var board = new five.Board().on("ready", function() { * property is keyFrames. See the Animation wiki page for a full * list of available properties */ - var sleep = { + const sleep = { duration: 500, cuePoints: [0, 0.5, 1.0], - oncomplete: function() { + oncomplete() { ph.state = "sleep"; }, keyFrames: [ @@ -57,11 +59,11 @@ var board = new five.Board().on("ready", function() { /** * Another animation segment */ - var stand = { + const stand = { duration: 500, loop: false, cuePoints: [0, 0.1, 0.3, 0.7, 1.0], - oncomplete: function() { + oncomplete() { ph.state = "stand"; }, keyFrames: [ @@ -72,19 +74,14 @@ var board = new five.Board().on("ready", function() { }; // Functions we can call from the REPL - ph.sleep = function() { - legAnimation.enqueue(sleep); - }; - - ph.stand = function() { - legAnimation.enqueue(stand); - }; + ph.sleep = () => legAnimation.enqueue(sleep); + ph.stand = () => legAnimation.enqueue(stand); // Inject the `servo` hardware into; // the Repl instance's context; // allows direct command line access board.repl.inject({ - ph: ph + ph }); console.log("Try running ph.stand() or ph.sleep()"); diff --git a/eg/servo-animation.js b/eg/servo-animation.js index 33522a503..6eaf2c3d1 100644 --- a/eg/servo-animation.js +++ b/eg/servo-animation.js @@ -1,13 +1,13 @@ -var five = require("../lib/johnny-five.js"); -var board = new five.Board(); +const {Animation, Board, Servo} = require("../lib/johnny-five.js"); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { // Create a new `servo` hardware instance. - var servo = new five.Servo(10); + const servo = new Servo(10); // Create a new `animation` instance. - var animation = new five.Animation(servo); + const animation = new Animation(servo); // Enqueue an animation segment with options param // See Animation example and docs for details @@ -21,7 +21,7 @@ board.on("ready", function() { // the Repl instance's context; // allows direct command line access board.repl.inject({ - servo: servo, - animation: animation + servo, + animation }); }); diff --git a/eg/servo-array.js b/eg/servo-array.js index 41ccebfd2..4f75e4df5 100644 --- a/eg/servo-array.js +++ b/eg/servo-array.js @@ -1,20 +1,18 @@ -var five = require("../lib/johnny-five.js"); -var board = new five.Board(); +const {Board, Servos} = require("../lib/johnny-five.js"); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { // Initialize a Servo collection - var servos = new five.Servos([9, 10]); - + const servos = new Servos([9, 10]); servos.center(); - // Inject the `servo` hardware into // the Repl instance's context; // allows direct command line access - this.repl.inject({ - servos: servos + board.repl.inject({ + servos }); diff --git a/eg/servo-continuous.js b/eg/servo-continuous.js index 0292091f3..1c93444ec 100644 --- a/eg/servo-continuous.js +++ b/eg/servo-continuous.js @@ -1,21 +1,21 @@ -var five = require("../lib/johnny-five.js"); -var keypress = require("keypress"); +const {Board, Servo} = require("../lib/johnny-five.js"); +const keypress = require("keypress"); keypress(process.stdin); -var board = new five.Board(); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { console.log("Use Up and Down arrows for CW and CCW respectively. Space to stop."); - var servo = new five.Servo.Continuous(10); + const servo = new Servo.Continuous(10); process.stdin.resume(); process.stdin.setEncoding("utf8"); process.stdin.setRawMode(true); - process.stdin.on("keypress", function(ch, key) { + process.stdin.on("keypress", (ch, key) => { if (!key) { return; diff --git a/eg/servo-diagnostic.js b/eg/servo-diagnostic.js index 9f39153f9..a4bdce176 100644 --- a/eg/servo-diagnostic.js +++ b/eg/servo-diagnostic.js @@ -1,5 +1,4 @@ -var five = require("../lib/johnny-five.js"), - args, pins, ranges; +const {Board, Servo, Servos} = require("../lib/johnny-five.js"); /** * This program is useful for manual servo administration. @@ -18,15 +17,12 @@ var five = require("../lib/johnny-five.js"), * */ -args = process.argv.slice(2); +const args = process.argv.slice(2); +let pins = []; +let ranges = []; -pins = []; -ranges = []; - -args.forEach(function(val) { - var vals = val.split(":").map(function(v) { - return +v; - }); +args.forEach(val => { + const vals = val.split(":").map(Number); pins.push(vals[0]); @@ -36,23 +32,23 @@ args.forEach(function(val) { ); }); -(new five.Board()).on("ready", function() { - var servos; +const board = new Board(); +board.on("ready", () => { // With each provided pin number, create a servo instance - pins.forEach(function(pin, k) { - new five.Servo({ - pin: pin, + pins.forEach((pin, k) => { + new Servo({ + pin, range: ranges[k] }); - }, this); + }); - servos = new five.Servos(); + const servos = new Servos(); servos.center(); - // Inject a Servo Array into the REPL as "s" - this.repl.inject({ - s: servos + // Inject a Servo Array into the REPL + board.repl.inject({ + servos }); }); diff --git a/eg/servo-drive.js b/eg/servo-drive.js index 91520c083..63a35edb5 100644 --- a/eg/servo-drive.js +++ b/eg/servo-drive.js @@ -1,14 +1,13 @@ -var five = require("../lib/johnny-five.js"), - board, wheels; +const {Board, Servo, Servos} = require("../lib/johnny-five.js"); -board = new five.Board(); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { - wheels = {}; + let wheels = {}; // Create two servos as our wheels - wheels.left = new five.Servo({ + wheels.left = new Servo({ pin: 9, // `type` defaults to standard servo. // For continuous rotation servos, override the default @@ -17,7 +16,7 @@ board.on("ready", function() { }); - wheels.right = new five.Servo({ + wheels.right = new Servo({ pin: 10, // `type` defaults to standard servo. // For continuous rotation servos, override the default @@ -26,11 +25,14 @@ board.on("ready", function() { invert: true // one wheel mounted inverted of the other }); - wheels.both = new five.Servos().stop(); // reference both together + // reference both together + wheels.both = new Servos([wheels.left, wheels.right]); + + wheels.stop(); // Add servos to REPL (optional) - this.repl.inject({ - wheels: wheels + board.repl.inject({ + wheels }); // Drive forwards @@ -39,8 +41,6 @@ board.on("ready", function() { wheels.both.cw(); // Stop driving after 3 seconds - this.wait(3000, function() { - wheels.both.stop(); - }); + board.wait(3000, wheels.both.stop); }); diff --git a/eg/servo-multi-turn.js b/eg/servo-multi-turn.js index 6ae0da15e..56b3b5ee5 100644 --- a/eg/servo-multi-turn.js +++ b/eg/servo-multi-turn.js @@ -1,8 +1,8 @@ -var five = require("../lib/johnny-five.js"); -var board = new five.Board(); +const {Board, Servo} = require("../lib/johnny-five.js"); +const board = new Board(); -board.on("ready", function() { - var servo = new five.Servo({ +board.on("ready", () => { + const servo = new Servo({ pin: 10, // Cheapo servo has limited PWM range pwmRange: [600, 2370], @@ -11,8 +11,8 @@ board.on("ready", function() { }); // Add servo to REPL (optional) - this.repl.inject({ - servo: servo + board.repl.inject({ + servo }); diff --git a/eg/servo-prompt.js b/eg/servo-prompt.js index 2ef11d7c0..3af880531 100644 --- a/eg/servo-prompt.js +++ b/eg/servo-prompt.js @@ -1,21 +1,21 @@ -var five = require("../lib/johnny-five.js"); -var readline = require("readline"); +const {Board, Servo} = require("../lib/johnny-five.js"); +const readline = require("readline"); -var rl = readline.createInterface({ +const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); -five.Board().on("ready", function() { - var servo = new five.Servo(10); +const board = new Board(); + +board.on("ready", () => { + const servo = new Servo(10); rl.setPrompt("SERVO TEST (0-180)> "); rl.prompt(); - rl.on("line", function(line) { + rl.on("line", (line) => { servo.to(+line.trim()); rl.prompt(); - }).on("close", function() { - process.exit(0); - }); + }).on("close", () => process.exit(0)); }); diff --git a/eg/servo-slider.js b/eg/servo-slider.js index 468c9dfac..aea4e77ff 100644 --- a/eg/servo-slider.js +++ b/eg/servo-slider.js @@ -1,14 +1,12 @@ -var five = require("../lib/johnny-five.js"); -var board = new five.Board(); +const {Board, Sensor, Servo} = require("../lib/johnny-five.js"); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { - var slider = new five.Sensor("A0"); - var tilt = new five.Servo(9); + const slider = new Sensor("A0"); + const tilt = new Servo(9); - slider.scale([0, 180]).on("slide", function() { - - // The slider's value will be scaled to match the tilt servo range - tilt.to(this.value); + slider.on("slide", () => { + tilt.to(slider.scaleTo(0, 180)); }); }); diff --git a/eg/servo-sweep.js b/eg/servo-sweep.js index 02bb477c2..490b7eb3a 100644 --- a/eg/servo-sweep.js +++ b/eg/servo-sweep.js @@ -1,32 +1,32 @@ -var five = require("../lib/johnny-five.js"), - board = new five.Board(); +const {Board, Servo} = require("../lib/johnny-five.js"); +const board = new Board(); -board.on("ready", function() { - var servo = new five.Servo({ +board.on("ready", () => { + const servo = new Servo({ pin: 10, startAt: 90 }); - var lap = 0; + let lap = 0; - servo.sweep().on("sweep:full", function() { - console.log("lap", ++lap); + servo.sweep().on("sweep:full", () => { + console.log(`lap ${++lap}`); if (lap === 1) { - this.sweep({ + servo.sweep({ range: [40, 140], step: 10 }); } if (lap === 2) { - this.sweep({ + servo.sweep({ range: [60, 120], step: 5 }); } if (lap === 3) { - this.sweep({ + servo.sweep({ range: [80, 100], step: 1 }); diff --git a/eg/servo-tessel-servo-module.js b/eg/servo-tessel-servo-module.js index b30f53478..c57848a64 100644 --- a/eg/servo-tessel-servo-module.js +++ b/eg/servo-tessel-servo-module.js @@ -1,15 +1,15 @@ -var five = require("../lib/johnny-five.js"); -var Tessel = require("tessel-io"); +const {Board, Servo} = require("../lib/johnny-five.js"); +const Tessel = require("tessel-io"); -var board = new five.Board({ +const board = new Board({ io: new Tessel() }); -board.on("ready", function() { +board.on("ready", () => { console.log("Connected"); // Initialize the servo instance - var servo = new five.Servo({ + const servo = new Servo({ controller: "PCA9685", port: "A", address: 0x73, diff --git a/eg/servo.js b/eg/servo.js index 65c8084dc..8ee8a9bde 100644 --- a/eg/servo.js +++ b/eg/servo.js @@ -1,8 +1,8 @@ -var five = require("../lib/johnny-five.js"); -var board = new five.Board(); +const {Board, Servo} = require("../lib/johnny-five.js"); +const board = new Board(); -board.on("ready", function() { - var servo = new five.Servo(10); +board.on("ready", () => { + const servo = new Servo(10); // Servo alternate constructor with options /* @@ -19,8 +19,8 @@ board.on("ready", function() { */ // Add servo to REPL (optional) - this.repl.inject({ - servo: servo + board.repl.inject({ + servo });