Brak opisu

h3lis331dl.js 3.1KB

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