No Description

mma7660.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/python
  2. # Author: Zion Orent <zorent@ics.com>
  3. # Copyright (c) 2015 Intel Corporation.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining
  6. # a copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish,
  9. # distribute, sublicense, and/or sell copies of the Software, and to
  10. # permit persons to whom the Software is furnished to do so, subject to
  11. # the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. import time, sys, signal, atexit
  24. import pyupm_mma7660 as upmMMA7660
  25. # Instantiate an MMA7660 on I2C bus 0
  26. myDigitalAccelerometer = upmMMA7660.MMA7660(
  27. upmMMA7660.MMA7660_I2C_BUS,
  28. upmMMA7660.MMA7660_DEFAULT_I2C_ADDR);
  29. ## Exit handlers ##
  30. # This function stops python from printing a stacktrace when you hit control-C
  31. def SIGINTHandler(signum, frame):
  32. raise SystemExit
  33. # This function lets you run code on exit, including functions from myDigitalAccelerometer
  34. def exitHandler():
  35. print "Exiting"
  36. sys.exit(0)
  37. # Register exit handlers
  38. atexit.register(exitHandler)
  39. signal.signal(signal.SIGINT, SIGINTHandler)
  40. # place device in standby mode so we can write registers
  41. myDigitalAccelerometer.setModeStandby()
  42. # enable 64 samples per second
  43. myDigitalAccelerometer.setSampleRate(upmMMA7660.MMA7660.AUTOSLEEP_64)
  44. # place device into active mode
  45. myDigitalAccelerometer.setModeActive()
  46. x = upmMMA7660.new_intp()
  47. y = upmMMA7660.new_intp()
  48. z = upmMMA7660.new_intp()
  49. ax = upmMMA7660.new_floatp()
  50. ay = upmMMA7660.new_floatp()
  51. az = upmMMA7660.new_floatp()
  52. while (1):
  53. myDigitalAccelerometer.getRawValues(x, y, z)
  54. outputStr = ("Raw values: x = {0}"
  55. " y = {1}"
  56. " z = {2}").format(upmMMA7660.intp_value(x),
  57. upmMMA7660.intp_value(y),
  58. upmMMA7660.intp_value(z))
  59. print outputStr
  60. myDigitalAccelerometer.getAcceleration(ax, ay, az)
  61. outputStr = ("Acceleration: x = {0}"
  62. "g y = {1}"
  63. "g z = {2}g").format(upmMMA7660.floatp_value(ax),
  64. upmMMA7660.floatp_value(ay),
  65. upmMMA7660.floatp_value(az))
  66. print outputStr
  67. time.sleep(.5)