Home | History | Annotate | Download | only in javascript
      1 /*jslint node:true, vars:true, bitwise:true, unparam:true */
      2 /*jshint unused:true */
      3 /*
      4 * Author: Zion Orent <zorent (at) ics.com>
      5 * Copyright (c) 2014 Intel Corporation.
      6 *
      7 * Permission is hereby granted, free of charge, to any person obtaining
      8 * a copy of this software and associated documentation files (the
      9 * "Software"), to deal in the Software without restriction, including
     10 * without limitation the rights to use, copy, modify, merge, publish,
     11 * distribute, sublicense, and/or sell copies of the Software, and to
     12 * permit persons to whom the Software is furnished to do so, subject to
     13 * the following conditions:
     14 *
     15 * The above copyright notice and this permission notice shall be
     16 * included in all copies or substantial portions of the Software.
     17 *
     18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     25 */
     26 
     27 var accelrCompassSensor = require('jsupm_lsm303');
     28 
     29 // Instantiate LSM303 compass on I2C
     30 var myAccelrCompass = new accelrCompassSensor.LSM303(0);
     31 
     32 var successFail, coords, outputStr, accel;
     33 var myInterval = setInterval(function()
     34 {
     35 	// Load coordinates into LSM303 object
     36 	successFail = myAccelrCompass.getCoordinates();
     37 	// in XYZ order. The sensor returns XZY,
     38 	// but the driver compensates and makes it XYZ
     39 	coords = myAccelrCompass.getRawCoorData();
     40 
     41     // Print out the X, Y, and Z coordinate data using two different methods
     42 	outputStr = "coor: rX " + coords.getitem(0)
     43 					+ " - rY " + coords.getitem(1)
     44 					+ " - rZ " + coords.getitem(2);
     45 	console.log(outputStr);
     46 	outputStr = "coor: gX " + myAccelrCompass.getCoorX()
     47 				+ " - gY " + myAccelrCompass.getCoorY()
     48 				+ " - gZ " + myAccelrCompass.getCoorZ();
     49 	console.log(outputStr);
     50 
     51     // Get and print out the heading
     52 	console.log("heading: " + myAccelrCompass.getHeading());
     53 
     54     // Get the acceleration
     55 	myAccelrCompass.getAcceleration();
     56 	accel = myAccelrCompass.getRawAccelData();
     57     // Print out the X, Y, and Z acceleration data using two different methods
     58 	outputStr = "acc: rX " + accel.getitem(0)
     59 				+ " - rY " + accel.getitem(1)
     60 				+ " - Z " + accel.getitem(2);
     61 	console.log(outputStr);
     62 	outputStr = "acc: gX " + myAccelrCompass.getAccelX()
     63 				+ " - gY " + myAccelrCompass.getAccelY()
     64 				+ " - gZ " + myAccelrCompass.getAccelZ();
     65 	console.log(outputStr);
     66 	console.log(" ");
     67 }, 1000);
     68 
     69 // Print message when exiting
     70 process.on('SIGINT', function()
     71 {
     72 	clearInterval(myInterval);
     73 	myAccelrCompass = null;
     74 	accelrCompassSensor.cleanUp();
     75 	accelrCompassSensor = null;
     76 	console.log("Exiting");
     77 	process.exit(0);
     78 });
     79