Skip to content

Commit

Permalink
Updated PCA9685 in expander to use special values
Browse files Browse the repository at this point in the history
This is documented in the [PCA9658
Specification](http://www.nxp.com/documents/data_sheet/PCA9685.pdf) on
page 24

Special values were backwards
  • Loading branch information
dtex committed Oct 23, 2015
1 parent b734a6d commit 03d739f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 120 deletions.
15 changes: 14 additions & 1 deletion lib/expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,11 +691,24 @@ var Controllers = {

value = Board.constrain(value, 0, 255);

var on = 0;
var off = this.pwmRange[1] * value / 255;

if (value === 0) {
// Special value for signal fully off.
on = 0;
off = 4096;
}

if (value === 255) {
// Special value for signal fully on.
on = 4096;
off = 0;
}

this.io.i2cWrite(this.address, [
this.REGISTER.BASE + 4 * pin,
0, 0,
on, on >> 8,
off, off >> 8
]);

Expand Down
128 changes: 64 additions & 64 deletions test/expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -1706,22 +1706,22 @@ exports["Expander - PCA9685"] = {
test.equal(this.i2cWriteReg.callCount, 5);

test.deepEqual(this.i2cWrite.args, [
[ 64, [ 6, 0, 0, 0, 0 ] ],
[ 64, [ 10, 0, 0, 0, 0 ] ],
[ 64, [ 14, 0, 0, 0, 0 ] ],
[ 64, [ 18, 0, 0, 0, 0 ] ],
[ 64, [ 22, 0, 0, 0, 0 ] ],
[ 64, [ 26, 0, 0, 0, 0 ] ],
[ 64, [ 30, 0, 0, 0, 0 ] ],
[ 64, [ 34, 0, 0, 0, 0 ] ],
[ 64, [ 38, 0, 0, 0, 0 ] ],
[ 64, [ 42, 0, 0, 0, 0 ] ],
[ 64, [ 46, 0, 0, 0, 0 ] ],
[ 64, [ 50, 0, 0, 0, 0 ] ],
[ 64, [ 54, 0, 0, 0, 0 ] ],
[ 64, [ 58, 0, 0, 0, 0 ] ],
[ 64, [ 62, 0, 0, 0, 0 ] ],
[ 64, [ 66, 0, 0, 0, 0 ] ]
[ 64, [ 6, 0, 0, 4096, 16 ] ],
[ 64, [ 10, 0, 0, 4096, 16 ] ],
[ 64, [ 14, 0, 0, 4096, 16 ] ],
[ 64, [ 18, 0, 0, 4096, 16 ] ],
[ 64, [ 22, 0, 0, 4096, 16 ] ],
[ 64, [ 26, 0, 0, 4096, 16 ] ],
[ 64, [ 30, 0, 0, 4096, 16 ] ],
[ 64, [ 34, 0, 0, 4096, 16 ] ],
[ 64, [ 38, 0, 0, 4096, 16 ] ],
[ 64, [ 42, 0, 0, 4096, 16 ] ],
[ 64, [ 46, 0, 0, 4096, 16 ] ],
[ 64, [ 50, 0, 0, 4096, 16 ] ],
[ 64, [ 54, 0, 0, 4096, 16 ] ],
[ 64, [ 58, 0, 0, 4096, 16 ] ],
[ 64, [ 62, 0, 0, 4096, 16 ] ],
[ 64, [ 66, 0, 0, 4096, 16 ] ]
]);

test.done();
Expand Down Expand Up @@ -1768,22 +1768,22 @@ exports["Expander - PCA9685"] = {
}

var expects = [
[ 64, [ 6, 0, 0, 4095, 15 ] ],
[ 64, [ 10, 0, 0, 4095, 15 ] ],
[ 64, [ 14, 0, 0, 4095, 15 ] ],
[ 64, [ 18, 0, 0, 4095, 15 ] ],
[ 64, [ 22, 0, 0, 4095, 15 ] ],
[ 64, [ 26, 0, 0, 4095, 15 ] ],
[ 64, [ 30, 0, 0, 4095, 15 ] ],
[ 64, [ 34, 0, 0, 4095, 15 ] ],
[ 64, [ 38, 0, 0, 4095, 15 ] ],
[ 64, [ 42, 0, 0, 4095, 15 ] ],
[ 64, [ 46, 0, 0, 4095, 15 ] ],
[ 64, [ 50, 0, 0, 4095, 15 ] ],
[ 64, [ 54, 0, 0, 4095, 15 ] ],
[ 64, [ 58, 0, 0, 4095, 15 ] ],
[ 64, [ 62, 0, 0, 4095, 15 ] ],
[ 64, [ 66, 0, 0, 4095, 15 ] ],
[ 64, [ 6, 4096, 16, 0, 0 ] ],
[ 64, [ 10, 4096, 16, 0, 0 ] ],
[ 64, [ 14, 4096, 16, 0, 0 ] ],
[ 64, [ 18, 4096, 16, 0, 0 ] ],
[ 64, [ 22, 4096, 16, 0, 0 ] ],
[ 64, [ 26, 4096, 16, 0, 0 ] ],
[ 64, [ 30, 4096, 16, 0, 0 ] ],
[ 64, [ 34, 4096, 16, 0, 0 ] ],
[ 64, [ 38, 4096, 16, 0, 0 ] ],
[ 64, [ 42, 4096, 16, 0, 0 ] ],
[ 64, [ 46, 4096, 16, 0, 0 ] ],
[ 64, [ 50, 4096, 16, 0, 0 ] ],
[ 64, [ 54, 4096, 16, 0, 0 ] ],
[ 64, [ 58, 4096, 16, 0, 0 ] ],
[ 64, [ 62, 4096, 16, 0, 0 ] ],
[ 64, [ 66, 4096, 16, 0, 0 ] ],
];
test.deepEqual(this.i2cWrite.args, expects);

Expand All @@ -1800,22 +1800,22 @@ exports["Expander - PCA9685"] = {
}

var expects = [
[ 64, [ 6, 0, 0, 4095, 15 ] ],
[ 64, [ 10, 0, 0, 4095, 15 ] ],
[ 64, [ 14, 0, 0, 4095, 15 ] ],
[ 64, [ 18, 0, 0, 4095, 15 ] ],
[ 64, [ 22, 0, 0, 4095, 15 ] ],
[ 64, [ 26, 0, 0, 4095, 15 ] ],
[ 64, [ 30, 0, 0, 4095, 15 ] ],
[ 64, [ 34, 0, 0, 4095, 15 ] ],
[ 64, [ 38, 0, 0, 4095, 15 ] ],
[ 64, [ 42, 0, 0, 4095, 15 ] ],
[ 64, [ 46, 0, 0, 4095, 15 ] ],
[ 64, [ 50, 0, 0, 4095, 15 ] ],
[ 64, [ 54, 0, 0, 4095, 15 ] ],
[ 64, [ 58, 0, 0, 4095, 15 ] ],
[ 64, [ 62, 0, 0, 4095, 15 ] ],
[ 64, [ 66, 0, 0, 4095, 15 ] ],
[ 64, [ 6, 4096, 16, 0, 0 ] ],
[ 64, [ 10, 4096, 16, 0, 0 ] ],
[ 64, [ 14, 4096, 16, 0, 0 ] ],
[ 64, [ 18, 4096, 16, 0, 0 ] ],
[ 64, [ 22, 4096, 16, 0, 0 ] ],
[ 64, [ 26, 4096, 16, 0, 0 ] ],
[ 64, [ 30, 4096, 16, 0, 0 ] ],
[ 64, [ 34, 4096, 16, 0, 0 ] ],
[ 64, [ 38, 4096, 16, 0, 0 ] ],
[ 64, [ 42, 4096, 16, 0, 0 ] ],
[ 64, [ 46, 4096, 16, 0, 0 ] ],
[ 64, [ 50, 4096, 16, 0, 0 ] ],
[ 64, [ 54, 4096, 16, 0, 0 ] ],
[ 64, [ 58, 4096, 16, 0, 0 ] ],
[ 64, [ 62, 4096, 16, 0, 0 ] ],
[ 64, [ 66, 4096, 16, 0, 0 ] ],
];

test.deepEqual(this.i2cWrite.args, expects);
Expand Down Expand Up @@ -1866,22 +1866,22 @@ exports["Expander - PCA9685"] = {
}

var expects = [
[ 64, [ 6, 0, 0, 4095, 15 ] ],
[ 64, [ 10, 0, 0, 4095, 15 ] ],
[ 64, [ 14, 0, 0, 4095, 15 ] ],
[ 64, [ 18, 0, 0, 4095, 15 ] ],
[ 64, [ 22, 0, 0, 4095, 15 ] ],
[ 64, [ 26, 0, 0, 4095, 15 ] ],
[ 64, [ 30, 0, 0, 4095, 15 ] ],
[ 64, [ 34, 0, 0, 4095, 15 ] ],
[ 64, [ 38, 0, 0, 4095, 15 ] ],
[ 64, [ 42, 0, 0, 4095, 15 ] ],
[ 64, [ 46, 0, 0, 4095, 15 ] ],
[ 64, [ 50, 0, 0, 4095, 15 ] ],
[ 64, [ 54, 0, 0, 4095, 15 ] ],
[ 64, [ 58, 0, 0, 4095, 15 ] ],
[ 64, [ 62, 0, 0, 4095, 15 ] ],
[ 64, [ 66, 0, 0, 4095, 15 ] ],
[ 64, [ 6, 4096, 16, 0, 0 ] ],
[ 64, [ 10, 4096, 16, 0, 0 ] ],
[ 64, [ 14, 4096, 16, 0, 0 ] ],
[ 64, [ 18, 4096, 16, 0, 0 ] ],
[ 64, [ 22, 4096, 16, 0, 0 ] ],
[ 64, [ 26, 4096, 16, 0, 0 ] ],
[ 64, [ 30, 4096, 16, 0, 0 ] ],
[ 64, [ 34, 4096, 16, 0, 0 ] ],
[ 64, [ 38, 4096, 16, 0, 0 ] ],
[ 64, [ 42, 4096, 16, 0, 0 ] ],
[ 64, [ 46, 4096, 16, 0, 0 ] ],
[ 64, [ 50, 4096, 16, 0, 0 ] ],
[ 64, [ 54, 4096, 16, 0, 0 ] ],
[ 64, [ 58, 4096, 16, 0, 0 ] ],
[ 64, [ 62, 4096, 16, 0, 0 ] ],
[ 64, [ 66, 4096, 16, 0, 0 ] ],
];

test.deepEqual(this.i2cWrite.args, expects);
Expand Down
45 changes: 30 additions & 15 deletions test/led.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ exports["Led - PCA9685 (I2C)"] = {
this.i2cWrite.reset();
this.led.on();

test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 0, 0, 4095, 15]));
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 4096, 16, 0, 0]));
test.equal(this.i2cWrite.callCount, 1);

