Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

first stab at rgb led #74

Merged
merged 3 commits into from

4 participants

@reconbot
Collaborator

I like this! I'd add an inverted option as I've seen tri color LED's with a common anode as well as a common cathode which flips voltage the pin needs to be at for "full on"

@carldanley

Bump. Would also like to see this happen as I had to recreate this earlier this week without the RGB LED object.

@rwaldron rwaldron merged commit 1c66acc into rwaldron:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 16, 2012
  1. @rmurphey

    first stab at rgb led

    rmurphey authored
  2. @rmurphey

    fix quotes, delete dead file

    rmurphey authored
  3. @rmurphey
This page is out of date. Refresh to see the latest.
Showing with 74 additions and 0 deletions.
  1. +1 −0  lib/johnny-five.js
  2. +73 −0 lib/rgbled.js
View
1  lib/johnny-five.js
@@ -16,6 +16,7 @@
"IR",
"LCD",
"Led",
+ "RGBLed",
"Joystick",
"Motor",
"Nunchuk",
View
73 lib/rgbled.js
@@ -0,0 +1,73 @@
+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 );
+ }
+
+ if ( typeof color === 'string' ) {
+ red = hexToInt( color.slice(0, 2) );
+ green = hexToInt( color.slice(2, 4) );
+ blue = hexToInt( color.slice(4, 6) );
+ } else {
+ red = color[ 0 ];
+ green = color[ 1 ];
+ blue = color[ 2 ];
+ }
+
+ 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;
Something went wrong with that request. Please try again.