accel-mma84
Advanced tools
Comparing version 0.2.3 to 0.2.4
44
index.js
@@ -46,3 +46,3 @@ // Copyright 2014 Technical Machine, Inc. See the COPYRIGHT | ||
if (err) { | ||
err = new Error("Could not connect to MMA8452Q. No reponse on I2C lines. Error: "+err); | ||
err = new Error("Could not connect to MMA8452Q. No response on I2C lines. Error: "+err); | ||
return self._failProcedure(err); | ||
@@ -92,4 +92,4 @@ } | ||
if (event == 'data' || event == 'sample') { | ||
// Enable interrupts at whatever rate was previously set. | ||
self.enableDataInterrupts(true, queueNext); | ||
// Enable interrupts at whatever rate was previously set | ||
self.enableDataInterrupts(true, self.queue.next); | ||
} | ||
@@ -102,3 +102,3 @@ }); | ||
// Disable interrupt. | ||
self.enableDataInterrupts(false, queueNext); | ||
self.enableDataInterrupts(false, self.queue.next); | ||
} | ||
@@ -256,13 +256,10 @@ }); | ||
// Go into standby to edit registers | ||
self._changeRegister(function change(complete) { | ||
if (err) { | ||
return complete(err); | ||
} | ||
else { | ||
// Write the new scale into the register | ||
self._writeRegister(XYZ_DATA_CFG, fsr, function wroteReg(err) { | ||
self.scaleRange = scaleRange; | ||
return complete(err); | ||
}); | ||
} | ||
self._changeRegister(function change(changeComplete) { | ||
// Write the new scale into the register | ||
self._writeRegister(XYZ_DATA_CFG, fsr, function wroteReg(err) { | ||
self.scaleRange = scaleRange; | ||
return changeComplete(err); | ||
}); | ||
}, function scaleSet(err) { | ||
@@ -274,3 +271,3 @@ if (callback) { | ||
}); | ||
} | ||
}; | ||
@@ -315,3 +312,3 @@ // Sets the output rate of the data (1.56-800 Hz) | ||
} | ||
}) | ||
}); | ||
}, | ||
@@ -342,9 +339,10 @@ function rateSet(err) { | ||
// Don't call unnecessarily. | ||
if (this._dataInterrupts == !!enable) { | ||
return callback && callback(); | ||
} | ||
this._dataInterrupts = !!enable; | ||
self.queue.place(function queueEnable() { | ||
// Don't call unnecessarily. | ||
if (self._dataInterrupts == !!enable) { | ||
setImmediate(self.queue.next); | ||
return callback && callback(); | ||
} | ||
self._dataInterrupts = !!enable; | ||
self.queue.place(function queueEnable() { | ||
// We're going to change register 4 | ||
@@ -351,0 +349,0 @@ self._changeRegister(function change(complete) { |
{ | ||
"name": "accel-mma84", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Library to run the MMA8452Q accelerometer.", | ||
@@ -19,4 +19,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tinytap": "~0.0.2" | ||
"async": "^0.9.0", | ||
"tinytap": "^0.2.0" | ||
} | ||
} |
#Accelerometer | ||
Driver for the accel-mma84 Tessel accelerometer module ([MMA8452Q](http://www.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf)). | ||
Driver for the accel-mma84 Tessel accelerometer module. The hardware documentation for this module can be found [here](https://github.com/tessel/hardware/blob/master/modules-overview.md#accelerometer). | ||
##Installation | ||
If you run into any issues you can ask for support on the [Accelerometer Module Forums](http://forums.tessel.io/category/accelerometer). | ||
###Installation | ||
```sh | ||
@@ -9,3 +11,3 @@ npm install accel-mma84 | ||
##Example | ||
###Example | ||
```js | ||
@@ -18,53 +20,55 @@ /********************************************* | ||
var tessel = require('tessel'); | ||
var accel = require('accel-mma84').use(tessel.port['A']); | ||
var accel = require('../').use(tessel.port['A']); // Replace '../' with 'accel-mma84' in your own code | ||
// Initialize the accelerometer. | ||
accel.on('ready', function () { | ||
// Stream accelerometer data | ||
// 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)); | ||
console.log('x:', xyz[0].toFixed(2), | ||
'y:', xyz[1].toFixed(2), | ||
'z:', xyz[2].toFixed(2)); | ||
}); | ||
}); | ||
accel.on('error', function(err) { | ||
console.log('error connecting', err); | ||
accel.on('error', function(err){ | ||
console.log('Error:', err); | ||
}); | ||
setInterval(function(){}, 20000); | ||
``` | ||
##Methods | ||
###Methods | ||
##### * `accel.availableOutputRates()` Logs the available interrupt rates in Hz. | ||
 <a href="#api-accel-availableOutputRates-Logs-the-available-interrupt-rates-in-Hz" name="api-accel-availableOutputRates-Logs-the-available-interrupt-rates-in-Hz">#</a> accel<b>.availableOutputRates</b>() | ||
Logs the available interrupt rates in Hz. | ||
##### * `accel.availableScaleRanges()` Logs the available accelerometer ranges (in units of Gs). | ||
 <a href="#api-accel-availableScaleRanges-Logs-the-available-accelerometer-ranges-in-units-of-Gs" name="api-accel-availableScaleRanges-Logs-the-available-accelerometer-ranges-in-units-of-Gs">#</a> accel<b>.availableScaleRanges</b>() | ||
Logs the available accelerometer ranges (in units of Gs). | ||
##### * `accel.enableDataInterrupts(trueOrFalse, callback(err))` Enables or disables data interrupts. Set the first param truthy to enable, falsy to disable. | ||
 <a href="#api-accel-enableDataInterrupts-trueOrFalse-callback-err-Enables-or-disables-data-interrupts-Set-the-first-param-truthy-to-enable-falsy-to-disable" name="api-accel-enableDataInterrupts-trueOrFalse-callback-err-Enables-or-disables-data-interrupts-Set-the-first-param-truthy-to-enable-falsy-to-disable">#</a> accel<b>.enableDataInterrupts</b>( trueOrFalse, callback(err) ) | ||
Enables or disables data interrupts, and thus, `data` events. Set the first param truthy to enable, falsy to disable. | ||
##### * `accel.getAcceleration(callback(err, xyz))` Gets the acceleration from the device, outputs as array [x, y, z]. | ||
 <a href="#api-accel-getAcceleration-callback-err-xyz-Gets-the-acceleration-from-the-device-outputs-as-array-x-y-z" name="api-accel-getAcceleration-callback-err-xyz-Gets-the-acceleration-from-the-device-outputs-as-array-x-y-z">#</a> accel<b>.getAcceleration</b>( callback(err, xyz) ) | ||
Gets the acceleration from the device, outputs as array [x, y, z]. | ||
##### * `accel.setOutputRate(rateInHz, callback(err))` Sets the output rate of the data (1.56-800 Hz). | ||
 <a href="#api-accel-setOutputRate-rateInHz-callback-err-Sets-the-output-rate-of-the-data-1-56-800-Hz" name="api-accel-setOutputRate-rateInHz-callback-err-Sets-the-output-rate-of-the-data-1-56-800-Hz">#</a> accel<b>.setOutputRate</b>( rateInHz, callback(err) ) | ||
Sets the output rate of the data (1.56-800 Hz). | ||
##### * `accel.setScaleRange(scaleRange, callback(err))` Sets the accelerometer to read up to 2, 4, or 8 Gs of acceleration (smaller range = better precision). | ||
 <a href="#api-accel-setScaleRange-scaleRange-callback-err-Sets-the-accelerometer-to-read-up-to-2-4-or-8-Gs-of-acceleration-smaller-range-better-precision" name="api-accel-setScaleRange-scaleRange-callback-err-Sets-the-accelerometer-to-read-up-to-2-4-or-8-Gs-of-acceleration-smaller-range-better-precision">#</a> accel<b>.setScaleRange</b>( scaleRange, callback(err) ) | ||
Sets the accelerometer to read up to 2, 4, or 8 Gs of acceleration (smaller range = better precision). | ||
##Events | ||
###Events | ||
 <a href="#api-accel-on-data-callback-xyz-Emitted-when-data-is-available-xyz-is-an-array-in-the-form-of-x-y-z" name="api-accel-on-data-callback-xyz-Emitted-when-data-is-available-xyz-is-an-array-in-the-form-of-x-y-z">#</a> accel<b>.on</b>( 'data', callback(xyz) ) | ||
Emitted when data is available. xyz is an array in the form of [x, y, z]. | ||
##### * `accel.on('data', callback(xyz))` Emitted when data is available. `xyz` is an array in the form of [x, y, z]. | ||
 <a href="#api-accel-on-error-callback-err-Emitted-upon-error" name="api-accel-on-error-callback-err-Emitted-upon-error">#</a> accel<b>.on</b>( 'error', callback(err) ) | ||
Emitted upon error. | ||
##### * `accel.on('error', callback(err))` Emitted upon error. | ||
 <a href="#api-accel-on-ready-callback-Emitted-upon-first-successful-communication-between-the-Tessel-and-the-module" name="api-accel-on-ready-callback-Emitted-upon-first-successful-communication-between-the-Tessel-and-the-module">#</a> accel<b>.on</b>( 'ready', callback() ) | ||
Emitted upon first successful communication between the Tessel and the module. | ||
##### * `accel.on('ready', callback())` Emitted upon first successful communication between the Tessel and the module. | ||
###Further Examples | ||
* [Change Rates](https://github.com/tessel/accel-mma84/blob/master/examples/change-rates.js). This more advanced accelerometer example logs a stream of x, y, and z data, then stops the stream, changes the polling rate, and resumes streaming from the accelerometer. | ||
* [Show Axes](https://github.com/tessel/accel-mma84/blob/master/examples/show-axes.js). Demonstrates axes by turning on a different LED per axis (x, y, z) only when that axis has positive acceleration. Also prints +/- per axis to the console. | ||
##Further Examples | ||
See the examples folder for code. | ||
* show-axes: Manipulate LEDs based on acceleration in the three axes. | ||
* change-rates: Change the polling rate. | ||
## License | ||
MIT | ||
APACHE | ||
###Licensing | ||
MIT or Apache 2.0, at your option |
@@ -1,17 +0,18 @@ | ||
/* test rig */ var t = 1, tmax = 5 | ||
function ok (a, d) { console.log(a ? 'ok ' + (t++) + ' -' : 'not ok ' + (t++) + ' -', d); } | ||
console.log(t + '..' + tmax); | ||
/* script */ | ||
var test = require('tinytap'); | ||
var tessel = require('tessel'); | ||
var accel = require('../').use(tessel.port[process.argv[2] || 'A']); | ||
accel.on('sample', function (xyz) { | ||
ok(Array.isArray(xyz), 'accelerometer data is array'); | ||
ok(xyz.length == 3, 'three samples'); | ||
ok(typeof xyz[0] == 'number', 'idx 0 is number'); | ||
ok(typeof xyz[1] == 'number', 'idx 1 is number'); | ||
ok(typeof xyz[2] == 'number', 'idx 2 is number'); | ||
process.exit(0); | ||
test.count(5); | ||
test('sample count', function (t) { | ||
accel.once('sample', function (xyz) { | ||
t.ok(Array.isArray(xyz), 'accelerometer data is array'); | ||
t.ok(xyz.length == 3, 'three samples'); | ||
t.ok(typeof xyz[0] == 'number', 'idx 0 is number'); | ||
t.ok(typeof xyz[1] == 'number', 'idx 1 is number'); | ||
t.ok(typeof xyz[2] == 'number', 'idx 2 is number'); | ||
t.end(); | ||
}); | ||
}); |
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
38338
12
592
73
2