Skip to content
Browse files

first stab at rgb led

  • Loading branch information...
1 parent 0d58187 commit 40f135901f7da1d5ada4cab9fe396ad409d455f1 @rmurphey committed Sep 16, 2012
Showing with 136 additions and 0 deletions.
  1. +1 −0 lib/johnny-five.js
  2. +68 −0 lib/rgb.js
  3. +67 −0 lib/rgbled.js
View
1 lib/johnny-five.js
@@ -16,6 +16,7 @@
"IR",
"LCD",
"Led",
+ "RGBLed",
"Joystick",
"Motor",
"Nunchuk",
View
68 lib/rgb.js
@@ -0,0 +1,68 @@
+// Derived and adapted from
+var Board = require("../lib/board.js"),
+ five = require("../lib/johnny-five.js"),
+ es6 = require("es6-collections"),
+ WeakMap = es6.WeakMap;
+
+// Led instance private data
+var priv = new WeakMap();
+var colors = [ 'red', 'green', 'blue' ];
+
+function RGBLed( opts ) {
+ if ( !(this instanceof RGBLed) ) {
+ return new RGBLed( opts );
+ }
+
+ opts = Board.options( opts );
+
+ this.board = Board.mount( opts );
+ this.firmata = this.board.firmata;
+
+ colors.forEach(function(color) {
+ this[ color ] = new five.Led( opts[ color ] );
+ }, this);
+
+ priv.set(this, {
+ red: 0,
+ green: 0,
+ blue: 0
+ });
+}
+
+RGBLed.prototype.setColor = function( color ) {
+ var red, green, blue;
+
+ function hexToInt( val ) {
+ return parseInt( val, 16 );
+ }
+
+ red = hexToInt( color.slice(0, 2) );
+ green = hexToInt( color.slice(2, 4) );
+ blue = hexToInt( color.slice(4, 6) );
+
+ priv.set( this, {
+ red: red,
+ green: green,
+ blue: blue
+ });
+
+ this.red.brightness( red );
+ this.green.brightness( green );
+ this.blue.brightness( blue );
+};
+
+RGBLed.prototype.on = function() {
+ var brightness = priv.get( this )
+ colors.forEach(function( color ) {
+ this[ color ].on();
+ this[ color ].brightness( brightness[ color ] );
+ }, this);
+};
+
+RGBLed.prototype.off = function() {
+ colors.forEach(function( color ) {
+ this[ color ].off();
+ });
+};
+
+module.exports = RGBLed;
View
67 lib/rgbled.js
@@ -0,0 +1,67 @@
+var Board = require("../lib/board.js"),
+ five = require("../lib/johnny-five.js"),
+ es6 = require("es6-collections"),
+ WeakMap = es6.WeakMap;
+
+// Led instance private data
+var priv = new WeakMap();
+var colors = [ 'red', 'green', 'blue' ];
+
+function RGBLed( opts ) {
+ if ( !(this instanceof RGBLed) ) {
+ return new RGBLed( opts );
+ }
+
+ opts = Board.options( opts );
+
+ this.board = Board.mount( opts );
+ this.firmata = this.board.firmata;
+
+ colors.forEach(function(color) {
+ this[ color ] = new five.Led( opts[ color ] );
+ }, this);
+
+ priv.set(this, {
+ red: 0,
+ green: 0,
+ blue: 0
+ });
+}
+
+RGBLed.prototype.setColor = function( color ) {
+ var red, green, blue;
+
+ function hexToInt( val ) {
+ return parseInt( val, 16 );
+ }
+
+ red = hexToInt( color.slice(0, 2) );
+ green = hexToInt( color.slice(2, 4) );
+ blue = hexToInt( color.slice(4, 6) );
+
+ priv.set( this, {
+ red: red,
+ green: green,
+ blue: blue
+ });
+
+ this.red.brightness( red );
+ this.green.brightness( green );
+ this.blue.brightness( blue );
+};
+
+RGBLed.prototype.on = function() {
+ var brightness = priv.get( this )
+ colors.forEach(function( color ) {
+ this[ color ].on();
+ this[ color ].brightness( brightness[ color ] );
+ }, this);
+};
+
+RGBLed.prototype.off = function() {
+ colors.forEach(function( color ) {
+ this[ color ].off();
+ }, this);
+};
+
+module.exports = RGBLed;

0 comments on commit 40f1359

Please sign in to comment.
Something went wrong with that request. Please try again.