Skip to content

Commit

Permalink
Log warnings when I2C peripherals cannot be read. Fixes gh-47
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Mar 8, 2016
1 parent 78d300f commit 1ed7030
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 62 deletions.
59 changes: 42 additions & 17 deletions lib/galileo.js
Expand Up @@ -526,7 +526,7 @@ if (isMiniboard) {
}

// Map to Board.prototype.sendI2CConfig
Galileo.prototype.i2cConfig = function(delay) {
Galileo.prototype.i2cConfig = function(opts) {
var state = priv.get(this);
var bus = i2cBus;

Expand All @@ -538,7 +538,12 @@ Galileo.prototype.i2cConfig = function(delay) {
// Initialize the I2C interface if none currently exists
if (!(state.i2c instanceof Pin.IO.I2c)) {
state.i2c = new Pin.IO.I2c(bus);
state.i2c.delay = delay || 10;

if (typeof opts === "number" || typeof opts === "undefined") {
opts = { delay: Number(opts) };
}

state.i2c.delay = opts.delay || 5;
}
};

Expand Down Expand Up @@ -638,17 +643,28 @@ Galileo.prototype.i2cRead = function(address, register, bytesToRead, callback) {
this.on(event, callback);

setInterval(function() {
if (register !== null) {
var data;

try {
state.i2c.address(address);
state.i2c.writeByte(register);

if (register !== null) {
data = state.i2c.readBytesReg(register, bytesToRead);
} else {
data = state.i2c.read(bytesToRead);
}
} catch (error) {
console.warn("I2C: Could not read %d Bytes from peripheral with address 0x%s", bytesToRead, address.toString(16));
}

state.i2c.address(address);
var data = new Buffer(state.i2c.read(bytesToRead));
var values = [];

for (var i = 0; i < bytesToRead; i++) {
values.push(data.readUInt8(i));
if (data && data.length === bytesToRead) {
for (var i = 0; i < bytesToRead; i++) {
values.push(data.readUInt8(i));
}
} else {
console.warn("I2C: Could not read %d Bytes from peripheral with address 0x%s", bytesToRead, address.toString(16));
}

this.emit(event, values);
Expand Down Expand Up @@ -682,23 +698,32 @@ Galileo.prototype.i2cReadOnce = function(address, register, bytesToRead, callbac
this.once(event, callback);

setTimeout(function() {
if (register !== null) {
var data;

try {
state.i2c.address(address);
state.i2c.writeByte(register);
}

state.i2c.address(address);
if (register !== null) {
data = state.i2c.readBytesReg(register, bytesToRead);
} else {
data = state.i2c.read(bytesToRead);
}
} catch (error) {
console.warn("I2C: Could not read %d Bytes from peripheral with address 0x%s", bytesToRead, address.toString(16));
}

var data = new Buffer(state.i2c.read(bytesToRead));
var values = [];

for (var i = 0; i < bytesToRead; i++) {
values.push(data.readUInt8(i));
if (data && data.length === bytesToRead) {
for (var i = 0; i < bytesToRead; i++) {
values.push(data.readUInt8(i));
}
} else {
console.warn("I2C: Could not read %d Bytes from peripheral with address 0x%s", bytesToRead, address.toString(16));
}

this.emit(event, values);
}.bind(this), state.i2c.delay);

}.bind(this), 1);

return this;
};
Expand Down

0 comments on commit 1ed7030

Please sign in to comment.