diff --git a/lib/accelerometer.js b/lib/accelerometer.js index cb2b63c..20b608d 100644 --- a/lib/accelerometer.js +++ b/lib/accelerometer.js @@ -1,4 +1,4 @@ -var i2c = require('i2c'); +var i2c = require('i2c-bus'); var utils = require('./util'); var accel_address = 0x19; @@ -15,10 +15,24 @@ function Accelerometer(options) { if (options && options.resolution) { resolution = options.resolution; } - this.accel = new i2c(accel_address, { - device: accel_device, - debug: false - }); + + var i2cbus = i2c.openSync(1); + this.accel = { + 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: accel_address + } this.init(); this.setResolution(); } diff --git a/lib/lsm303.js b/lib/lsm303.js index dd11249..a239d76 100644 --- a/lib/lsm303.js +++ b/lib/lsm303.js @@ -1,4 +1,3 @@ -var i2c = require('i2c'); var Accelerometer = require('./accelerometer') var Magnetometer = require('./magnetometer') @@ -21,4 +20,4 @@ LSM303.prototype.magnetometer = function(options){ return this.mag; } -module.exports = LSM303; \ No newline at end of file +module.exports = LSM303; diff --git a/lib/magnetometer.js b/lib/magnetometer.js index 4768537..1f1bf82 100644 --- a/lib/magnetometer.js +++ b/lib/magnetometer.js @@ -1,4 +1,4 @@ -var i2c = require('i2c'); +var i2c = require('i2c-bus'); var utils = require('./util'); var mag_address = 0x1e; @@ -11,10 +11,23 @@ function Magnetometer(options) { if (options && options.device) { mag_device = options.device; } - this.mag = new i2c(mag_address, { - device: mag_device, - debug: false - }); + var i2cbus = i2c.openSync(1); + this.mag = { + 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: mag_address + } this.init(); this.enableTempSensor(); } diff --git a/package.json b/package.json index 1e0d8f1..7abc261 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "example": "example" }, "dependencies": { - "i2c": "^0.2.3" + "i2c-bus": "^4.0.7" }, "devDependencies": {}, "repository": {