Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z by Axeda Corp

Committer:
AxedaCorp
Date:
Wed Jul 02 19:57:37 2014 +0000
Revision:
2:2f9019c5a9fc
Parent:
0:65004368569c
ip switch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AxedaCorp 0:65004368569c 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
AxedaCorp 0:65004368569c 2 *
AxedaCorp 0:65004368569c 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
AxedaCorp 0:65004368569c 4 * and associated documentation files (the "Software"), to deal in the Software without
AxedaCorp 0:65004368569c 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
AxedaCorp 0:65004368569c 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
AxedaCorp 0:65004368569c 7 * Software is furnished to do so, subject to the following conditions:
AxedaCorp 0:65004368569c 8 *
AxedaCorp 0:65004368569c 9 * The above copyright notice and this permission notice shall be included in all copies or
AxedaCorp 0:65004368569c 10 * substantial portions of the Software.
AxedaCorp 0:65004368569c 11 *
AxedaCorp 0:65004368569c 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
AxedaCorp 0:65004368569c 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
AxedaCorp 0:65004368569c 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
AxedaCorp 0:65004368569c 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AxedaCorp 0:65004368569c 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AxedaCorp 0:65004368569c 17 */
AxedaCorp 0:65004368569c 18
AxedaCorp 0:65004368569c 19 #ifndef MMA8451Q_H
AxedaCorp 0:65004368569c 20 #define MMA8451Q_H
AxedaCorp 0:65004368569c 21
AxedaCorp 0:65004368569c 22 #include "mbed.h"
AxedaCorp 0:65004368569c 23
AxedaCorp 0:65004368569c 24 /**
AxedaCorp 0:65004368569c 25 * MMA8451Q accelerometer example
AxedaCorp 0:65004368569c 26 *
AxedaCorp 0:65004368569c 27 * @code
AxedaCorp 0:65004368569c 28 * #include "mbed.h"
AxedaCorp 0:65004368569c 29 * #include "MMA8451Q.h"
AxedaCorp 0:65004368569c 30 *
AxedaCorp 0:65004368569c 31 * #define MMA8451_I2C_ADDRESS (0x1d<<1)
AxedaCorp 0:65004368569c 32 *
AxedaCorp 0:65004368569c 33 * int main(void) {
AxedaCorp 0:65004368569c 34 *
AxedaCorp 0:65004368569c 35 * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS);
AxedaCorp 0:65004368569c 36 * PwmOut rled(LED_RED);
AxedaCorp 0:65004368569c 37 * PwmOut gled(LED_GREEN);
AxedaCorp 0:65004368569c 38 * PwmOut bled(LED_BLUE);
AxedaCorp 0:65004368569c 39 *
AxedaCorp 0:65004368569c 40 * while (true) {
AxedaCorp 0:65004368569c 41 * rled = 1.0 - abs(acc.getAccX());
AxedaCorp 0:65004368569c 42 * gled = 1.0 - abs(acc.getAccY());
AxedaCorp 0:65004368569c 43 * bled = 1.0 - abs(acc.getAccZ());
AxedaCorp 0:65004368569c 44 * wait(0.1);
AxedaCorp 0:65004368569c 45 * }
AxedaCorp 0:65004368569c 46 * }
AxedaCorp 0:65004368569c 47 * @endcode
AxedaCorp 0:65004368569c 48 */
AxedaCorp 0:65004368569c 49
AxedaCorp 0:65004368569c 50 // Z-Lock Threshold Angles
AxedaCorp 0:65004368569c 51 #define Z_LOCKOUT_14 0 // Angle to 14°
AxedaCorp 0:65004368569c 52 #define Z_LOCKOUT_18 1 // Angle to 18°
AxedaCorp 0:65004368569c 53 #define Z_LOCKOUT_21 2 // Angle to 21°
AxedaCorp 0:65004368569c 54 #define Z_LOCKOUT_25 3 // Angle to 25°
AxedaCorp 0:65004368569c 55 #define Z_LOCKOUT_29 4 // Angle to 29°
AxedaCorp 0:65004368569c 56 #define Z_LOCKOUT_33 5 // Angle to 33°
AxedaCorp 0:65004368569c 57 #define Z_LOCKOUT_37 6 // Angle to 37°
AxedaCorp 0:65004368569c 58 #define Z_LOCKOUT_42 7 // Angle to 42°
AxedaCorp 0:65004368569c 59 // Back/Front Orientation Definition
AxedaCorp 0:65004368569c 60 #define Z_BKFR_80 0 // Back and Front trip angle
AxedaCorp 0:65004368569c 61 #define Z_BKFR_75 1 // Back and Front trip angle
AxedaCorp 0:65004368569c 62 #define Z_BKFR_70 2 // Back and Front trip angle
AxedaCorp 0:65004368569c 63 #define Z_BKFR_65 3 // Back and Front trip angle
AxedaCorp 0:65004368569c 64 // Threshold Angle Thresholds Lookup Table
AxedaCorp 0:65004368569c 65 #define PL_THS_15 0x07 // Set Threshold to 15°
AxedaCorp 0:65004368569c 66 #define PL_THS_20 0x09 // Set Threshold to 20°
AxedaCorp 0:65004368569c 67 #define PL_THS_30 0x0C // Set Threshold to 30°
AxedaCorp 0:65004368569c 68 #define PL_THS_35 0x0D // Set Threshold to 35°
AxedaCorp 0:65004368569c 69 #define PL_THS_40 0x0F // Set Threshold to 40°
AxedaCorp 0:65004368569c 70 #define PL_THS_45 0x10 // Set Threshold to 45°
AxedaCorp 0:65004368569c 71 #define PL_THS_55 0x13 // Set Threshold to 55°
AxedaCorp 0:65004368569c 72 #define PL_THS_60 0x14 // Set Threshold to 60°
AxedaCorp 0:65004368569c 73 #define PL_THS_70 0x17 // Set Threshold to 70°
AxedaCorp 0:65004368569c 74 #define PL_THS_75 0x19 // Set Threshold to 75°
AxedaCorp 0:65004368569c 75 // Trip Angles with Hysteresis for 45° Angle
AxedaCorp 0:65004368569c 76 #define PL_HYS_0 0x00 // Set Hysteresis to ±0°
AxedaCorp 0:65004368569c 77 #define PL_HYS_4 0x01 // Set Hysteresis to ±4°
AxedaCorp 0:65004368569c 78 #define PL_HYS_7 0x02 // Set Hysteresis to ±7°
AxedaCorp 0:65004368569c 79 #define PL_HYS_11 0x03 // Set Hysteresis to ±11°
AxedaCorp 0:65004368569c 80 #define PL_HYS_14 0x04 // Set Hysteresis to ±14°
AxedaCorp 0:65004368569c 81 #define PL_HYS_17 0x05 // Set Hysteresis to ±17°
AxedaCorp 0:65004368569c 82 #define PL_HYS_21 0x06 // Set Hysteresis to ±21°
AxedaCorp 0:65004368569c 83 #define PL_HYS_24 0x07 // Set Hysteresis to ±24°
AxedaCorp 0:65004368569c 84 // Landscape/Portrait orientation
AxedaCorp 0:65004368569c 85 #define cLAPO_PU 0 // Portrait Up: Equipment standing vertically in the normal orientation
AxedaCorp 0:65004368569c 86 #define cLAPO_PD 1 // Portrait Down: Equipment standing vertically in the inverted orientation
AxedaCorp 0:65004368569c 87 #define cLAPO_LR 2 // Landscape Right: Equipment is in landscape mode to the right
AxedaCorp 0:65004368569c 88 #define cLAPO_LL 3 // Landscape Left: Equipment is in landscape mode to the left.
AxedaCorp 0:65004368569c 89 // System Output Data Rate for acceleration samples
AxedaCorp 0:65004368569c 90 #define cODR_800HZ 0 // 1.25 ms
AxedaCorp 0:65004368569c 91 #define cODR_400HZ 1 // 2.5 ms
AxedaCorp 0:65004368569c 92 #define cODR_200HZ 2 // 5 ms
AxedaCorp 0:65004368569c 93 #define cODR_100HZ 3 // 10 ms
AxedaCorp 0:65004368569c 94 #define cODR_50HZ 4 // 20 ms
AxedaCorp 0:65004368569c 95 #define cODR_12_5HZ 5 // 80 ms
AxedaCorp 0:65004368569c 96 #define cODR_6_25HZ 6 // 160 ms
AxedaCorp 0:65004368569c 97 #define cODR_1_56HZ 7 // 640 ms
AxedaCorp 0:65004368569c 98
AxedaCorp 0:65004368569c 99 class MMA8451Q
AxedaCorp 0:65004368569c 100 {
AxedaCorp 0:65004368569c 101 public:
AxedaCorp 0:65004368569c 102 /**
AxedaCorp 0:65004368569c 103 * MMA8451Q constructor
AxedaCorp 0:65004368569c 104 *
AxedaCorp 0:65004368569c 105 * @param sda SDA pin
AxedaCorp 0:65004368569c 106 * @param sdl SCL pin
AxedaCorp 0:65004368569c 107 * @param addr addr of the I2C peripheral
AxedaCorp 0:65004368569c 108 */
AxedaCorp 0:65004368569c 109 MMA8451Q(PinName sda, PinName scl, int addr);
AxedaCorp 0:65004368569c 110
AxedaCorp 0:65004368569c 111 /**
AxedaCorp 0:65004368569c 112 * MMA8451Q destructor
AxedaCorp 0:65004368569c 113 */
AxedaCorp 0:65004368569c 114 ~MMA8451Q();
AxedaCorp 0:65004368569c 115
AxedaCorp 0:65004368569c 116 /**
AxedaCorp 0:65004368569c 117 * Get the value of the WHO_AM_I register
AxedaCorp 0:65004368569c 118 *
AxedaCorp 0:65004368569c 119 * @returns WHO_AM_I value
AxedaCorp 0:65004368569c 120 */
AxedaCorp 0:65004368569c 121 uint8_t getWhoAmI();
AxedaCorp 0:65004368569c 122
AxedaCorp 0:65004368569c 123 /**
AxedaCorp 0:65004368569c 124 * Get X axis acceleration
AxedaCorp 0:65004368569c 125 *
AxedaCorp 0:65004368569c 126 * @returns X axis acceleration
AxedaCorp 0:65004368569c 127 */
AxedaCorp 0:65004368569c 128 float getAccX();
AxedaCorp 0:65004368569c 129
AxedaCorp 0:65004368569c 130 /**
AxedaCorp 0:65004368569c 131 * Get Y axis acceleration
AxedaCorp 0:65004368569c 132 *
AxedaCorp 0:65004368569c 133 * @returns Y axis acceleration
AxedaCorp 0:65004368569c 134 */
AxedaCorp 0:65004368569c 135 float getAccY();
AxedaCorp 0:65004368569c 136
AxedaCorp 0:65004368569c 137 /**
AxedaCorp 0:65004368569c 138 * Get Z axis acceleration
AxedaCorp 0:65004368569c 139 *
AxedaCorp 0:65004368569c 140 * @returns Z axis acceleration
AxedaCorp 0:65004368569c 141 */
AxedaCorp 0:65004368569c 142 float getAccZ();
AxedaCorp 0:65004368569c 143
AxedaCorp 0:65004368569c 144 /**
AxedaCorp 0:65004368569c 145 * Get XYZ axis acceleration
AxedaCorp 0:65004368569c 146 *
AxedaCorp 0:65004368569c 147 * @param res array where acceleration data will be stored
AxedaCorp 0:65004368569c 148 */
AxedaCorp 0:65004368569c 149 void getAccAllAxis(float * res);
AxedaCorp 0:65004368569c 150
AxedaCorp 0:65004368569c 151 /**
AxedaCorp 0:65004368569c 152 * Get raw value for X axis acceleration
AxedaCorp 0:65004368569c 153 *
AxedaCorp 0:65004368569c 154 * @returns X axis acceleration
AxedaCorp 0:65004368569c 155 */
AxedaCorp 0:65004368569c 156 int16_t getAccRawX( int16_t * res);
AxedaCorp 0:65004368569c 157
AxedaCorp 0:65004368569c 158 /**
AxedaCorp 0:65004368569c 159 * Get raw value for Y axis acceleration
AxedaCorp 0:65004368569c 160 *
AxedaCorp 0:65004368569c 161 * @returns Y axis acceleration
AxedaCorp 0:65004368569c 162 */
AxedaCorp 0:65004368569c 163 int16_t getAccRawY( int16_t * res);
AxedaCorp 0:65004368569c 164
AxedaCorp 0:65004368569c 165 /**
AxedaCorp 0:65004368569c 166 * Get raw value for Z axis acceleration
AxedaCorp 0:65004368569c 167 *
AxedaCorp 0:65004368569c 168 * @returns Z axis acceleration
AxedaCorp 0:65004368569c 169 */
AxedaCorp 0:65004368569c 170 int16_t getAccRawZ( int16_t * res);
AxedaCorp 0:65004368569c 171
AxedaCorp 0:65004368569c 172 /**
AxedaCorp 0:65004368569c 173 * Get raw values for XYZ axis acceleration
AxedaCorp 0:65004368569c 174 *
AxedaCorp 0:65004368569c 175 * @param res array where acceleration data will be stored
AxedaCorp 0:65004368569c 176 */
AxedaCorp 0:65004368569c 177 unsigned int getAccRawAllAxis(int16_t * res);
AxedaCorp 0:65004368569c 178
AxedaCorp 0:65004368569c 179 /**
AxedaCorp 0:65004368569c 180 * Configure the Accelerometere for free fall detection
AxedaCorp 0:65004368569c 181 *
AxedaCorp 0:65004368569c 182 * @param pointer to the user function to execute after IRQ assertion
AxedaCorp 0:65004368569c 183 * @return none
AxedaCorp 0:65004368569c 184 */
AxedaCorp 0:65004368569c 185 void FreeFallDetection( void(*fptr)(void));
AxedaCorp 0:65004368569c 186
AxedaCorp 0:65004368569c 187 /**
AxedaCorp 0:65004368569c 188 * Configure the Accelerometere for motion detection
AxedaCorp 0:65004368569c 189 *
AxedaCorp 0:65004368569c 190 * @param pointer to the user function to execute after IRQ assertion
AxedaCorp 0:65004368569c 191 * @return none
AxedaCorp 0:65004368569c 192 */
AxedaCorp 0:65004368569c 193 void MotionDetection( void(*fptr)(void));
AxedaCorp 0:65004368569c 194
AxedaCorp 0:65004368569c 195 /**
AxedaCorp 0:65004368569c 196 * Configure the Accelerometere for orientation detection
AxedaCorp 0:65004368569c 197 *
AxedaCorp 0:65004368569c 198 * @param pointer to the user function to execute after IRQ assertion
AxedaCorp 0:65004368569c 199 * @param Z lockout value, Z Backfront, Port/Landscape Thsld, Port/Landscape Hysteresis
AxedaCorp 0:65004368569c 200 * @return none
AxedaCorp 0:65004368569c 201 */
AxedaCorp 0:65004368569c 202 void OrientationDetect( void(*fptr)(void), unsigned int Z_LockOut, unsigned int Z_BkFr, unsigned int PL_Thsld, unsigned int PL_Hyst);
AxedaCorp 0:65004368569c 203 void OrientationDetect( void(*fptr)(void));
AxedaCorp 0:65004368569c 204
AxedaCorp 0:65004368569c 205 /**
AxedaCorp 0:65004368569c 206 * Get the orientation state.
AxedaCorp 0:65004368569c 207 *
AxedaCorp 0:65004368569c 208 * 0x10: PL_STATUS Register (Read Only)
AxedaCorp 0:65004368569c 209 * Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
AxedaCorp 0:65004368569c 210 * NEWLP LO — — — LAPO[1] LAPO[0] BAFRO
AxedaCorp 0:65004368569c 211 *
AxedaCorp 0:65004368569c 212 * NEWLP
AxedaCorp 0:65004368569c 213 * Landscape/Portrait status change flag. Default value: 0.
AxedaCorp 0:65004368569c 214 * 0: No change, 1: BAFRO and/or LAPO and/or Z-Tilt lockout value has changed
AxedaCorp 0:65004368569c 215 * LO
AxedaCorp 0:65004368569c 216 * Z-Tilt Angle Lockout. Default value: 0.
AxedaCorp 0:65004368569c 217 * 0: Lockout condition has not been detected.
AxedaCorp 0:65004368569c 218 * 1: Z-Tilt lockout trip angle has been exceeded. Lockout has been detected.
AxedaCorp 0:65004368569c 219 * LAPO[1:0](*)
AxedaCorp 0:65004368569c 220 * Landscape/Portrait orientation. Default value: 00
AxedaCorp 0:65004368569c 221 * 00: Portrait Up: Equipment standing vertically in the normal orientation
AxedaCorp 0:65004368569c 222 * 01: Portrait Down: Equipment standing vertically in the inverted orientation
AxedaCorp 0:65004368569c 223 * 10: Landscape Right: Equipment is in landscape mode to the right
AxedaCorp 0:65004368569c 224 * 11: Landscape Left: Equipment is in landscape mode to the left.
AxedaCorp 0:65004368569c 225 * (*) The default power up state is BAFRO = 0, LAPO = 0, and LO = 0.
AxedaCorp 0:65004368569c 226 * BAFRO
AxedaCorp 0:65004368569c 227 * Back or Front orientation. Default value: 0
AxedaCorp 0:65004368569c 228 * 0: Front: Equipment is in the front facing orientation.
AxedaCorp 0:65004368569c 229 * 1: Back: Equipment is in the back facing orientation.
AxedaCorp 0:65004368569c 230 */
AxedaCorp 0:65004368569c 231 unsigned char GetOrientationState( void);
AxedaCorp 0:65004368569c 232
AxedaCorp 0:65004368569c 233 /**
AxedaCorp 0:65004368569c 234 * Configure the Accelerometer to generate a IRQ when a new sample is available
AxedaCorp 0:65004368569c 235 *
AxedaCorp 0:65004368569c 236 * @param pointer to the user function to execute after IRQ assertion
AxedaCorp 0:65004368569c 237 * @return none
AxedaCorp 0:65004368569c 238 */
AxedaCorp 0:65004368569c 239 void DataReady( void(*fptr)(void), unsigned char ODR);
AxedaCorp 0:65004368569c 240
AxedaCorp 0:65004368569c 241 unsigned int isDataAvailable( void);
AxedaCorp 0:65004368569c 242
AxedaCorp 0:65004368569c 243 /**
AxedaCorp 0:65004368569c 244 * Soft Reset
AxedaCorp 0:65004368569c 245 * @param none
AxedaCorp 0:65004368569c 246 * @return none
AxedaCorp 0:65004368569c 247 */
AxedaCorp 0:65004368569c 248 void Reset( void);
AxedaCorp 0:65004368569c 249
AxedaCorp 0:65004368569c 250 private:
AxedaCorp 0:65004368569c 251 I2C m_i2c;
AxedaCorp 0:65004368569c 252 int m_addr;
AxedaCorp 0:65004368569c 253 void readRegs(int addr, uint8_t * data, int len);
AxedaCorp 0:65004368569c 254 void writeRegs(uint8_t * data, int len);
AxedaCorp 0:65004368569c 255 int16_t getAccAxis(uint8_t addr);
AxedaCorp 0:65004368569c 256 void Standby( void);
AxedaCorp 0:65004368569c 257 void Active( void);
AxedaCorp 0:65004368569c 258 void Fall_IRQ( void);
AxedaCorp 0:65004368569c 259 void Motion_IRQ( void);
AxedaCorp 0:65004368569c 260 void Orientation_IRQ( void);
AxedaCorp 0:65004368569c 261 void DataReady_IRQ( void);
AxedaCorp 0:65004368569c 262 //
AxedaCorp 0:65004368569c 263 unsigned char OrientationState;
AxedaCorp 0:65004368569c 264 unsigned char OrientationStateUpdated;
AxedaCorp 0:65004368569c 265 };
AxedaCorp 0:65004368569c 266
AxedaCorp 0:65004368569c 267 #endif