Added code to manage Orientation, FreeFall and Motion Detection. Data is also available via IRQ.
Dependents: Test_FRDM_MMA8451Q AccelTest FRDM-KL46-Template KL25Z_Demo ... more
Fork of MMA8451Q by
MMA8451Q.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 MMA8451Q_H 00020 #define MMA8451Q_H 00021 00022 #include "mbed.h" 00023 00024 /** 00025 * MMA8451Q accelerometer example 00026 * 00027 * @code 00028 * #include "mbed.h" 00029 * #include "MMA8451Q.h" 00030 * 00031 * #define MMA8451_I2C_ADDRESS (0x1d<<1) 00032 * 00033 * int main(void) { 00034 * 00035 * MMA8451Q acc(P_E25, P_E24, MMA8451_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 00050 // Z-Lock Threshold Angles 00051 #define Z_LOCKOUT_14 0 // Angle to 14° 00052 #define Z_LOCKOUT_18 1 // Angle to 18° 00053 #define Z_LOCKOUT_21 2 // Angle to 21° 00054 #define Z_LOCKOUT_25 3 // Angle to 25° 00055 #define Z_LOCKOUT_29 4 // Angle to 29° 00056 #define Z_LOCKOUT_33 5 // Angle to 33° 00057 #define Z_LOCKOUT_37 6 // Angle to 37° 00058 #define Z_LOCKOUT_42 7 // Angle to 42° 00059 // Back/Front Orientation Definition 00060 #define Z_BKFR_80 0 // Back and Front trip angle 00061 #define Z_BKFR_75 1 // Back and Front trip angle 00062 #define Z_BKFR_70 2 // Back and Front trip angle 00063 #define Z_BKFR_65 3 // Back and Front trip angle 00064 // Threshold Angle Thresholds Lookup Table 00065 #define PL_THS_15 0x07 // Set Threshold to 15° 00066 #define PL_THS_20 0x09 // Set Threshold to 20° 00067 #define PL_THS_30 0x0C // Set Threshold to 30° 00068 #define PL_THS_35 0x0D // Set Threshold to 35° 00069 #define PL_THS_40 0x0F // Set Threshold to 40° 00070 #define PL_THS_45 0x10 // Set Threshold to 45° 00071 #define PL_THS_55 0x13 // Set Threshold to 55° 00072 #define PL_THS_60 0x14 // Set Threshold to 60° 00073 #define PL_THS_70 0x17 // Set Threshold to 70° 00074 #define PL_THS_75 0x19 // Set Threshold to 75° 00075 // Trip Angles with Hysteresis for 45° Angle 00076 #define PL_HYS_0 0x00 // Set Hysteresis to ±0° 00077 #define PL_HYS_4 0x01 // Set Hysteresis to ±4° 00078 #define PL_HYS_7 0x02 // Set Hysteresis to ±7° 00079 #define PL_HYS_11 0x03 // Set Hysteresis to ±11° 00080 #define PL_HYS_14 0x04 // Set Hysteresis to ±14° 00081 #define PL_HYS_17 0x05 // Set Hysteresis to ±17° 00082 #define PL_HYS_21 0x06 // Set Hysteresis to ±21° 00083 #define PL_HYS_24 0x07 // Set Hysteresis to ±24° 00084 // Landscape/Portrait orientation 00085 #define cLAPO_PU 0 // Portrait Up: Equipment standing vertically in the normal orientation 00086 #define cLAPO_PD 1 // Portrait Down: Equipment standing vertically in the inverted orientation 00087 #define cLAPO_LR 2 // Landscape Right: Equipment is in landscape mode to the right 00088 #define cLAPO_LL 3 // Landscape Left: Equipment is in landscape mode to the left. 00089 // System Output Data Rate for acceleration samples 00090 #define cODR_800HZ 0 // 1.25 ms 00091 #define cODR_400HZ 1 // 2.5 ms 00092 #define cODR_200HZ 2 // 5 ms 00093 #define cODR_100HZ 3 // 10 ms 00094 #define cODR_50HZ 4 // 20 ms 00095 #define cODR_12_5HZ 5 // 80 ms 00096 #define cODR_6_25HZ 6 // 160 ms 00097 #define cODR_1_56HZ 7 // 640 ms 00098 00099 class MMA8451Q 00100 { 00101 public: 00102 /** 00103 * MMA8451Q constructor 00104 * 00105 * @param sda SDA pin 00106 * @param sdl SCL pin 00107 * @param addr addr of the I2C peripheral 00108 */ 00109 MMA8451Q(PinName sda, PinName scl, int addr); 00110 00111 /** 00112 * MMA8451Q destructor 00113 */ 00114 ~MMA8451Q(); 00115 00116 /** 00117 * Get the value of the WHO_AM_I register 00118 * 00119 * @returns WHO_AM_I value 00120 */ 00121 uint8_t getWhoAmI(); 00122 00123 /** 00124 * Get X axis acceleration 00125 * 00126 * @returns X axis acceleration 00127 */ 00128 float getAccX(); 00129 00130 /** 00131 * Get Y axis acceleration 00132 * 00133 * @returns Y axis acceleration 00134 */ 00135 float getAccY(); 00136 00137 /** 00138 * Get Z axis acceleration 00139 * 00140 * @returns Z axis acceleration 00141 */ 00142 float getAccZ(); 00143 00144 /** 00145 * Get XYZ axis acceleration 00146 * 00147 * @param res array where acceleration data will be stored 00148 */ 00149 void getAccAllAxis(float * res); 00150 00151 /** 00152 * Get raw value for X axis acceleration 00153 * 00154 * @returns X axis acceleration 00155 */ 00156 int16_t getAccRawX( int16_t * res); 00157 00158 /** 00159 * Get raw value for Y axis acceleration 00160 * 00161 * @returns Y axis acceleration 00162 */ 00163 int16_t getAccRawY( int16_t * res); 00164 00165 /** 00166 * Get raw value for Z axis acceleration 00167 * 00168 * @returns Z axis acceleration 00169 */ 00170 int16_t getAccRawZ( int16_t * res); 00171 00172 /** 00173 * Get raw values for XYZ axis acceleration 00174 * 00175 * @param res array where acceleration data will be stored 00176 */ 00177 unsigned int getAccRawAllAxis(int16_t * res); 00178 00179 /** 00180 * Configure the Accelerometere for free fall detection 00181 * 00182 * @param pointer to the user function to execute after IRQ assertion 00183 * @return none 00184 */ 00185 void FreeFallDetection( void(*fptr)(void)); 00186 00187 /** 00188 * Configure the Accelerometere for motion detection 00189 * 00190 * @param pointer to the user function to execute after IRQ assertion 00191 * @return none 00192 */ 00193 void MotionDetection( void(*fptr)(void)); 00194 00195 /** 00196 * Configure the Accelerometere for orientation detection 00197 * 00198 * @param pointer to the user function to execute after IRQ assertion 00199 * @param Z lockout value, Z Backfront, Port/Landscape Thsld, Port/Landscape Hysteresis 00200 * @return none 00201 */ 00202 void OrientationDetect( void(*fptr)(void), unsigned int Z_LockOut, unsigned int Z_BkFr, unsigned int PL_Thsld, unsigned int PL_Hyst); 00203 void OrientationDetect( void(*fptr)(void)); 00204 00205 /** 00206 * Get the orientation state. 00207 * 00208 * 0x10: PL_STATUS Register (Read Only) 00209 * Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 00210 * NEWLP LO — — — LAPO[1] LAPO[0] BAFRO 00211 * 00212 * NEWLP 00213 * Landscape/Portrait status change flag. Default value: 0. 00214 * 0: No change, 1: BAFRO and/or LAPO and/or Z-Tilt lockout value has changed 00215 * LO 00216 * Z-Tilt Angle Lockout. Default value: 0. 00217 * 0: Lockout condition has not been detected. 00218 * 1: Z-Tilt lockout trip angle has been exceeded. Lockout has been detected. 00219 * LAPO[1:0](*) 00220 * Landscape/Portrait orientation. Default value: 00 00221 * 00: Portrait Up: Equipment standing vertically in the normal orientation 00222 * 01: Portrait Down: Equipment standing vertically in the inverted orientation 00223 * 10: Landscape Right: Equipment is in landscape mode to the right 00224 * 11: Landscape Left: Equipment is in landscape mode to the left. 00225 * (*) The default power up state is BAFRO = 0, LAPO = 0, and LO = 0. 00226 * BAFRO 00227 * Back or Front orientation. Default value: 0 00228 * 0: Front: Equipment is in the front facing orientation. 00229 * 1: Back: Equipment is in the back facing orientation. 00230 */ 00231 unsigned char GetOrientationState( void); 00232 00233 /** 00234 * Configure the Accelerometer to generate a IRQ when a new sample is available 00235 * 00236 * @param pointer to the user function to execute after IRQ assertion 00237 * @return none 00238 */ 00239 void DataReady( void(*fptr)(void), unsigned char ODR); 00240 00241 unsigned int isDataAvailable( void); 00242 00243 /** 00244 * Soft Reset 00245 * @param none 00246 * @return none 00247 */ 00248 void Reset( void); 00249 00250 private: 00251 I2C m_i2c; 00252 int m_addr; 00253 void readRegs(int addr, uint8_t * data, int len); 00254 void writeRegs(uint8_t * data, int len); 00255 int16_t getAccAxis(uint8_t addr); 00256 void Standby( void); 00257 void Active( void); 00258 void Fall_IRQ( void); 00259 void Motion_IRQ( void); 00260 void Orientation_IRQ( void); 00261 void DataReady_IRQ( void); 00262 // 00263 unsigned char OrientationState; 00264 unsigned char OrientationStateUpdated; 00265 }; 00266 00267 #endif
Generated on Wed Jul 13 2022 22:03:21 by 1.7.2