暫無描述

adxl335.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*jslint node:true, vars:true, bitwise:true, unparam:true */
  2. /*jshint unused:true */
  3. /*
  4. * Author: Zion Orent <zorent@ics.com>
  5. * Copyright (c) 2015 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. var analogGyro3Axis = require("jsupm_adxl335");
  27. var g_addnumBool = true;
  28. var g_cycleNum = 0.0;
  29. var g_cycleCount = 0;
  30. // Instantiate an ADXL335 accelerometer on analog pins A0, A1, and A2
  31. var myAnalogGyro3Axis = new analogGyro3Axis.ADXL335(0, 1, 2);
  32. console.log("Please make sure the sensor is completely still.");
  33. console.log("Sleeping for 2 seconds");
  34. var g_myInterval;
  35. setTimeout(function()
  36. {
  37. console.log("Calibrating...");
  38. myAnalogGyro3Axis.calibrate();
  39. // Get values from accelerometer every 0.2 seconds
  40. g_myInterval = setInterval(runAccelerometer, 200);
  41. }, 2000);
  42. var x = new analogGyro3Axis.new_intPointer();
  43. var y = new analogGyro3Axis.new_intPointer();
  44. var z = new analogGyro3Axis.new_intPointer();
  45. var aX = new analogGyro3Axis.new_floatPointer();
  46. var aY = new analogGyro3Axis.new_floatPointer();
  47. var aZ = new analogGyro3Axis.new_floatPointer();
  48. var outputStr;
  49. function runAccelerometer()
  50. {
  51. myAnalogGyro3Axis.values(x, y, z);
  52. outputStr = "Raw Values: X: " +
  53. analogGyro3Axis.intPointer_value(x) +
  54. " Y: " + analogGyro3Axis.intPointer_value(y) +
  55. " Z: " + analogGyro3Axis.intPointer_value(z);
  56. console.log(outputStr);
  57. myAnalogGyro3Axis.acceleration(aX, aY, aZ);
  58. outputStr = "Acceleration: X: " +
  59. analogGyro3Axis.floatPointer_value(aX) + "g\n" +
  60. "Acceleration: Y: " +
  61. analogGyro3Axis.floatPointer_value(aY) + "g\n" +
  62. "Acceleration: Z: " +
  63. analogGyro3Axis.floatPointer_value(aZ) + "g";
  64. console.log(outputStr);
  65. console.log(" ");
  66. }
  67. // When exiting: clear interval and print exit message
  68. process.on('SIGINT', function()
  69. {
  70. clearInterval(g_myInterval);
  71. console.log("Exiting...");
  72. process.exit(0);
  73. });