Skip to content

Commit

Permalink
Migrating from i2c to i2c-bus due to kelly/node-i2c#90
Browse files Browse the repository at this point in the history
  • Loading branch information
szakharchenko committed Feb 6, 2019
1 parent 8775952 commit a2cd597
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions lib/BNO055.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var i2c = require('i2c');
var i2c = require('i2c-bus');
var async = require('async');

// https://www.adafruit.com/datasheets/BST_BNO055_DS000_12.pdf
Expand Down Expand Up @@ -165,7 +165,29 @@ var BNO055 = module.exports = function(options) {
if ('address' in options)
address = options.address;

this.wire = new i2c(address, options);
var bus;
if ('bus' in options)
bus = options.bus;
else
bus = parseInt(options.device.match(new RegExp("^/dev/i2c-([0-9]*)"))[1]);

var i2cbus = i2c.openSync(bus);
this.wire = {
i2cbus: i2cbus,
readBytes: function(addr, count, callback) {
var buffer = Buffer.alloc(count);
return this.i2cbus.readI2cBlock(this.address, addr, count, buffer, function(err, bytesRead, buffer) { return callback(err, err?null:Array.prototype.slice.call(buffer, 0));});
},
writeBytes: function(addr, bytes, callback) {
if (bytes && bytes.length == 1)
return this.i2cbus.writeByte(this.address, addr, bytes[0], callback);
else {
var buffer = Buffer.from(bytes || []);
return this.i2cbus.writeI2cBlock(this.address, addr, buffer.length, buffer, callback);
}
},
address: address
}

this.options = options;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"homepage": "https://github.com/landlessness/node-BNO055#readme",
"dependencies": {
"async": "^1.4.2",
"i2c": "git+https://github.com/polaris/node-i2c"
"i2c-bus": "^4.0.7"
}
}

0 comments on commit a2cd597

Please sign in to comment.