A compilation of some hardware sensors and their shared programming interfaces.
Embed:
(wiki syntax)
Show/hide line numbers
MMA8451Q.h
00001 /* MMA8451Q.h 00002 * Tested with mbed board: FRDM-KL46Z 00003 * Author: Mark Gottscho 00004 * mgottscho@ucla.edu 00005 */ 00006 00007 #ifndef MMA8451Q_H 00008 #define MMA8451Q_H 00009 00010 #include "mbed.h" 00011 #include "I2CSensor.h" 00012 #include "PeriodicSensor.h" 00013 #include "SleepableSensor.h" 00014 00015 /** 00016 * This class allows for easy control of an MMA8451Q accelerometer IC. 00017 */ 00018 class MMA8451Q : public I2CSensor, public PeriodicSensor, public SleepableSensor { 00019 public: 00020 /** 00021 * Enumeration of allowed output data sampling rates. 00022 */ 00023 typedef enum { 00024 HZ800, 00025 HZ400, 00026 HZ200, 00027 HZ100, 00028 HZ50, 00029 HZ12_5, 00030 HZ6_25, 00031 HZ1_56 00032 } smpl_rate_t; 00033 00034 /** 00035 * Enumeration of allowed dynamic ranges (full scale) of the accelerometer data. 00036 * G2: +/- 2G (0.25 mg/level at 14b, 15.6 mg/level at 8b) 00037 * G4: +/- 4G (0.5 mg/level at 14b, 31.25 mg/level at 8b) 00038 * G8: +/- 8G (1.0 mg/level at 14b, 62.5 mg/level at 8b) 00039 */ 00040 typedef enum { 00041 G2, 00042 G4, 00043 G8 00044 } scale_t; 00045 00046 /** 00047 * @param sda the pin identifier for SDA I2C signal 00048 * @param scl the pin identifier for SCL I2C signal 00049 * @param i2c_addr the 8-bit I2C address for this device. Note that LSB is a don't care. 00050 */ 00051 MMA8451Q (PinName sda, PinName scl, int i2c_addr); 00052 00053 /** 00054 */ 00055 ~MMA8451Q(); 00056 00057 /** 00058 * Self-initialization to some nice preset. You must ensure the device is first deactivated using setActive(). 00059 */ 00060 void selfInit(); 00061 00062 /** 00063 * Does a soft reset of the device. 00064 */ 00065 void reset(); 00066 00067 //I2C specific methods 00068 00069 /** 00070 * Implements the pure virtual method of the parent I2CSensor class. 00071 * @returns the 8-bit device identifier. 00072 */ 00073 uint8_t whoAmI(); 00074 00075 //Device-specific methods 00076 00077 /** 00078 * @returns true if the device is active 00079 */ 00080 bool isActive (); 00081 00082 /** 00083 * @param activate if true, enables the device, else disables it 00084 */ 00085 void setActive (bool activate); 00086 00087 /** 00088 * @returns the 8-bit system mode status 00089 */ 00090 uint8_t getSystemMode (); 00091 00092 /** 00093 * @returns true if the device is generating 14b resolution data, otherwise 8b 00094 */ 00095 bool is14bDataEnabled (); 00096 00097 /** 00098 * @param enable if true, sets 14b data, otherwise 8b data 00099 */ 00100 void set14bData (bool enable); 00101 00102 /** 00103 * @returns an enumerated value corresponding to the current output data sample rate 00104 */ 00105 smpl_rate_t getOutputDataRate (); 00106 00107 /** 00108 * @param the enumerated value corresponding to the output data sample rate to use 00109 */ 00110 void setOutputDataRate (smpl_rate_t rate); 00111 00112 /** 00113 * @returns an enumerated value corresponding to the full scale (range) of the output data 00114 */ 00115 scale_t getScale (); 00116 00117 /** 00118 * @param scale sets the sample scale via the enumerated value 00119 */ 00120 void setScale (scale_t scale); 00121 00122 /** 00123 * Enables an interrupt output signal from the device whenever data (XYZ) is generated. 00124 * @param enable Enables/disables interrupt 00125 * @param pinSelect if false, INT2. if true, INT1 00126 */ 00127 void enableDataReadyInterrupt(bool enable, bool pinSelect); 00128 00129 00130 //Device-specific data retrieval methods 00131 /** 00132 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00133 * The latter is preferred if this object is set up to sample using interrupts. 00134 * @returns a 14-bit value representing the latest data sample for the X dimension, centered at 0. 00135 */ 00136 int16_t getX (bool sampleNow); 00137 00138 /** 00139 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00140 * The latter is preferred if this object is set up to sample using interrupts. 00141 * @returns a 14-bit value representing the latest data sample for the Y dimension, centered at 0. 00142 */ 00143 int16_t getY (bool sampleNow); 00144 00145 /** 00146 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00147 * The latter is preferred if this object is set up to sample using interrupts. 00148 * @returns a 14-bit value representing the latest data sample for the Z dimension, centered at 0. 00149 */ 00150 int16_t getZ (bool sampleNow); 00151 00152 /** 00153 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00154 * The latter is preferred if this object is set up to sample using interrupts. 00155 * Returns the latest X data reading as a float in Gs 00156 */ 00157 float getFloatX (bool sampleNow); 00158 00159 /** 00160 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00161 * The latter is preferred if this object is set up to sample using interrupts. 00162 * Returns the latest Y data reading as a float in Gs 00163 */ 00164 float getFloatY (bool sampleNow); 00165 00166 /** 00167 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00168 * The latter is preferred if this object is set up to sample using interrupts. 00169 * Returns the latest Z data reading as a float in Gs 00170 */ 00171 float getFloatZ (bool sampleNow); 00172 00173 virtual void sleep(); 00174 00175 virtual void wake(); 00176 00177 private: 00178 /** 00179 * Interrupt service routine for fetching accelerometer data from the device. 00180 */ 00181 virtual void __sample_data_ISR(); 00182 00183 ///////////////// CONSTANTS ///////////////////// 00184 00185 //Device register addresses 00186 static const uint8_t STATUS = 0x00; 00187 static const uint8_t F_STATUS = 0x00; 00188 static const uint8_t OUT_X_MSB = 0x01; 00189 static const uint8_t OUT_X_LSB = 0x02; 00190 static const uint8_t OUT_Y_MSB = 0x03; 00191 static const uint8_t OUT_Y_LSB = 0x04; 00192 static const uint8_t OUT_Z_MSB = 0x05; 00193 static const uint8_t OUT_Z_LSB = 0x06; 00194 static const uint8_t F_SETUP = 0x09; 00195 static const uint8_t TRIG_CFG = 0x0A; 00196 static const uint8_t SYSMOD = 0x0B; 00197 static const uint8_t INT_SOURCE = 0x0C; 00198 static const uint8_t WHO_AM_I = 0x0D; 00199 static const uint8_t XYZ_DATA_CFG = 0x0E; 00200 static const uint8_t HP_FILTER_CUTOFF = 0x0F; 00201 static const uint8_t PL_STATUS = 0x10; 00202 static const uint8_t PL_CFG = 0x11; 00203 static const uint8_t PL_COUNT = 0x12; 00204 static const uint8_t PL_BF_ZCOMP = 0x13; 00205 static const uint8_t P_L_THS_REG = 0x14; 00206 static const uint8_t FF_MT_CFG = 0x15; 00207 static const uint8_t FF_MT_SRC = 0x16; 00208 static const uint8_t FF_MT_THS = 0x17; 00209 static const uint8_t FF_MT_COUNT = 0x18; 00210 static const uint8_t TRANSIENT_CFG = 0x1D; 00211 static const uint8_t TRANSIENT_SRC = 0x1E; 00212 static const uint8_t TRANSIENT_THS = 0x1F; 00213 static const uint8_t PULSE_CFG = 0x21; 00214 static const uint8_t PULSE_SRC = 0x22; 00215 static const uint8_t PULSE_THSX = 0x23; 00216 static const uint8_t PULSE_THSY = 0x24; 00217 static const uint8_t PULSE_THSZ = 0x25; 00218 static const uint8_t PULSE_TMLT = 0x26; 00219 static const uint8_t PULSE_LTCY = 0x27; 00220 static const uint8_t PULSE_WIND = 0x28; 00221 static const uint8_t ASLP_COUNT = 0x29; 00222 static const uint8_t CTRL_REG1 = 0x2A; 00223 static const uint8_t CTRL_REG2 = 0x2B; 00224 static const uint8_t CTRL_REG3 = 0x2C; 00225 static const uint8_t CTRL_REG4 = 0x2D; 00226 static const uint8_t CTRL_REG5 = 0x2E; 00227 static const uint8_t OFF_X = 0x2F; 00228 static const uint8_t OFF_Y = 0x30; 00229 static const uint8_t OFF_Z = 0x31; 00230 00231 //Register masks 00232 static const uint8_t XYZ_DATA_CFG_HPF_OUT_MASK = 0x10; //b0001 0000 00233 static const uint8_t XYZ_DATA_CFG_FS_MASK = 0x03; //b0000 0011 00234 static const uint8_t CTRL_REG1_ASLP_RATE_MASK = 0xC0; //b1100 0000 00235 static const uint8_t CTRL_REG1_DR_MASK = 0x38; //b0011 1000 00236 static const uint8_t CTRL_REG1_LNOISE_MASK = 0x04; //b0000 0100 00237 static const uint8_t CTRL_REG1_F_READ_MASK = 0x02; //b0000 0010 00238 static const uint8_t CTRL_REG1_ACTIVE_MASK = 0x01; //b0000 0001 00239 static const uint8_t CTRL_REG2_ST_MASK = 0x80; //b1000 0000 00240 static const uint8_t CTRL_REG2_RST_MASK = 0x40; //b0100 0000 00241 static const uint8_t CTRL_REG2_SMODS_MASK = 0x18; //b0001 1000 00242 static const uint8_t CTRL_REG2_SLPE_MASK = 0x04; //b0000 0100 00243 static const uint8_t CTRL_REG2_MODS_MASK = 0x03; //b0000 0011 00244 static const uint8_t CTRL_REG3_FIFO_GATE_MASK = 0x80; //b1000 0000 00245 static const uint8_t CTRL_REG3_WAKE_TRANS_MASK = 0x40; //b0100 0000 00246 static const uint8_t CTRL_REG3_WAKE_LNDPRT_MASK = 0x20; //b0010 0000 00247 static const uint8_t CTRL_REG3_WAKE_PULSE_MASK = 0x10; //b0001 0000 00248 static const uint8_t CTRL_REG3_WAKE_FF_MT_MASK = 0x08; //b0000 1000 00249 static const uint8_t CTRL_REG3_IPOL_MASK = 0x02; //b0000 0010 00250 static const uint8_t CTRL_REG3_PP_OD_MASK = 0x01; //b0000 0001 00251 static const uint8_t CTRL_REG4_INT_EN_ASLP_MASK = 0x80; //b1000 0000 00252 static const uint8_t CTRL_REG4_INT_EN_FIFO_MASK = 0x40; //b0100 0000 00253 static const uint8_t CTRL_REG4_INT_EN_TRANS_MASK = 0x20; //b0010 0000 00254 static const uint8_t CTRL_REG4_INT_EN_LNDPR_MASK = 0x10; //b0001 0000 00255 static const uint8_t CTRL_REG4_INT_EN_PULSE_MASK = 0x08; //b0000 1000 00256 static const uint8_t CTRL_REG4_INT_EN_FF_MT_MASK = 0x04; //b0000 0100 00257 static const uint8_t CTRL_REG4_INT_EN_DRDY_MASK = 0x01; //b0000 0001 00258 static const uint8_t CTRL_REG5_INT_CFG_ASLP_MASK = 0x80; //b1000 0000 00259 static const uint8_t CTRL_REG5_INT_CFG_FIFO_MASK = 0x40; //b0100 0000 00260 static const uint8_t CTRL_REG5_INT_CFG_TRANS_MASK = 0x20; //b0010 0000 00261 static const uint8_t CTRL_REG5_INT_CFG_LNDPRT_MASK = 0x10; //b0001 0000 00262 static const uint8_t CTRL_REG5_INT_CFG_PULSE_MASK = 0x08; //b0000 1000 00263 static const uint8_t CTRL_REG5_INT_CFG_FF_MT_MASK = 0x04; //b0000 0100 00264 static const uint8_t CTRL_REG5_INT_CFG_DRDY_MASK = 0x01; //b0000 0001 00265 00266 //Mapping of sensor values 00267 static const float G2_DIV = 0.00024414; // in Gs/level, 1/4096 00268 static const float G4_DIV = 0.00048828; // in Gs/level, 1/2048 00269 static const float G8_DIV = 0.00097656; // in Gs/level, 1/1024 00270 00271 00272 ////////////// VARIABLES ///////////////// 00273 00274 //MMA8451Q state ("cached" from the values actually on the device) 00275 volatile int16_t __x; 00276 volatile int16_t __y; 00277 volatile int16_t __z; 00278 00279 bool __active; 00280 bool __14b_data_enabled; 00281 smpl_rate_t __output_data_rate; 00282 scale_t __scale; 00283 float __div; //current sensor mapping 00284 }; 00285 00286 #endif
Generated on Wed Jul 13 2022 19:24:41 by 1.7.2