From 1e9c148df2fd420dc06666192f862083f8285a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Scott=20Gonz=C3=A1lez?= Date: Tue, 26 May 2015 17:37:33 -0400 Subject: [PATCH] Led.RGB: Add BlinkM support Fixes #742 --- lib/led/rgb.js | 36 ++++++++++++++++++++++++++++++++++ test/led.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/lib/led/rgb.js b/lib/led/rgb.js index e2371cfcb..8d7a45adb 100644 --- a/lib/led/rgb.js +++ b/lib/led/rgb.js @@ -103,6 +103,42 @@ var Controllers = { this.io.i2cWrite(this.address, [this.COMMANDS.LED0_ON_L + 4 * pin, on, on >> 8, off, off >> 8]); }, this); + Object.assign(state, colors); + } + } + }, + BLINKM: { + COMMANDS: { + value: { + GO_TO_RGB_COLOR_NOW: 0x6e, + STOP_SCRIPT: 0x6f + } + }, + initialize: { + value: function(opts) { + this.address = opts.address || 0x09; + + if (!this.board.Drivers[this.address]) { + this.io.i2cConfig(); + this.board.Drivers[this.address] = { + initialized: false + }; + + // Stop the current script + this.board.io.i2cWrite(this.address, [this.COMMANDS.STOP_SCRIPT]); + + this.board.Drivers[this.address].initialized = true; + } + } + }, + write: { + writable: true, + value: function(colors) { + var state = priv.get(this); + + this.board.io.i2cWrite(this.address, + [this.COMMANDS.GO_TO_RGB_COLOR_NOW, colors.red, colors.green, colors.blue]); + Object.assign(state, colors); } } diff --git a/test/led.js b/test/led.js index 47bc28422..30fbfe100 100644 --- a/test/led.js +++ b/test/led.js @@ -955,6 +955,59 @@ exports["Led.RGB - PCA9685 (I2C)"] = { } }; +exports["Led.RGB - BlinkM (I2C)"] = { + setUp: function(done) { + this.board = newBoard(); + + this.ledRgb = new Led.RGB({ + controller: "BlinkM", + board: this.board + }); + + this.i2cWrite = sinon.spy(this.board.io, "i2cWrite"); + + done(); + }, + + shape: function(test) { + test.expect(rgbProtoProperties.length + rgbInstanceProperties.length); + + rgbProtoProperties.forEach(function(method) { + test.equal(typeof this.ledRgb[method.name], "function"); + }, this); + + rgbInstanceProperties.forEach(function(property) { + test.notEqual(typeof this.ledRgb[property.name], "undefined"); + }, this); + + test.done(); + }, + + write: function(test) { + test.expect(6); + + // Fully off + this.ledRgb.write({ red: 0x00, green: 0x00, blue: 0x00 }); + test.equal(this.i2cWrite.callCount, 1); + test.ok(this.i2cWrite.calledWith(0x09, [0x6e, 0x00, 0x00, 0x00])); + this.i2cWrite.reset(); + + // Fully on + this.ledRgb.write({ red: 0xff, green: 0xff, blue: 0xff }); + test.equal(this.i2cWrite.callCount, 1); + test.ok(this.i2cWrite.calledWith(0x09, [0x6e, 0xff, 0xff, 0xff])); + this.i2cWrite.reset(); + + // Custom color + this.ledRgb.write({ red: 0xbb, green: 0xcc, blue: 0xaa }); + test.equal(this.i2cWrite.callCount, 1); + test.ok(this.i2cWrite.calledWith(0x09, [0x6e, 0xbb, 0xcc, 0xaa])); + this.i2cWrite.reset(); + + test.done(); + } +}; + exports["Led - Default Pin w/ Firmata"] = { shape: function(test) { test.expect(8);