From 1dad5ecb5834ee341d4fc7f107a41c4e6d6db43f Mon Sep 17 00:00:00 2001 From: Avishai Cohen <22681864+avicohen123@users.noreply.github.com> Date: Thu, 21 Feb 2019 10:59:14 -0500 Subject: [PATCH 1/3] Migrate from i2c to i2c-bus due to node-i2c bug --- lib/accelerometer.js | 24 +++++++++++++++++++----- lib/lsm303.js | 3 +-- lib/magnetometer.js | 23 ++++++++++++++++++----- package.json | 2 +- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/lib/accelerometer.js b/lib/accelerometer.js index cb2b63c..f8b81bd 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(accel_device); + 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..06ee148 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(mag_device); + 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: 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": { From d35c49a3541cb7200e4319651f7ce69a366efaf7 Mon Sep 17 00:00:00 2001 From: Avishai Cohen <22681864+avicohen123@users.noreply.github.com> Date: Thu, 21 Feb 2019 12:01:18 -0500 Subject: [PATCH 2/3] Fix openSync function params --- lib/accelerometer.js | 2 +- lib/magnetometer.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/accelerometer.js b/lib/accelerometer.js index f8b81bd..20b608d 100644 --- a/lib/accelerometer.js +++ b/lib/accelerometer.js @@ -16,7 +16,7 @@ function Accelerometer(options) { resolution = options.resolution; } - var i2cbus = i2c.openSync(accel_device); + var i2cbus = i2c.openSync(1); this.accel = { i2cbus : i2cbus, readBytes: function(addr, count, callback) { diff --git a/lib/magnetometer.js b/lib/magnetometer.js index 06ee148..c35addd 100644 --- a/lib/magnetometer.js +++ b/lib/magnetometer.js @@ -11,7 +11,7 @@ function Magnetometer(options) { if (options && options.device) { mag_device = options.device; } - var i2cbus = i2c.openSync(mag_device); + var i2cbus = i2c.openSync(1); this.accel = { i2cbus : i2cbus, readBytes: function(addr, count, callback) { From ae15e1cb6365706e494029460c20a2579031a376 Mon Sep 17 00:00:00 2001 From: Avishai Cohen <22681864+avicohen123@users.noreply.github.com> Date: Thu, 21 Feb 2019 12:06:21 -0500 Subject: [PATCH 3/3] fix mag --- lib/magnetometer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/magnetometer.js b/lib/magnetometer.js index c35addd..1f1bf82 100644 --- a/lib/magnetometer.js +++ b/lib/magnetometer.js @@ -12,7 +12,7 @@ function Magnetometer(options) { mag_device = options.device; } var i2cbus = i2c.openSync(1); - this.accel = { + this.mag = { i2cbus : i2cbus, readBytes: function(addr, count, callback) { var buffer = Buffer.alloc(count);