Skip to content

Commit

Permalink
Merge pull request #821 from scottgonzalez/led-rgb-intensity-state
Browse files Browse the repository at this point in the history
Led.RGB: Add intensity method
  • Loading branch information
rwaldron committed Jul 3, 2015
2 parents a400f42 + c3fccf4 commit b52cf12
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lib/led/rgb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Board = require("../board.js");
var nanosleep = require("../sleep.js").nano;
var __ = require("../fn.js");

var priv = new Map();

Expand Down Expand Up @@ -201,10 +202,19 @@ var RGB = function(opts) {
red: 255,
green: 255,
blue: 255,
intensity: 100,
isAnode: opts.isAnode || false,
interval: null
};

// red, green, and blue store the raw color set via .color()
// values takes state into account, such as on/off and intensity
state.values = {
red: state.red,
green: state.green,
blue: state.blue
};

priv.set(this, state);

Object.defineProperties(this, {
Expand All @@ -227,17 +237,22 @@ var RGB = function(opts) {
},
values: {
get: function() {
return RGB.colors.reduce(function(current, color) {
return (current[color] = state[color], current);
}, {});
return Object.assign({}, state.values);
}
},
update: {
value: function(colors) {
var state = priv.get(this);
var scale = state.intensity / 100;

colors = colors || this.color();
var scaledColors = RGB.colors.reduce(function(current, color) {
return (current[color] = Math.round(colors[color] * scale), current);
}, {});

this.write(colors);
this.write(scaledColors);

state.values = scaledColors;
Object.assign(state, colors);
}
}
Expand Down Expand Up @@ -269,7 +284,7 @@ RGB.prototype.color = function(red, green, blue) {
// not a reference to the state object itself.
colors = this.isOn ? state : state.prev;
return RGB.colors.reduce(function(current, color) {
return (current[color] = colors[color], current);
return (current[color] = Math.round(colors[color]), current);
}, {});
}

Expand Down Expand Up @@ -332,8 +347,7 @@ RGB.prototype.color = function(red, green, blue) {
throw new Error("Led.RGB.color: invalid color ([" + [update.red, update.green, update.blue].join(",") + "])");
}

// constrain to [0,255]
value = Math.min(255, Math.max(0, value));
value = __.constrain(value, 0, 255);
update[color] = value;
}, this);

Expand Down Expand Up @@ -405,4 +419,18 @@ RGB.prototype.stop = function() {
return this;
};

RGB.prototype.intensity = function(intensity) {
var state = priv.get(this);

if (arguments.length === 0) {
return state.intensity;
}

state.intensity = __.constrain(intensity, 0, 100);

this.update();

return this;
};

module.exports = RGB;
46 changes: 46 additions & 0 deletions test/led.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,52 @@ exports["Led.RGB"] = {
blink: function(test) {
test.expect(1);
test.equal(this.ledRgb.blink, this.ledRgb.strobe);
test.done();
},

intensity: function(test) {
test.expect(24);

this.ledRgb.color("#33aa00");
test.equal(this.ledRgb.intensity(), 100);
this.write.reset();

// partial intensity
test.equal(this.ledRgb.intensity(20), this.ledRgb);
test.ok(this.write.calledOnce);
test.ok(this.write.calledWith({ red: 10, green: 34, blue: 0 }));
test.deepEqual(this.ledRgb.values, { red: 10, green: 34, blue: 0 });
test.deepEqual(this.ledRgb.color(), { red: 0x33, green: 0xaa, blue: 0x00 });
test.equal(this.ledRgb.intensity(), 20);
this.write.reset();

// change color
this.ledRgb.color("#ff0000");
test.ok(this.write.calledOnce);
test.ok(this.write.calledWith({ red: 51, green: 0, blue: 0 }));
test.deepEqual(this.ledRgb.values, { red: 51, green: 0, blue: 0 });
test.deepEqual(this.ledRgb.color(), { red: 0xff, green: 0x00, blue: 0x00 });
test.equal(this.ledRgb.intensity(), 20);
this.write.reset();

// fully off
test.equal(this.ledRgb.intensity(0), this.ledRgb);
test.ok(this.write.calledOnce);
test.ok(this.write.calledWith({ red: 0, green: 0, blue: 0 }));
test.deepEqual(this.ledRgb.values, { red: 0, green: 0, blue: 0 });
test.deepEqual(this.ledRgb.color(), { red: 0xff, green: 0x00, blue: 0x00 });
test.equal(this.ledRgb.intensity(), 0);
this.write.reset();

// restore from off
test.equal(this.ledRgb.intensity(50), this.ledRgb);
test.ok(this.write.calledOnce);
test.ok(this.write.calledWith({ red: 128, green: 0, blue: 0 }));
test.deepEqual(this.ledRgb.values, { red: 128, green: 0, blue: 0 });
test.deepEqual(this.ledRgb.color(), { red: 0xff, green: 0x00, blue: 0x00 });
test.equal(this.ledRgb.intensity(), 50);
this.write.reset();

test.done();
}
};
Expand Down

0 comments on commit b52cf12

Please sign in to comment.