Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support user specified I2C bus. Fixes gh-11 #12

Merged
merged 1 commit into from Jul 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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