]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/peripherals/MMA7660/MMA7660.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / peripherals / MMA7660 / MMA7660.h
1 /* Copyright (c) <year> <copyright holders>, MIT License
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4  * and associated documentation files (the "Software"), to deal in the Software without restriction,
5  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
6  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7  * furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in all copies or
10  * substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17  */
18
19 #include "mbed.h"
20
21
22 #ifndef MMA7660_H
23 #define MMA7660_H
24
25 #define MMA7660_ADDRESS     0x98
26 #define MMA7660_SENSITIVITY 21.33
27
28 #define MMA7660_XOUT_R      0x00
29 #define MMA7660_YOUT_R      0x01
30 #define MMA7660_ZOUT_R      0x02
31 #define MMA7660_TILT_R      0x03
32 #define MMA7660_INT_R       0x06
33 #define MMA7660_MODE_R      0x07
34 #define MMA7660_SR_R        0x08
35
36
37 /** An interface for the MMA7660 triple axis accelerometer
38  *
39  * @code
40  * //Uses the measured z-acceleration to drive leds 2 and 3 of the mbed
41  *
42  * #include "mbed.h"
43  * #include "MMA7660.h"
44  *
45  * MMA7660 MMA(p28, p27);
46  *
47  * DigitalOut connectionLed(LED1);
48  * PwmOut Zaxis_p(LED2);
49  * PwmOut Zaxis_n(LED3);
50  *
51  * int main() {
52  *     if (MMA.testConnection())
53  *         connectionLed = 1;
54  *
55  *     while(1) {
56  *         Zaxis_p = MMA.z();
57  *         Zaxis_n = -MMA.z();
58  *     }
59  *
60  * }
61  * @endcode
62  */
63 class MMA7660
64 {
65 public:
66     /**
67     * The 6 different orientations and unknown
68     *
69     * Up & Down = X-axis
70     * Right & Left = Y-axis
71     * Back & Front = Z-axis
72     *
73     */
74     enum Orientation {Up, Down,
75                       Right, Left,
76                       Back, Front,
77                       Unknown
78                      };
79
80     /**
81     * Creates a new MMA7660 object
82     *
83     * @param sda - I2C data pin
84     * @param scl - I2C clock pin
85     * @param active - true (default) to enable the device, false to keep it standby
86     */
87     MMA7660(PinName sda, PinName scl, bool active = true);
88
89     /**
90     * Tests if communication is possible with the MMA7660
91     *
92     * Because the MMA7660 lacks a WHO_AM_I register, this function can only check
93     * if there is an I2C device that responds to the MMA7660 address
94     *
95     * @param return - true for successfull connection, false for no connection
96     */
97     bool testConnection( void );
98
99     /**
100     * Sets the active state of the MMA7660
101     *
102     * Note: This is unrelated to awake/sleep mode
103     *
104     * @param state - true for active, false for standby
105     */
106     void setActive( bool state);
107
108     /**
109     * Reads acceleration data from the sensor
110     *
111     * When the parameter is a pointer to an integer array it will be the raw data.
112     * When it is a pointer to a float array it will be the acceleration in g's
113     *
114     * @param data - pointer to array with length 3 where the acceleration data will be stored, X-Y-Z
115     */
116     void readData( int *data);
117     void readData( float *data);
118
119     /**
120     * Get X-data
121     *
122     * @param return - X-acceleration in g's
123     */
124     float x( void );
125
126     /**
127     * Get Y-data
128     *
129     * @param return - Y-acceleration in g's
130     */
131     float y( void );
132
133     /**
134     * Get Z-data
135     *
136     * @param return - Z-acceleration in g's
137     */
138     float z( void );
139
140     /**
141     * Sets the active samplerate
142     *
143     * The entered samplerate will be rounded to nearest supported samplerate.
144     * Supported samplerates are: 120 - 64 - 32 - 16 - 8 - 4 - 2 - 1 samples/second.
145     *
146     * @param samplerate - the samplerate that will be set
147     */
148     void setSampleRate(int samplerate);
149
150     /**
151     * Returns if it is on its front, back, or unknown side
152     *
153     * This is read from MMA7760s registers, page 12 of datasheet
154     *
155     * @param return - Front, Back or Unknown orientation
156     */
157     Orientation getSide( void );
158
159     /**
160     * Returns if it is on it left, right, down or up side
161     *
162     * This is read from MMA7760s registers, page 12 of datasheet
163     *
164     * @param return - Left, Right, Down, Up or Unknown orientation
165     */
166     Orientation getOrientation ( void );
167
168
169 private:
170
171     /**
172     * Writes data to the device
173     *
174     * @param adress - register address to write to
175     * @param data - data to write
176     */
177     void write( char address, char data);
178
179     /**
180     * Read data from the device
181     *
182     * @param adress - register address to write to
183     * @return - data from the register specified by RA
184     */
185     char read( char adress);
186
187     /**
188      * Read multiple regigsters from the device, more efficient than using multiple normal reads.
189      *
190      * @param adress - register address to write to
191      * @param length - number of bytes to read
192      * @param data - pointer where the data needs to be written to
193      */
194     void read( char adress, char *data, int length);
195
196     /**
197     * Reads single axis
198     */
199     float getSingle(int number);
200
201     I2C _i2c;
202     bool active;
203     float samplerate;
204 };
205
206
207 #endif