]> Pileus Git - ~andy/csm213a-hw/blob - yue/MMA8451Q/MMA8451Q.h
Add sensors code from Yue
[~andy/csm213a-hw] / yue / MMA8451Q / MMA8451Q.h
1 /* Copyright (c) 2010-2011 mbed.org, 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
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is 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 #ifndef MMA8451Q_H
20 #define MMA8451Q_H
21
22 #include "mbed.h"
23
24 /**
25 * MMA8451Q accelerometer example
26 *
27 * @code
28 * #include "mbed.h"
29 * #include "MMA8451Q.h"
30
31 * #define MMA8451_I2C_ADDRESS (0x1d<<1)
32
33 * int main(void) {
34
35 * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS);
36 * PwmOut rled(LED_RED);
37 * PwmOut gled(LED_GREEN);
38 * PwmOut bled(LED_BLUE);
39
40 *     while (true) {       
41 *         rled = 1.0 - abs(acc.getAccX());
42 *         gled = 1.0 - abs(acc.getAccY());
43 *         bled = 1.0 - abs(acc.getAccZ());
44 *         wait(0.1);
45 *     }
46 * }
47 * @endcode
48 */
49 class MMA8451Q
50 {
51 public:
52   /**
53   * MMA8451Q constructor
54   *
55   * @param sda SDA pin
56   * @param sdl SCL pin
57   * @param addr addr of the I2C peripheral
58   */
59   MMA8451Q(PinName sda, PinName scl, int addr);
60
61   /**
62   * MMA8451Q destructor
63   */
64   ~MMA8451Q();
65
66   /**
67    * Get the value of the WHO_AM_I register
68    *
69    * @returns WHO_AM_I value
70    */
71   uint8_t getWhoAmI();
72
73   /**
74    * Get X axis acceleration
75    *
76    * @returns X axis acceleration
77    */
78   float getAccX();
79
80   /**
81    * Get Y axis acceleration
82    *
83    * @returns Y axis acceleration
84    */
85   float getAccY();
86
87   /**
88    * Get Z axis acceleration
89    *
90    * @returns Z axis acceleration
91    */
92   float getAccZ();
93
94   /**
95    * Get XYZ axis acceleration
96    *
97    * @param res array where acceleration data will be stored
98    */
99   void getAccAllAxis(float * res);
100   
101   /** JK
102   * Setup Double Tap detection
103  
104  
105 Example:
106
107 #include "mbed.h"
108 #include "MMA8451Q.h"
109
110 #define MMA8451_I2C_ADDRESS (0x1d<<1)
111 #define ON  0
112 #define OFF !ON
113
114 //Setup the interrupts for the MMA8451Q
115 InterruptIn accInt1(PTA14);
116 InterruptIn accInt2(PTA15);//not used in this prog but this is the other int from the accelorometer
117
118 uint8_t togstat=0;//Led status
119 DigitalOut bled(LED_BLUE);
120
121
122 void tapTrue(void){//ISR
123     if(togstat == 0){
124         togstat = 1;
125         bled=ON;
126     } else {
127         togstat = 0;
128         bled=OFF;
129     }
130         
131 }
132
133
134 int main(void) {
135
136     MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);//accelorometer instance
137   
138     acc.setDoubleTap();//Setup the MMA8451Q to look for a double Tap
139     accInt1.rise(&tapTrue);//call tapTrue when an interrupt is generated on PTA14
140     
141     while (true) {
142     //Interrupt driven so nothing in main loop
143     }
144 }
145
146
147   */
148   void setDoubleTap(void);
149
150 private:
151   I2C m_i2c;
152   int m_addr;
153   void readRegs(int addr, uint8_t * data, int len);
154   void writeRegs(uint8_t * data, int len);
155   int16_t getAccAxis(uint8_t addr);
156
157 };
158
159 #endif