Browse Source

mma7660: python example for mma7660 accelerometer

Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Zion Orent 9 years ago
parent
commit
d8dc267fef
2 changed files with 86 additions and 0 deletions
  1. 81
    0
      examples/python/mma7660.py
  2. 5
    0
      src/mma7660/pyupm_mma7660.i

+ 81
- 0
examples/python/mma7660.py View File

@@ -0,0 +1,81 @@
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
+
24
+import time, sys, signal, atexit
25
+import pyupm_mma7660 as upmMMA7660
26
+
27
+# Instantiate an MMA7660 on I2C bus 0
28
+myDigitalAccelerometer = 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
35
+def SIGINTHandler(signum, frame):
36
+	raise SystemExit
37
+
38
+# This function lets you run code on exit, including functions from myDigitalAccelerometer
39
+def exitHandler():
40
+	print "Exiting"
41
+	sys.exit(0)
42
+
43
+# Register exit handlers
44
+atexit.register(exitHandler)
45
+signal.signal(signal.SIGINT, SIGINTHandler)
46
+
47
+
48
+# place device in standby mode so we can write registers
49
+myDigitalAccelerometer.setModeStandby()
50
+
51
+# enable 64 samples per second
52
+myDigitalAccelerometer.setSampleRate(upmMMA7660.MMA7660.AUTOSLEEP_64)
53
+
54
+# place device into active mode
55
+myDigitalAccelerometer.setModeActive()
56
+
57
+x = upmMMA7660.new_intp()
58
+y = upmMMA7660.new_intp()
59
+z = upmMMA7660.new_intp()
60
+
61
+ax = upmMMA7660.new_floatp()
62
+ay = upmMMA7660.new_floatp()
63
+az = upmMMA7660.new_floatp()
64
+
65
+while (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)

+ 5
- 0
src/mma7660/pyupm_mma7660.i View File

@@ -1,5 +1,10 @@
1 1
 %module pyupm_mma7660
2 2
 %include "../upm.i"
3
+%include "cpointer.i"
4
+
5
+/* Send "int *" and "float *" to python as intp and floatp */
6
+%pointer_functions(int, intp);
7
+%pointer_functions(float, floatp);
3 8
 
4 9
 %feature("autodoc", "3");
5 10