1/*jslint node:true, vars:true, bitwise:true, unparam:true */ 2/*jshint unused:true */ 3 4/* 5* Author: Zion Orent <zorent@ics.com> 6* Copyright (c) 2015 Intel Corporation. 7* 8* Permission is hereby granted, free of charge, to any person obtaining 9* a copy of this software and associated documentation files (the 10* "Software"), to deal in the Software without restriction, including 11* without limitation the rights to use, copy, modify, merge, publish, 12* distribute, sublicense, and/or sell copies of the Software, and to 13* permit persons to whom the Software is furnished to do so, subject to 14* the following conditions: 15* 16* The above copyright notice and this permission notice shall be 17* included in all copies or substantial portions of the Software. 18* 19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26*/ 27 28var digitalAccelerometer = require('jsupm_mma7660'); 29 30// Instantiate an MMA7660 on I2C bus 0 31var myDigitalAccelerometer = new digitalAccelerometer.MMA7660( 32 digitalAccelerometer.MMA7660_I2C_BUS, 33 digitalAccelerometer.MMA7660_DEFAULT_I2C_ADDR); 34 35// place device in standby mode so we can write registers 36myDigitalAccelerometer.setModeStandby(); 37 38// enable 64 samples per second 39myDigitalAccelerometer.setSampleRate(digitalAccelerometer.MMA7660.AUTOSLEEP_64); 40 41// place device into active mode 42myDigitalAccelerometer.setModeActive(); 43 44var x, y, z; 45x = digitalAccelerometer.new_intp(); 46y = digitalAccelerometer.new_intp(); 47z = digitalAccelerometer.new_intp(); 48 49var ax, ay, az; 50ax = digitalAccelerometer.new_floatp(); 51ay = digitalAccelerometer.new_floatp(); 52az = digitalAccelerometer.new_floatp(); 53 54var outputStr; 55 56var myInterval = setInterval(function() 57{ 58 myDigitalAccelerometer.getRawValues(x, y, z); 59 outputStr = "Raw values: x = " + digitalAccelerometer.intp_value(x) + 60 " y = " + digitalAccelerometer.intp_value(y) + 61 " z = " + digitalAccelerometer.intp_value(z); 62 console.log(outputStr); 63 64 myDigitalAccelerometer.getAcceleration(ax, ay, az); 65 outputStr = "Acceleration: x = " 66 + roundNum(digitalAccelerometer.floatp_value(ax), 6) 67 + "g y = " + roundNum(digitalAccelerometer.floatp_value(ay), 6) 68 + "g z = " + roundNum(digitalAccelerometer.floatp_value(az), 6) + "g"; 69 console.log(outputStr); 70}, 500); 71 72// round off output to match C example, which has 6 decimal places 73function roundNum(num, decimalPlaces) 74{ 75 var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000)); 76 return (Math.round((num + extraNum) 77 * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces)); 78} 79 80// When exiting: clear interval and print message 81process.on('SIGINT', function() 82{ 83 clearInterval(myInterval); 84 85 // clean up memory 86 digitalAccelerometer.delete_intp(x); 87 digitalAccelerometer.delete_intp(y); 88 digitalAccelerometer.delete_intp(z); 89 90 digitalAccelerometer.delete_floatp(ax); 91 digitalAccelerometer.delete_floatp(ay); 92 digitalAccelerometer.delete_floatp(az); 93 94 myDigitalAccelerometer.setModeStandby(); 95 96 console.log("Exiting..."); 97 process.exit(0); 98}); 99