Skip to content

Commit

Permalink
Allow i2cConfig(options) to pass address object.
Browse files Browse the repository at this point in the history
This is necessary for supporting LCD JHD1313M1 which has 2 addresses:

- lcd
- rgb (Backlight control)

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Apr 17, 2016
1 parent a4068fc commit ffbd045
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/tessel.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ Tessel.prototype.servoWrite = function(pin, value) {
// Note: Tessel-IO will default to Port A if no bus is specified.
Tessel.prototype.i2cConfig = function(options) {
var state = priv.get(this);
var addresses = [];
var delay;

if (options === undefined) {
Expand All @@ -613,17 +614,23 @@ Tessel.prototype.i2cConfig = function(options) {
}
}

if (options.addresses && Array.isArray(options.addresses)) {
options.addresses.forEach(function(address) {
if (address && !state.i2c[address]) {
state.i2c[address] = new tessel.port[ToI2CBusPort(options.bus)].I2C(address);
}
});
if (typeof options.address === "number") {
addresses = [options.address];
} else {
if (options.address && !state.i2c[options.address]) {
state.i2c[options.address] = new tessel.port[ToI2CBusPort(options.bus)].I2C(options.address);
if (typeof options.address === "object" && options.address !== null) {
addresses = Object.keys(options.address).map(key => options.address[key]);
}
}

if (options.addresses && Array.isArray(options.addresses)) {
addresses = addresses.concat(options.addresses);
}

addresses.forEach(function(address) {
if (address && !state.i2c[address]) {
state.i2c[address] = new tessel.port[ToI2CBusPort(options.bus)].I2C(address);
}
});
};

// Map to Board.prototype.sendI2CWriteRequest
Expand Down
10 changes: 10 additions & 0 deletions test/tessel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,16 @@ exports["Tessel.prototype.i2cConfig"] = {
test.done();
},

calledWithObjectOfAddresses: function(test) {
test.expect(3);
this.tessel.i2cConfig({ address: { lcd: 0x04, rgb: 0x05 }, port: "B" });
// One call for each address
test.equal(this.b.callCount, 2);
test.equal(this.b.firstCall.args[0], 0x04);
test.equal(this.b.lastCall.args[0], 0x05);
test.done();
}

};

exports["Tessel.prototype.i2cWrite"] = {
Expand Down

0 comments on commit ffbd045

Please sign in to comment.