test.done();
Expand All @@ -438,7 +438,7 @@ exports["Led - PCA9685 (I2C)"] = {

this.i2cWrite.reset();
this.led.off();
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 0, 0, 0, 0]));
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 0, 0, 4096, 16]));
test.equal(this.i2cWrite.callCount, 1);

test.done();
Expand All @@ -451,11 +451,11 @@ exports["Led - PCA9685 (I2C)"] = {
this.i2cWrite.reset();

this.led.toggle();
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 0, 0, 4095, 15]));
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 4096, 16, 0, 0]));
test.ok(this.led.isOn);

this.led.toggle();
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 0, 0, 0, 0]));
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 0, 0, 4096, 16]));
test.ok(!this.led.isOn);

test.equal(this.i2cWrite.callCount, 2);
Expand All @@ -470,13 +470,13 @@ exports["Led - PCA9685 (I2C)"] = {
this.i2cWrite.reset();

this.led.brightness(255);
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 0, 0, 4095, 15]));
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 4096, 16, 0, 0]));

this.led.brightness(100);
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 0, 0, 4095 * 100 / 255, 6]));

this.led.brightness(0);
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 0, 0, 0, 0]));
test.ok(this.i2cWrite.lastCall.calledWith(64, [6, 0, 0, 4096, 16]));

