mirror of
https://github.com/eclipse/upm.git
synced 2025-07-01 17:31:13 +03:00
mma7660: Initial implementation
This was tested on the Grove I2C 3-axis digital accelerometer. Signed-off-by: Jon Trulson <jtrulson@ics.com> Signed-off-by: Zion Orent <zorent@ics.com> Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
This commit is contained in:

committed by
John Van Drasek

parent
e0e9405634
commit
12c290de70
83
examples/javascript/mma7660.js
Normal file
83
examples/javascript/mma7660.js
Normal file
@ -0,0 +1,83 @@
|
||||
/*jslint node:true, vars:true, bitwise:true, unparam:true */
|
||||
/*jshint unused:true */
|
||||
|
||||
/*
|
||||
* Author: Zion Orent <zorent@ics.com>
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
var digitalAccelerometer = require('jsupm_mma7660');
|
||||
|
||||
// Instantiate an MMA7660 on I2C bus 0
|
||||
var myDigitalAccelerometer = new digitalAccelerometer.MMA7660(
|
||||
digitalAccelerometer.MMA7660_I2C_BUS,
|
||||
digitalAccelerometer.MMA7660_DEFAULT_I2C_ADDR
|
||||
);
|
||||
|
||||
// place device in standby mode so we can write registers
|
||||
myDigitalAccelerometer.setModeStandby();
|
||||
|
||||
// enable 64 samples per second
|
||||
myDigitalAccelerometer.setSampleRate(digitalAccelerometer.MMA7660.AUTOSLEEP_64);
|
||||
|
||||
// place device into active mode
|
||||
myDigitalAccelerometer.setModeActive();
|
||||
|
||||
var myInterval = setInterval(function()
|
||||
{
|
||||
var x, y, z;
|
||||
x = digitalAccelerometer.new_intp();
|
||||
y = digitalAccelerometer.new_intp();
|
||||
z = digitalAccelerometer.new_intp();
|
||||
|
||||
myDigitalAccelerometer.getRawValues(x, y, z);
|
||||
var outputStr = "Raw values: x = " + digitalAccelerometer.intp_value(x) +
|
||||
" y = " + digitalAccelerometer.intp_value(y) +
|
||||
" z = " + digitalAccelerometer.intp_value(z);
|
||||
console.log(outputStr);
|
||||
|
||||
var ax, ay, az;
|
||||
ax = digitalAccelerometer.new_floatp();
|
||||
ay = digitalAccelerometer.new_floatp();
|
||||
az = digitalAccelerometer.new_floatp();
|
||||
|
||||
myDigitalAccelerometer.getAcceleration(ax, ay, az);
|
||||
outputStr = "Acceleration: x = " + roundNum(digitalAccelerometer.floatp_value(ax), 6) +
|
||||
"g y = " + roundNum(digitalAccelerometer.floatp_value(ay), 6) +
|
||||
"g z = " + roundNum(digitalAccelerometer.floatp_value(az), 6) + "g";
|
||||
console.log(outputStr);
|
||||
}, 500);
|
||||
|
||||
// round off output to match C example, which has 6 decimal places
|
||||
function roundNum(num, decimalPlaces)
|
||||
{
|
||||
var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000));
|
||||
return (Math.round((num + extraNum) * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces));
|
||||
}
|
||||
|
||||
// When exiting: clear interval and print message
|
||||
process.on('SIGINT', function()
|
||||
{
|
||||
clearInterval(myInterval);
|
||||
console.log("Exiting...");
|
||||
process.exit(0);
|
||||
});
|
Reference in New Issue
Block a user