An abstraction layer for the I2C communication with a MPU6050 accelerometer/gyroscope

Dependents:   BLE_Nano_MPU6050Service

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPU6050.h Source File

MPU6050.h

00001 #pragma once
00002 #ifndef __MPU6050_H__
00003 #define __MPU6050_H__
00004 
00005 /**
00006  * Based on the work of authors of
00007  * https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050
00008  * 
00009  * Ported by Felix Rüdiger, 2015
00010  * (scrapping all the I2C-slave-bus stuff)
00011  */
00012  
00013 #include "mbed.h"
00014 #include "helpers.h"
00015 
00016 class MPU6050
00017 {
00018     public:
00019         /**
00020          * Constants
00021          */  
00022     
00023         enum BaseAddress
00024         {
00025                                                     // left shift by 1 bit is neccessary for mbed's I2C abstraction class
00026                                                     // look at https://developer.mbed.org/users/okano/notebook/i2c-access-examples/
00027             ADDRESS_0                               = BYTE(01101000) << 1,
00028             ADRESSS_1                               = BYTE(01101001) << 1
00029         };              
00030 
00031         typedef struct 
00032         {
00033             int16_t ax, ay, az;
00034             int16_t temp;
00035             int16_t ox, oy, oz;
00036         } SensorReading;
00037         
00038         enum Register
00039         {
00040             REG_AUX_VDDIO                           = 0x01, // R/W
00041             REG_SMPLRT_DIV                          = 0x19, // R/W
00042             REG_CONFIG                              = 0x1A, // R/W
00043             REG_GYRO_CONFIG                         = 0x1B, // R/W
00044             REG_ACCEL_CONFIG                        = 0x1C, // R/W
00045             REG_FF_THR                              = 0x1D, // R/W
00046             REG_FF_DUR                              = 0x1E, // R/W
00047             REG_MOT_THR                             = 0x1F, // R/W
00048             REG_MOT_DUR                             = 0x20, // R/W
00049             REG_ZRMOT_THR                           = 0x21, // R/W
00050             REG_ZRMOT_DUR                           = 0x22, // R/W
00051             REG_FIFO_EN                             = 0x23, // R/W
00052 /*          REG_I2C_MST_CTRL                        = 0x24, // R/W
00053             REG_I2C_SLV0_ADDR                       = 0x25, // R/W
00054             REG_I2C_SLV0_REG                        = 0x26, // R/W
00055             REG_I2C_SLV0_CTRL                       = 0x27, // R/W
00056             REG_I2C_SLV1_ADDR                       = 0x28, // R/W
00057             REG_I2C_SLV1_REG                        = 0x29, // R/W
00058             REG_I2C_SLV1_CTRL                       = 0x2A, // R/W
00059             REG_I2C_SLV2_ADDR                       = 0x2B, // R/W
00060             REG_I2C_SLV2_REG                        = 0x2C, // R/W
00061             REG_I2C_SLV2_CTRL                       = 0x2D, // R/W
00062             REG_I2C_SLV3_ADDR                       = 0x2E, // R/W
00063             REG_I2C_SLV3_REG                        = 0x2F, // R/W
00064             REG_I2C_SLV3_CTRL                       = 0x30, // R/W
00065             REG_I2C_SLV4_ADDR                       = 0x31, // R/W
00066             REG_I2C_SLV4_REG                        = 0x32, // R/W
00067             REG_I2C_SLV4_DO                         = 0x33, // R/W
00068             REG_I2C_SLV4_CTRL                       = 0x34, // R/W
00069             REG_I2C_SLV4_DI                         = 0x35, // R  
00070             REG_I2C_MST_STATUS                      = 0x36, // R            */
00071             REG_INT_PIN_CFG                         = 0x37, // R/W
00072             REG_INT_ENABLE                          = 0x38, // R/W
00073             REG_INT_STATUS                          = 0x3A, // R    
00074             REG_ACCEL_XOUT_H                        = 0x3B, // R  
00075             REG_ACCEL_XOUT_L                        = 0x3C, // R  
00076             REG_ACCEL_YOUT_H                        = 0x3D, // R  
00077             REG_ACCEL_YOUT_L                        = 0x3E, // R  
00078             REG_ACCEL_ZOUT_H                        = 0x3F, // R  
00079             REG_ACCEL_ZOUT_L                        = 0x40, // R  
00080             REG_TEMP_OUT_H                          = 0x41, // R  
00081             REG_TEMP_OUT_L                          = 0x42, // R  
00082             REG_GYRO_XOUT_H                         = 0x43, // R  
00083             REG_GYRO_XOUT_L                         = 0x44, // R  
00084             REG_GYRO_YOUT_H                         = 0x45, // R  
00085             REG_GYRO_YOUT_L                         = 0x46, // R  
00086             REG_GYRO_ZOUT_H                         = 0x47, // R  
00087             REG_GYRO_ZOUT_L                         = 0x48, // R  
00088 /*          REG_EXT_SENS_DATA_00                    = 0x49, // R  
00089             REG_EXT_SENS_DATA_01                    = 0x4A, // R  
00090             REG_EXT_SENS_DATA_02                    = 0x4B, // R  
00091             REG_EXT_SENS_DATA_03                    = 0x4C, // R  
00092             REG_EXT_SENS_DATA_04                    = 0x4D, // R  
00093             REG_EXT_SENS_DATA_05                    = 0x4E, // R  
00094             REG_EXT_SENS_DATA_06                    = 0x4F, // R  
00095             REG_EXT_SENS_DATA_07                    = 0x50, // R  
00096             REG_EXT_SENS_DATA_08                    = 0x51, // R  
00097             REG_EXT_SENS_DATA_09                    = 0x52, // R  
00098             REG_EXT_SENS_DATA_10                    = 0x53, // R  
00099             REG_EXT_SENS_DATA_11                    = 0x54, // R  
00100             REG_EXT_SENS_DATA_12                    = 0x55, // R  
00101             REG_EXT_SENS_DATA_13                    = 0x56, // R  
00102             REG_EXT_SENS_DATA_14                    = 0x57, // R  
00103             REG_EXT_SENS_DATA_15                    = 0x58, // R  
00104             REG_EXT_SENS_DATA_16                    = 0x59, // R  
00105             REG_EXT_SENS_DATA_17                    = 0x5A, // R  
00106             REG_EXT_SENS_DATA_18                    = 0x5B, // R  
00107             REG_EXT_SENS_DATA_19                    = 0x5C, // R  
00108             REG_EXT_SENS_DATA_20                    = 0x5D, // R  
00109             REG_EXT_SENS_DATA_21                    = 0x5E, // R  
00110             REG_EXT_SENS_DATA_22                    = 0x5F, // R  
00111             REG_EXT_SENS_DATA_23                    = 0x60, // R            */
00112             REG_MOT_DETECT_STATUS                   = 0x61, // R  
00113 /*          REG_I2C_SLV0_DO                         = 0x63, // R/W
00114             REG_I2C_SLV1_DO                         = 0x64, // R/W
00115             REG_I2C_SLV2_DO                         = 0x65, // R/W
00116             REG_I2C_SLV3_DO                         = 0x66, // R/W          
00117             REG_I2C_MST_DELAY_CTRL                  = 0x67, // R/W          */
00118             REG_SIGNAL_PATH_RESET                   = 0x68, // R/W
00119             REG_MOT_DETECT_CTRL                     = 0x69, // R/W
00120             REG_USER_CTRL                           = 0x6A, // R/W
00121             REG_PWR_MGMT_1                          = 0x6B, // R/W
00122             REG_PWR_MGMT_2                          = 0x6C, // R/W
00123             REG_FIFO_COUNTH                         = 0x72, // R/W
00124             REG_FIFO_COUNTL                         = 0x73, // R/W
00125             REG_FIFO_R_W                            = 0x74, // R/W
00126             REG_WHO_AM_I                            = 0x75  // R
00127         };
00128         
00129         static const uint8_t AUX_VDDIO_MASK         = BYTE(10000000);        
00130         enum AuxVDDIOLevel
00131         {
00132             AUX_VDDIO_VLOGIC                        = BYTE(00000000),
00133             AUX_VDDIO_VDD                           = BYTE(10000000)
00134         };
00135         
00136         static const uint8_t EXT_SYNC_MASK          = BYTE(00111000);
00137         enum ExtFrameSync
00138         {
00139             EXT_SYNC_DISABLED                       = BYTE(00000000),
00140             EXT_SYNC_TEMP_OUT_L                     = BYTE(00001000),
00141             EXT_SYNC_GYRO_XOUT_L                    = BYTE(00010000),
00142             EXT_SYNC_GYRO_YOUT_L                    = BYTE(00011000),
00143             EXT_SYNC_GYRO_ZOUT_L                    = BYTE(00100000),
00144             EXT_SYNC_ACCEL_XOUT_L                   = BYTE(00101000),
00145             EXT_SYNC_ACCEL_YOUT_L                   = BYTE(00110000),
00146             EXT_SYNC_ACCEL_ZOUT_L                   = BYTE(00111000)
00147         };
00148         
00149         static const uint8_t DLPF_MASK              = BYTE(00000111);
00150         enum DLPFBandwidth
00151         {
00152             DLPF_260HZ_256Hz                        = BYTE(00000000),
00153             DLPF_184HZ_188Hz                        = BYTE(00000001),
00154             DLPF_94HZ_98HZ                          = BYTE(00000010),
00155             DLPF_44HZ_42HZ                          = BYTE(00000011),
00156             DLPF_21HZ_20HZ                          = BYTE(00000100),
00157             DLPF_10HZ_10HZ                          = BYTE(00000101),
00158             DLPF_5HZ_5HZ                            = BYTE(00000110),
00159             DLPF_RESERVED                           = BYTE(00000111)
00160         };
00161         
00162         static const uint8_t GYRO_X_ST_MASK         = BYTE(10000000);
00163         static const uint8_t GYRO_Y_ST_MASK         = BYTE(01000000);
00164         static const uint8_t GYRO_Z_ST_MASK         = BYTE(00100000);
00165         
00166         static const uint8_t GYRO_RANGE_MASK        = BYTE(00011000);
00167         enum GyroRange
00168         {
00169             GYRO_RANGE_250                          = BYTE(00000000),
00170             GYRO_RANGE_500                          = BYTE(00001000),
00171             GYRO_RANGE_1000                         = BYTE(00010000),
00172             GYRO_RANGE_2000                         = BYTE(00011000)
00173         };
00174         
00175         static const uint8_t ACCEL_X_ST_MASK        = BYTE(10000000);
00176         static const uint8_t ACCEL_Y_ST_MASK        = BYTE(01000000);
00177         static const uint8_t ACCEL_Z_ST_MASK        = BYTE(00100000);
00178         
00179         static const uint8_t ACCEL_HPF_MASK         = BYTE(00000111);
00180         enum AccelHPFCutOff
00181         {
00182             ACCEL_HPF_RESET                         = BYTE(00000000),
00183             ACCEL_HPF_5HZ                           = BYTE(00000001),
00184             ACCEL_HPF_2_5HZ                         = BYTE(00000010),
00185             ACCEL_HPF_1_25HZ                        = BYTE(00000011),
00186             ACCEL_HPF_0_63HZ                        = BYTE(00000100),
00187             ACCEL_HPF_HOLD                          = BYTE(00000111)
00188         };
00189         
00190         static const uint8_t ACCEL_RANGE_MASK       = BYTE(00011000);
00191         enum AccelRange
00192         {
00193             ACCEL_RANGE_2G                          = BYTE(00000000),
00194             ACCEL_RANGE_4G                          = BYTE(00001000),
00195             ACCEL_RANGE_8G                          = BYTE(00010000),
00196             ACCEL_RANGE_16G                         = BYTE(00011000)
00197         };
00198         
00199         static const uint8_t TEMP_FIFO_EN_MASK      = BYTE(10000000);
00200         static const uint8_t GYRO_X_FIFO_EN_MASK    = BYTE(01000000);
00201         static const uint8_t GYRO_Y_FIFO_EN_MASK    = BYTE(00100000);
00202         static const uint8_t GYRO_Z_FIFO_EN_MASK    = BYTE(00010000);
00203         static const uint8_t ACCEL_FIFO_EN_MASK     = BYTE(00001000);
00204         
00205         static const uint8_t INT_LEVEL_MASK         = BYTE(10000000);
00206         enum InterruptLevel
00207         {
00208             INT_LEVEL_ACTIVE_HIGH                   = BYTE(00000000),
00209             INT_LEVEL_ACTIVE_LOW                    = BYTE(10000000)
00210         };
00211         
00212         static const uint8_t INT_DRIVE_MASK         = BYTE(01000000);
00213         enum InterruptDrive
00214         {
00215             INT_DRIVE_PUSH_PULL                     = BYTE(00000000),
00216             INT_DRIVE_OPEN_DRAIN                    = BYTE(01000000)
00217         };
00218         
00219         static const uint8_t INT_LATCH_MASK         = BYTE(00100000);
00220         enum InterruptLatch
00221         {
00222             INT_LATCH_50US_PULSE                    = BYTE(00000000),
00223             INT_LATCH_WAIT_CLEARED                  = BYTE(00100000)
00224         };
00225         
00226         static const uint8_t INT_CLEAR_MASK         = BYTE(00010000);
00227         enum InterruptClear
00228         {
00229             INT_CLEAR_STATUS_ONLY_READ              = BYTE(00000000),
00230             INT_CLEAR_ANY_READ                      = BYTE(00010000)
00231         };
00232         
00233         static const uint8_t INT_FSYNC_LEVEL_MASK   = BYTE(00001000);
00234         enum InterruptFSyncLevel
00235         {
00236             INT_FSYNC_LEVEL_ACTIVE_HIGH             = BYTE(00000000),
00237             INT_FSYNC_LEVEL_ACTIVE_LOW              = BYTE(00001000)
00238         };
00239         
00240         static const uint8_t INT_FSYNC_EN_MASK      = BYTE(00000100);
00241         
00242         enum Interrupt
00243         {
00244             INT_FREEFALL                            = BYTE(10000000),
00245             INT_MOTION                              = BYTE(01000000),
00246             INT_ZERO_MOTION                         = BYTE(00100000),
00247             INT_FIFO_OVERFLOW                       = BYTE(00010000),
00248 /*          INT_I2C_MASTER                          = BYTE(00001000),
00249             INT_PLL_READY                           = BYTE(00000100),
00250             INT_DMP_INIT                            = BYTE(00000010),       */
00251             INT_DATA_READY                          = BYTE(00000001)
00252         };
00253         friend inline Interrupt operator|(Interrupt a, Interrupt b) { return static_cast<Interrupt>(static_cast<int>(a) | static_cast<int>(b)); }
00254         friend inline Interrupt operator&(Interrupt a, Interrupt b) { return static_cast<Interrupt>(static_cast<int>(a) & static_cast<int>(b)); }
00255         
00256         enum Motion
00257         {
00258             MOT_NEGATIVE_X                          = BYTE(10000000),
00259             MOT_POSITIVE_X                          = BYTE(01000000),
00260             MOT_NEGATIVE_Y                          = BYTE(00100000),
00261             MOT_POSITIVE_Y                          = BYTE(00010000),
00262             MOT_NEGATIVE_Z                          = BYTE(00001000),
00263             MOT_POSITIVE_Z                          = BYTE(00000100),
00264             MOT_ZERO                                = BYTE(00000001)
00265         };
00266         friend inline Motion operator|(Motion a, Motion b) { return static_cast<Motion>(static_cast<int>(a) | static_cast<int>(b)); }
00267         friend inline Motion operator&(Motion a, Motion b) { return static_cast<Motion>(static_cast<int>(a) & static_cast<int>(b)); }
00268         
00269         static const uint8_t GYRO_PATH_RESET_MASK   = BYTE(00000100);
00270         static const uint8_t ACCEL_PATH_RESET_MASK  = BYTE(00000010);
00271         static const uint8_t TEMP_PATH_RESET_MASK   = BYTE(00000001);
00272         
00273         static const uint8_t POWER_ON_DELAY_MASK    = BYTE(00110000);
00274         enum PowerOnDelay
00275         {
00276             POWER_ON_DELAY_0MS                      = BYTE(00000000),
00277             POWER_ON_DELAY_1MS                      = BYTE(00010000),
00278             POWER_ON_DELAY_2MS                      = BYTE(00100000),
00279             POWER_ON_DELAY_3MS                      = BYTE(00110000)
00280         };
00281         
00282         static const uint8_t FF_DETECTION_DEC_MASK  = BYTE(00001100);
00283         enum FreefallDetectionDecrement
00284         {
00285             FF_DETECTION_DEC_RESET                  = BYTE(00000000),
00286             FF_DETECTION_DEC_1                      = BYTE(00000100),
00287             FF_DETECTION_DEC_2                      = BYTE(00001000),
00288             FF_DETECTION_DEC_4                      = BYTE(00001100)
00289         };
00290         
00291         static const uint8_t MOT_DETECTION_DEC_MASK = BYTE(00000011);
00292         enum MotionDetectionDecrement
00293         {
00294             MOT_DETECTION_DEC_RESET                 = BYTE(00000000),
00295             MOT_DETECTION_DEC_1                     = BYTE(00000001),
00296             MOT_DETECTION_DEC_2                     = BYTE(00000010),
00297             MOT_DETECTION_DEC_4                     = BYTE(00000011)
00298         };
00299         
00300         static const uint8_t FIFO_EN_MASK           = BYTE(01000000);
00301         static const uint8_t FIFO_RESET_MASK        = BYTE(00000100);
00302         static const uint8_t SIG_COND_RESET_MASK    = BYTE(00000001);
00303         
00304         static const uint8_t RESET_MASK             = BYTE(10000000);
00305         static const uint8_t SLEEP_EN_MASK          = BYTE(01000000);
00306         static const uint8_t WAKE_CYCLE_EN_MASK     = BYTE(00100000);
00307         static const uint8_t TEMP_EN_MASK           = BYTE(00001000);
00308         
00309         static const uint8_t CLOCK_SOURCE_MASK      = BYTE(00000111);
00310         enum ClockSource
00311         {
00312             CLOCK_SRC_INTERNAL_8MHZ                 = BYTE(00000000),
00313             CLOCK_SRC_PLL_GYRO_X_REF                = BYTE(00000001),
00314             CLOCK_SRC_PLL_GYRO_Y_REF                = BYTE(00000010),
00315             CLOCK_SRC_PLL_GYRO_Z_REF                = BYTE(00000011),
00316             CLOCK_SRC_EXTERNAL_32KHZ                = BYTE(00000100),
00317             CLOCK_SRC_EXTERNAL_19MHZ                = BYTE(00000101),
00318             CLOCK_SRC_KEEP_RESET                    = BYTE(00000111)
00319         };
00320         
00321         static const uint8_t WAKE_FREQ_MASK         = BYTE(11000000);
00322         enum WakeFrequency
00323         {
00324             WAKE_FREQ_1_25Hz                        = BYTE(00000000),
00325             WAKE_FREQ_2_5Hz                         = BYTE(01000000),
00326             WAKE_FREQ_5Hz                           = BYTE(10000000),
00327             WAKE_FREQ_10HZ                          = BYTE(11000000)
00328         };
00329         
00330         static const uint8_t ACCEL_X_STB_EN_MASK    = BYTE(00100000);
00331         static const uint8_t ACCEL_Y_STB_EN_MASK    = BYTE(00010000);
00332         static const uint8_t ACCEL_Z_STB_EN_MASK    = BYTE(00001000);
00333         static const uint8_t GYRO_X_STB_EN_MASK     = BYTE(00000100);
00334         static const uint8_t GYRO_Y_STB_EN_MASK     = BYTE(00000010);
00335         static const uint8_t GYRO_Z_STB_EN_MASK     = BYTE(00000001);
00336         
00337         static const uint8_t DEVICE_ID_MASK         = BYTE(01111110);
00338         
00339         static const uint8_t MPU6050_ID             = BYTE(01101000);   // should be 0x34 << 1
00340         
00341         /**
00342          * basic members
00343          */
00344         
00345         MPU6050(PinName sda, PinName scl, BaseAddress address = ADDRESS_0) : i2c(sda, scl), baseAddress(address)
00346         { }
00347         
00348         BaseAddress getAddress() { return baseAddress; }   
00349         
00350         /**
00351          * REG_AUX_VDDIO register
00352          */
00353          
00354         bool        getAuxVDDIOLevel(AuxVDDIOLevel *level,  float timeout_secs = defaultTimeout_secs);
00355         bool        setAuxVDDIOLevel(AuxVDDIOLevel level,   float timeout_secs = defaultTimeout_secs);
00356         
00357         /**
00358          * REG_SMPLRT_DIV register
00359          */
00360          
00361         bool        getGyroSampleRateDivider(uint8_t *rateDivider,  float timeout_secs = defaultTimeout_secs);
00362         bool        setGyroSampleRateDivider(uint8_t rateDivider,   float timeout_secs = defaultTimeout_secs);
00363         
00364         /**
00365          * REG_CONFIG register
00366          */
00367          
00368         bool        getExternalFrameSync(ExtFrameSync *frameSync,   float timeout_secs = defaultTimeout_secs);
00369         bool        setExternalFrameSync(ExtFrameSync frameSync,    float timeout_secs = defaultTimeout_secs);
00370          
00371         bool        getDLPFBandwidth(DLPFBandwidth *bandwidth,  float timeout_secs = defaultTimeout_secs);
00372         bool        setDLPFBandwidth(DLPFBandwidth bandwidth,   float timeout_secs = defaultTimeout_secs);
00373         
00374         /**
00375          * REG_GYRO_CONFIG register
00376          */      
00377         
00378         bool        getGyroXSelfTestEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00379         bool        setGyroXSelfTestEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs);
00380         
00381         bool        getGyroYSelfTestEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00382         bool        setGyroYSelfTestEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs);    
00383         
00384         bool        getGyroZSelfTestEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00385         bool        setGyroZSelfTestEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs); 
00386         
00387         bool        getGyroRange(GyroRange *gyroRange,  float timeout_secs = defaultTimeout_secs);
00388         bool        setGyroRange(GyroRange gyroRange,   float timeout_secs = defaultTimeout_secs);
00389         
00390         /**
00391          * REG_ACCEL_CONFIG register
00392          */
00393          
00394         bool        getAccelXSelfTestEnabled(bool *enabled, float timeout_secs = defaultTimeout_secs);
00395         bool        setAccelXSelfTestEnabled(bool enabled,  float timeout_secs = defaultTimeout_secs);
00396         
00397         bool        getAccelYSelfTestEnabled(bool *enabled, float timeout_secs = defaultTimeout_secs);
00398         bool        setAccelYSelfTestEnabled(bool enabled,  float timeout_secs = defaultTimeout_secs);   
00399         
00400         bool        getAccelZSelfTestEnabled(bool *enabled, float timeout_secs = defaultTimeout_secs);
00401         bool        setAccelZSelfTestEnabled(bool enabled,  float timeout_secs = defaultTimeout_secs); 
00402         
00403         bool        getAccelHPFCutOff(AccelHPFCutOff *frequency,    float timeout_secs = defaultTimeout_secs);
00404         bool        setAccelHPFCutOff(AccelHPFCutOff frequency,     float timeout_secs = defaultTimeout_secs);
00405         
00406         bool        getAccelRange(AccelRange *accelRange,   float timeout_secs = defaultTimeout_secs);
00407         bool        setAccelRange(AccelRange accelRange,    float timeout_secs = defaultTimeout_secs);           
00408         
00409         /**
00410          * REG_FF_THR register
00411          */
00412         
00413         bool        getFreefallDetectionThreshold(uint8_t *threshold,   float timeout_secs = defaultTimeout_secs);
00414         bool        setFreefallDetectionThreshold(uint8_t threshold,    float timeout_secs = defaultTimeout_secs);
00415         
00416         /**
00417          * REG_FF_DUR register
00418          */
00419          
00420         bool        getFreefallDetectionDuration(uint8_t *duration, float timeout_secs = defaultTimeout_secs);
00421         bool        setFreefallDetectionDuration(uint8_t duration,  float timeout_secs = defaultTimeout_secs);
00422         
00423         /**
00424          * REG_MOT_THR register
00425          */
00426          
00427         bool        getMotionDetectionThreshold(uint8_t *threshold, float timeout_secs = defaultTimeout_secs);
00428         bool        setMotionDetectionThreshold(uint8_t threshold,  float timeout_secs = defaultTimeout_secs);
00429         
00430         /**
00431          * REG_MOT_DUR register
00432          */
00433          
00434         bool        getMotionDetectionDuration(uint8_t *duration,   float timeout_secs = defaultTimeout_secs);
00435         bool        setMotionDetectionDuration(uint8_t duration,    float timeout_secs = defaultTimeout_secs);
00436         
00437         /**
00438          * REG_ZRMOT_THR register
00439          */
00440          
00441         bool        getZeroMotionDetectionThreshold(uint8_t *threshold, float timeout_secs = defaultTimeout_secs);
00442         bool        setZeroMotionDetectionThreshold(uint8_t threshold,  float timeout_secs = defaultTimeout_secs);
00443         
00444         /**
00445          * REG_ZRMOT_DUR register
00446          */
00447          
00448         bool        getZeroMotionDetectionDuration(uint8_t *duration,   float timeout_secs = defaultTimeout_secs);
00449         bool        setZeroMotionDetectionDuration(uint8_t duration,    float timeout_secs = defaultTimeout_secs);
00450         
00451         /**
00452          * REG_FIFO_EN register
00453          */
00454         
00455         bool        getTempFIFOEnabled(bool *enabled,   float timeout_secs = defaultTimeout_secs);
00456         bool        setTempFIFOEnabled(bool enabled,    float timeout_secs = defaultTimeout_secs);
00457         
00458         bool        getGyroXFIFOEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00459         bool        setGyroXFIFOEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs);   
00460         
00461         bool        getGyroYFIFOEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00462         bool        setGyroYFIFOEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs); 
00463         
00464         bool        getGyroZFIFOEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00465         bool        setGyroZFIFOEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs);         
00466         
00467         bool        getAccelFIFOEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00468         bool        setAccelFIFOEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs); 
00469         
00470         /**
00471          * REG_INT_PIN_CFG register
00472          */
00473          
00474         bool        getInterruptLevel(InterruptLevel *level,    float timeout_secs = defaultTimeout_secs);
00475         bool        setInterruptLevel(InterruptLevel level,     float timeout_secs = defaultTimeout_secs);
00476         
00477         bool        getInterruptDrive(InterruptDrive *drive,    float timeout_secs = defaultTimeout_secs);
00478         bool        setInterruptDrive(InterruptDrive drive,     float timeout_secs = defaultTimeout_secs); 
00479         
00480         bool        getInterruptLatch(InterruptLatch *latch,    float timeout_secs = defaultTimeout_secs);
00481         bool        setInterruptLatch(InterruptLatch latch,     float timeout_secs = defaultTimeout_secs); 
00482         
00483         bool        getInterruptLatchClear(InterruptClear *latchClear,  float timeout_secs = defaultTimeout_secs);
00484         bool        setInterruptLatchClear(InterruptClear latchClear,   float timeout_secs = defaultTimeout_secs); 
00485         
00486         bool        getInterruptFSyncLevel(InterruptFSyncLevel *fsyncLevel, float timeout_secs = defaultTimeout_secs);
00487         bool        setInterruptFSyncLevel(InterruptFSyncLevel fsyncLevel,  float timeout_secs = defaultTimeout_secs);        
00488               
00489         bool        getInterruptFSyncEnabled(bool *enabled, float timeout_secs = defaultTimeout_secs);
00490         bool        setInterruptFSyncEnabled(bool enabled,  float timeout_secs = defaultTimeout_secs);   
00491         
00492         /**
00493          * REG_INT_ENABLE register
00494          */
00495          
00496         bool        getInterruptsEnabled(Interrupt *interruptSet,   float timeout_secs = defaultTimeout_secs);
00497         bool        setInterruptsEnabled(Interrupt interruptSet,    float timeout_secs = defaultTimeout_secs);
00498         
00499         bool        getInterruptFreefallEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00500         bool        setInterruptFreefallEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs); 
00501         
00502         bool        getInterruptMotionEnabled(bool *enabled,    float timeout_secs = defaultTimeout_secs);
00503         bool        setInterruptMotionEnabled(bool enabled,     float timeout_secs = defaultTimeout_secs); 
00504         
00505         bool        getInterruptZeroMotionEnabled(bool *enabled,    float timeout_secs = defaultTimeout_secs);
00506         bool        setInterruptZeroMotionEnabled(bool enabled,     float timeout_secs = defaultTimeout_secs); 
00507         
00508         bool        getInterruptFIFOOverflowEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00509         bool        setInterruptFIFOOverflowEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs); 
00510         
00511         bool        getInterruptDataReadyEnabled(bool *enabled, float timeout_secs = defaultTimeout_secs);
00512         bool        setInterruptDataReadyEnabled(bool enabled,  float timeout_secs = defaultTimeout_secs); 
00513         
00514         /**
00515          * REG_INT_STATUS register
00516          */
00517         
00518         bool        getInterruptStatuses(Interrupt *interruptSet,   float timeout_secs = defaultTimeout_secs);
00519         
00520         bool        getInterruptFreefallStatus(bool *status,    float timeout_secs = defaultTimeout_secs);
00521         
00522         bool        getInterruptMotionStatus(bool *status,  float timeout_secs = defaultTimeout_secs); 
00523         
00524         bool        getInterruptZeroMotionStatus(bool *status,  float timeout_secs = defaultTimeout_secs);
00525         
00526         bool        getInterruptFIFOOverflowStatus(bool *status,    float timeout_secs = defaultTimeout_secs);
00527         
00528         bool        getInterruptDataReadyStatus(bool *status,   float timeout_secs = defaultTimeout_secs);
00529         
00530         /**
00531          * REG_ACCEL_XOUT_H 
00532          * REG_ACCEL_XOUT_L 
00533          * REG_ACCEL_YOUT_H 
00534          * REG_ACCEL_YOUT_L 
00535          * REG_ACCEL_ZOUT_H 
00536          * REG_ACCEL_ZOUT_L 
00537          * REG_TEMP_OUT_H 
00538          * REG_TEMP_OUT_L 
00539          * REG_GYRO_XOUT_H 
00540          * REG_GYRO_XOUT_L 
00541          * REG_GYRO_YOUT_H 
00542          * REG_GYRO_YOUT_L
00543          * REG_GYRO_ZOUT_H  
00544          * REG_GYRO_ZOUT_L registers
00545          */
00546          
00547         /** Acceleration */
00548          
00549         bool        getAcceleration(int16_t *ax,    int16_t *ay,    int16_t *az,    float timeout_secs = defaultTimeout_secs);
00550         
00551         bool        getAccelerationX(int16_t* ax,   float timeout_secs = defaultTimeout_secs);
00552         
00553         bool        getAccelerationY(int16_t* ay,   float timeout_secs = defaultTimeout_secs);
00554         
00555         bool        getAccelerationZ(int16_t* az,   float timeout_secs = defaultTimeout_secs);
00556                  
00557         /** Temperature */
00558         
00559         bool        getTemperature(int16_t* temp,   float timeout_secs = defaultTimeout_secs);
00560         
00561         /** Rotation speed */
00562         
00563         bool        getRotationSpeed(int16_t *ox,   int16_t *oy,    int16_t *oz,    float timeout_secs = defaultTimeout_secs);
00564         
00565         bool        getRotationSpeedX(int16_t* ox,   float timeout_secs = defaultTimeout_secs);
00566         
00567         bool        getRotationSpeedY(int16_t* oy,   float timeout_secs = defaultTimeout_secs);
00568         
00569         bool        getRotationSpeedZ(int16_t* oz,   float timeout_secs = defaultTimeout_secs);
00570         
00571         /** All three */
00572         
00573         bool        getMotionAndTemperature(int16_t* ax,    int16_t* ay,    int16_t* az,    
00574                                             int16_t* ox,    int16_t* oy,    int16_t* oz,
00575                                             int16_t* temp,  float timeout_secs = defaultTimeout_secs);
00576                                             
00577         bool        getMotionAndTemperature(SensorReading *sensorReading,    float timeout_secs = defaultTimeout_secs);
00578         
00579         /**
00580          * REG_MOT_DETECT_STATUS register
00581          */
00582          
00583         bool        getMotionStatus(Motion *motionSet,  float timeout_secs = defaultTimeout_secs);
00584         
00585         bool        getNegativeXMotionDetected(bool *detected,  float timeout_secs = defaultTimeout_secs);
00586         
00587         bool        getPositiveXMotionDetected(bool *detected,  float timeout_secs = defaultTimeout_secs); 
00588         
00589         bool        getNegativeYMotionDetected(bool *detected,  float timeout_secs = defaultTimeout_secs);
00590         
00591         bool        getPositiveYMotionDetected(bool *detected,  float timeout_secs = defaultTimeout_secs);
00592         
00593         bool        getNegativeZMotionDetected(bool *detected,  float timeout_secs = defaultTimeout_secs);
00594         
00595         bool        getPositiveZMotionDetected(bool *detected,  float timeout_secs = defaultTimeout_secs);
00596         
00597         bool        getZeroMotionDetected(bool *detected,   float timeout_secs = defaultTimeout_secs);
00598         
00599         /**
00600          * REG_SIGNAL_PATH_RESET register
00601          */
00602          
00603         bool        resetGyroscopeSignalPath(float timeout_secs = defaultTimeout_secs);
00604         
00605         bool        resetAccelerometerSignalPath(float timeout_secs = defaultTimeout_secs);
00606         
00607         bool        resetTemperatureSignalPath(float timeout_secs = defaultTimeout_secs);
00608                  
00609         /**
00610          * REG_MOT_DETECT_CTRL register
00611          */
00612          
00613         bool        getAccelerometerPowerOnDelay(PowerOnDelay* delay,    float timeout_secs = defaultTimeout_secs);
00614         bool        setAccelerometerPowerOnDelay(PowerOnDelay delay,     float timeout_secs = defaultTimeout_secs);
00615         
00616         bool        getFreefallDetectionDecrement(FreefallDetectionDecrement* dec,  float timeout_secs = defaultTimeout_secs);
00617         bool        setFreefallDetectionDecrement(FreefallDetectionDecrement dec,   float timeout_secs = defaultTimeout_secs);
00618         
00619         bool        getMotionDetectionDecrement(MotionDetectionDecrement* dec,  float timeout_secs = defaultTimeout_secs);
00620         bool        setMotionDetectionDecrement(MotionDetectionDecrement dec,   float timeout_secs = defaultTimeout_secs);
00621         
00622         /**
00623          * REG_USER_CTRL register
00624          */
00625          
00626         bool        getFIFOEnabled(bool *enabled,   float timeout_secs = defaultTimeout_secs);
00627         bool        setFIFOEnabled(bool enabled,    float timeout_secs = defaultTimeout_secs);
00628         
00629         bool        resetFIFO(float timeout_secs = defaultTimeout_secs);
00630         
00631         bool        resetSensors(float timeout_secs = defaultTimeout_secs);
00632         
00633         /**
00634          * REG_PWR_MGMT_1 register
00635          */
00636          
00637         bool        reset(float timeout_secs = defaultTimeout_secs);
00638         
00639         bool        getSleepEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00640         bool        setSleepEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs);
00641         
00642         bool        getWakeCycleEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00643         bool        setWakeCycleEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs);
00644         
00645         bool        getTemperatureSensorEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00646         bool        setTemperatureSensorEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs);
00647         
00648         bool        getClockSource(ClockSource* clockSource,    float timeout_secs = defaultTimeout_secs);
00649         bool        setClockSource(ClockSource clockSource,     float timeout_secs = defaultTimeout_secs);
00650         
00651         /**
00652          * REG_PWR_MGMT_2 register
00653          */
00654         
00655         bool        getWakeFrequency(WakeFrequency* wakeFrequency,  float timeout_secs = defaultTimeout_secs);
00656         bool        setWakeFrequency(WakeFrequency wakeFrequency,   float timeout_secs = defaultTimeout_secs);
00657         
00658         bool        getGyroXStandbyEnabled(bool *enabled,   float timeout_secs = defaultTimeout_secs);
00659         bool        setGyroXStandbyEnabled(bool enabled,    float timeout_secs = defaultTimeout_secs);
00660         
00661         bool        getGyroYStandbyEnabled(bool *enabled,   float timeout_secs = defaultTimeout_secs);
00662         bool        setGyroYStandbyEnabled(bool enabled,    float timeout_secs = defaultTimeout_secs);
00663         
00664         bool        getGyroZStandbyEnabled(bool *enabled,   float timeout_secs = defaultTimeout_secs);
00665         bool        setGyroZStandbyEnabled(bool enabled,    float timeout_secs = defaultTimeout_secs);
00666         
00667         bool        getAccelXStandbyEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00668         bool        setAccelXStandbyEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs);
00669         
00670         bool        getAccelYStandbyEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00671         bool        setAccelYStandbyEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs);
00672         
00673         bool        getAccelZStandbyEnabled(bool *enabled,  float timeout_secs = defaultTimeout_secs);
00674         bool        setAccelZStandbyEnabled(bool enabled,   float timeout_secs = defaultTimeout_secs);
00675         
00676         /**
00677          * REG_FIFO_COUNTH
00678          * REG_FIFO_COUNTL registers
00679          */
00680          
00681         bool        getFIFOCount(uint16_t *count,   float timeout_secs = defaultTimeout_secs);
00682         
00683         /**
00684          * REG_FIFO_R_W register
00685          */
00686          
00687         bool        readFIFO(uint8_t *data, float timeout_secs = defaultTimeout_secs);
00688         bool        writeFIFO(uint8_t data, float timeout_secs = defaultTimeout_secs);
00689         
00690         bool        readFIFO(uint8_t *data,     size_t length,  float timeout_secs = defaultTimeout_secs);
00691         bool        writeFIFO(uint8_t *data,    size_t length,  float timeout_secs = defaultTimeout_secs);
00692         
00693         /**
00694          * REG_WHO_AM_I register
00695          */
00696          
00697         bool        getDeviceId(uint8_t *id,    float timeout_secs = defaultTimeout_secs);
00698          
00699         /**
00700          * read and write registers
00701          */
00702          
00703         bool        read(Register reg,  uint8_t *data,  float timeout_secs = defaultTimeout_secs);        
00704         bool        read(Register reg,  uint8_t *data,  size_t length,  float timeout_secs = defaultTimeout_secs);
00705         
00706         bool        write(Register reg, uint8_t data,   float timeout_secs = defaultTimeout_secs);        
00707         bool        write(Register reg, uint8_t *data,  size_t length,  float timeout_secs = defaultTimeout_secs);
00708         
00709     private:
00710         static const float retryDelay_secs      = 0.005;
00711         static const float defaultTimeout_secs  = 0.1;      // makes a maximum of 20 tries
00712     
00713         I2C         i2c;
00714         BaseAddress baseAddress;
00715         
00716         union
00717         {
00718             struct
00719             {
00720                 uint8_t ax_h,   ax_l;
00721                 uint8_t ay_h,   ay_l;
00722                 uint8_t az_h,   az_l;
00723                 
00724                 uint8_t temp_h, temp_l;
00725                 
00726                 uint8_t ox_h,   ox_l;
00727                 uint8_t oy_h,   oy_l;
00728                 uint8_t oz_h,   oz_l;
00729             }               reg;
00730             
00731             SensorReading   value;
00732         } converter;
00733 };
00734 
00735 #endif