test.equal(this.i2cWrite.callCount, 3);

Expand Down Expand Up @@ -973,6 +973,13 @@ exports["Led.RGB - PCA9685 (I2C)"] = {
done();
},

tearDown: function(done) {
Board.purge();
restore(this);
Expander.purge();
done();
},

shape: testLedRgbShape,

write: function(test) {
Expand All @@ -981,17 +988,17 @@ exports["Led.RGB - PCA9685 (I2C)"] = {
// Fully off
this.ledRgb.write({ red: 0x00, green: 0x00, blue: 0x00 });
test.equal(this.i2cWrite.callCount, 3);
test.ok(this.i2cWrite.calledWith(64, [6, 0, 0, 0, 0]));
test.ok(this.i2cWrite.calledWith(64, [10, 0, 0, 0, 0]));
test.ok(this.i2cWrite.calledWith(64, [14, 0, 0, 0, 0]));
test.ok(this.i2cWrite.calledWith(64, [6, 0, 0, 4096, 16]));
test.ok(this.i2cWrite.calledWith(64, [10, 0, 0, 4096, 16]));
test.ok(this.i2cWrite.calledWith(64, [14, 0, 0, 4096, 16]));
this.i2cWrite.reset();

// Fully on
this.ledRgb.write({ red: 0xff, green: 0xff, blue: 0xff });
test.equal(this.i2cWrite.callCount, 3);
test.ok(this.i2cWrite.calledWith(64, [6, 0, 0, 4095, 15]));
test.ok(this.i2cWrite.calledWith(64, [10, 0, 0, 4095, 15]));
test.ok(this.i2cWrite.calledWith(64, [14, 0, 0, 4095, 15]));
test.ok(this.i2cWrite.calledWith(64, [6, 4096, 16, 0, 0]));
test.ok(this.i2cWrite.calledWith(64, [10, 4096, 16, 0, 0]));
test.ok(this.i2cWrite.calledWith(64, [14, 4096, 16, 0, 0]));
this.i2cWrite.reset();

// Custom color
Expand Down Expand Up @@ -1026,13 +1033,21 @@ exports["Led.RGB - PCA9685 (I2C) Common Anode"] = {
done();
},

tearDown: function(done) {
Board.purge();
restore(this);
Expander.purge();
done();
},

shape: testLedRgbShape,

write: function(test) {
test.expect(12);

// Fully off
this.ledRgb.write({ red: 0x00, green: 0x00, blue: 0x00 });

test.equal(this.i2cWrite.callCount, 3);
test.ok(this.i2cWrite.calledWith(64, [6, 4096, 16, 0, 0]));
test.ok(this.i2cWrite.calledWith(64, [10, 4096, 16, 0, 0]));
Expand All @@ -1042,9 +1057,9 @@ exports["Led.RGB - PCA9685 (I2C) Common Anode"] = {
// Fully on
this.ledRgb.write({ red: 0xff, green: 0xff, blue: 0xff });
test.equal(this.i2cWrite.callCount, 3);
test.ok(this.i2cWrite.calledWith(64, [6, 0, 0, 0, 0]));
test.ok(this.i2cWrite.calledWith(64, [10, 0, 0, 0, 0]));
test.ok(this.i2cWrite.calledWith(64, [14, 0, 0, 0, 0]));
test.ok(this.i2cWrite.calledWith(64, [6, 0, 0, 4096, 16]));
test.ok(this.i2cWrite.calledWith(64, [10, 0, 0, 4096, 16]));
test.ok(this.i2cWrite.calledWith(64, [14, 0, 0, 4096, 16]));
this.i2cWrite.reset();

// Custom color
Expand Down
Loading

0 comments on commit 03d739f

Please sign in to comment.