summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/adxl335/adxl335.h
blob: e7671da2beb5abbbabff633b7fa8c5b4e472ee6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Adapted from the seeedstudio example
 * https://github.com/Seeed-Studio/Accelerometer_ADXL335
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#pragma once

#include <string>
#include <mraa/aio.h>

#define ADXL335_DEFAULT_AREF 5.0
#define ADXL335_SENSITIVITY 0.25 // 0.25v/g

namespace upm {

  /**
   * @brief ADXL335 Accelerometer library
   * @defgroup adxl335 libupm-adxl335
   * @ingroup seeed analog accelerometer
   */

  /**
   * @library adxl335
   * @sensor adxl335
   * @comname ADXL335 3-Axis Analog Accelerometer
   * @altname Grove 3-Axis Analog Accelerometer
   * @type accelerometer
   * @man seeed
   * @con analog
   *
   * @brief API for the ADXL335 3-Axis Analog Accelerometer
   *
   * UPM module for the ADXL335 3-axis analog accelerometer. This
   * was tested on a Grove 3-axis Analog Accelerometer. It uses 3
   * analog pins, one for each axis: X, Y, and Z.
   *
   * @image html adxl335.jpg
   * @snippet adxl335.cxx Interesting
   */
  class ADXL335 {
  public:
    /**
     * ADXL335 constructor
     *
     * @param pinX Analog pin to use for X-axis
     * @param pinY Analog pin to use for Y-axis
     * @param pinZ Analog pin to use for Z-axis
     * @param aref Analog reference voltage; default is 5.0v
     */
    ADXL335(int pinX, int pinY, int pinZ, float aref=ADXL335_DEFAULT_AREF);

    /**
     * ADXL335 destructor
     */
    ~ADXL335();

    /**
     * Sets the "zero" value of the X-axis, determined through calibration
     *
     * @param zeroX  "Zero" value of the X-axis
     */
    void setZeroX(float zeroX) { m_zeroX = zeroX; };

    /**
     * Sets the "zero" value of the Y-axis, determined through calibration
     *
     * @param zeroY  "Zero" value of the Y-axis
     */
    void setZeroY(float zeroY) { m_zeroY = zeroY; };

    /**
     * Sets the "zero" value of the Z-axis, determined through calibration
     *
     * @param zeroZ  "Zero" value of the Z-axis
     */
    void setZeroZ(float zeroZ) { m_zeroZ = zeroZ; };

    /**
     * Gets the analog values for the 3 axes
     *
     * @param xVal Pointer to the returned X-axis value
     * @param yVal Pointer to the returned Y-axis value
     * @param zVal Pointer to the returned Z-axis value
     */
    void values(int *xVal, int *yVal, int *zVal);

#ifdef SWIGJAVA
    /**
     * Gets the analog values for the 3 axes
     *
     * @return Array containing value of X, Y, Z axes
     */
    int *values();
#endif

    /**
     * Gets the acceleration along all 3 axes
     *
     * @param xAccel Pointer to returned X-axis value
     * @param yAccel Pointer to returned Y-axis value
     * @param zAccel Pointer to returned Z-axis value
     */
    void acceleration(float *xAccel, float *yAccel, float *zAccel);

#ifdef SWIGJAVA
    /**
     * Gets the acceleration along all 3 axes
     *
     * @return Array containing acceleration on X, Y, Z axes
     */
    float *acceleration();
#endif

    /**
     * While the sensor is still, measures the X-axis, Y-axis, and Z-axis
     * values and uses those values as the zero values.
     */
    void calibrate();

  private:
    mraa_aio_context m_aioX;
    mraa_aio_context m_aioY;
    mraa_aio_context m_aioZ;
    float m_aref;
    float m_zeroX, m_zeroY, m_zeroZ;
  };
}