Skip to content

Commit

Permalink
Controllers: update all controllers to forward opts param as argument…
Browse files Browse the repository at this point in the history
… to i2cConfig

This is needed by platforms that have > 1 active I2C bus
  • Loading branch information
rwaldron committed Jul 16, 2015
1 parent 077784b commit 408a8c0
Show file tree
Hide file tree
Showing 31 changed files with 718 additions and 179 deletions.
11 changes: 5 additions & 6 deletions lib/accelerometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ var Controllers = {
initialize: {
value: function(opts, dataHandler) {
var READLENGTH = 6;
var io = this.board.io;
var address = opts.address || this.ADDRESSES[0];
var state = priv.get(this);

Expand All @@ -137,18 +136,18 @@ var Controllers = {
//
state.sensitivity = opts.sensitivity || 250;

io.i2cConfig();
this.io.i2cConfig(opts);

// Standby mode
io.i2cWrite(address, this.COMMANDS.POWER, 0);
this.io.i2cWrite(address, this.COMMANDS.POWER, 0);

// Enable measurements
io.i2cWrite(address, this.COMMANDS.POWER, 8);
this.io.i2cWrite(address, this.COMMANDS.POWER, 8);

// Set range (this is 2G range, should be user defined?)
io.i2cWrite(address, this.COMMANDS.RANGE, 8);
this.io.i2cWrite(address, this.COMMANDS.RANGE, 8);

io.i2cRead(address, this.COMMANDS.READREGISTER, READLENGTH, function(data) {
this.io.i2cRead(address, this.COMMANDS.READREGISTER, READLENGTH, function(data) {
dataHandler.call(this, {
x: int16(data[1], data[0]),
y: int16(data[3], data[2]),
Expand Down
6 changes: 4 additions & 2 deletions lib/compass.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var Controllers = {

Object.assign(state, new Compass.Scale(opts.gauss || 0.88));

this.io.i2cConfig();
this.io.i2cConfig(opts);

// Set CRA
this.io.i2cWrite(address, this.COMMANDS.CRA, 0x70);
Expand Down Expand Up @@ -98,7 +98,9 @@ var Controllers = {

state.scale = 1;

this.io.i2cConfig(10);
opts.delay = 10;

this.io.i2cConfig(opts);

this.io.i2cWrite(address, this.COMMANDS.READREGISTER);

Expand Down
3 changes: 1 addition & 2 deletions lib/esc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var Controllers = {
this.pwmRange = opts.pwmRange || [544, 2400];

if (!this.board.Drivers[this.address]) {
this.io.i2cConfig();
this.io.i2cConfig(opts);
this.board.Drivers[this.address] = {
initialized: false
};
Expand Down Expand Up @@ -175,7 +175,6 @@ function ESC(opts) {

priv.set(this, state);

this.id = opts.id || Board.uid();
this.startAt = typeof opts.startAt !== "undefined" ? opts.startAt : null;
this.neutral = opts.neutral;
this.range = opts.range || [0, 100];
Expand Down
12 changes: 6 additions & 6 deletions lib/expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var Controllers = {

this.address = opts.address || this.REGISTER.ADDRESS;

this.io.i2cConfig();
this.io.i2cConfig(opts);
this.io.i2cWrite(this.address, [ this.REGISTER.IODIRA, state.iodir[this.REGISTER.IODIRA] ]);
this.io.i2cWrite(this.address, [ this.REGISTER.IODIRB, state.iodir[this.REGISTER.IODIRB] ]);

Expand Down Expand Up @@ -229,7 +229,7 @@ var Controllers = {

this.address = opts.address || this.REGISTER.ADDRESS;

this.io.i2cConfig();
this.io.i2cConfig(opts);
this.io.i2cWrite(this.address, [ this.REGISTER.IODIR, state.iodir[this.REGISTER.IODIR] ]);

Object.assign(this.MODES, this.io.MODES);
Expand Down Expand Up @@ -361,7 +361,7 @@ var Controllers = {

this.address = opts.address || this.REGISTER.ADDRESS;

this.io.i2cConfig();
this.io.i2cConfig(opts);

Object.assign(this.MODES, this.io.MODES);

Expand Down Expand Up @@ -476,7 +476,7 @@ var Controllers = {

this.address = opts.address || this.REGISTER.ADDRESS;

this.io.i2cConfig();
this.io.i2cConfig(opts);

Object.assign(this.MODES, this.io.MODES);

Expand Down Expand Up @@ -587,7 +587,7 @@ var Controllers = {
this.address = opts.address || this.REGISTER.ADDRESS;
this.range = opts.range || [0, 4095];

this.io.i2cConfig();
this.io.i2cConfig(opts);

// Reset
this.io.i2cWriteReg(this.address, this.REGISTER.MODE1, 0x00);
Expand Down Expand Up @@ -706,7 +706,7 @@ var Controllers = {

this.address = opts.address || this.REGISTER.ADDRESS;

this.io.i2cConfig();
this.io.i2cConfig(opts);

Object.assign(this.MODES, this.io.MODES);

Expand Down
6 changes: 3 additions & 3 deletions lib/imu.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var Drivers = {
gyro: {}
};

io.i2cConfig();
io.i2cConfig(opts);
io.i2cWrite(address, this.REGISTER.SETUP);

io.i2cRead(address, this.REGISTER.READ, READLENGTH, function(data) {
Expand Down Expand Up @@ -92,7 +92,7 @@ var Drivers = {
c12: null
};

io.i2cConfig();
io.i2cConfig(opts);

var pCoefficients = new Promise(function(resolve) {
io.i2cReadOnce(address, this.REGISTER.COEFFICIENTS, 8, function(data) {
Expand Down Expand Up @@ -191,7 +191,7 @@ var Drivers = {
md: null,
};

io.i2cConfig();
io.i2cConfig(opts);

var pCoefficients = new Promise(function(resolve) {
io.i2cReadOnce(address, this.REGISTER.COEFFICIENTS, 22, function(data) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ir.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function IR(opts) {
});

// Set up I2C data connection
this.io.i2cConfig();
this.io.i2cConfig(opts);

// Enumerate and write each set of setup instructions
setup.forEach(function(byteArray) {
Expand Down
2 changes: 1 addition & 1 deletion lib/keypad.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var Controllers = {

var length = mapping.length;

this.io.i2cConfig();
this.io.i2cConfig(opts);

this.io.i2cWrite(address, this.REGISTER.MHD_RISING, 0x01);
this.io.i2cWrite(address, this.REGISTER.NHD_AMOUNT_RISING, 0x01);
Expand Down
5 changes: 2 additions & 3 deletions lib/lcd.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ function Expander(address, io) {
this.mask = 0xFF;
this.shadow = 0x00;
this.io = io;
this.io.i2cConfig();
}

Expander.prototype.pinMode = function(pin, dir) {
Expand Down Expand Up @@ -135,7 +134,7 @@ var Controllers = {
initialize: {
value: function(opts) {

this.io.i2cConfig();
this.io.i2cConfig(opts);

this.lines = opts.lines || 2;
this.rows = opts.rows || 2;
Expand Down Expand Up @@ -276,7 +275,7 @@ var Controllers = {
initialize: {
value: function(opts) {

this.io.i2cConfig();
this.io.i2cConfig(opts);

this.bitMode = opts.bitMode || 4;
this.lines = opts.lines || 2;
Expand Down
2 changes: 1 addition & 1 deletion lib/led/led.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var Controllers = {
this.pwmRange = opts.pwmRange || [0, 4095];

if (!this.board.Drivers[this.address]) {
this.io.i2cConfig();
this.io.i2cConfig(opts);
this.board.Drivers[this.address] = {
initialized: false
};
Expand Down
2 changes: 1 addition & 1 deletion lib/led/ledcontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ Controllers = {
this.displaybuffers[i] = [];
}
// Set up I2C data connection
this.io.i2cConfig();
this.io.i2cConfig(opts);
// TODO allow setup to be configured through opts
this.each(function(device) {
this.on(device);
Expand Down
4 changes: 2 additions & 2 deletions lib/led/rgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var Controllers = {
this.address = opts.address || 0x40;

if (!this.board.Drivers[this.address]) {
this.io.i2cConfig();
this.io.i2cConfig(opts);
this.board.Drivers[this.address] = {
initialized: false
};
Expand Down Expand Up @@ -122,7 +122,7 @@ var Controllers = {
this.address = opts.address || 0x09;

if (!this.board.Drivers[this.address]) {
this.io.i2cConfig();
this.io.i2cConfig(opts);
this.board.Drivers[this.address] = {
initialized: false
};
Expand Down
2 changes: 1 addition & 1 deletion lib/motion.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var Controllers = {
}.bind(this), 10);

// Set up I2C data connection
this.io.i2cConfig();
this.io.i2cConfig(opts);

this.io.i2cWriteReg(address, 0x03, 0xFE);
this.io.i2cWrite(address, [0x00]);
Expand Down
2 changes: 1 addition & 1 deletion lib/motor.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ var Controllers = {
value: function() {

if (!this.board.Drivers[this.opts.address]) {
this.io.i2cConfig();
this.io.i2cConfig(this.opts);
this.board.Drivers[this.opts.address] = {
initialized: false
};
Expand Down
8 changes: 4 additions & 4 deletions lib/proximity.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ var Controllers = {
value: function(opts, dataHandler) {

var address = 0x70;
var delay = 65;
var msUntilNextRead = 65;

// Set up I2C data connection
this.io.i2cConfig(0);
this.io.i2cConfig(opts);

// Startup parameter
this.io.i2cWrite(address, [0x01, 16]);
Expand All @@ -101,7 +101,7 @@ var Controllers = {
// 0x51 result in cm (centimeters)
this.io.i2cWrite(address, [0x00, 0x51]);

setTimeout(read.bind(this), delay);
setTimeout(read.bind(this), msUntilNextRead);
}

prime.call(this);
Expand Down Expand Up @@ -193,7 +193,7 @@ var Controllers = {
var READREGISTER = 0x8f;
var BYTES_TO_READ = 0x02;

this.io.i2cConfig();
this.io.i2cConfig(opts);

var read = function() {
this.io.i2cWrite(ADDRESS, ENABLE, MEASUREMODE);
Expand Down
3 changes: 1 addition & 2 deletions lib/servo.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var Controllers = {
this.pwmRange = opts.pwmRange || [544, 2400];

if (!this.board.Drivers[this.address]) {
this.io.i2cConfig();
this.io.i2cConfig(opts);
this.board.Drivers[this.address] = {
initialized: false
};
Expand Down Expand Up @@ -134,7 +134,6 @@ function Servo(opts) {
this, opts = Board.Options(opts)
);

this.id = opts.id || Board.uid();
this.range = opts.range || [0, 180];
this.deadband = opts.deadband || [90, 90];
this.fps = opts.fps || 100;
Expand Down
6 changes: 3 additions & 3 deletions lib/sonar.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function Sonar(opts) {
device = Devices.DEFAULT;
}

device.initialize.call(this);
device.initialize.call(this, opts);

if (!device.descriptor.inches) {
device.descriptor.inches = {
Expand Down Expand Up @@ -103,14 +103,14 @@ __.mixin(Sonar.prototype, within);

Devices = {
SRF10: {
initialize: function() {
initialize: function(opts) {

var samples = priv.get(this).samples;
var address = 0x70;
var delay = 65;

// Set up I2C data connection
this.io.i2cConfig(0);
this.io.i2cConfig(opts);

// Startup parameter
this.io.i2cWrite(address, [0x01, 16]);
Expand Down
2 changes: 1 addition & 1 deletion lib/wii.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function Wii(opts) {
last.set(this, [0, 0, 0, 0, 0, 0, 0]);

// Set up I2C data connection
this.io.i2cConfig();
this.io.i2cConfig(opts);

// Iterate and write each set of setup instructions
setup.forEach(function(bytes) {
Expand Down
47 changes: 37 additions & 10 deletions test/accelerometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,26 @@ exports["Accelerometer -- MPU-6050"] = {
done();
},

// config: function(test) {
// test.expect(1);
fwdOptionsToi2cConfig: function(test) {
test.expect(3);

this.i2cConfig.reset();

// this.i2cConfig.reset();
new Accelerometer({
controller: "MPU6050",
address: 0xff,
bus: "i2c-1",
board: this.board
});

// new Accelerometer({
// controller: "MPU6050",
// freq: 100,
// board: board
// });
var forwarded = this.i2cConfig.lastCall.args[0];

test.equal(this.i2cConfig.callCount, 1);
test.equal(forwarded.address, 0xff);
test.equal(forwarded.bus, "i2c-1");

// test.done();
// },
test.done();
},

data: function(test) {
var read, dataSpy = sinon.spy(), changeSpy = sinon.spy();
Expand Down Expand Up @@ -458,6 +464,27 @@ exports["Accelerometer -- ADXL345"] = {
done();
},

fwdOptionsToi2cConfig: function(test) {
test.expect(3);

this.i2cConfig.reset();

new Accelerometer({
controller: "ADXL345",
address: 0xff,
bus: "i2c-1",
board: this.board
});

var forwarded = this.i2cConfig.lastCall.args[0];

test.equal(this.i2cConfig.callCount, 1);
test.equal(forwarded.address, 0xff);
test.equal(forwarded.bus, "i2c-1");

test.done();
},

data: function(test) {
var read, dataSpy = sinon.spy(), changeSpy = sinon.spy();

Expand Down
Loading

0 comments on commit 408a8c0

Please sign in to comment.