JP PANG / MMA8491Q

Dependents:   XtrinsicSensorEVK SDFileSystem_HelloWorld5_copy xtrinsic_sensors

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MMA8491Q.h Source File

MMA8491Q.h

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #ifndef MMA8491Q_H
00020 #define MMA8491Q_H
00021 
00022 #include "mbed.h"
00023 
00024 /**
00025 * MMA8491Q accelerometer example
00026 *
00027 * @code
00028 * #include "mbed.h"
00029 * #include "MMA8491Q.h"
00030 * 
00031 * #define MMA8451_I2C_ADDRESS (0x1d<<1)
00032 * 
00033 * int main(void) {
00034 * 
00035 * MMA8491Q acc(P_E25, P_E24, MMA8491_I2C_ADDRESS);
00036 * PwmOut rled(LED_RED);
00037 * PwmOut gled(LED_GREEN);
00038 * PwmOut bled(LED_BLUE);
00039 * 
00040 *     while (true) {       
00041 *         rled = 1.0 - abs(acc.getAccX());
00042 *         gled = 1.0 - abs(acc.getAccY());
00043 *         bled = 1.0 - abs(acc.getAccZ());
00044 *         wait(0.1);
00045 *     }
00046 * }
00047 * @endcode
00048 */
00049 class MMA8491Q
00050 {
00051 public:
00052   /**
00053   * MMA8491Q constructor
00054   *
00055   * @param sda SDA pin
00056   * @param sdl SCL pin
00057   * @param addr addr of the I2C peripheral
00058   */
00059   MMA8491Q(PinName sda, PinName scl, int addr);
00060   
00061  // MMA8491Q(PinName sda, PinName scl, int addr, PinName Xout, PinName Yout, PinName Zout);
00062 
00063   /**
00064   * MMA8491Q destructor
00065   */
00066   ~MMA8491Q();
00067 
00068   /**
00069    * Get the value of the WHO_AM_I register
00070    *
00071    * @returns WHO_AM_I value
00072    */
00073   uint8_t getWhoAmI();
00074 
00075   /**
00076    * Get X axis acceleration
00077    *
00078    * @returns X axis acceleration
00079    */
00080   float getAccX();
00081 
00082   /**
00083    * Get Y axis acceleration
00084    *
00085    * @returns Y axis acceleration
00086    */
00087   float getAccY();
00088 
00089   /**
00090    * Get Z axis acceleration
00091    *
00092    * @returns Z axis acceleration
00093    */
00094   float getAccZ();
00095 
00096   /**
00097    * Get XYZ axis acceleration
00098    *
00099    * @param res array where acceleration data will be stored
00100    */
00101   void getAccAllAxis(float * res);
00102   
00103   /** JK
00104   * Setup Double Tap detection
00105  
00106  
00107 Example:
00108 
00109 #include "mbed.h"
00110 #include "MMA8491Q.h"
00111 
00112 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00113 #define ON  0
00114 #define OFF !ON
00115 
00116 //Setup the interrupts for the MMA8491Q
00117 InterruptIn accInt1(PTA14);
00118 InterruptIn accInt2(PTA15);//not used in this prog but this is the other int from the accelorometer
00119 
00120 uint8_t togstat=0;//Led status
00121 DigitalOut bled(LED_BLUE);
00122 
00123 
00124 void tapTrue(void){//ISR
00125     if(togstat == 0){
00126         togstat = 1;
00127         bled=ON;
00128     } else {
00129         togstat = 0;
00130         bled=OFF;
00131     }
00132         
00133 }
00134 
00135 
00136 int main(void) {
00137 
00138     MMA8491Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);//accelorometer instance
00139   
00140     acc.setDoubleTap();//Setup the MMA8491Q to look for a double Tap
00141     accInt1.rise(&tapTrue);//call tapTrue when an interrupt is generated on PTA14
00142     
00143     while (true) {
00144     //Interrupt driven so nothing in main loop
00145     }
00146 }
00147 
00148 
00149   */
00150   void setDoubleTap(void);
00151 
00152 private:
00153   I2C m_i2c;
00154   int m_addr;
00155   void readRegs(int addr, uint8_t * data, int len);
00156   void writeRegs(uint8_t * data, int len);
00157   int16_t getAccAxis(uint8_t addr);
00158   
00159 
00160 };
00161 
00162 #endif