1 /*
2 * Author: Jon Trulson <jtrulson@ics.com>
3 * Copyright (c) 2015 Intel Corporation.
4 *
5 * Adapted from the seeedstudio example
6 * https://github.com/Seeed-Studio/Accelerometer_ADXL335
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 #include <iostream>
29 #include <string>
30 #include <stdexcept>
31
32 #include "adxl335.h"
33
34 using namespace std;
35 using namespace upm;
36
ADXL335(int pinX,int pinY,int pinZ,float aref)37 ADXL335::ADXL335(int pinX, int pinY, int pinZ, float aref)
38 {
39 m_aref = aref;
40 m_zeroX = 0.0;
41 m_zeroY = 0.0;
42 m_zeroZ = 0.0;
43
44 if ( !(m_aioX = mraa_aio_init(pinX)) )
45 {
46 throw std::invalid_argument(std::string(__FUNCTION__) +
47 ": mraa_aio_init(X) failed, invalid pin?");
48 return;
49 }
50
51 if ( !(m_aioY = mraa_aio_init(pinY)) )
52 {
53 throw std::invalid_argument(std::string(__FUNCTION__) +
54 ": mraa_aio_init(Y) failed, invalid pin?");
55 return;
56 }
57
58 if ( !(m_aioZ = mraa_aio_init(pinZ)) )
59 {
60 throw std::invalid_argument(std::string(__FUNCTION__) +
61 ": mraa_aio_init(Z) failed, invalid pin?");
62 return;
63 }
64 }
65
~ADXL335()66 ADXL335::~ADXL335()
67 {
68 mraa_aio_close(m_aioX);
69 mraa_aio_close(m_aioY);
70 mraa_aio_close(m_aioZ);
71 }
72
values(int * xVal,int * yVal,int * zVal)73 void ADXL335::values(int *xVal, int *yVal, int *zVal)
74 {
75 *xVal = mraa_aio_read(m_aioX);
76 *yVal = mraa_aio_read(m_aioY);
77 *zVal = mraa_aio_read(m_aioZ);
78 }
79
80 #ifdef SWIGJAVA
values()81 int *ADXL335::values()
82 {
83 int *v = new int[3];
84 values(&v[0], &v[1], &v[2]);
85 return v;
86 }
87 #endif
88
acceleration(float * xAccel,float * yAccel,float * zAccel)89 void ADXL335::acceleration(float *xAccel, float *yAccel, float *zAccel)
90 {
91 int x, y, z;
92 float xVolts, yVolts, zVolts;
93
94 values(&x, &y, &z);
95 xVolts = float(x) * m_aref / 1024.0;
96 yVolts = float(y) * m_aref / 1024.0;
97 zVolts = float(z) * m_aref / 1024.0;
98
99 *xAccel = (xVolts - m_zeroX) / ADXL335_SENSITIVITY;
100 *yAccel = (yVolts - m_zeroY) / ADXL335_SENSITIVITY;
101 *zAccel = (zVolts - m_zeroZ) / ADXL335_SENSITIVITY;
102 }
103
104 #ifdef SWIGJAVA
acceleration()105 float *ADXL335::acceleration()
106 {
107 float *v = new float[3];
108 acceleration(&v[0], &v[1], &v[2]);
109 return v;
110 }
111 #endif
112
calibrate()113 void ADXL335::calibrate()
114 {
115 // make sure the sensor is still before running calibration.
116
117 int x, y, z;
118 usleep(10000);
119
120 values(&x, &y, &z);
121
122 setZeroX(float(x) * m_aref / 1024.0);
123 setZeroY(float(y) * m_aref / 1024.0);
124 setZeroZ(float(z) * m_aref / 1024.0);
125 }
126