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

i2c dependency fix #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/accelerometer.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 utils = require('./util');

var accel_address = 0x19;
Expand All @@ -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();
}
Expand Down
3 changes: 1 addition & 2 deletions lib/lsm303.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var i2c = require('i2c');
var Accelerometer = require('./accelerometer')
var Magnetometer = require('./magnetometer')

Expand All @@ -21,4 +20,4 @@ LSM303.prototype.magnetometer = function(options){
return this.mag;
}

module.exports = LSM303;
module.exports = LSM303;
23 changes: 18 additions & 5 deletions lib/magnetometer.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 utils = require('./util');

var mag_address = 0x1e;
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"example": "example"
},
"dependencies": {
"i2c": "^0.2.3"
"i2c-bus": "^4.0.7"
},
"devDependencies": {},
"repository": {
Expand Down