1/*jslint node:true, vars:true, bitwise:true, unparam:true */ 2/*jshint unused:true */ 3 4/* 5* Author: Jon Trulson <jtrulson@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_h3lis331dl'); 29 30// Instantiate an H3LIS331DL on I2C bus 0 31var myDigitalAccelerometer = new digitalAccelerometer.H3LIS331DL( 32 digitalAccelerometer.H3LIS331DL_I2C_BUS, 33 digitalAccelerometer.H3LIS331DL_DEFAULT_I2C_ADDR); 34 35// Initialize the device with default values 36myDigitalAccelerometer.init(); 37 38var x, y, z; 39x = digitalAccelerometer.new_intp(); 40y = digitalAccelerometer.new_intp(); 41z = digitalAccelerometer.new_intp(); 42 43var ax, ay, az; 44ax = digitalAccelerometer.new_floatp(); 45ay = digitalAccelerometer.new_floatp(); 46az = digitalAccelerometer.new_floatp(); 47 48var outputStr; 49 50var myInterval = setInterval(function() 51{ 52 myDigitalAccelerometer.update(); 53 myDigitalAccelerometer.getRawXYZ(x, y, z); 54 outputStr = "Raw: X = " + digitalAccelerometer.intp_value(x) + 55 " Y = " + digitalAccelerometer.intp_value(y) + 56 " Z = " + digitalAccelerometer.intp_value(z); 57 console.log(outputStr); 58 59 myDigitalAccelerometer.getAcceleration(ax, ay, az); 60 outputStr = "Acceleration: AX = " 61 + roundNum(digitalAccelerometer.floatp_value(ax), 6) 62 + " AY = " + roundNum(digitalAccelerometer.floatp_value(ay), 6) 63 + " AZ = " + roundNum(digitalAccelerometer.floatp_value(az), 6); 64 console.log(outputStr); 65}, 500); 66 67// round off output to match C example, which has 6 decimal places 68function roundNum(num, decimalPlaces) 69{ 70 var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000)); 71 return (Math.round((num + extraNum) 72 * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces)); 73} 74 75// When exiting: clear interval and print message 76process.on('SIGINT', function() 77{ 78 clearInterval(myInterval); 79 80 // clean up memory 81 digitalAccelerometer.delete_intp(x); 82 digitalAccelerometer.delete_intp(y); 83 digitalAccelerometer.delete_intp(z); 84 85 digitalAccelerometer.delete_floatp(ax); 86 digitalAccelerometer.delete_floatp(ay); 87 digitalAccelerometer.delete_floatp(az); 88 89 console.log("Exiting..."); 90 process.exit(0); 91}); 92