accel-mma84
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,4 +0,7 @@ | ||
// var hardware = require('hardware'); | ||
/********************************************* | ||
This basic accelerometer example logs a stream | ||
of x, y, and z data from the accelerometer | ||
*********************************************/ | ||
var tessel = require('tessel'); | ||
console.log("Connecting to acceleroemter on port bank A"); | ||
var accel = require('../').connect(tessel.port("A")); | ||
@@ -8,10 +11,8 @@ | ||
accel.on('connected', function () { | ||
// Loop forever. | ||
setInterval(function () { | ||
accel.getAcceleration(function (err, xyz) { | ||
console.log("x:", xyz[0].toFixed(2), | ||
"y:", xyz[1].toFixed(2), | ||
"z:", xyz[2].toFixed(2)); | ||
}); | ||
}, 100); | ||
// Stream accelerometer data | ||
accel.on('data', function (xyz) { | ||
console.log("x:", xyz[0].toFixed(2), | ||
"y:", xyz[1].toFixed(2), | ||
"z:", xyz[2].toFixed(2)); | ||
}); | ||
}); |
@@ -1,4 +0,7 @@ | ||
// This example is a more colorful accelerometer example. | ||
/********************************************* | ||
This is a more colorful accelerometer example. | ||
*********************************************/ | ||
var mma8452q = require('accel-mma84'); | ||
var tessel = require('tessel'); | ||
var accel = require('../').connect(tessel.port("A")); | ||
@@ -33,12 +36,14 @@ function pad (f) { | ||
mma8452q.initialize(); | ||
// Initialize the accelerometer. | ||
accel.on('connected', function () { | ||
// Stream accelerometer data | ||
accel.on('data', function (xyz) { | ||
var avgs = []; | ||
for (var i = 0; i < 5; i++) { | ||
avgs.push(xyz); | ||
} | ||
while (1) { | ||
var avgs = []; | ||
for (var i = 0; i < 5; i++) { | ||
avgs.push(mma8452q.getAcceleration()); | ||
} | ||
var x = avg(avgs, 0), y = avg(avgs, 1), z = avg(avgs, 2); | ||
console.log("x:", red(pad(x)), " y:", green(pad(y)), " z:", yellow(pad(z))); | ||
} | ||
var x = avg(avgs, 0), y = avg(avgs, 1), z = avg(avgs, 2); | ||
console.log("x:", red(pad(x)), " y:", green(pad(y)), " z:", yellow(pad(z))); | ||
}); | ||
}); |
67
index.js
@@ -33,6 +33,8 @@ var util = require('util'); | ||
this.hardware = hardware; | ||
self.hardware = hardware; | ||
self.numListeners = 0; | ||
self.listening = false; | ||
self.pollFrequency = 100; | ||
self.i2c = new hardware.I2C(I2C_ADDRESS); | ||
self.i2c.initialize(); | ||
@@ -60,3 +62,35 @@ self._readRegister(WHO_AM_I, function (err, c) { | ||
}); | ||
}) | ||
// If we get a new listener | ||
self.on('newListener', function(event) { | ||
if (event == "data") { | ||
// Add to the number of things listening | ||
self.numListeners += 1; | ||
// If we're not already listening | ||
if (!self.listening) { | ||
// Start listening | ||
self.setListening(); | ||
} | ||
} | ||
}); | ||
// If we remove a listener | ||
self.on('removeListener', function(event) { | ||
if (event == "data") { | ||
// Remove from the number of things listening | ||
console.log('prev listeners', self.numListeners); | ||
self.numListeners -= 1; | ||
console.log('now', self.numListeners); | ||
// Because we listen in a while loop, if this.listening goes to 0, we'll stop listening automatically | ||
if (self.numListeners < 1) { | ||
self.listening = 0; | ||
} | ||
} | ||
}); | ||
// If all listeners removed | ||
self.on('removeAllListeners', function(event) { | ||
self.numListeners = 0; | ||
self.listening = 0; | ||
}); | ||
}); | ||
} | ||
@@ -84,2 +118,18 @@ | ||
Accelerometer.prototype.setListening = function () { | ||
var self = this; | ||
self.listening = true; | ||
// Loop until nothing is listening | ||
self.listeningLoop = setInterval (function () { | ||
if (self.numListeners) { | ||
self.getAcceleration(function (err, xyz) { | ||
if (err) throw err; | ||
self.emit('data', xyz); | ||
}); | ||
} else { | ||
clearInterval(listeningLoop); | ||
} | ||
}, self.pollFrequency); | ||
} | ||
// Sets the MMA8452 to standby mode. It must be in standby to change most register settings | ||
@@ -109,2 +159,3 @@ Accelerometer.prototype.modeStandby = function (next) | ||
self._readRegisters(OUT_X_MSB, 6, function (err, rawData) { | ||
if (err) throw err; | ||
// Loop to calculate 12-bit ADC and g value for each axis | ||
@@ -129,2 +180,12 @@ var out = []; | ||
// Sets the polling frequency for streamed data (default 100ms) | ||
Accelerometer.prototype.setPollFrequency = function (milliseconds) { | ||
var self = this; | ||
self.pollFrequency = milliseconds; | ||
if (self.listening) { | ||
clearInterval(self.listeningLoop); | ||
self.setListening(); | ||
} | ||
} | ||
exports.Accelerometer = Accelerometer; | ||
@@ -131,0 +192,0 @@ exports.connect = function (hardware) { |
{ | ||
"name": "accel-mma84", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Library to run the MMA8452Q accelerometer.", | ||
@@ -9,2 +9,5 @@ "main": "index.js", | ||
}, | ||
"hardware": { | ||
"./examples": false | ||
}, | ||
"repository": "", | ||
@@ -11,0 +14,0 @@ "author": "Tim Cameron Ryan <tim@timryan.org>", |
@@ -1,16 +0,30 @@ | ||
# MMA8452Q accelerometer driver | ||
#Accelerometer | ||
Driver for the accel-mma84 Tessel accelerometer module ([MMA8452Q](http://www.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf)). | ||
Install: | ||
``` | ||
##Installation | ||
```sh | ||
npm install accel-mma84 | ||
``` | ||
Import: | ||
##Example | ||
```js | ||
// var hardware = require('hardware'); | ||
var tessel = require('tessel'); | ||
console.log("Connecting to accelerometer on port bank A"); | ||
var accel = require('accel-mma84').connect(tessel.port("A")); | ||
// Initialize the accelerometer. | ||
accel.on('connected', function () { | ||
// Loop forever. | ||
setInterval(function () { | ||
accel.getAcceleration(function (err, xyz) { | ||
console.log("x:", xyz[0].toFixed(2), | ||
"y:", xyz[1].toFixed(2), | ||
"z:", xyz[2].toFixed(2)); | ||
}); | ||
}, 100); | ||
}); | ||
``` | ||
var accel = require('accel-mma84').connect(myhardwareapi); | ||
``` | ||
Functions: | ||
##Methods | ||
@@ -21,4 +35,14 @@ * **`accel`.initialize()** | ||
##Events | ||
* *connected* | ||
* *data* | ||
##Further Examples | ||
* [Average (more advanced use)](https://github.com/tessel/modules/blob/master/accel-mma84/examples/average.js) | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9586
6
244
48