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 #pragma once
28 
29 #include <string>
30 #include <mraa/aio.h>
31 
32 #define ADXL335_DEFAULT_AREF 5.0
33 #define ADXL335_SENSITIVITY 0.25 // 0.25v/g
34 
35 namespace upm {
36 
37   /**
38    * @brief ADXL335 Accelerometer library
39    * @defgroup adxl335 libupm-adxl335
40    * @ingroup seeed analog accelerometer
41    */
42 
43   /**
44    * @library adxl335
45    * @sensor adxl335
46    * @comname ADXL335 3-Axis Analog Accelerometer
47    * @altname Grove 3-Axis Analog Accelerometer
48    * @type accelerometer
49    * @man seeed
50    * @con analog
51    *
52    * @brief API for the ADXL335 3-Axis Analog Accelerometer
53    *
54    * UPM module for the ADXL335 3-axis analog accelerometer. This
55    * was tested on a Grove 3-axis Analog Accelerometer. It uses 3
56    * analog pins, one for each axis: X, Y, and Z.
57    *
58    * @image html adxl335.jpg
59    * @snippet adxl335.cxx Interesting
60    */
61   class ADXL335 {
62   public:
63     /**
64      * ADXL335 constructor
65      *
66      * @param pinX Analog pin to use for X-axis
67      * @param pinY Analog pin to use for Y-axis
68      * @param pinZ Analog pin to use for Z-axis
69      * @param aref Analog reference voltage; default is 5.0v
70      */
71     ADXL335(int pinX, int pinY, int pinZ, float aref=ADXL335_DEFAULT_AREF);
72 
73     /**
74      * ADXL335 destructor
75      */
76     ~ADXL335();
77 
78     /**
79      * Sets the "zero" value of the X-axis, determined through calibration
80      *
81      * @param zeroX  "Zero" value of the X-axis
82      */
setZeroX(float zeroX)83     void setZeroX(float zeroX) { m_zeroX = zeroX; };
84 
85     /**
86      * Sets the "zero" value of the Y-axis, determined through calibration
87      *
88      * @param zeroY  "Zero" value of the Y-axis
89      */
setZeroY(float zeroY)90     void setZeroY(float zeroY) { m_zeroY = zeroY; };
91 
92     /**
93      * Sets the "zero" value of the Z-axis, determined through calibration
94      *
95      * @param zeroZ  "Zero" value of the Z-axis
96      */
setZeroZ(float zeroZ)97     void setZeroZ(float zeroZ) { m_zeroZ = zeroZ; };
98 
99     /**
100      * Gets the analog values for the 3 axes
101      *
102      * @param xVal Pointer to the returned X-axis value
103      * @param yVal Pointer to the returned Y-axis value
104      * @param zVal Pointer to the returned Z-axis value
105      */
106     void values(int *xVal, int *yVal, int *zVal);
107 
108 #ifdef SWIGJAVA
109     /**
110      * Gets the analog values for the 3 axes
111      *
112      * @return Array containing value of X, Y, Z axes
113      */
114     int *values();
115 #endif
116 
117     /**
118      * Gets the acceleration along all 3 axes
119      *
120      * @param xAccel Pointer to returned X-axis value
121      * @param yAccel Pointer to returned Y-axis value
122      * @param zAccel Pointer to returned Z-axis value
123      */
124     void acceleration(float *xAccel, float *yAccel, float *zAccel);
125 
126 #ifdef SWIGJAVA
127     /**
128      * Gets the acceleration along all 3 axes
129      *
130      * @return Array containing acceleration on X, Y, Z axes
131      */
132     float *acceleration();
133 #endif
134 
135     /**
136      * While the sensor is still, measures the X-axis, Y-axis, and Z-axis
137      * values and uses those values as the zero values.
138      */
139     void calibrate();
140 
141   private:
142     mraa_aio_context m_aioX;
143     mraa_aio_context m_aioY;
144     mraa_aio_context m_aioZ;
145     float m_aref;
146     float m_zeroX, m_zeroY, m_zeroZ;
147   };
148 }
149 
150 
151