Skip to content

Commit

Permalink
Adding initial support for MPL3115A2 barometer/temperature. Altimiter…
Browse files Browse the repository at this point in the history
… to come
  • Loading branch information
BrianGenisio committed Oct 5, 2015
1 parent e468a40 commit d110e26
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
18 changes: 18 additions & 0 deletions eg/barometer-mpl3115a2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var five = require("../lib/johnny-five.js");
var board = new five.Board();

board.on("ready", function() {
var barometer = new five.Barometer({
controller: "MPL3115A2"
});

barometer.on("data", function() {
console.log("barometer");
console.log(" pressure : ", this.pressure);
console.log("--------------------------------------");
});
});

// @markdown
// - [MPLe115A2 - I2C Barometric Pressure/Altimiter/Temperature Sensor](https://www.adafruit.com/products/1893)
// @markdown
20 changes: 20 additions & 0 deletions eg/temperature-mpl3115a2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var five = require("../lib/johnny-five.js");
var board = new five.Board();

board.on("ready", function() {
var temperature = new five.Temperature({
controller: "MPL3115A2"
});

temperature.on("data", function() {
console.log("temperature");
console.log(" celsius : ", this.celsius);
console.log(" fahrenheit : ", this.fahrenheit);
console.log(" kelvin : ", this.kelvin);
console.log("--------------------------------------");
});
});

// @markdown
// - [MPLe115A2 - I2C Barometric Pressure/Altimiter/Temperature Sensor](https://www.adafruit.com/products/1893)
// @markdown
18 changes: 18 additions & 0 deletions lib/barometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ var Controllers = {
}
}
},
MPL3115A2: {
initialize: {
value: function(opts, dataHandler) {
var Multi = require("../lib/imu");
var driver = Multi.Drivers.get(this.board, "MPL3115A2", opts);
driver.on("data", function(data) {
dataHandler.call(this, data.pressure);
}.bind(this));
}
},
// kPa (Kilopascals)
toPressure: {
value: function(raw) {
var inches = (raw / 4) / 3377;
return inches * 3.39;
}
}
},
BMP180: {
initialize: {
value: function(opts, dataHandler) {
Expand Down
106 changes: 106 additions & 0 deletions lib/imu.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,112 @@ var Drivers = {
}
}
},
// Based off of the AdaFruit Arduino library for this chip
// https://github.com/adafruit/Adafruit_MPL3115A2_Library
MPL3115A2: {
ADDRESSES: {
value: [0x60]
},
REGISTER: {
value: {
STATUS: 0x00,
CONTROL: 0x26,
DATA_CONFIG: 0x13,
PRESSURE_MSB: 0x01
}
},
MASK: {
value: {
STATUS: {
PRESSURE_DATA_READ: 0x04
},
CONTROL: {
SBYB: 0x01,
OS128: 0x38,
ALTIMITER: 0x80,
PRESSURE: 0x00
},
DATA_CONFIG: {
TDEFE: 0x01,
PDEFE: 0x02,
DREM: 0x04
}
}
},
initialize: {
value: function(board, opts) {
var READLENGTH = 6;
var io = board.io;
var address = opts.address || this.ADDRESSES[0];

var computed = {
pressure: 0,
altitude: 0,
temperature: 0
}

var waitForReady = function(next) {
io.i2cReadOnce(address, this.REGISTER.STATUS, 1, function(data) {
if(data[0] & this.MASK.STATUS.PRESSURE_DATA_READ) {
next();
} else {
setTimeout(function() {
waitForReady(next);
}, 100);
}
}.bind(this));
}.bind(this);

var readValues = function(isPressure) {
var modeMask = isPressure ? this.MASK.CONTROL.PRESSURE : this.MASK.CONTROL.ALTIMITER;

io.i2cWrite(address, this.REGISTER.CONTROL,
this.MASK.CONTROL.SBYB |
this.MASK.CONTROL.OS128 |
modeMask);

waitForReady(function() {
io.i2cReadOnce(address, this.REGISTER.PRESSURE_MSB, 6, function(data) {
var value = uint24(data[1], data[2], data[3]) >> 4;
var temperature = uint16(data[4], data[5]) >> 4;

computed.temperature = temperature;

if(isPressure) {
computed.pressure = value;
this.emit('data', computed);
} else {
if (value & 0x800000) {
value |= 0xFF000000;
}
//value /= 16;

computed.altitude = value;
}

readValues(!isPressure);
}.bind(this));
}.bind(this));
}.bind(this);

io.i2cConfig(opts);

// configure the chip
io.i2cWrite(address, this.REGISTER.DATA_CONFIG,
this.MASK.DATA_CONFIG.TDEFE |
this.MASK.DATA_CONFIG.PDEFE |
this.MASK.DATA_CONFIG.DREM);

readValues(false);
}
},
identifier: {
value: function(opts) {
var address = opts.address || Drivers["MPL3115A2"].ADDRESSES.value[0];
return "mpl3115a2-" + address;
}
}
},
BMP180: {
ADDRESSES: {
value: [0x77]
Expand Down
16 changes: 16 additions & 0 deletions lib/temperature.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,22 @@ var Controllers = {
}
}
},
MPL3115A2: {
initialize: {
value: function(opts, dataHandler) {
var Multi = require("../lib/imu");
var driver = Multi.Drivers.get(this.board, "MPL3115A2", opts);
driver.on("data", function(data) {
dataHandler(data.temperature);
});
}
},
toCelsius: {
value: function(raw) {
return raw / 16;
}
}
},
GROVE: {
initialize: {
value: analogHandler
Expand Down

0 comments on commit d110e26

Please sign in to comment.