No Description

mma7660.js 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 digitalAccelerometer = require('jsupm_mma7660');
  27. // Instantiate an MMA7660 on I2C bus 0
  28. var myDigitalAccelerometer = new digitalAccelerometer.MMA7660(
  29. digitalAccelerometer.MMA7660_I2C_BUS,
  30. digitalAccelerometer.MMA7660_DEFAULT_I2C_ADDR);
  31. // place device in standby mode so we can write registers
  32. myDigitalAccelerometer.setModeStandby();
  33. // enable 64 samples per second
  34. myDigitalAccelerometer.setSampleRate(digitalAccelerometer.MMA7660.AUTOSLEEP_64);
  35. // place device into active mode
  36. myDigitalAccelerometer.setModeActive();
  37. var x, y, z;
  38. x = digitalAccelerometer.new_intp();
  39. y = digitalAccelerometer.new_intp();
  40. z = digitalAccelerometer.new_intp();
  41. var ax, ay, az;
  42. ax = digitalAccelerometer.new_floatp();
  43. ay = digitalAccelerometer.new_floatp();
  44. az = digitalAccelerometer.new_floatp();
  45. var outputStr;
  46. var myInterval = setInterval(function()
  47. {
  48. myDigitalAccelerometer.getRawValues(x, y, z);
  49. outputStr = "Raw values: x = " + digitalAccelerometer.intp_value(x) +
  50. " y = " + digitalAccelerometer.intp_value(y) +
  51. " z = " + digitalAccelerometer.intp_value(z);
  52. console.log(outputStr);
  53. myDigitalAccelerometer.getAcceleration(ax, ay, az);
  54. outputStr = "Acceleration: x = "
  55. + roundNum(digitalAccelerometer.floatp_value(ax), 6)
  56. + "g y = " + roundNum(digitalAccelerometer.floatp_value(ay), 6)
  57. + "g z = " + roundNum(digitalAccelerometer.floatp_value(az), 6) + "g";
  58. console.log(outputStr);
  59. }, 500);
  60. // round off output to match C example, which has 6 decimal places
  61. function roundNum(num, decimalPlaces)
  62. {
  63. var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000));
  64. return (Math.round((num + extraNum)
  65. * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces));
  66. }
  67. // When exiting: clear interval and print message
  68. process.on('SIGINT', function()
  69. {
  70. clearInterval(myInterval);
  71. // clean up memory
  72. digitalAccelerometer.delete_intp(x);
  73. digitalAccelerometer.delete_intp(y);
  74. digitalAccelerometer.delete_intp(z);
  75. digitalAccelerometer.delete_floatp(ax);
  76. digitalAccelerometer.delete_floatp(ay);
  77. digitalAccelerometer.delete_floatp(az);
  78. myDigitalAccelerometer.setModeStandby();
  79. console.log("Exiting...");
  80. process.exit(0);
  81. });