Skip to content

Commit

Permalink
Support user specified I2C bus. Fixes gh-11
Browse files Browse the repository at this point in the history
This will make use of forwarded I2C options, eg.

  var a = new five.Servo({
    bus: 1,
    address: 0x40,
    controller: "PCA9685",
    pin: 0,
  });

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Jul 21, 2016
1 parent 949806c commit f81824c
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/chip-io.js
Expand Up @@ -11,6 +11,7 @@ var PCF8574A = require('./pcf8574a');
var pins = require('./pins');

var TICK_INTERVAL = 19;
var addressToBus = {};

function ChipIO() {
// call super constructor
Expand Down Expand Up @@ -176,8 +177,15 @@ ChipIO.prototype.digitalRead = function(pin, handler) {
return this;
};

ChipIO.prototype.i2cConfig = function(delay) {
debug('i2cConfig', delay);
ChipIO.prototype.i2cConfig = function(options) {
debug('i2cConfig', options);

// options.address is _always_ sent by all I2C component
// classes in Johnny-Five.
// If a bus was sent, then we need to associate the address to the bus.
if (options.bus && !Reflect.has(addressToBus, options.address)) {
addressToBus[options.address] = options.bus;
}

return this;
};
Expand All @@ -190,7 +198,8 @@ ChipIO.prototype.i2cWrite = function(address, register, data) {

debug('i2cWrite', address, register, data);

var i2c = new I2C(1, address);
var bus = typeof addressToBus[address] !== undefined ? addressToBus[address] : 1;
var i2c = new I2C(bus, address);

if (typeof(data) === 'number') {
data = [data];
Expand Down Expand Up @@ -242,7 +251,8 @@ ChipIO.prototype.i2cReadOnce = function(address, register, size, handler) {

debug('i2cReadOnce', address, register, size, handler);

var i2c = new I2C(1, address);
var bus = typeof addressToBus[address] !== undefined ? addressToBus[address] : 1;
var i2c = new I2C(bus, address);

i2c.open(function(err) {
if (err) {
Expand Down

0 comments on commit f81824c

Please sign in to comment.