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
24import time, sys, signal, atexit
25import pyupm_mma7660 as upmMMA7660
26
27# Instantiate an MMA7660 on I2C bus 0
28myDigitalAccelerometer = upmMMA7660.MMA7660(
29					upmMMA7660.MMA7660_I2C_BUS,
30					upmMMA7660.MMA7660_DEFAULT_I2C_ADDR);
31
32
33## Exit handlers ##
34# This function stops python from printing a stacktrace when you hit control-C
35def SIGINTHandler(signum, frame):
36	raise SystemExit
37
38# This function lets you run code on exit, including functions from myDigitalAccelerometer
39def exitHandler():
40	print "Exiting"
41	sys.exit(0)
42
43# Register exit handlers
44atexit.register(exitHandler)
45signal.signal(signal.SIGINT, SIGINTHandler)
46
47
48# place device in standby mode so we can write registers
49myDigitalAccelerometer.setModeStandby()
50
51# enable 64 samples per second
52myDigitalAccelerometer.setSampleRate(upmMMA7660.MMA7660.AUTOSLEEP_64)
53
54# place device into active mode
55myDigitalAccelerometer.setModeActive()
56
57x = upmMMA7660.new_intp()
58y = upmMMA7660.new_intp()
59z = upmMMA7660.new_intp()
60
61ax = upmMMA7660.new_floatp()
62ay = upmMMA7660.new_floatp()
63az = upmMMA7660.new_floatp()
64
65while (1):
66	myDigitalAccelerometer.getRawValues(x, y, z)
67	outputStr = ("Raw values: x = {0}"
68	" y = {1}"
69	" z = {2}").format(upmMMA7660.intp_value(x),
70	upmMMA7660.intp_value(y),
71	upmMMA7660.intp_value(z))
72	print outputStr
73
74	myDigitalAccelerometer.getAcceleration(ax, ay, az)
75	outputStr = ("Acceleration: x = {0}"
76	"g y = {1}"
77	"g z = {2}g").format(upmMMA7660.floatp_value(ax),
78	upmMMA7660.floatp_value(ay),
79	upmMMA7660.floatp_value(az))
80	print outputStr
81	time.sleep(.5)
82