From ca68b6e80a76bbc1cf2071c4dee3bba8c69e0fa4 Mon Sep 17 00:00:00 2001 From: Donovan Buck Date: Wed, 21 Aug 2019 16:03:04 -0500 Subject: [PATCH] Update Switch examples --- README.md | 1 + docs/switch-magnetic-door.md | 18 +++++-------- docs/switch-tilt-SW_200D.md | 21 +++++---------- docs/switch.md | 51 ++++++++++++++++++++++++++++++++++++ eg/switch-magnetic-door.js | 18 +++++-------- eg/switch-tilt-SW_200D.js | 21 +++++---------- eg/switch.js | 21 ++++++--------- tpl/programs.json | 4 +++ 8 files changed, 90 insertions(+), 65 deletions(-) create mode 100644 docs/switch.md diff --git a/README.md b/README.md index 3252f5846..bf2a5da96 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,7 @@ To interactively navigate the examples, visit the [Johnny-Five examples](http:// - [Button - Options](https://github.com/rwaldron/johnny-five/blob/master/docs/button-options.md) - [Button - Pullup](https://github.com/rwaldron/johnny-five/blob/master/docs/button-pullup.md) - [Buttons - Collection w/ AT42QT1070](https://github.com/rwaldron/johnny-five/blob/master/docs/button-collection-AT42QT1070.md) +- [Switch](https://github.com/rwaldron/johnny-five/blob/master/docs/switch.md) - [Switch - Magnetic Door](https://github.com/rwaldron/johnny-five/blob/master/docs/switch-magnetic-door.md) - [Switch - Tilt SW-200D](https://github.com/rwaldron/johnny-five/blob/master/docs/switch-tilt-SW_200D.md) - [Toggle Switch](https://github.com/rwaldron/johnny-five/blob/master/docs/toggle-switch.md) diff --git a/docs/switch-magnetic-door.md b/docs/switch-magnetic-door.md index 52b56fd2e..58580738f 100644 --- a/docs/switch-magnetic-door.md +++ b/docs/switch-magnetic-door.md @@ -29,20 +29,14 @@ node eg/switch-magnetic-door.js ```javascript -var five = require("johnny-five"); -var board = new five.Board(); +const {Board, Switch} = require("johnny-five"); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { // Contact Mode: Normally Open (default!) - var sw = new five.Switch(7); - - sw.on("open", function() { - console.log("open"); - }); - - sw.on("close", function() { - console.log("close"); - }); + const sw = new Switch(7); + sw.on("open", () => console.log("open")); + sw.on("close", () => console.log("close")); }); ``` diff --git a/docs/switch-tilt-SW_200D.md b/docs/switch-tilt-SW_200D.md index c2a753f75..0e760ec4b 100644 --- a/docs/switch-tilt-SW_200D.md +++ b/docs/switch-tilt-SW_200D.md @@ -29,31 +29,24 @@ node eg/switch-tilt-SW_200D.js ```javascript -var five = require("johnny-five"); -var board = new five.Board(); -var tilt; +const {Board, Button} = require("johnny-five"); +const board = new Board(); -board.on("ready", function() { - tilt = new five.Button(2); // digital pin 2 +board.on("ready", () => { + const tilt = new Button(2); // digital pin 2 board.repl.inject({ button: tilt }); // tilt the breadboard to the right, towards to the ground pin - tilt.on("down", function() { - console.log("down"); - }); + tilt.on("down", () => console.log("down")); // tilt and hold - tilt.on("hold", function() { - console.log("hold"); - }); + tilt.on("hold", () => console.log("hold")); // tilt back the breadboard to the stable position - tilt.on("up", function() { - console.log("up"); - }); + tilt.on("up", () => console.log("up")); }); ``` diff --git a/docs/switch.md b/docs/switch.md new file mode 100644 index 000000000..3c49dc255 --- /dev/null +++ b/docs/switch.md @@ -0,0 +1,51 @@ + + +# Switch + + + + + + + + + + +Run this example from the command line with: +```bash +node eg/switch.js +``` + + +```javascript +const {Board, Led, Switch} = require("johnny-five"); +const board = new Board(); + +board.on("ready", () => { + const spdt = new Switch(8); + const led = new Led(13); + + spdt.on("open", () => led.off()); + spdt.on("close", () => led.on()); +}); + +``` + + + + + + + + +  + + + +## License +Copyright (c) 2012-2014 Rick Waldron +Licensed under the MIT license. +Copyright (c) 2015-2019 The Johnny-Five Contributors +Licensed under the MIT license. + + diff --git a/eg/switch-magnetic-door.js b/eg/switch-magnetic-door.js index cea801eae..211e4a0b1 100644 --- a/eg/switch-magnetic-door.js +++ b/eg/switch-magnetic-door.js @@ -1,15 +1,9 @@ -var five = require("../lib/johnny-five.js"); -var board = new five.Board(); +const {Board, Switch} = require("../lib/johnny-five.js"); +const board = new Board(); -board.on("ready", function() { +board.on("ready", () => { // Contact Mode: Normally Open (default!) - var sw = new five.Switch(7); - - sw.on("open", function() { - console.log("open"); - }); - - sw.on("close", function() { - console.log("close"); - }); + const sw = new Switch(7); + sw.on("open", () => console.log("open")); + sw.on("close", () => console.log("close")); }); diff --git a/eg/switch-tilt-SW_200D.js b/eg/switch-tilt-SW_200D.js index c32747814..0ea0c013d 100644 --- a/eg/switch-tilt-SW_200D.js +++ b/eg/switch-tilt-SW_200D.js @@ -1,26 +1,19 @@ -var five = require("../lib/johnny-five.js"); -var board = new five.Board(); -var tilt; +const {Board, Button} = require("../lib/johnny-five.js"); +const board = new Board(); -board.on("ready", function() { - tilt = new five.Button(2); // digital pin 2 +board.on("ready", () => { + const tilt = new Button(2); // digital pin 2 board.repl.inject({ button: tilt }); // tilt the breadboard to the right, towards to the ground pin - tilt.on("down", function() { - console.log("down"); - }); + tilt.on("down", () => console.log("down")); // tilt and hold - tilt.on("hold", function() { - console.log("hold"); - }); + tilt.on("hold", () => console.log("hold")); // tilt back the breadboard to the stable position - tilt.on("up", function() { - console.log("up"); - }); + tilt.on("up", () => console.log("up")); }); diff --git a/eg/switch.js b/eg/switch.js index 213051d11..8f05c3850 100644 --- a/eg/switch.js +++ b/eg/switch.js @@ -1,15 +1,10 @@ -var five = require("../lib/johnny-five.js"); -var board = new five.Board(); +const {Board, Led, Switch} = require("johnny-five.js"); +const board = new Board(); -board.on("ready", function() { - var spdt = new five.Switch(8); - var led = new five.Led(13); - - spdt.on("open", function() { - led.off(); - }); - - spdt.on("close", function() { - led.on(); - }); +board.on("ready", () => { + const spdt = new Switch(8); + const led = new Led(13); + + spdt.on("open", () => led.off()); + spdt.on("close", () => led.on()); }); diff --git a/tpl/programs.json b/tpl/programs.json index c151b1f96..2086a7a0e 100644 --- a/tpl/programs.json +++ b/tpl/programs.json @@ -564,6 +564,10 @@ { "file": "switch-tilt-SW_200D.js", "title": "Switch - Tilt SW-200D" + }, + { + "file": "switch.js", + "title": "Switch" } ] },