diff --git a/lib/led/rgb.js b/lib/led/rgb.js index 73bcb956b..4f494ee41 100644 --- a/lib/led/rgb.js +++ b/lib/led/rgb.js @@ -1,5 +1,5 @@ var Board = require("../board.js"); -var nanosleep = require("../sleep.js").nano; +var Expander = require("../../lib/expander"); var __ = require("../fn.js"); var priv = new Map(); @@ -43,43 +43,25 @@ var Controllers = { } }, PCA9685: { - REGISTER: { - value: { - PCA9685_MODE1: 0x0, - PCA9685_PRESCALE: 0xFE, - LED0_ON_L: 0x6 - } - }, initialize: { value: function(opts) { - this.address = opts.address || 0x40; - if (!this.board.Drivers[this.address]) { - this.io.i2cConfig(opts); - this.board.Drivers[this.address] = { - initialized: false - }; + var state = priv.get(this); - // Reset - this.io.i2cWrite(this.address, [this.REGISTER.PCA9685_MODE1, 0x0]); - // Sleep - this.io.i2cWrite(this.address, [this.REGISTER.PCA9685_MODE1, 0x10]); - // Set prescalar - this.io.i2cWrite(this.address, [this.REGISTER.PCA9685_PRESCALE, 0x70]); - // Wake up - this.io.i2cWrite(this.address, [this.REGISTER.PCA9685_MODE1, 0x0]); - // Wait 5 nanoseconds for restart - nanosleep(5); - // Auto-increment - this.io.i2cWrite(this.address, [this.REGISTER.PCA9685_MODE1, 0xa1]); + this.address = opts.address || 0x40; + this.pwmRange = opts.pwmRange || [0, 4095]; + + state.expander = Expander.get({ + address: this.address, + controller: this.controller, + bus: this.bus, + pwmRange: this.pwmRange, + frequency: 50, // Hz + }); - this.board.Drivers[this.address].initialized = true; - } + this.pin = typeof opts.pin === "undefined" ? 0 : opts.pin; - RGB.colors.forEach(function(color, index) { - var pin = opts.pins[index]; - this.pins[index] = pin; - }, this); + state.mode = this.io.MODES.PWM; } }, write: { @@ -90,22 +72,13 @@ var Controllers = { RGB.colors.forEach(function(color, index) { var pin = this.pins[index]; var value = colors[color]; - var on, off; if (state.isAnode) { value = 255 - Board.constrain(value, 0, 255); } - on = 0; - off = value * 4095 / 255; - - // Special value for fully off - if (state.isAnode && value === 255) { - on = 4096; - off = 0; - } + state.expander.analogWrite(pin, value); - this.io.i2cWrite(this.address, [this.REGISTER.LED0_ON_L + 4 * pin, on, on >> 8, off, off >> 8]); }, this); } }