From a3c16a44f42620467ea2129fdc1da6d89814466d Mon Sep 17 00:00:00 2001 From: David Resseguie Date: Tue, 16 Sep 2014 21:56:51 -0400 Subject: [PATCH] Added LED examples --- eg/led-functions.js | 104 +++++++++++++++++++++++++++++++++++++++++ eg/led-rgb-keypress.js | 74 +++++++++++++++++++++++++++++ eg/led-rgb-rainbow.js | 30 ++++++++++++ eg/led-rgb.js | 36 ++------------ eg/led.js | 33 +++++++++++++ 5 files changed, 245 insertions(+), 32 deletions(-) create mode 100644 eg/led-functions.js create mode 100644 eg/led-rgb-keypress.js create mode 100644 eg/led-rgb-rainbow.js create mode 100644 eg/led.js diff --git a/eg/led-functions.js b/eg/led-functions.js new file mode 100644 index 0000000..65f4c41 --- /dev/null +++ b/eg/led-functions.js @@ -0,0 +1,104 @@ +var five = require("johnny-five"), + Spark = require("../lib/spark"), + temporal = require("temporal"), + board; + + +// Create Johnny-Five board connected via Spark +board = new five.Board({ + io: new Spark({ + token: process.env.SPARK_TOKEN, + deviceId: process.env.SPARK_DEVICE_ID + }) +}); + +board.on("ready", function() { + var led = new five.Led(process.argv[2] || "D0"); + + this.repl.inject({ + led: led + }); + + temporal.queue([{ + delay: 0, + task: function() { + // on() + // + // Turns the led on + led.on(); + console.log("led on"); + } + }, { + delay: 1000, + task: function() { + // off() + // + // Turns the led off + led.off(); + console.log("led off"); + } + }, { + delay: 3000, + task: function() { + // strobe() + // + // Strobe the led (on/off) + led.strobe(1000); + console.log("led strobe"); + } + }, { + delay: 5000, + task: function() { + // stop() + // + // Stop the pulse + led.stop(); + console.log("led stop"); + + // If you want to make sure it's off + // in case it stopped it while on + led.off(); + } + }, { + delay: 1000, + task: function() { + // fadeIn() + // + // Fade in the led + led.fadeIn(1000); + console.log("led fadeIn"); + } + }, { + delay: 3000, + task: function() { + // fadeOut() + // + // Fade out the led + led.fadeOut(1000); + console.log("led fadeOut"); + } + }, { + delay: 5000, + task: function() { + // brightness () + // + // set analog brightness (0-255) + led.brightness(100); + console.log("led brightness"); + + // Exit gracefully + process.exit(0); + } + } + ]); + + +}); + +// @markdown +// To make use of `Led` methods like `fade`, `pulse`, `animate`, you'll need to +// wire an LED to a PWM pin (A0, A1, A4, A5, A6, A7, D0 and D1). +// If you use a different pin, make sure to run the script with the correct pin number: +// +// `node eg/led.js [pinNumber]` +// @markdown diff --git a/eg/led-rgb-keypress.js b/eg/led-rgb-keypress.js new file mode 100644 index 0000000..c533e51 --- /dev/null +++ b/eg/led-rgb-keypress.js @@ -0,0 +1,74 @@ +var five = require("johnny-five"), + Spark = require("../lib/spark"), + keypress = require("keypress"), + board; + + +// Create Johnny-Five board connected via Spark +board = new five.Board({ + io: new Spark({ + token: process.env.SPARK_TOKEN, + deviceId: process.env.SPARK_DEVICE_ID + }) +}); + +// The board's pins will not be accessible until +// the board has reported that it is ready +board.on("ready", function() { + console.log("CONNECTED"); + + + // Initialize the RGB LED + var led = new five.Led.RGB({ + pins: { + red: "A5", + green: "A6", + blue: "A7" + } + }); + + // RGB LED alternate constructor + // This will normalize an array of pins in [r, g, b] + // order to an object (like above) that's shaped like: + // { + // red: r, + // green: g, + // blue: b + // } + //var led = new five.Led.RGB(["A5","A6","A7"]); + + // Turn it on and set the initial color + led.color("#FF0000"); + + // Listen for user input to change the RGB color + process.stdin.resume(); + process.stdin.setEncoding("utf8"); + process.stdin.setRawMode(true); + + var keymap = { + r: "#FF0000", // red + g: "#00FF00", // green + b: "#0000FF", // blue + w: "#FFFFFF" // white + }; + + process.stdin.on("keypress", function (ch, key) { + + if ( !key ) { + return; + } + + if (keymap[key.name]) { + led.color(keymap[key.name]); + console.log(" ", led.color()); + } else { + led.off(); + } + + }); + +}); + +board.on("error", function(error) { + console.log(error); +}); diff --git a/eg/led-rgb-rainbow.js b/eg/led-rgb-rainbow.js new file mode 100644 index 0000000..b5f352e --- /dev/null +++ b/eg/led-rgb-rainbow.js @@ -0,0 +1,30 @@ +var five = require("johnny-five"), + Spark = require("../lib/spark"), + board; + + +// Create Johnny-Five board connected via Spark +board = new five.Board({ + io: new Spark({ + token: process.env.SPARK_TOKEN, + deviceId: process.env.SPARK_DEVICE_ID + }) +}); + +board.on("ready", function() { + var rgb, rainbow, index; + + + // Initialize the RGB LED + rgb = new five.Led.RGB(["A5", "A6", "A7"]); + rainbow = ["FF000", "FF7F00", "00FF00", "FFFF00", "0000FF", "4B0082", "8F00FF"]; + index = 0; + + setInterval(function() { + if (index + 1 === rainbow.length) { + index = 0; + } + rgb.color(rainbow[index++]); + }, 1000); + +}); \ No newline at end of file diff --git a/eg/led-rgb.js b/eg/led-rgb.js index c0892fb..b22697d 100644 --- a/eg/led-rgb.js +++ b/eg/led-rgb.js @@ -1,6 +1,5 @@ var five = require("johnny-five"), Spark = require("../lib/spark"), - keypress = require('keypress'), board; @@ -19,7 +18,7 @@ board.on("ready", function() { // Initialize the RGB LED - var a = new five.Led.RGB({ + var led = new five.Led.RGB({ pins: { red: "A5", green: "A6", @@ -35,38 +34,11 @@ board.on("ready", function() { // green: g, // blue: b // } - //var a = new five.Led.RGB(["A5","A6","A7"]); + //var led = new five.Led.RGB(["A5","A6","A7"]); // Turn it on and set the initial color - a.on(); - a.color("#FF0000"); - - // Listen for user input to change the RGB color - process.stdin.resume(); - process.stdin.setEncoding('utf8'); - process.stdin.setRawMode(true); - - var keymap = { - r: "#FF0000", // red - g: "#00FF00", // green - b: "#0000FF", // blue - w: "#FFFFFF" // white - }; - - process.stdin.on('keypress', function (ch, key) { - - if ( !key ) { - return; - } - - if (keymap[key.name]) { - a.color(keymap[key.name]); - a.on(); - } else { - a.off(); - } - - }); + led.color("#FF0000"); + led.strobe(1000); }); diff --git a/eg/led.js b/eg/led.js new file mode 100644 index 0000000..bccdf6f --- /dev/null +++ b/eg/led.js @@ -0,0 +1,33 @@ +var five = require("johnny-five"), + Spark = require("../lib/spark"), + board; + + +// Create Johnny-Five board connected via Spark +board = new five.Board({ + io: new Spark({ + token: process.env.SPARK_TOKEN, + deviceId: process.env.SPARK_DEVICE_ID + }) +}); + +// The board's pins will not be accessible until +// the board has reported that it is ready +board.on("ready", function() { + console.log("CONNECTED"); + + + // Initialize the LED + var led = new five.Led("A5"); + + board.repl.inject({ + led: led + }); + + led.blink(1000); + +}); + +board.on("error", function(error) { + console.log(error); +});