Skip to content

Commit

Permalink
Switch to use expander class for PCA9685 in LED/RGB
Browse files Browse the repository at this point in the history
  • Loading branch information
dtex committed Oct 22, 2015
1 parent 8341b85 commit b734a6d
Showing 1 changed file with 15 additions and 42 deletions.
57 changes: 15 additions & 42 deletions lib/led/rgb.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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: {
Expand All @@ -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);
}
}
Expand Down

0 comments on commit b734a6d

Please sign in to comment.