Quadcopter Attitude Control(Yaw-Pitch-Roll)

Dependencies:   mbed

Committer:
khaledelmadawi
Date:
Fri Jul 03 11:16:02 2015 +0000
Revision:
0:e63996fd7d3e
Quadcopter Attitude Control(Yaw-Pitch-Roll)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
khaledelmadawi 0:e63996fd7d3e 1 //ported from arduino library: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050
khaledelmadawi 0:e63996fd7d3e 2 //written by szymon gaertig (email: szymon@gaertig.com.pl)
khaledelmadawi 0:e63996fd7d3e 3 //
khaledelmadawi 0:e63996fd7d3e 4 //Changelog:
khaledelmadawi 0:e63996fd7d3e 5 //2013-01-08 - first beta release
khaledelmadawi 0:e63996fd7d3e 6
khaledelmadawi 0:e63996fd7d3e 7 // I2Cdev library collection - MPU6050 I2C device class
khaledelmadawi 0:e63996fd7d3e 8 // Based on InvenSense MPU-6050 register map document rev. 2.0, 5/19/2011 (RM-MPU-6000A-00)
khaledelmadawi 0:e63996fd7d3e 9 // 8/24/2011 by Jeff Rowberg <jeff@rowberg.net>
khaledelmadawi 0:e63996fd7d3e 10 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
khaledelmadawi 0:e63996fd7d3e 11 //
khaledelmadawi 0:e63996fd7d3e 12 // Changelog:
khaledelmadawi 0:e63996fd7d3e 13 // ... - ongoing debug release
khaledelmadawi 0:e63996fd7d3e 14
khaledelmadawi 0:e63996fd7d3e 15 // NOTE: THIS IS ONLY A PARIAL RELEASE. THIS DEVICE CLASS IS CURRENTLY UNDERGOING ACTIVE
khaledelmadawi 0:e63996fd7d3e 16 // DEVELOPMENT AND IS STILL MISSING SOME IMPORTANT FEATURES. PLEASE KEEP THIS IN MIND IF
khaledelmadawi 0:e63996fd7d3e 17 // YOU DECIDE TO USE THIS PARTICULAR CODE FOR ANYTHING.
khaledelmadawi 0:e63996fd7d3e 18
khaledelmadawi 0:e63996fd7d3e 19 /* ============================================
khaledelmadawi 0:e63996fd7d3e 20 I2Cdev device library code is placed under the MIT license
khaledelmadawi 0:e63996fd7d3e 21 Copyright (c) 2012 Jeff Rowberg
khaledelmadawi 0:e63996fd7d3e 22
khaledelmadawi 0:e63996fd7d3e 23 Permission is hereby granted, free of charge, to any person obtaining a copy
khaledelmadawi 0:e63996fd7d3e 24 of this software and associated documentation files (the "Software"), to deal
khaledelmadawi 0:e63996fd7d3e 25 in the Software without restriction, including without limitation the rights
khaledelmadawi 0:e63996fd7d3e 26 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
khaledelmadawi 0:e63996fd7d3e 27 copies of the Software, and to permit persons to whom the Software is
khaledelmadawi 0:e63996fd7d3e 28 furnished to do so, subject to the following conditions:
khaledelmadawi 0:e63996fd7d3e 29
khaledelmadawi 0:e63996fd7d3e 30 The above copyright notice and this permission notice shall be included in
khaledelmadawi 0:e63996fd7d3e 31 all copies or substantial portions of the Software.
khaledelmadawi 0:e63996fd7d3e 32
khaledelmadawi 0:e63996fd7d3e 33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
khaledelmadawi 0:e63996fd7d3e 34 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
khaledelmadawi 0:e63996fd7d3e 35 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
khaledelmadawi 0:e63996fd7d3e 36 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
khaledelmadawi 0:e63996fd7d3e 37 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
khaledelmadawi 0:e63996fd7d3e 38 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
khaledelmadawi 0:e63996fd7d3e 39 THE SOFTWARE.
khaledelmadawi 0:e63996fd7d3e 40 ===============================================
khaledelmadawi 0:e63996fd7d3e 41 */
khaledelmadawi 0:e63996fd7d3e 42
khaledelmadawi 0:e63996fd7d3e 43 #include "MPU6050.h"
khaledelmadawi 0:e63996fd7d3e 44
khaledelmadawi 0:e63996fd7d3e 45 #define useDebugSerial
khaledelmadawi 0:e63996fd7d3e 46
khaledelmadawi 0:e63996fd7d3e 47 //instead of using pgmspace.h
khaledelmadawi 0:e63996fd7d3e 48 typedef const unsigned char prog_uchar;
khaledelmadawi 0:e63996fd7d3e 49 #define pgm_read_byte_near(x) (*(prog_uchar*)(x))//<- I modified here
khaledelmadawi 0:e63996fd7d3e 50 #define pgm_read_byte(x) (*(prog_uchar*)(x))//<- I modified here
khaledelmadawi 0:e63996fd7d3e 51
khaledelmadawi 0:e63996fd7d3e 52 /** Default constructor, uses default I2C address.
khaledelmadawi 0:e63996fd7d3e 53 * @see MPU6050_DEFAULT_ADDRESS
khaledelmadawi 0:e63996fd7d3e 54 */
khaledelmadawi 0:e63996fd7d3e 55 MPU6050::MPU6050() : debugSerial(USBTX, USBRX)
khaledelmadawi 0:e63996fd7d3e 56 {
khaledelmadawi 0:e63996fd7d3e 57 devAddr = MPU6050_DEFAULT_ADDRESS;
khaledelmadawi 0:e63996fd7d3e 58 }
khaledelmadawi 0:e63996fd7d3e 59
khaledelmadawi 0:e63996fd7d3e 60 /** Specific address constructor.
khaledelmadawi 0:e63996fd7d3e 61 * @param address I2C address
khaledelmadawi 0:e63996fd7d3e 62 * @see MPU6050_DEFAULT_ADDRESS
khaledelmadawi 0:e63996fd7d3e 63 * @see MPU6050_ADDRESS_AD0_LOW
khaledelmadawi 0:e63996fd7d3e 64 * @see MPU6050_ADDRESS_AD0_HIGH
khaledelmadawi 0:e63996fd7d3e 65 */
khaledelmadawi 0:e63996fd7d3e 66 MPU6050::MPU6050(uint8_t address) : debugSerial(USBTX, USBRX)
khaledelmadawi 0:e63996fd7d3e 67 {
khaledelmadawi 0:e63996fd7d3e 68 devAddr = address;
khaledelmadawi 0:e63996fd7d3e 69 }
khaledelmadawi 0:e63996fd7d3e 70
khaledelmadawi 0:e63996fd7d3e 71 /** Power on and prepare for general usage.
khaledelmadawi 0:e63996fd7d3e 72 * This will activate the device and take it out of sleep mode (which must be done
khaledelmadawi 0:e63996fd7d3e 73 * after start-up). This function also sets both the accelerometer and the gyroscope
khaledelmadawi 0:e63996fd7d3e 74 * to their most sensitive settings, namely +/- 2g and +/- 250 degrees/sec, and sets
khaledelmadawi 0:e63996fd7d3e 75 * the clock source to use the X Gyro for reference, which is slightly better than
khaledelmadawi 0:e63996fd7d3e 76 * the default internal clock source.
khaledelmadawi 0:e63996fd7d3e 77 */
khaledelmadawi 0:e63996fd7d3e 78 void MPU6050::initialize()
khaledelmadawi 0:e63996fd7d3e 79 {
khaledelmadawi 0:e63996fd7d3e 80
khaledelmadawi 0:e63996fd7d3e 81 #ifdef useDebugSerial
khaledelmadawi 0:e63996fd7d3e 82 debugSerial.printf("MPU6050::initialize start\n");
khaledelmadawi 0:e63996fd7d3e 83 #endif
khaledelmadawi 0:e63996fd7d3e 84
khaledelmadawi 0:e63996fd7d3e 85 setClockSource(MPU6050_CLOCK_PLL_XGYRO);
khaledelmadawi 0:e63996fd7d3e 86 setFullScaleGyroRange(MPU6050_GYRO_FS_250);
khaledelmadawi 0:e63996fd7d3e 87 setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
khaledelmadawi 0:e63996fd7d3e 88 setSleepEnabled(false); // thanks to Jack Elston for pointing this one out!
khaledelmadawi 0:e63996fd7d3e 89
khaledelmadawi 0:e63996fd7d3e 90 #ifdef useDebugSerial
khaledelmadawi 0:e63996fd7d3e 91 debugSerial.printf("MPU6050::initialize end\n");
khaledelmadawi 0:e63996fd7d3e 92 #endif
khaledelmadawi 0:e63996fd7d3e 93 }
khaledelmadawi 0:e63996fd7d3e 94
khaledelmadawi 0:e63996fd7d3e 95 /** Verify the I2C connection.
khaledelmadawi 0:e63996fd7d3e 96 * Make sure the device is connected and responds as expected.
khaledelmadawi 0:e63996fd7d3e 97 * @return True if connection is valid, false otherwise
khaledelmadawi 0:e63996fd7d3e 98 */
khaledelmadawi 0:e63996fd7d3e 99 bool MPU6050::testConnection()
khaledelmadawi 0:e63996fd7d3e 100 {
khaledelmadawi 0:e63996fd7d3e 101 #ifdef useDebugSerial
khaledelmadawi 0:e63996fd7d3e 102 debugSerial.printf("MPU6050::testConnection start\n");
khaledelmadawi 0:e63996fd7d3e 103 #endif
khaledelmadawi 0:e63996fd7d3e 104 uint8_t deviceId = getDeviceID();
khaledelmadawi 0:e63996fd7d3e 105 #ifdef useDebugSerial
khaledelmadawi 0:e63996fd7d3e 106 debugSerial.printf("DeviceId = %d\n",deviceId);
khaledelmadawi 0:e63996fd7d3e 107 #endif
khaledelmadawi 0:e63996fd7d3e 108 return deviceId == 0x34;
khaledelmadawi 0:e63996fd7d3e 109 }
khaledelmadawi 0:e63996fd7d3e 110
khaledelmadawi 0:e63996fd7d3e 111 // AUX_VDDIO register (InvenSense demo code calls this RA_*G_OFFS_TC)
khaledelmadawi 0:e63996fd7d3e 112
khaledelmadawi 0:e63996fd7d3e 113 /** Get the auxiliary I2C supply voltage level.
khaledelmadawi 0:e63996fd7d3e 114 * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
khaledelmadawi 0:e63996fd7d3e 115 * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
khaledelmadawi 0:e63996fd7d3e 116 * the MPU-6000, which does not have a VLOGIC pin.
khaledelmadawi 0:e63996fd7d3e 117 * @return I2C supply voltage level (0=VLOGIC, 1=VDD)
khaledelmadawi 0:e63996fd7d3e 118 */
khaledelmadawi 0:e63996fd7d3e 119 uint8_t MPU6050::getAuxVDDIOLevel()
khaledelmadawi 0:e63996fd7d3e 120 {
khaledelmadawi 0:e63996fd7d3e 121 i2Cdev.readBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 122 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 123 }
khaledelmadawi 0:e63996fd7d3e 124 /** Set the auxiliary I2C supply voltage level.
khaledelmadawi 0:e63996fd7d3e 125 * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
khaledelmadawi 0:e63996fd7d3e 126 * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
khaledelmadawi 0:e63996fd7d3e 127 * the MPU-6000, which does not have a VLOGIC pin.
khaledelmadawi 0:e63996fd7d3e 128 * @param level I2C supply voltage level (0=VLOGIC, 1=VDD)
khaledelmadawi 0:e63996fd7d3e 129 */
khaledelmadawi 0:e63996fd7d3e 130 void MPU6050::setAuxVDDIOLevel(uint8_t level)
khaledelmadawi 0:e63996fd7d3e 131 {
khaledelmadawi 0:e63996fd7d3e 132 i2Cdev.writeBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, level);
khaledelmadawi 0:e63996fd7d3e 133 }
khaledelmadawi 0:e63996fd7d3e 134
khaledelmadawi 0:e63996fd7d3e 135 // SMPLRT_DIV register
khaledelmadawi 0:e63996fd7d3e 136
khaledelmadawi 0:e63996fd7d3e 137 /** Get gyroscope output rate divider.
khaledelmadawi 0:e63996fd7d3e 138 * The sensor register output, FIFO output, DMP sampling, Motion detection, Zero
khaledelmadawi 0:e63996fd7d3e 139 * Motion detection, and Free Fall detection are all based on the Sample Rate.
khaledelmadawi 0:e63996fd7d3e 140 * The Sample Rate is generated by dividing the gyroscope output rate by
khaledelmadawi 0:e63996fd7d3e 141 * SMPLRT_DIV:
khaledelmadawi 0:e63996fd7d3e 142 *
khaledelmadawi 0:e63996fd7d3e 143 * Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)
khaledelmadawi 0:e63996fd7d3e 144 *
khaledelmadawi 0:e63996fd7d3e 145 * where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or
khaledelmadawi 0:e63996fd7d3e 146 * 7), and 1kHz when the DLPF is enabled (see Register 26).
khaledelmadawi 0:e63996fd7d3e 147 *
khaledelmadawi 0:e63996fd7d3e 148 * Note: The accelerometer output rate is 1kHz. This means that for a Sample
khaledelmadawi 0:e63996fd7d3e 149 * Rate greater than 1kHz, the same accelerometer sample may be output to the
khaledelmadawi 0:e63996fd7d3e 150 * FIFO, DMP, and sensor registers more than once.
khaledelmadawi 0:e63996fd7d3e 151 *
khaledelmadawi 0:e63996fd7d3e 152 * For a diagram of the gyroscope and accelerometer signal paths, see Section 8
khaledelmadawi 0:e63996fd7d3e 153 * of the MPU-6000/MPU-6050 Product Specification document.
khaledelmadawi 0:e63996fd7d3e 154 *
khaledelmadawi 0:e63996fd7d3e 155 * @return Current sample rate
khaledelmadawi 0:e63996fd7d3e 156 * @see MPU6050_RA_SMPLRT_DIV
khaledelmadawi 0:e63996fd7d3e 157 */
khaledelmadawi 0:e63996fd7d3e 158 uint8_t MPU6050::getRate()
khaledelmadawi 0:e63996fd7d3e 159 {
khaledelmadawi 0:e63996fd7d3e 160 i2Cdev.readByte(devAddr, MPU6050_RA_SMPLRT_DIV, buffer);
khaledelmadawi 0:e63996fd7d3e 161 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 162 }
khaledelmadawi 0:e63996fd7d3e 163 /** Set gyroscope sample rate divider.
khaledelmadawi 0:e63996fd7d3e 164 * @param rate New sample rate divider
khaledelmadawi 0:e63996fd7d3e 165 * @see getRate()
khaledelmadawi 0:e63996fd7d3e 166 * @see MPU6050_RA_SMPLRT_DIV
khaledelmadawi 0:e63996fd7d3e 167 */
khaledelmadawi 0:e63996fd7d3e 168 void MPU6050::setRate(uint8_t rate)
khaledelmadawi 0:e63996fd7d3e 169 {
khaledelmadawi 0:e63996fd7d3e 170 i2Cdev.writeByte(devAddr, MPU6050_RA_SMPLRT_DIV, rate);
khaledelmadawi 0:e63996fd7d3e 171 }
khaledelmadawi 0:e63996fd7d3e 172
khaledelmadawi 0:e63996fd7d3e 173 // CONFIG register
khaledelmadawi 0:e63996fd7d3e 174
khaledelmadawi 0:e63996fd7d3e 175 /** Get external FSYNC configuration.
khaledelmadawi 0:e63996fd7d3e 176 * Configures the external Frame Synchronization (FSYNC) pin sampling. An
khaledelmadawi 0:e63996fd7d3e 177 * external signal connected to the FSYNC pin can be sampled by configuring
khaledelmadawi 0:e63996fd7d3e 178 * EXT_SYNC_SET. Signal changes to the FSYNC pin are latched so that short
khaledelmadawi 0:e63996fd7d3e 179 * strobes may be captured. The latched FSYNC signal will be sampled at the
khaledelmadawi 0:e63996fd7d3e 180 * Sampling Rate, as defined in register 25. After sampling, the latch will
khaledelmadawi 0:e63996fd7d3e 181 * reset to the current FSYNC signal state.
khaledelmadawi 0:e63996fd7d3e 182 *
khaledelmadawi 0:e63996fd7d3e 183 * The sampled value will be reported in place of the least significant bit in
khaledelmadawi 0:e63996fd7d3e 184 * a sensor data register determined by the value of EXT_SYNC_SET according to
khaledelmadawi 0:e63996fd7d3e 185 * the following table.
khaledelmadawi 0:e63996fd7d3e 186 *
khaledelmadawi 0:e63996fd7d3e 187 * <pre>
khaledelmadawi 0:e63996fd7d3e 188 * EXT_SYNC_SET | FSYNC Bit Location
khaledelmadawi 0:e63996fd7d3e 189 * -------------+-------------------
khaledelmadawi 0:e63996fd7d3e 190 * 0 | Input disabled
khaledelmadawi 0:e63996fd7d3e 191 * 1 | TEMP_OUT_L[0]
khaledelmadawi 0:e63996fd7d3e 192 * 2 | GYRO_XOUT_L[0]
khaledelmadawi 0:e63996fd7d3e 193 * 3 | GYRO_YOUT_L[0]
khaledelmadawi 0:e63996fd7d3e 194 * 4 | GYRO_ZOUT_L[0]
khaledelmadawi 0:e63996fd7d3e 195 * 5 | ACCEL_XOUT_L[0]
khaledelmadawi 0:e63996fd7d3e 196 * 6 | ACCEL_YOUT_L[0]
khaledelmadawi 0:e63996fd7d3e 197 * 7 | ACCEL_ZOUT_L[0]
khaledelmadawi 0:e63996fd7d3e 198 * </pre>
khaledelmadawi 0:e63996fd7d3e 199 *
khaledelmadawi 0:e63996fd7d3e 200 * @return FSYNC configuration value
khaledelmadawi 0:e63996fd7d3e 201 */
khaledelmadawi 0:e63996fd7d3e 202 uint8_t MPU6050::getExternalFrameSync()
khaledelmadawi 0:e63996fd7d3e 203 {
khaledelmadawi 0:e63996fd7d3e 204 i2Cdev.readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 205 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 206 }
khaledelmadawi 0:e63996fd7d3e 207 /** Set external FSYNC configuration.
khaledelmadawi 0:e63996fd7d3e 208 * @see getExternalFrameSync()
khaledelmadawi 0:e63996fd7d3e 209 * @see MPU6050_RA_CONFIG
khaledelmadawi 0:e63996fd7d3e 210 * @param sync New FSYNC configuration value
khaledelmadawi 0:e63996fd7d3e 211 */
khaledelmadawi 0:e63996fd7d3e 212 void MPU6050::setExternalFrameSync(uint8_t sync)
khaledelmadawi 0:e63996fd7d3e 213 {
khaledelmadawi 0:e63996fd7d3e 214 i2Cdev.writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, sync);
khaledelmadawi 0:e63996fd7d3e 215 }
khaledelmadawi 0:e63996fd7d3e 216 /** Get digital low-pass filter configuration.
khaledelmadawi 0:e63996fd7d3e 217 * The DLPF_CFG parameter sets the digital low pass filter configuration. It
khaledelmadawi 0:e63996fd7d3e 218 * also determines the internal sampling rate used by the device as shown in
khaledelmadawi 0:e63996fd7d3e 219 * the table below.
khaledelmadawi 0:e63996fd7d3e 220 *
khaledelmadawi 0:e63996fd7d3e 221 * Note: The accelerometer output rate is 1kHz. This means that for a Sample
khaledelmadawi 0:e63996fd7d3e 222 * Rate greater than 1kHz, the same accelerometer sample may be output to the
khaledelmadawi 0:e63996fd7d3e 223 * FIFO, DMP, and sensor registers more than once.
khaledelmadawi 0:e63996fd7d3e 224 *
khaledelmadawi 0:e63996fd7d3e 225 * <pre>
khaledelmadawi 0:e63996fd7d3e 226 * | ACCELEROMETER | GYROSCOPE
khaledelmadawi 0:e63996fd7d3e 227 * DLPF_CFG | Bandwidth | Delay | Bandwidth | Delay | Sample Rate
khaledelmadawi 0:e63996fd7d3e 228 * ---------+-----------+--------+-----------+--------+-------------
khaledelmadawi 0:e63996fd7d3e 229 * 0 | 260Hz | 0ms | 256Hz | 0.98ms | 8kHz
khaledelmadawi 0:e63996fd7d3e 230 * 1 | 184Hz | 2.0ms | 188Hz | 1.9ms | 1kHz
khaledelmadawi 0:e63996fd7d3e 231 * 2 | 94Hz | 3.0ms | 98Hz | 2.8ms | 1kHz
khaledelmadawi 0:e63996fd7d3e 232 * 3 | 44Hz | 4.9ms | 42Hz | 4.8ms | 1kHz
khaledelmadawi 0:e63996fd7d3e 233 * 4 | 21Hz | 8.5ms | 20Hz | 8.3ms | 1kHz
khaledelmadawi 0:e63996fd7d3e 234 * 5 | 10Hz | 13.8ms | 10Hz | 13.4ms | 1kHz
khaledelmadawi 0:e63996fd7d3e 235 * 6 | 5Hz | 19.0ms | 5Hz | 18.6ms | 1kHz
khaledelmadawi 0:e63996fd7d3e 236 * 7 | -- Reserved -- | -- Reserved -- | Reserved
khaledelmadawi 0:e63996fd7d3e 237 * </pre>
khaledelmadawi 0:e63996fd7d3e 238 *
khaledelmadawi 0:e63996fd7d3e 239 * @return DLFP configuration
khaledelmadawi 0:e63996fd7d3e 240 * @see MPU6050_RA_CONFIG
khaledelmadawi 0:e63996fd7d3e 241 * @see MPU6050_CFG_DLPF_CFG_BIT
khaledelmadawi 0:e63996fd7d3e 242 * @see MPU6050_CFG_DLPF_CFG_LENGTH
khaledelmadawi 0:e63996fd7d3e 243 */
khaledelmadawi 0:e63996fd7d3e 244 uint8_t MPU6050::getDLPFMode()
khaledelmadawi 0:e63996fd7d3e 245 {
khaledelmadawi 0:e63996fd7d3e 246 i2Cdev.readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 247 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 248 }
khaledelmadawi 0:e63996fd7d3e 249 /** Set digital low-pass filter configuration.
khaledelmadawi 0:e63996fd7d3e 250 * @param mode New DLFP configuration setting
khaledelmadawi 0:e63996fd7d3e 251 * @see getDLPFBandwidth()
khaledelmadawi 0:e63996fd7d3e 252 * @see MPU6050_DLPF_BW_256
khaledelmadawi 0:e63996fd7d3e 253 * @see MPU6050_RA_CONFIG
khaledelmadawi 0:e63996fd7d3e 254 * @see MPU6050_CFG_DLPF_CFG_BIT
khaledelmadawi 0:e63996fd7d3e 255 * @see MPU6050_CFG_DLPF_CFG_LENGTH
khaledelmadawi 0:e63996fd7d3e 256 */
khaledelmadawi 0:e63996fd7d3e 257 void MPU6050::setDLPFMode(uint8_t mode)
khaledelmadawi 0:e63996fd7d3e 258 {
khaledelmadawi 0:e63996fd7d3e 259 i2Cdev.writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, mode);
khaledelmadawi 0:e63996fd7d3e 260 }
khaledelmadawi 0:e63996fd7d3e 261
khaledelmadawi 0:e63996fd7d3e 262 // GYRO_CONFIG register
khaledelmadawi 0:e63996fd7d3e 263
khaledelmadawi 0:e63996fd7d3e 264 /** Get full-scale gyroscope range.
khaledelmadawi 0:e63996fd7d3e 265 * The FS_SEL parameter allows setting the full-scale range of the gyro sensors,
khaledelmadawi 0:e63996fd7d3e 266 * as described in the table below.
khaledelmadawi 0:e63996fd7d3e 267 *
khaledelmadawi 0:e63996fd7d3e 268 * <pre>
khaledelmadawi 0:e63996fd7d3e 269 * 0 = +/- 250 degrees/sec
khaledelmadawi 0:e63996fd7d3e 270 * 1 = +/- 500 degrees/sec
khaledelmadawi 0:e63996fd7d3e 271 * 2 = +/- 1000 degrees/sec
khaledelmadawi 0:e63996fd7d3e 272 * 3 = +/- 2000 degrees/sec
khaledelmadawi 0:e63996fd7d3e 273 * </pre>
khaledelmadawi 0:e63996fd7d3e 274 *
khaledelmadawi 0:e63996fd7d3e 275 * @return Current full-scale gyroscope range setting
khaledelmadawi 0:e63996fd7d3e 276 * @see MPU6050_GYRO_FS_250
khaledelmadawi 0:e63996fd7d3e 277 * @see MPU6050_RA_GYRO_CONFIG
khaledelmadawi 0:e63996fd7d3e 278 * @see MPU6050_GCONFIG_FS_SEL_BIT
khaledelmadawi 0:e63996fd7d3e 279 * @see MPU6050_GCONFIG_FS_SEL_LENGTH
khaledelmadawi 0:e63996fd7d3e 280 */
khaledelmadawi 0:e63996fd7d3e 281 uint8_t MPU6050::getFullScaleGyroRange()
khaledelmadawi 0:e63996fd7d3e 282 {
khaledelmadawi 0:e63996fd7d3e 283 i2Cdev.readBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 284 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 285 }
khaledelmadawi 0:e63996fd7d3e 286 /** Set full-scale gyroscope range.
khaledelmadawi 0:e63996fd7d3e 287 * @param range New full-scale gyroscope range value
khaledelmadawi 0:e63996fd7d3e 288 * @see getFullScaleRange()
khaledelmadawi 0:e63996fd7d3e 289 * @see MPU6050_GYRO_FS_250
khaledelmadawi 0:e63996fd7d3e 290 * @see MPU6050_RA_GYRO_CONFIG
khaledelmadawi 0:e63996fd7d3e 291 * @see MPU6050_GCONFIG_FS_SEL_BIT
khaledelmadawi 0:e63996fd7d3e 292 * @see MPU6050_GCONFIG_FS_SEL_LENGTH
khaledelmadawi 0:e63996fd7d3e 293 */
khaledelmadawi 0:e63996fd7d3e 294 void MPU6050::setFullScaleGyroRange(uint8_t range)
khaledelmadawi 0:e63996fd7d3e 295 {
khaledelmadawi 0:e63996fd7d3e 296 i2Cdev.writeBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, range);
khaledelmadawi 0:e63996fd7d3e 297 }
khaledelmadawi 0:e63996fd7d3e 298
khaledelmadawi 0:e63996fd7d3e 299 // ACCEL_CONFIG register
khaledelmadawi 0:e63996fd7d3e 300
khaledelmadawi 0:e63996fd7d3e 301 /** Get self-test enabled setting for accelerometer X axis.
khaledelmadawi 0:e63996fd7d3e 302 * @return Self-test enabled value
khaledelmadawi 0:e63996fd7d3e 303 * @see MPU6050_RA_ACCEL_CONFIG
khaledelmadawi 0:e63996fd7d3e 304 */
khaledelmadawi 0:e63996fd7d3e 305 bool MPU6050::getAccelXSelfTest()
khaledelmadawi 0:e63996fd7d3e 306 {
khaledelmadawi 0:e63996fd7d3e 307 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 308 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 309 }
khaledelmadawi 0:e63996fd7d3e 310 /** Get self-test enabled setting for accelerometer X axis.
khaledelmadawi 0:e63996fd7d3e 311 * @param enabled Self-test enabled value
khaledelmadawi 0:e63996fd7d3e 312 * @see MPU6050_RA_ACCEL_CONFIG
khaledelmadawi 0:e63996fd7d3e 313 */
khaledelmadawi 0:e63996fd7d3e 314 void MPU6050::setAccelXSelfTest(bool enabled)
khaledelmadawi 0:e63996fd7d3e 315 {
khaledelmadawi 0:e63996fd7d3e 316 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 317 }
khaledelmadawi 0:e63996fd7d3e 318 /** Get self-test enabled value for accelerometer Y axis.
khaledelmadawi 0:e63996fd7d3e 319 * @return Self-test enabled value
khaledelmadawi 0:e63996fd7d3e 320 * @see MPU6050_RA_ACCEL_CONFIG
khaledelmadawi 0:e63996fd7d3e 321 */
khaledelmadawi 0:e63996fd7d3e 322 bool MPU6050::getAccelYSelfTest()
khaledelmadawi 0:e63996fd7d3e 323 {
khaledelmadawi 0:e63996fd7d3e 324 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 325 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 326 }
khaledelmadawi 0:e63996fd7d3e 327 /** Get self-test enabled value for accelerometer Y axis.
khaledelmadawi 0:e63996fd7d3e 328 * @param enabled Self-test enabled value
khaledelmadawi 0:e63996fd7d3e 329 * @see MPU6050_RA_ACCEL_CONFIG
khaledelmadawi 0:e63996fd7d3e 330 */
khaledelmadawi 0:e63996fd7d3e 331 void MPU6050::setAccelYSelfTest(bool enabled)
khaledelmadawi 0:e63996fd7d3e 332 {
khaledelmadawi 0:e63996fd7d3e 333 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 334 }
khaledelmadawi 0:e63996fd7d3e 335 /** Get self-test enabled value for accelerometer Z axis.
khaledelmadawi 0:e63996fd7d3e 336 * @return Self-test enabled value
khaledelmadawi 0:e63996fd7d3e 337 * @see MPU6050_RA_ACCEL_CONFIG
khaledelmadawi 0:e63996fd7d3e 338 */
khaledelmadawi 0:e63996fd7d3e 339 bool MPU6050::getAccelZSelfTest()
khaledelmadawi 0:e63996fd7d3e 340 {
khaledelmadawi 0:e63996fd7d3e 341 i2Cdev.readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 342 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 343 }
khaledelmadawi 0:e63996fd7d3e 344 /** Set self-test enabled value for accelerometer Z axis.
khaledelmadawi 0:e63996fd7d3e 345 * @param enabled Self-test enabled value
khaledelmadawi 0:e63996fd7d3e 346 * @see MPU6050_RA_ACCEL_CONFIG
khaledelmadawi 0:e63996fd7d3e 347 */
khaledelmadawi 0:e63996fd7d3e 348 void MPU6050::setAccelZSelfTest(bool enabled)
khaledelmadawi 0:e63996fd7d3e 349 {
khaledelmadawi 0:e63996fd7d3e 350 i2Cdev.writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 351 }
khaledelmadawi 0:e63996fd7d3e 352 /** Get full-scale accelerometer range.
khaledelmadawi 0:e63996fd7d3e 353 * The FS_SEL parameter allows setting the full-scale range of the accelerometer
khaledelmadawi 0:e63996fd7d3e 354 * sensors, as described in the table below.
khaledelmadawi 0:e63996fd7d3e 355 *
khaledelmadawi 0:e63996fd7d3e 356 * <pre>
khaledelmadawi 0:e63996fd7d3e 357 * 0 = +/- 2g
khaledelmadawi 0:e63996fd7d3e 358 * 1 = +/- 4g
khaledelmadawi 0:e63996fd7d3e 359 * 2 = +/- 8g
khaledelmadawi 0:e63996fd7d3e 360 * 3 = +/- 16g
khaledelmadawi 0:e63996fd7d3e 361 * </pre>
khaledelmadawi 0:e63996fd7d3e 362 *
khaledelmadawi 0:e63996fd7d3e 363 * @return Current full-scale accelerometer range setting
khaledelmadawi 0:e63996fd7d3e 364 * @see MPU6050_ACCEL_FS_2
khaledelmadawi 0:e63996fd7d3e 365 * @see MPU6050_RA_ACCEL_CONFIG
khaledelmadawi 0:e63996fd7d3e 366 * @see MPU6050_ACONFIG_AFS_SEL_BIT
khaledelmadawi 0:e63996fd7d3e 367 * @see MPU6050_ACONFIG_AFS_SEL_LENGTH
khaledelmadawi 0:e63996fd7d3e 368 */
khaledelmadawi 0:e63996fd7d3e 369 uint8_t MPU6050::getFullScaleAccelRange()
khaledelmadawi 0:e63996fd7d3e 370 {
khaledelmadawi 0:e63996fd7d3e 371 i2Cdev.readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 372 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 373 }
khaledelmadawi 0:e63996fd7d3e 374 /** Set full-scale accelerometer range.
khaledelmadawi 0:e63996fd7d3e 375 * @param range New full-scale accelerometer range setting
khaledelmadawi 0:e63996fd7d3e 376 * @see getFullScaleAccelRange()
khaledelmadawi 0:e63996fd7d3e 377 */
khaledelmadawi 0:e63996fd7d3e 378 void MPU6050::setFullScaleAccelRange(uint8_t range)
khaledelmadawi 0:e63996fd7d3e 379 {
khaledelmadawi 0:e63996fd7d3e 380 i2Cdev.writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, range);
khaledelmadawi 0:e63996fd7d3e 381 }
khaledelmadawi 0:e63996fd7d3e 382 /** Get the high-pass filter configuration.
khaledelmadawi 0:e63996fd7d3e 383 * The DHPF is a filter module in the path leading to motion detectors (Free
khaledelmadawi 0:e63996fd7d3e 384 * Fall, Motion threshold, and Zero Motion). The high pass filter output is not
khaledelmadawi 0:e63996fd7d3e 385 * available to the data registers (see Figure in Section 8 of the MPU-6000/
khaledelmadawi 0:e63996fd7d3e 386 * MPU-6050 Product Specification document).
khaledelmadawi 0:e63996fd7d3e 387 *
khaledelmadawi 0:e63996fd7d3e 388 * The high pass filter has three modes:
khaledelmadawi 0:e63996fd7d3e 389 *
khaledelmadawi 0:e63996fd7d3e 390 * <pre>
khaledelmadawi 0:e63996fd7d3e 391 * Reset: The filter output settles to zero within one sample. This
khaledelmadawi 0:e63996fd7d3e 392 * effectively disables the high pass filter. This mode may be toggled
khaledelmadawi 0:e63996fd7d3e 393 * to quickly settle the filter.
khaledelmadawi 0:e63996fd7d3e 394 *
khaledelmadawi 0:e63996fd7d3e 395 * On: The high pass filter will pass signals above the cut off frequency.
khaledelmadawi 0:e63996fd7d3e 396 *
khaledelmadawi 0:e63996fd7d3e 397 * Hold: When triggered, the filter holds the present sample. The filter
khaledelmadawi 0:e63996fd7d3e 398 * output will be the difference between the input sample and the held
khaledelmadawi 0:e63996fd7d3e 399 * sample.
khaledelmadawi 0:e63996fd7d3e 400 * </pre>
khaledelmadawi 0:e63996fd7d3e 401 *
khaledelmadawi 0:e63996fd7d3e 402 * <pre>
khaledelmadawi 0:e63996fd7d3e 403 * ACCEL_HPF | Filter Mode | Cut-off Frequency
khaledelmadawi 0:e63996fd7d3e 404 * ----------+-------------+------------------
khaledelmadawi 0:e63996fd7d3e 405 * 0 | Reset | None
khaledelmadawi 0:e63996fd7d3e 406 * 1 | On | 5Hz
khaledelmadawi 0:e63996fd7d3e 407 * 2 | On | 2.5Hz
khaledelmadawi 0:e63996fd7d3e 408 * 3 | On | 1.25Hz
khaledelmadawi 0:e63996fd7d3e 409 * 4 | On | 0.63Hz
khaledelmadawi 0:e63996fd7d3e 410 * 7 | Hold | None
khaledelmadawi 0:e63996fd7d3e 411 * </pre>
khaledelmadawi 0:e63996fd7d3e 412 *
khaledelmadawi 0:e63996fd7d3e 413 * @return Current high-pass filter configuration
khaledelmadawi 0:e63996fd7d3e 414 * @see MPU6050_DHPF_RESET
khaledelmadawi 0:e63996fd7d3e 415 * @see MPU6050_RA_ACCEL_CONFIG
khaledelmadawi 0:e63996fd7d3e 416 */
khaledelmadawi 0:e63996fd7d3e 417 uint8_t MPU6050::getDHPFMode()
khaledelmadawi 0:e63996fd7d3e 418 {
khaledelmadawi 0:e63996fd7d3e 419 i2Cdev.readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 420 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 421 }
khaledelmadawi 0:e63996fd7d3e 422 /** Set the high-pass filter configuration.
khaledelmadawi 0:e63996fd7d3e 423 * @param bandwidth New high-pass filter configuration
khaledelmadawi 0:e63996fd7d3e 424 * @see setDHPFMode()
khaledelmadawi 0:e63996fd7d3e 425 * @see MPU6050_DHPF_RESET
khaledelmadawi 0:e63996fd7d3e 426 * @see MPU6050_RA_ACCEL_CONFIG
khaledelmadawi 0:e63996fd7d3e 427 */
khaledelmadawi 0:e63996fd7d3e 428 void MPU6050::setDHPFMode(uint8_t bandwidth)
khaledelmadawi 0:e63996fd7d3e 429 {
khaledelmadawi 0:e63996fd7d3e 430 i2Cdev.writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, bandwidth);
khaledelmadawi 0:e63996fd7d3e 431 }
khaledelmadawi 0:e63996fd7d3e 432
khaledelmadawi 0:e63996fd7d3e 433 // FF_THR register
khaledelmadawi 0:e63996fd7d3e 434
khaledelmadawi 0:e63996fd7d3e 435 /** Get free-fall event acceleration threshold.
khaledelmadawi 0:e63996fd7d3e 436 * This register configures the detection threshold for Free Fall event
khaledelmadawi 0:e63996fd7d3e 437 * detection. The unit of FF_THR is 1LSB = 2mg. Free Fall is detected when the
khaledelmadawi 0:e63996fd7d3e 438 * absolute value of the accelerometer measurements for the three axes are each
khaledelmadawi 0:e63996fd7d3e 439 * less than the detection threshold. This condition increments the Free Fall
khaledelmadawi 0:e63996fd7d3e 440 * duration counter (Register 30). The Free Fall interrupt is triggered when the
khaledelmadawi 0:e63996fd7d3e 441 * Free Fall duration counter reaches the time specified in FF_DUR.
khaledelmadawi 0:e63996fd7d3e 442 *
khaledelmadawi 0:e63996fd7d3e 443 * For more details on the Free Fall detection interrupt, see Section 8.2 of the
khaledelmadawi 0:e63996fd7d3e 444 * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and
khaledelmadawi 0:e63996fd7d3e 445 * 58 of this document.
khaledelmadawi 0:e63996fd7d3e 446 *
khaledelmadawi 0:e63996fd7d3e 447 * @return Current free-fall acceleration threshold value (LSB = 2mg)
khaledelmadawi 0:e63996fd7d3e 448 * @see MPU6050_RA_FF_THR
khaledelmadawi 0:e63996fd7d3e 449 */
khaledelmadawi 0:e63996fd7d3e 450 uint8_t MPU6050::getFreefallDetectionThreshold()
khaledelmadawi 0:e63996fd7d3e 451 {
khaledelmadawi 0:e63996fd7d3e 452 i2Cdev.readByte(devAddr, MPU6050_RA_FF_THR, buffer);
khaledelmadawi 0:e63996fd7d3e 453 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 454 }
khaledelmadawi 0:e63996fd7d3e 455 /** Get free-fall event acceleration threshold.
khaledelmadawi 0:e63996fd7d3e 456 * @param threshold New free-fall acceleration threshold value (LSB = 2mg)
khaledelmadawi 0:e63996fd7d3e 457 * @see getFreefallDetectionThreshold()
khaledelmadawi 0:e63996fd7d3e 458 * @see MPU6050_RA_FF_THR
khaledelmadawi 0:e63996fd7d3e 459 */
khaledelmadawi 0:e63996fd7d3e 460 void MPU6050::setFreefallDetectionThreshold(uint8_t threshold)
khaledelmadawi 0:e63996fd7d3e 461 {
khaledelmadawi 0:e63996fd7d3e 462 i2Cdev.writeByte(devAddr, MPU6050_RA_FF_THR, threshold);
khaledelmadawi 0:e63996fd7d3e 463 }
khaledelmadawi 0:e63996fd7d3e 464
khaledelmadawi 0:e63996fd7d3e 465 // FF_DUR register
khaledelmadawi 0:e63996fd7d3e 466
khaledelmadawi 0:e63996fd7d3e 467 /** Get free-fall event duration threshold.
khaledelmadawi 0:e63996fd7d3e 468 * This register configures the duration counter threshold for Free Fall event
khaledelmadawi 0:e63996fd7d3e 469 * detection. The duration counter ticks at 1kHz, therefore FF_DUR has a unit
khaledelmadawi 0:e63996fd7d3e 470 * of 1 LSB = 1 ms.
khaledelmadawi 0:e63996fd7d3e 471 *
khaledelmadawi 0:e63996fd7d3e 472 * The Free Fall duration counter increments while the absolute value of the
khaledelmadawi 0:e63996fd7d3e 473 * accelerometer measurements are each less than the detection threshold
khaledelmadawi 0:e63996fd7d3e 474 * (Register 29). The Free Fall interrupt is triggered when the Free Fall
khaledelmadawi 0:e63996fd7d3e 475 * duration counter reaches the time specified in this register.
khaledelmadawi 0:e63996fd7d3e 476 *
khaledelmadawi 0:e63996fd7d3e 477 * For more details on the Free Fall detection interrupt, see Section 8.2 of
khaledelmadawi 0:e63996fd7d3e 478 * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56
khaledelmadawi 0:e63996fd7d3e 479 * and 58 of this document.
khaledelmadawi 0:e63996fd7d3e 480 *
khaledelmadawi 0:e63996fd7d3e 481 * @return Current free-fall duration threshold value (LSB = 1ms)
khaledelmadawi 0:e63996fd7d3e 482 * @see MPU6050_RA_FF_DUR
khaledelmadawi 0:e63996fd7d3e 483 */
khaledelmadawi 0:e63996fd7d3e 484 uint8_t MPU6050::getFreefallDetectionDuration()
khaledelmadawi 0:e63996fd7d3e 485 {
khaledelmadawi 0:e63996fd7d3e 486 i2Cdev.readByte(devAddr, MPU6050_RA_FF_DUR, buffer);
khaledelmadawi 0:e63996fd7d3e 487 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 488 }
khaledelmadawi 0:e63996fd7d3e 489 /** Get free-fall event duration threshold.
khaledelmadawi 0:e63996fd7d3e 490 * @param duration New free-fall duration threshold value (LSB = 1ms)
khaledelmadawi 0:e63996fd7d3e 491 * @see getFreefallDetectionDuration()
khaledelmadawi 0:e63996fd7d3e 492 * @see MPU6050_RA_FF_DUR
khaledelmadawi 0:e63996fd7d3e 493 */
khaledelmadawi 0:e63996fd7d3e 494 void MPU6050::setFreefallDetectionDuration(uint8_t duration)
khaledelmadawi 0:e63996fd7d3e 495 {
khaledelmadawi 0:e63996fd7d3e 496 i2Cdev.writeByte(devAddr, MPU6050_RA_FF_DUR, duration);
khaledelmadawi 0:e63996fd7d3e 497 }
khaledelmadawi 0:e63996fd7d3e 498
khaledelmadawi 0:e63996fd7d3e 499 // MOT_THR register
khaledelmadawi 0:e63996fd7d3e 500
khaledelmadawi 0:e63996fd7d3e 501 /** Get motion detection event acceleration threshold.
khaledelmadawi 0:e63996fd7d3e 502 * This register configures the detection threshold for Motion interrupt
khaledelmadawi 0:e63996fd7d3e 503 * generation. The unit of MOT_THR is 1LSB = 2mg. Motion is detected when the
khaledelmadawi 0:e63996fd7d3e 504 * absolute value of any of the accelerometer measurements exceeds this Motion
khaledelmadawi 0:e63996fd7d3e 505 * detection threshold. This condition increments the Motion detection duration
khaledelmadawi 0:e63996fd7d3e 506 * counter (Register 32). The Motion detection interrupt is triggered when the
khaledelmadawi 0:e63996fd7d3e 507 * Motion Detection counter reaches the time count specified in MOT_DUR
khaledelmadawi 0:e63996fd7d3e 508 * (Register 32).
khaledelmadawi 0:e63996fd7d3e 509 *
khaledelmadawi 0:e63996fd7d3e 510 * The Motion interrupt will indicate the axis and polarity of detected motion
khaledelmadawi 0:e63996fd7d3e 511 * in MOT_DETECT_STATUS (Register 97).
khaledelmadawi 0:e63996fd7d3e 512 *
khaledelmadawi 0:e63996fd7d3e 513 * For more details on the Motion detection interrupt, see Section 8.3 of the
khaledelmadawi 0:e63996fd7d3e 514 * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and
khaledelmadawi 0:e63996fd7d3e 515 * 58 of this document.
khaledelmadawi 0:e63996fd7d3e 516 *
khaledelmadawi 0:e63996fd7d3e 517 * @return Current motion detection acceleration threshold value (LSB = 2mg)
khaledelmadawi 0:e63996fd7d3e 518 * @see MPU6050_RA_MOT_THR
khaledelmadawi 0:e63996fd7d3e 519 */
khaledelmadawi 0:e63996fd7d3e 520 uint8_t MPU6050::getMotionDetectionThreshold()
khaledelmadawi 0:e63996fd7d3e 521 {
khaledelmadawi 0:e63996fd7d3e 522 i2Cdev.readByte(devAddr, MPU6050_RA_MOT_THR, buffer);
khaledelmadawi 0:e63996fd7d3e 523 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 524 }
khaledelmadawi 0:e63996fd7d3e 525 /** Set free-fall event acceleration threshold.
khaledelmadawi 0:e63996fd7d3e 526 * @param threshold New motion detection acceleration threshold value (LSB = 2mg)
khaledelmadawi 0:e63996fd7d3e 527 * @see getMotionDetectionThreshold()
khaledelmadawi 0:e63996fd7d3e 528 * @see MPU6050_RA_MOT_THR
khaledelmadawi 0:e63996fd7d3e 529 */
khaledelmadawi 0:e63996fd7d3e 530 void MPU6050::setMotionDetectionThreshold(uint8_t threshold)
khaledelmadawi 0:e63996fd7d3e 531 {
khaledelmadawi 0:e63996fd7d3e 532 i2Cdev.writeByte(devAddr, MPU6050_RA_MOT_THR, threshold);
khaledelmadawi 0:e63996fd7d3e 533 }
khaledelmadawi 0:e63996fd7d3e 534
khaledelmadawi 0:e63996fd7d3e 535 // MOT_DUR register
khaledelmadawi 0:e63996fd7d3e 536
khaledelmadawi 0:e63996fd7d3e 537 /** Get motion detection event duration threshold.
khaledelmadawi 0:e63996fd7d3e 538 * This register configures the duration counter threshold for Motion interrupt
khaledelmadawi 0:e63996fd7d3e 539 * generation. The duration counter ticks at 1 kHz, therefore MOT_DUR has a unit
khaledelmadawi 0:e63996fd7d3e 540 * of 1LSB = 1ms. The Motion detection duration counter increments when the
khaledelmadawi 0:e63996fd7d3e 541 * absolute value of any of the accelerometer measurements exceeds the Motion
khaledelmadawi 0:e63996fd7d3e 542 * detection threshold (Register 31). The Motion detection interrupt is
khaledelmadawi 0:e63996fd7d3e 543 * triggered when the Motion detection counter reaches the time count specified
khaledelmadawi 0:e63996fd7d3e 544 * in this register.
khaledelmadawi 0:e63996fd7d3e 545 *
khaledelmadawi 0:e63996fd7d3e 546 * For more details on the Motion detection interrupt, see Section 8.3 of the
khaledelmadawi 0:e63996fd7d3e 547 * MPU-6000/MPU-6050 Product Specification document.
khaledelmadawi 0:e63996fd7d3e 548 *
khaledelmadawi 0:e63996fd7d3e 549 * @return Current motion detection duration threshold value (LSB = 1ms)
khaledelmadawi 0:e63996fd7d3e 550 * @see MPU6050_RA_MOT_DUR
khaledelmadawi 0:e63996fd7d3e 551 */
khaledelmadawi 0:e63996fd7d3e 552 uint8_t MPU6050::getMotionDetectionDuration()
khaledelmadawi 0:e63996fd7d3e 553 {
khaledelmadawi 0:e63996fd7d3e 554 i2Cdev.readByte(devAddr, MPU6050_RA_MOT_DUR, buffer);
khaledelmadawi 0:e63996fd7d3e 555 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 556 }
khaledelmadawi 0:e63996fd7d3e 557 /** Set motion detection event duration threshold.
khaledelmadawi 0:e63996fd7d3e 558 * @param duration New motion detection duration threshold value (LSB = 1ms)
khaledelmadawi 0:e63996fd7d3e 559 * @see getMotionDetectionDuration()
khaledelmadawi 0:e63996fd7d3e 560 * @see MPU6050_RA_MOT_DUR
khaledelmadawi 0:e63996fd7d3e 561 */
khaledelmadawi 0:e63996fd7d3e 562 void MPU6050::setMotionDetectionDuration(uint8_t duration)
khaledelmadawi 0:e63996fd7d3e 563 {
khaledelmadawi 0:e63996fd7d3e 564 i2Cdev.writeByte(devAddr, MPU6050_RA_MOT_DUR, duration);
khaledelmadawi 0:e63996fd7d3e 565 }
khaledelmadawi 0:e63996fd7d3e 566
khaledelmadawi 0:e63996fd7d3e 567 // ZRMOT_THR register
khaledelmadawi 0:e63996fd7d3e 568
khaledelmadawi 0:e63996fd7d3e 569 /** Get zero motion detection event acceleration threshold.
khaledelmadawi 0:e63996fd7d3e 570 * This register configures the detection threshold for Zero Motion interrupt
khaledelmadawi 0:e63996fd7d3e 571 * generation. The unit of ZRMOT_THR is 1LSB = 2mg. Zero Motion is detected when
khaledelmadawi 0:e63996fd7d3e 572 * the absolute value of the accelerometer measurements for the 3 axes are each
khaledelmadawi 0:e63996fd7d3e 573 * less than the detection threshold. This condition increments the Zero Motion
khaledelmadawi 0:e63996fd7d3e 574 * duration counter (Register 34). The Zero Motion interrupt is triggered when
khaledelmadawi 0:e63996fd7d3e 575 * the Zero Motion duration counter reaches the time count specified in
khaledelmadawi 0:e63996fd7d3e 576 * ZRMOT_DUR (Register 34).
khaledelmadawi 0:e63996fd7d3e 577 *
khaledelmadawi 0:e63996fd7d3e 578 * Unlike Free Fall or Motion detection, Zero Motion detection triggers an
khaledelmadawi 0:e63996fd7d3e 579 * interrupt both when Zero Motion is first detected and when Zero Motion is no
khaledelmadawi 0:e63996fd7d3e 580 * longer detected.
khaledelmadawi 0:e63996fd7d3e 581 *
khaledelmadawi 0:e63996fd7d3e 582 * When a zero motion event is detected, a Zero Motion Status will be indicated
khaledelmadawi 0:e63996fd7d3e 583 * in the MOT_DETECT_STATUS register (Register 97). When a motion-to-zero-motion
khaledelmadawi 0:e63996fd7d3e 584 * condition is detected, the status bit is set to 1. When a zero-motion-to-
khaledelmadawi 0:e63996fd7d3e 585 * motion condition is detected, the status bit is set to 0.
khaledelmadawi 0:e63996fd7d3e 586 *
khaledelmadawi 0:e63996fd7d3e 587 * For more details on the Zero Motion detection interrupt, see Section 8.4 of
khaledelmadawi 0:e63996fd7d3e 588 * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56
khaledelmadawi 0:e63996fd7d3e 589 * and 58 of this document.
khaledelmadawi 0:e63996fd7d3e 590 *
khaledelmadawi 0:e63996fd7d3e 591 * @return Current zero motion detection acceleration threshold value (LSB = 2mg)
khaledelmadawi 0:e63996fd7d3e 592 * @see MPU6050_RA_ZRMOT_THR
khaledelmadawi 0:e63996fd7d3e 593 */
khaledelmadawi 0:e63996fd7d3e 594 uint8_t MPU6050::getZeroMotionDetectionThreshold()
khaledelmadawi 0:e63996fd7d3e 595 {
khaledelmadawi 0:e63996fd7d3e 596 i2Cdev.readByte(devAddr, MPU6050_RA_ZRMOT_THR, buffer);
khaledelmadawi 0:e63996fd7d3e 597 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 598 }
khaledelmadawi 0:e63996fd7d3e 599 /** Set zero motion detection event acceleration threshold.
khaledelmadawi 0:e63996fd7d3e 600 * @param threshold New zero motion detection acceleration threshold value (LSB = 2mg)
khaledelmadawi 0:e63996fd7d3e 601 * @see getZeroMotionDetectionThreshold()
khaledelmadawi 0:e63996fd7d3e 602 * @see MPU6050_RA_ZRMOT_THR
khaledelmadawi 0:e63996fd7d3e 603 */
khaledelmadawi 0:e63996fd7d3e 604 void MPU6050::setZeroMotionDetectionThreshold(uint8_t threshold)
khaledelmadawi 0:e63996fd7d3e 605 {
khaledelmadawi 0:e63996fd7d3e 606 i2Cdev.writeByte(devAddr, MPU6050_RA_ZRMOT_THR, threshold);
khaledelmadawi 0:e63996fd7d3e 607 }
khaledelmadawi 0:e63996fd7d3e 608
khaledelmadawi 0:e63996fd7d3e 609 // ZRMOT_DUR register
khaledelmadawi 0:e63996fd7d3e 610
khaledelmadawi 0:e63996fd7d3e 611 /** Get zero motion detection event duration threshold.
khaledelmadawi 0:e63996fd7d3e 612 * This register configures the duration counter threshold for Zero Motion
khaledelmadawi 0:e63996fd7d3e 613 * interrupt generation. The duration counter ticks at 16 Hz, therefore
khaledelmadawi 0:e63996fd7d3e 614 * ZRMOT_DUR has a unit of 1 LSB = 64 ms. The Zero Motion duration counter
khaledelmadawi 0:e63996fd7d3e 615 * increments while the absolute value of the accelerometer measurements are
khaledelmadawi 0:e63996fd7d3e 616 * each less than the detection threshold (Register 33). The Zero Motion
khaledelmadawi 0:e63996fd7d3e 617 * interrupt is triggered when the Zero Motion duration counter reaches the time
khaledelmadawi 0:e63996fd7d3e 618 * count specified in this register.
khaledelmadawi 0:e63996fd7d3e 619 *
khaledelmadawi 0:e63996fd7d3e 620 * For more details on the Zero Motion detection interrupt, see Section 8.4 of
khaledelmadawi 0:e63996fd7d3e 621 * the MPU-6000/MPU-6050 Product Specification document, as well as Registers 56
khaledelmadawi 0:e63996fd7d3e 622 * and 58 of this document.
khaledelmadawi 0:e63996fd7d3e 623 *
khaledelmadawi 0:e63996fd7d3e 624 * @return Current zero motion detection duration threshold value (LSB = 64ms)
khaledelmadawi 0:e63996fd7d3e 625 * @see MPU6050_RA_ZRMOT_DUR
khaledelmadawi 0:e63996fd7d3e 626 */
khaledelmadawi 0:e63996fd7d3e 627 uint8_t MPU6050::getZeroMotionDetectionDuration()
khaledelmadawi 0:e63996fd7d3e 628 {
khaledelmadawi 0:e63996fd7d3e 629 i2Cdev.readByte(devAddr, MPU6050_RA_ZRMOT_DUR, buffer);
khaledelmadawi 0:e63996fd7d3e 630 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 631 }
khaledelmadawi 0:e63996fd7d3e 632 /** Set zero motion detection event duration threshold.
khaledelmadawi 0:e63996fd7d3e 633 * @param duration New zero motion detection duration threshold value (LSB = 1ms)
khaledelmadawi 0:e63996fd7d3e 634 * @see getZeroMotionDetectionDuration()
khaledelmadawi 0:e63996fd7d3e 635 * @see MPU6050_RA_ZRMOT_DUR
khaledelmadawi 0:e63996fd7d3e 636 */
khaledelmadawi 0:e63996fd7d3e 637 void MPU6050::setZeroMotionDetectionDuration(uint8_t duration)
khaledelmadawi 0:e63996fd7d3e 638 {
khaledelmadawi 0:e63996fd7d3e 639 i2Cdev.writeByte(devAddr, MPU6050_RA_ZRMOT_DUR, duration);
khaledelmadawi 0:e63996fd7d3e 640 }
khaledelmadawi 0:e63996fd7d3e 641
khaledelmadawi 0:e63996fd7d3e 642 // FIFO_EN register
khaledelmadawi 0:e63996fd7d3e 643
khaledelmadawi 0:e63996fd7d3e 644 /** Get temperature FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 645 * When set to 1, this bit enables TEMP_OUT_H and TEMP_OUT_L (Registers 65 and
khaledelmadawi 0:e63996fd7d3e 646 * 66) to be written into the FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 647 * @return Current temperature FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 648 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 649 */
khaledelmadawi 0:e63996fd7d3e 650 bool MPU6050::getTempFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 651 {
khaledelmadawi 0:e63996fd7d3e 652 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 653 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 654 }
khaledelmadawi 0:e63996fd7d3e 655 /** Set temperature FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 656 * @param enabled New temperature FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 657 * @see getTempFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 658 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 659 */
khaledelmadawi 0:e63996fd7d3e 660 void MPU6050::setTempFIFOEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 661 {
khaledelmadawi 0:e63996fd7d3e 662 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 663 }
khaledelmadawi 0:e63996fd7d3e 664 /** Get gyroscope X-axis FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 665 * When set to 1, this bit enables GYRO_XOUT_H and GYRO_XOUT_L (Registers 67 and
khaledelmadawi 0:e63996fd7d3e 666 * 68) to be written into the FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 667 * @return Current gyroscope X-axis FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 668 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 669 */
khaledelmadawi 0:e63996fd7d3e 670 bool MPU6050::getXGyroFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 671 {
khaledelmadawi 0:e63996fd7d3e 672 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 673 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 674 }
khaledelmadawi 0:e63996fd7d3e 675 /** Set gyroscope X-axis FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 676 * @param enabled New gyroscope X-axis FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 677 * @see getXGyroFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 678 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 679 */
khaledelmadawi 0:e63996fd7d3e 680 void MPU6050::setXGyroFIFOEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 681 {
khaledelmadawi 0:e63996fd7d3e 682 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 683 }
khaledelmadawi 0:e63996fd7d3e 684 /** Get gyroscope Y-axis FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 685 * When set to 1, this bit enables GYRO_YOUT_H and GYRO_YOUT_L (Registers 69 and
khaledelmadawi 0:e63996fd7d3e 686 * 70) to be written into the FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 687 * @return Current gyroscope Y-axis FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 688 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 689 */
khaledelmadawi 0:e63996fd7d3e 690 bool MPU6050::getYGyroFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 691 {
khaledelmadawi 0:e63996fd7d3e 692 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 693 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 694 }
khaledelmadawi 0:e63996fd7d3e 695 /** Set gyroscope Y-axis FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 696 * @param enabled New gyroscope Y-axis FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 697 * @see getYGyroFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 698 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 699 */
khaledelmadawi 0:e63996fd7d3e 700 void MPU6050::setYGyroFIFOEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 701 {
khaledelmadawi 0:e63996fd7d3e 702 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 703 }
khaledelmadawi 0:e63996fd7d3e 704 /** Get gyroscope Z-axis FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 705 * When set to 1, this bit enables GYRO_ZOUT_H and GYRO_ZOUT_L (Registers 71 and
khaledelmadawi 0:e63996fd7d3e 706 * 72) to be written into the FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 707 * @return Current gyroscope Z-axis FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 708 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 709 */
khaledelmadawi 0:e63996fd7d3e 710 bool MPU6050::getZGyroFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 711 {
khaledelmadawi 0:e63996fd7d3e 712 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 713 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 714 }
khaledelmadawi 0:e63996fd7d3e 715 /** Set gyroscope Z-axis FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 716 * @param enabled New gyroscope Z-axis FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 717 * @see getZGyroFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 718 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 719 */
khaledelmadawi 0:e63996fd7d3e 720 void MPU6050::setZGyroFIFOEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 721 {
khaledelmadawi 0:e63996fd7d3e 722 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 723 }
khaledelmadawi 0:e63996fd7d3e 724 /** Get accelerometer FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 725 * When set to 1, this bit enables ACCEL_XOUT_H, ACCEL_XOUT_L, ACCEL_YOUT_H,
khaledelmadawi 0:e63996fd7d3e 726 * ACCEL_YOUT_L, ACCEL_ZOUT_H, and ACCEL_ZOUT_L (Registers 59 to 64) to be
khaledelmadawi 0:e63996fd7d3e 727 * written into the FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 728 * @return Current accelerometer FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 729 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 730 */
khaledelmadawi 0:e63996fd7d3e 731 bool MPU6050::getAccelFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 732 {
khaledelmadawi 0:e63996fd7d3e 733 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 734 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 735 }
khaledelmadawi 0:e63996fd7d3e 736 /** Set accelerometer FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 737 * @param enabled New accelerometer FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 738 * @see getAccelFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 739 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 740 */
khaledelmadawi 0:e63996fd7d3e 741 void MPU6050::setAccelFIFOEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 742 {
khaledelmadawi 0:e63996fd7d3e 743 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 744 }
khaledelmadawi 0:e63996fd7d3e 745 /** Get Slave 2 FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 746 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
khaledelmadawi 0:e63996fd7d3e 747 * associated with Slave 2 to be written into the FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 748 * @return Current Slave 2 FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 749 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 750 */
khaledelmadawi 0:e63996fd7d3e 751 bool MPU6050::getSlave2FIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 752 {
khaledelmadawi 0:e63996fd7d3e 753 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 754 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 755 }
khaledelmadawi 0:e63996fd7d3e 756 /** Set Slave 2 FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 757 * @param enabled New Slave 2 FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 758 * @see getSlave2FIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 759 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 760 */
khaledelmadawi 0:e63996fd7d3e 761 void MPU6050::setSlave2FIFOEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 762 {
khaledelmadawi 0:e63996fd7d3e 763 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 764 }
khaledelmadawi 0:e63996fd7d3e 765 /** Get Slave 1 FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 766 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
khaledelmadawi 0:e63996fd7d3e 767 * associated with Slave 1 to be written into the FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 768 * @return Current Slave 1 FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 769 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 770 */
khaledelmadawi 0:e63996fd7d3e 771 bool MPU6050::getSlave1FIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 772 {
khaledelmadawi 0:e63996fd7d3e 773 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 774 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 775 }
khaledelmadawi 0:e63996fd7d3e 776 /** Set Slave 1 FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 777 * @param enabled New Slave 1 FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 778 * @see getSlave1FIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 779 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 780 */
khaledelmadawi 0:e63996fd7d3e 781 void MPU6050::setSlave1FIFOEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 782 {
khaledelmadawi 0:e63996fd7d3e 783 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 784 }
khaledelmadawi 0:e63996fd7d3e 785 /** Get Slave 0 FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 786 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
khaledelmadawi 0:e63996fd7d3e 787 * associated with Slave 0 to be written into the FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 788 * @return Current Slave 0 FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 789 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 790 */
khaledelmadawi 0:e63996fd7d3e 791 bool MPU6050::getSlave0FIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 792 {
khaledelmadawi 0:e63996fd7d3e 793 i2Cdev.readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 794 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 795 }
khaledelmadawi 0:e63996fd7d3e 796 /** Set Slave 0 FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 797 * @param enabled New Slave 0 FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 798 * @see getSlave0FIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 799 * @see MPU6050_RA_FIFO_EN
khaledelmadawi 0:e63996fd7d3e 800 */
khaledelmadawi 0:e63996fd7d3e 801 void MPU6050::setSlave0FIFOEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 802 {
khaledelmadawi 0:e63996fd7d3e 803 i2Cdev.writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 804 }
khaledelmadawi 0:e63996fd7d3e 805
khaledelmadawi 0:e63996fd7d3e 806 // I2C_MST_CTRL register
khaledelmadawi 0:e63996fd7d3e 807
khaledelmadawi 0:e63996fd7d3e 808 /** Get multi-master enabled value.
khaledelmadawi 0:e63996fd7d3e 809 * Multi-master capability allows multiple I2C masters to operate on the same
khaledelmadawi 0:e63996fd7d3e 810 * bus. In circuits where multi-master capability is required, set MULT_MST_EN
khaledelmadawi 0:e63996fd7d3e 811 * to 1. This will increase current drawn by approximately 30uA.
khaledelmadawi 0:e63996fd7d3e 812 *
khaledelmadawi 0:e63996fd7d3e 813 * In circuits where multi-master capability is required, the state of the I2C
khaledelmadawi 0:e63996fd7d3e 814 * bus must always be monitored by each separate I2C Master. Before an I2C
khaledelmadawi 0:e63996fd7d3e 815 * Master can assume arbitration of the bus, it must first confirm that no other
khaledelmadawi 0:e63996fd7d3e 816 * I2C Master has arbitration of the bus. When MULT_MST_EN is set to 1, the
khaledelmadawi 0:e63996fd7d3e 817 * MPU-60X0's bus arbitration detection logic is turned on, enabling it to
khaledelmadawi 0:e63996fd7d3e 818 * detect when the bus is available.
khaledelmadawi 0:e63996fd7d3e 819 *
khaledelmadawi 0:e63996fd7d3e 820 * @return Current multi-master enabled value
khaledelmadawi 0:e63996fd7d3e 821 * @see MPU6050_RA_I2C_MST_CTRL
khaledelmadawi 0:e63996fd7d3e 822 */
khaledelmadawi 0:e63996fd7d3e 823 bool MPU6050::getMultiMasterEnabled()
khaledelmadawi 0:e63996fd7d3e 824 {
khaledelmadawi 0:e63996fd7d3e 825 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 826 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 827 }
khaledelmadawi 0:e63996fd7d3e 828 /** Set multi-master enabled value.
khaledelmadawi 0:e63996fd7d3e 829 * @param enabled New multi-master enabled value
khaledelmadawi 0:e63996fd7d3e 830 * @see getMultiMasterEnabled()
khaledelmadawi 0:e63996fd7d3e 831 * @see MPU6050_RA_I2C_MST_CTRL
khaledelmadawi 0:e63996fd7d3e 832 */
khaledelmadawi 0:e63996fd7d3e 833 void MPU6050::setMultiMasterEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 834 {
khaledelmadawi 0:e63996fd7d3e 835 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 836 }
khaledelmadawi 0:e63996fd7d3e 837 /** Get wait-for-external-sensor-data enabled value.
khaledelmadawi 0:e63996fd7d3e 838 * When the WAIT_FOR_ES bit is set to 1, the Data Ready interrupt will be
khaledelmadawi 0:e63996fd7d3e 839 * delayed until External Sensor data from the Slave Devices are loaded into the
khaledelmadawi 0:e63996fd7d3e 840 * EXT_SENS_DATA registers. This is used to ensure that both the internal sensor
khaledelmadawi 0:e63996fd7d3e 841 * data (i.e. from gyro and accel) and external sensor data have been loaded to
khaledelmadawi 0:e63996fd7d3e 842 * their respective data registers (i.e. the data is synced) when the Data Ready
khaledelmadawi 0:e63996fd7d3e 843 * interrupt is triggered.
khaledelmadawi 0:e63996fd7d3e 844 *
khaledelmadawi 0:e63996fd7d3e 845 * @return Current wait-for-external-sensor-data enabled value
khaledelmadawi 0:e63996fd7d3e 846 * @see MPU6050_RA_I2C_MST_CTRL
khaledelmadawi 0:e63996fd7d3e 847 */
khaledelmadawi 0:e63996fd7d3e 848 bool MPU6050::getWaitForExternalSensorEnabled()
khaledelmadawi 0:e63996fd7d3e 849 {
khaledelmadawi 0:e63996fd7d3e 850 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 851 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 852 }
khaledelmadawi 0:e63996fd7d3e 853 /** Set wait-for-external-sensor-data enabled value.
khaledelmadawi 0:e63996fd7d3e 854 * @param enabled New wait-for-external-sensor-data enabled value
khaledelmadawi 0:e63996fd7d3e 855 * @see getWaitForExternalSensorEnabled()
khaledelmadawi 0:e63996fd7d3e 856 * @see MPU6050_RA_I2C_MST_CTRL
khaledelmadawi 0:e63996fd7d3e 857 */
khaledelmadawi 0:e63996fd7d3e 858 void MPU6050::setWaitForExternalSensorEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 859 {
khaledelmadawi 0:e63996fd7d3e 860 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 861 }
khaledelmadawi 0:e63996fd7d3e 862 /** Get Slave 3 FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 863 * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
khaledelmadawi 0:e63996fd7d3e 864 * associated with Slave 3 to be written into the FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 865 * @return Current Slave 3 FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 866 * @see MPU6050_RA_MST_CTRL
khaledelmadawi 0:e63996fd7d3e 867 */
khaledelmadawi 0:e63996fd7d3e 868 bool MPU6050::getSlave3FIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 869 {
khaledelmadawi 0:e63996fd7d3e 870 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 871 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 872 }
khaledelmadawi 0:e63996fd7d3e 873 /** Set Slave 3 FIFO enabled value.
khaledelmadawi 0:e63996fd7d3e 874 * @param enabled New Slave 3 FIFO enabled value
khaledelmadawi 0:e63996fd7d3e 875 * @see getSlave3FIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 876 * @see MPU6050_RA_MST_CTRL
khaledelmadawi 0:e63996fd7d3e 877 */
khaledelmadawi 0:e63996fd7d3e 878 void MPU6050::setSlave3FIFOEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 879 {
khaledelmadawi 0:e63996fd7d3e 880 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 881 }
khaledelmadawi 0:e63996fd7d3e 882 /** Get slave read/write transition enabled value.
khaledelmadawi 0:e63996fd7d3e 883 * The I2C_MST_P_NSR bit configures the I2C Master's transition from one slave
khaledelmadawi 0:e63996fd7d3e 884 * read to the next slave read. If the bit equals 0, there will be a restart
khaledelmadawi 0:e63996fd7d3e 885 * between reads. If the bit equals 1, there will be a stop followed by a start
khaledelmadawi 0:e63996fd7d3e 886 * of the following read. When a write transaction follows a read transaction,
khaledelmadawi 0:e63996fd7d3e 887 * the stop followed by a start of the successive write will be always used.
khaledelmadawi 0:e63996fd7d3e 888 *
khaledelmadawi 0:e63996fd7d3e 889 * @return Current slave read/write transition enabled value
khaledelmadawi 0:e63996fd7d3e 890 * @see MPU6050_RA_I2C_MST_CTRL
khaledelmadawi 0:e63996fd7d3e 891 */
khaledelmadawi 0:e63996fd7d3e 892 bool MPU6050::getSlaveReadWriteTransitionEnabled()
khaledelmadawi 0:e63996fd7d3e 893 {
khaledelmadawi 0:e63996fd7d3e 894 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 895 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 896 }
khaledelmadawi 0:e63996fd7d3e 897 /** Set slave read/write transition enabled value.
khaledelmadawi 0:e63996fd7d3e 898 * @param enabled New slave read/write transition enabled value
khaledelmadawi 0:e63996fd7d3e 899 * @see getSlaveReadWriteTransitionEnabled()
khaledelmadawi 0:e63996fd7d3e 900 * @see MPU6050_RA_I2C_MST_CTRL
khaledelmadawi 0:e63996fd7d3e 901 */
khaledelmadawi 0:e63996fd7d3e 902 void MPU6050::setSlaveReadWriteTransitionEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 903 {
khaledelmadawi 0:e63996fd7d3e 904 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 905 }
khaledelmadawi 0:e63996fd7d3e 906 /** Get I2C master clock speed.
khaledelmadawi 0:e63996fd7d3e 907 * I2C_MST_CLK is a 4 bit unsigned value which configures a divider on the
khaledelmadawi 0:e63996fd7d3e 908 * MPU-60X0 internal 8MHz clock. It sets the I2C master clock speed according to
khaledelmadawi 0:e63996fd7d3e 909 * the following table:
khaledelmadawi 0:e63996fd7d3e 910 *
khaledelmadawi 0:e63996fd7d3e 911 * <pre>
khaledelmadawi 0:e63996fd7d3e 912 * I2C_MST_CLK | I2C Master Clock Speed | 8MHz Clock Divider
khaledelmadawi 0:e63996fd7d3e 913 * ------------+------------------------+-------------------
khaledelmadawi 0:e63996fd7d3e 914 * 0 | 348kHz | 23
khaledelmadawi 0:e63996fd7d3e 915 * 1 | 333kHz | 24
khaledelmadawi 0:e63996fd7d3e 916 * 2 | 320kHz | 25
khaledelmadawi 0:e63996fd7d3e 917 * 3 | 308kHz | 26
khaledelmadawi 0:e63996fd7d3e 918 * 4 | 296kHz | 27
khaledelmadawi 0:e63996fd7d3e 919 * 5 | 286kHz | 28
khaledelmadawi 0:e63996fd7d3e 920 * 6 | 276kHz | 29
khaledelmadawi 0:e63996fd7d3e 921 * 7 | 267kHz | 30
khaledelmadawi 0:e63996fd7d3e 922 * 8 | 258kHz | 31
khaledelmadawi 0:e63996fd7d3e 923 * 9 | 500kHz | 16
khaledelmadawi 0:e63996fd7d3e 924 * 10 | 471kHz | 17
khaledelmadawi 0:e63996fd7d3e 925 * 11 | 444kHz | 18
khaledelmadawi 0:e63996fd7d3e 926 * 12 | 421kHz | 19
khaledelmadawi 0:e63996fd7d3e 927 * 13 | 400kHz | 20
khaledelmadawi 0:e63996fd7d3e 928 * 14 | 381kHz | 21
khaledelmadawi 0:e63996fd7d3e 929 * 15 | 364kHz | 22
khaledelmadawi 0:e63996fd7d3e 930 * </pre>
khaledelmadawi 0:e63996fd7d3e 931 *
khaledelmadawi 0:e63996fd7d3e 932 * @return Current I2C master clock speed
khaledelmadawi 0:e63996fd7d3e 933 * @see MPU6050_RA_I2C_MST_CTRL
khaledelmadawi 0:e63996fd7d3e 934 */
khaledelmadawi 0:e63996fd7d3e 935 uint8_t MPU6050::getMasterClockSpeed()
khaledelmadawi 0:e63996fd7d3e 936 {
khaledelmadawi 0:e63996fd7d3e 937 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 938 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 939 }
khaledelmadawi 0:e63996fd7d3e 940 /** Set I2C master clock speed.
khaledelmadawi 0:e63996fd7d3e 941 * @reparam speed Current I2C master clock speed
khaledelmadawi 0:e63996fd7d3e 942 * @see MPU6050_RA_I2C_MST_CTRL
khaledelmadawi 0:e63996fd7d3e 943 */
khaledelmadawi 0:e63996fd7d3e 944 void MPU6050::setMasterClockSpeed(uint8_t speed)
khaledelmadawi 0:e63996fd7d3e 945 {
khaledelmadawi 0:e63996fd7d3e 946 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, speed);
khaledelmadawi 0:e63996fd7d3e 947 }
khaledelmadawi 0:e63996fd7d3e 948
khaledelmadawi 0:e63996fd7d3e 949 // I2C_SLV* registers (Slave 0-3)
khaledelmadawi 0:e63996fd7d3e 950
khaledelmadawi 0:e63996fd7d3e 951 /** Get the I2C address of the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 952 * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read
khaledelmadawi 0:e63996fd7d3e 953 * operation, and if it is cleared, then it's a write operation. The remaining
khaledelmadawi 0:e63996fd7d3e 954 * bits (6-0) are the 7-bit device address of the slave device.
khaledelmadawi 0:e63996fd7d3e 955 *
khaledelmadawi 0:e63996fd7d3e 956 * In read mode, the result of the read is placed in the lowest available
khaledelmadawi 0:e63996fd7d3e 957 * EXT_SENS_DATA register. For further information regarding the allocation of
khaledelmadawi 0:e63996fd7d3e 958 * read results, please refer to the EXT_SENS_DATA register description
khaledelmadawi 0:e63996fd7d3e 959 * (Registers 73 - 96).
khaledelmadawi 0:e63996fd7d3e 960 *
khaledelmadawi 0:e63996fd7d3e 961 * The MPU-6050 supports a total of five slaves, but Slave 4 has unique
khaledelmadawi 0:e63996fd7d3e 962 * characteristics, and so it has its own functions (getSlave4* and setSlave4*).
khaledelmadawi 0:e63996fd7d3e 963 *
khaledelmadawi 0:e63996fd7d3e 964 * I2C data transactions are performed at the Sample Rate, as defined in
khaledelmadawi 0:e63996fd7d3e 965 * Register 25. The user is responsible for ensuring that I2C data transactions
khaledelmadawi 0:e63996fd7d3e 966 * to and from each enabled Slave can be completed within a single period of the
khaledelmadawi 0:e63996fd7d3e 967 * Sample Rate.
khaledelmadawi 0:e63996fd7d3e 968 *
khaledelmadawi 0:e63996fd7d3e 969 * The I2C slave access rate can be reduced relative to the Sample Rate. This
khaledelmadawi 0:e63996fd7d3e 970 * reduced access rate is determined by I2C_MST_DLY (Register 52). Whether a
khaledelmadawi 0:e63996fd7d3e 971 * slave's access rate is reduced relative to the Sample Rate is determined by
khaledelmadawi 0:e63996fd7d3e 972 * I2C_MST_DELAY_CTRL (Register 103).
khaledelmadawi 0:e63996fd7d3e 973 *
khaledelmadawi 0:e63996fd7d3e 974 * The processing order for the slaves is fixed. The sequence followed for
khaledelmadawi 0:e63996fd7d3e 975 * processing the slaves is Slave 0, Slave 1, Slave 2, Slave 3 and Slave 4. If a
khaledelmadawi 0:e63996fd7d3e 976 * particular Slave is disabled it will be skipped.
khaledelmadawi 0:e63996fd7d3e 977 *
khaledelmadawi 0:e63996fd7d3e 978 * Each slave can either be accessed at the sample rate or at a reduced sample
khaledelmadawi 0:e63996fd7d3e 979 * rate. In a case where some slaves are accessed at the Sample Rate and some
khaledelmadawi 0:e63996fd7d3e 980 * slaves are accessed at the reduced rate, the sequence of accessing the slaves
khaledelmadawi 0:e63996fd7d3e 981 * (Slave 0 to Slave 4) is still followed. However, the reduced rate slaves will
khaledelmadawi 0:e63996fd7d3e 982 * be skipped if their access rate dictates that they should not be accessed
khaledelmadawi 0:e63996fd7d3e 983 * during that particular cycle. For further information regarding the reduced
khaledelmadawi 0:e63996fd7d3e 984 * access rate, please refer to Register 52. Whether a slave is accessed at the
khaledelmadawi 0:e63996fd7d3e 985 * Sample Rate or at the reduced rate is determined by the Delay Enable bits in
khaledelmadawi 0:e63996fd7d3e 986 * Register 103.
khaledelmadawi 0:e63996fd7d3e 987 *
khaledelmadawi 0:e63996fd7d3e 988 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 989 * @return Current address for specified slave
khaledelmadawi 0:e63996fd7d3e 990 * @see MPU6050_RA_I2C_SLV0_ADDR
khaledelmadawi 0:e63996fd7d3e 991 */
khaledelmadawi 0:e63996fd7d3e 992 uint8_t MPU6050::getSlaveAddress(uint8_t num)
khaledelmadawi 0:e63996fd7d3e 993 {
khaledelmadawi 0:e63996fd7d3e 994 if (num > 3) return 0;
khaledelmadawi 0:e63996fd7d3e 995 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, buffer);
khaledelmadawi 0:e63996fd7d3e 996 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 997 }
khaledelmadawi 0:e63996fd7d3e 998 /** Set the I2C address of the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 999 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1000 * @param address New address for specified slave
khaledelmadawi 0:e63996fd7d3e 1001 * @see getSlaveAddress()
khaledelmadawi 0:e63996fd7d3e 1002 * @see MPU6050_RA_I2C_SLV0_ADDR
khaledelmadawi 0:e63996fd7d3e 1003 */
khaledelmadawi 0:e63996fd7d3e 1004 void MPU6050::setSlaveAddress(uint8_t num, uint8_t address)
khaledelmadawi 0:e63996fd7d3e 1005 {
khaledelmadawi 0:e63996fd7d3e 1006 if (num > 3) return;
khaledelmadawi 0:e63996fd7d3e 1007 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, address);
khaledelmadawi 0:e63996fd7d3e 1008 }
khaledelmadawi 0:e63996fd7d3e 1009 /** Get the active internal register for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1010 * Read/write operations for this slave will be done to whatever internal
khaledelmadawi 0:e63996fd7d3e 1011 * register address is stored in this MPU register.
khaledelmadawi 0:e63996fd7d3e 1012 *
khaledelmadawi 0:e63996fd7d3e 1013 * The MPU-6050 supports a total of five slaves, but Slave 4 has unique
khaledelmadawi 0:e63996fd7d3e 1014 * characteristics, and so it has its own functions.
khaledelmadawi 0:e63996fd7d3e 1015 *
khaledelmadawi 0:e63996fd7d3e 1016 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1017 * @return Current active register for specified slave
khaledelmadawi 0:e63996fd7d3e 1018 * @see MPU6050_RA_I2C_SLV0_REG
khaledelmadawi 0:e63996fd7d3e 1019 */
khaledelmadawi 0:e63996fd7d3e 1020 uint8_t MPU6050::getSlaveRegister(uint8_t num)
khaledelmadawi 0:e63996fd7d3e 1021 {
khaledelmadawi 0:e63996fd7d3e 1022 if (num > 3) return 0;
khaledelmadawi 0:e63996fd7d3e 1023 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV0_REG + num*3, buffer);
khaledelmadawi 0:e63996fd7d3e 1024 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1025 }
khaledelmadawi 0:e63996fd7d3e 1026 /** Set the active internal register for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1027 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1028 * @param reg New active register for specified slave
khaledelmadawi 0:e63996fd7d3e 1029 * @see getSlaveRegister()
khaledelmadawi 0:e63996fd7d3e 1030 * @see MPU6050_RA_I2C_SLV0_REG
khaledelmadawi 0:e63996fd7d3e 1031 */
khaledelmadawi 0:e63996fd7d3e 1032 void MPU6050::setSlaveRegister(uint8_t num, uint8_t reg)
khaledelmadawi 0:e63996fd7d3e 1033 {
khaledelmadawi 0:e63996fd7d3e 1034 if (num > 3) return;
khaledelmadawi 0:e63996fd7d3e 1035 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_REG + num*3, reg);
khaledelmadawi 0:e63996fd7d3e 1036 }
khaledelmadawi 0:e63996fd7d3e 1037 /** Get the enabled value for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1038 * When set to 1, this bit enables Slave 0 for data transfer operations. When
khaledelmadawi 0:e63996fd7d3e 1039 * cleared to 0, this bit disables Slave 0 from data transfer operations.
khaledelmadawi 0:e63996fd7d3e 1040 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1041 * @return Current enabled value for specified slave
khaledelmadawi 0:e63996fd7d3e 1042 * @see MPU6050_RA_I2C_SLV0_CTRL
khaledelmadawi 0:e63996fd7d3e 1043 */
khaledelmadawi 0:e63996fd7d3e 1044 bool MPU6050::getSlaveEnabled(uint8_t num)
khaledelmadawi 0:e63996fd7d3e 1045 {
khaledelmadawi 0:e63996fd7d3e 1046 if (num > 3) return 0;
khaledelmadawi 0:e63996fd7d3e 1047 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1048 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1049 }
khaledelmadawi 0:e63996fd7d3e 1050 /** Set the enabled value for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1051 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1052 * @param enabled New enabled value for specified slave
khaledelmadawi 0:e63996fd7d3e 1053 * @see getSlaveEnabled()
khaledelmadawi 0:e63996fd7d3e 1054 * @see MPU6050_RA_I2C_SLV0_CTRL
khaledelmadawi 0:e63996fd7d3e 1055 */
khaledelmadawi 0:e63996fd7d3e 1056 void MPU6050::setSlaveEnabled(uint8_t num, bool enabled)
khaledelmadawi 0:e63996fd7d3e 1057 {
khaledelmadawi 0:e63996fd7d3e 1058 if (num > 3) return;
khaledelmadawi 0:e63996fd7d3e 1059 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1060 }
khaledelmadawi 0:e63996fd7d3e 1061 /** Get word pair byte-swapping enabled for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1062 * When set to 1, this bit enables byte swapping. When byte swapping is enabled,
khaledelmadawi 0:e63996fd7d3e 1063 * the high and low bytes of a word pair are swapped. Please refer to
khaledelmadawi 0:e63996fd7d3e 1064 * I2C_SLV0_GRP for the pairing convention of the word pairs. When cleared to 0,
khaledelmadawi 0:e63996fd7d3e 1065 * bytes transferred to and from Slave 0 will be written to EXT_SENS_DATA
khaledelmadawi 0:e63996fd7d3e 1066 * registers in the order they were transferred.
khaledelmadawi 0:e63996fd7d3e 1067 *
khaledelmadawi 0:e63996fd7d3e 1068 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1069 * @return Current word pair byte-swapping enabled value for specified slave
khaledelmadawi 0:e63996fd7d3e 1070 * @see MPU6050_RA_I2C_SLV0_CTRL
khaledelmadawi 0:e63996fd7d3e 1071 */
khaledelmadawi 0:e63996fd7d3e 1072 bool MPU6050::getSlaveWordByteSwap(uint8_t num)
khaledelmadawi 0:e63996fd7d3e 1073 {
khaledelmadawi 0:e63996fd7d3e 1074 if (num > 3) return 0;
khaledelmadawi 0:e63996fd7d3e 1075 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1076 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1077 }
khaledelmadawi 0:e63996fd7d3e 1078 /** Set word pair byte-swapping enabled for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1079 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1080 * @param enabled New word pair byte-swapping enabled value for specified slave
khaledelmadawi 0:e63996fd7d3e 1081 * @see getSlaveWordByteSwap()
khaledelmadawi 0:e63996fd7d3e 1082 * @see MPU6050_RA_I2C_SLV0_CTRL
khaledelmadawi 0:e63996fd7d3e 1083 */
khaledelmadawi 0:e63996fd7d3e 1084 void MPU6050::setSlaveWordByteSwap(uint8_t num, bool enabled)
khaledelmadawi 0:e63996fd7d3e 1085 {
khaledelmadawi 0:e63996fd7d3e 1086 if (num > 3) return;
khaledelmadawi 0:e63996fd7d3e 1087 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1088 }
khaledelmadawi 0:e63996fd7d3e 1089 /** Get write mode for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1090 * When set to 1, the transaction will read or write data only. When cleared to
khaledelmadawi 0:e63996fd7d3e 1091 * 0, the transaction will write a register address prior to reading or writing
khaledelmadawi 0:e63996fd7d3e 1092 * data. This should equal 0 when specifying the register address within the
khaledelmadawi 0:e63996fd7d3e 1093 * Slave device to/from which the ensuing data transaction will take place.
khaledelmadawi 0:e63996fd7d3e 1094 *
khaledelmadawi 0:e63996fd7d3e 1095 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1096 * @return Current write mode for specified slave (0 = register address + data, 1 = data only)
khaledelmadawi 0:e63996fd7d3e 1097 * @see MPU6050_RA_I2C_SLV0_CTRL
khaledelmadawi 0:e63996fd7d3e 1098 */
khaledelmadawi 0:e63996fd7d3e 1099 bool MPU6050::getSlaveWriteMode(uint8_t num)
khaledelmadawi 0:e63996fd7d3e 1100 {
khaledelmadawi 0:e63996fd7d3e 1101 if (num > 3) return 0;
khaledelmadawi 0:e63996fd7d3e 1102 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1103 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1104 }
khaledelmadawi 0:e63996fd7d3e 1105 /** Set write mode for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1106 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1107 * @param mode New write mode for specified slave (0 = register address + data, 1 = data only)
khaledelmadawi 0:e63996fd7d3e 1108 * @see getSlaveWriteMode()
khaledelmadawi 0:e63996fd7d3e 1109 * @see MPU6050_RA_I2C_SLV0_CTRL
khaledelmadawi 0:e63996fd7d3e 1110 */
khaledelmadawi 0:e63996fd7d3e 1111 void MPU6050::setSlaveWriteMode(uint8_t num, bool mode)
khaledelmadawi 0:e63996fd7d3e 1112 {
khaledelmadawi 0:e63996fd7d3e 1113 if (num > 3) return;
khaledelmadawi 0:e63996fd7d3e 1114 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, mode);
khaledelmadawi 0:e63996fd7d3e 1115 }
khaledelmadawi 0:e63996fd7d3e 1116 /** Get word pair grouping order offset for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1117 * This sets specifies the grouping order of word pairs received from registers.
khaledelmadawi 0:e63996fd7d3e 1118 * When cleared to 0, bytes from register addresses 0 and 1, 2 and 3, etc (even,
khaledelmadawi 0:e63996fd7d3e 1119 * then odd register addresses) are paired to form a word. When set to 1, bytes
khaledelmadawi 0:e63996fd7d3e 1120 * from register addresses are paired 1 and 2, 3 and 4, etc. (odd, then even
khaledelmadawi 0:e63996fd7d3e 1121 * register addresses) are paired to form a word.
khaledelmadawi 0:e63996fd7d3e 1122 *
khaledelmadawi 0:e63996fd7d3e 1123 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1124 * @return Current word pair grouping order offset for specified slave
khaledelmadawi 0:e63996fd7d3e 1125 * @see MPU6050_RA_I2C_SLV0_CTRL
khaledelmadawi 0:e63996fd7d3e 1126 */
khaledelmadawi 0:e63996fd7d3e 1127 bool MPU6050::getSlaveWordGroupOffset(uint8_t num)
khaledelmadawi 0:e63996fd7d3e 1128 {
khaledelmadawi 0:e63996fd7d3e 1129 if (num > 3) return 0;
khaledelmadawi 0:e63996fd7d3e 1130 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1131 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1132 }
khaledelmadawi 0:e63996fd7d3e 1133 /** Set word pair grouping order offset for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1134 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1135 * @param enabled New word pair grouping order offset for specified slave
khaledelmadawi 0:e63996fd7d3e 1136 * @see getSlaveWordGroupOffset()
khaledelmadawi 0:e63996fd7d3e 1137 * @see MPU6050_RA_I2C_SLV0_CTRL
khaledelmadawi 0:e63996fd7d3e 1138 */
khaledelmadawi 0:e63996fd7d3e 1139 void MPU6050::setSlaveWordGroupOffset(uint8_t num, bool enabled)
khaledelmadawi 0:e63996fd7d3e 1140 {
khaledelmadawi 0:e63996fd7d3e 1141 if (num > 3) return;
khaledelmadawi 0:e63996fd7d3e 1142 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1143 }
khaledelmadawi 0:e63996fd7d3e 1144 /** Get number of bytes to read for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1145 * Specifies the number of bytes transferred to and from Slave 0. Clearing this
khaledelmadawi 0:e63996fd7d3e 1146 * bit to 0 is equivalent to disabling the register by writing 0 to I2C_SLV0_EN.
khaledelmadawi 0:e63996fd7d3e 1147 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1148 * @return Number of bytes to read for specified slave
khaledelmadawi 0:e63996fd7d3e 1149 * @see MPU6050_RA_I2C_SLV0_CTRL
khaledelmadawi 0:e63996fd7d3e 1150 */
khaledelmadawi 0:e63996fd7d3e 1151 uint8_t MPU6050::getSlaveDataLength(uint8_t num)
khaledelmadawi 0:e63996fd7d3e 1152 {
khaledelmadawi 0:e63996fd7d3e 1153 if (num > 3) return 0;
khaledelmadawi 0:e63996fd7d3e 1154 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 1155 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1156 }
khaledelmadawi 0:e63996fd7d3e 1157 /** Set number of bytes to read for the specified slave (0-3).
khaledelmadawi 0:e63996fd7d3e 1158 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 1159 * @param length Number of bytes to read for specified slave
khaledelmadawi 0:e63996fd7d3e 1160 * @see getSlaveDataLength()
khaledelmadawi 0:e63996fd7d3e 1161 * @see MPU6050_RA_I2C_SLV0_CTRL
khaledelmadawi 0:e63996fd7d3e 1162 */
khaledelmadawi 0:e63996fd7d3e 1163 void MPU6050::setSlaveDataLength(uint8_t num, uint8_t length)
khaledelmadawi 0:e63996fd7d3e 1164 {
khaledelmadawi 0:e63996fd7d3e 1165 if (num > 3) return;
khaledelmadawi 0:e63996fd7d3e 1166 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, length);
khaledelmadawi 0:e63996fd7d3e 1167 }
khaledelmadawi 0:e63996fd7d3e 1168
khaledelmadawi 0:e63996fd7d3e 1169 // I2C_SLV* registers (Slave 4)
khaledelmadawi 0:e63996fd7d3e 1170
khaledelmadawi 0:e63996fd7d3e 1171 /** Get the I2C address of Slave 4.
khaledelmadawi 0:e63996fd7d3e 1172 * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read
khaledelmadawi 0:e63996fd7d3e 1173 * operation, and if it is cleared, then it's a write operation. The remaining
khaledelmadawi 0:e63996fd7d3e 1174 * bits (6-0) are the 7-bit device address of the slave device.
khaledelmadawi 0:e63996fd7d3e 1175 *
khaledelmadawi 0:e63996fd7d3e 1176 * @return Current address for Slave 4
khaledelmadawi 0:e63996fd7d3e 1177 * @see getSlaveAddress()
khaledelmadawi 0:e63996fd7d3e 1178 * @see MPU6050_RA_I2C_SLV4_ADDR
khaledelmadawi 0:e63996fd7d3e 1179 */
khaledelmadawi 0:e63996fd7d3e 1180 uint8_t MPU6050::getSlave4Address()
khaledelmadawi 0:e63996fd7d3e 1181 {
khaledelmadawi 0:e63996fd7d3e 1182 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_ADDR, buffer);
khaledelmadawi 0:e63996fd7d3e 1183 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1184 }
khaledelmadawi 0:e63996fd7d3e 1185 /** Set the I2C address of Slave 4.
khaledelmadawi 0:e63996fd7d3e 1186 * @param address New address for Slave 4
khaledelmadawi 0:e63996fd7d3e 1187 * @see getSlave4Address()
khaledelmadawi 0:e63996fd7d3e 1188 * @see MPU6050_RA_I2C_SLV4_ADDR
khaledelmadawi 0:e63996fd7d3e 1189 */
khaledelmadawi 0:e63996fd7d3e 1190 void MPU6050::setSlave4Address(uint8_t address)
khaledelmadawi 0:e63996fd7d3e 1191 {
khaledelmadawi 0:e63996fd7d3e 1192 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_ADDR, address);
khaledelmadawi 0:e63996fd7d3e 1193 }
khaledelmadawi 0:e63996fd7d3e 1194 /** Get the active internal register for the Slave 4.
khaledelmadawi 0:e63996fd7d3e 1195 * Read/write operations for this slave will be done to whatever internal
khaledelmadawi 0:e63996fd7d3e 1196 * register address is stored in this MPU register.
khaledelmadawi 0:e63996fd7d3e 1197 *
khaledelmadawi 0:e63996fd7d3e 1198 * @return Current active register for Slave 4
khaledelmadawi 0:e63996fd7d3e 1199 * @see MPU6050_RA_I2C_SLV4_REG
khaledelmadawi 0:e63996fd7d3e 1200 */
khaledelmadawi 0:e63996fd7d3e 1201 uint8_t MPU6050::getSlave4Register()
khaledelmadawi 0:e63996fd7d3e 1202 {
khaledelmadawi 0:e63996fd7d3e 1203 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_REG, buffer);
khaledelmadawi 0:e63996fd7d3e 1204 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1205 }
khaledelmadawi 0:e63996fd7d3e 1206 /** Set the active internal register for Slave 4.
khaledelmadawi 0:e63996fd7d3e 1207 * @param reg New active register for Slave 4
khaledelmadawi 0:e63996fd7d3e 1208 * @see getSlave4Register()
khaledelmadawi 0:e63996fd7d3e 1209 * @see MPU6050_RA_I2C_SLV4_REG
khaledelmadawi 0:e63996fd7d3e 1210 */
khaledelmadawi 0:e63996fd7d3e 1211 void MPU6050::setSlave4Register(uint8_t reg)
khaledelmadawi 0:e63996fd7d3e 1212 {
khaledelmadawi 0:e63996fd7d3e 1213 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_REG, reg);
khaledelmadawi 0:e63996fd7d3e 1214 }
khaledelmadawi 0:e63996fd7d3e 1215 /** Set new byte to write to Slave 4.
khaledelmadawi 0:e63996fd7d3e 1216 * This register stores the data to be written into the Slave 4. If I2C_SLV4_RW
khaledelmadawi 0:e63996fd7d3e 1217 * is set 1 (set to read), this register has no effect.
khaledelmadawi 0:e63996fd7d3e 1218 * @param data New byte to write to Slave 4
khaledelmadawi 0:e63996fd7d3e 1219 * @see MPU6050_RA_I2C_SLV4_DO
khaledelmadawi 0:e63996fd7d3e 1220 */
khaledelmadawi 0:e63996fd7d3e 1221 void MPU6050::setSlave4OutputByte(uint8_t data)
khaledelmadawi 0:e63996fd7d3e 1222 {
khaledelmadawi 0:e63996fd7d3e 1223 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV4_DO, data);
khaledelmadawi 0:e63996fd7d3e 1224 }
khaledelmadawi 0:e63996fd7d3e 1225 /** Get the enabled value for the Slave 4.
khaledelmadawi 0:e63996fd7d3e 1226 * When set to 1, this bit enables Slave 4 for data transfer operations. When
khaledelmadawi 0:e63996fd7d3e 1227 * cleared to 0, this bit disables Slave 4 from data transfer operations.
khaledelmadawi 0:e63996fd7d3e 1228 * @return Current enabled value for Slave 4
khaledelmadawi 0:e63996fd7d3e 1229 * @see MPU6050_RA_I2C_SLV4_CTRL
khaledelmadawi 0:e63996fd7d3e 1230 */
khaledelmadawi 0:e63996fd7d3e 1231 bool MPU6050::getSlave4Enabled()
khaledelmadawi 0:e63996fd7d3e 1232 {
khaledelmadawi 0:e63996fd7d3e 1233 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1234 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1235 }
khaledelmadawi 0:e63996fd7d3e 1236 /** Set the enabled value for Slave 4.
khaledelmadawi 0:e63996fd7d3e 1237 * @param enabled New enabled value for Slave 4
khaledelmadawi 0:e63996fd7d3e 1238 * @see getSlave4Enabled()
khaledelmadawi 0:e63996fd7d3e 1239 * @see MPU6050_RA_I2C_SLV4_CTRL
khaledelmadawi 0:e63996fd7d3e 1240 */
khaledelmadawi 0:e63996fd7d3e 1241 void MPU6050::setSlave4Enabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1242 {
khaledelmadawi 0:e63996fd7d3e 1243 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1244 }
khaledelmadawi 0:e63996fd7d3e 1245 /** Get the enabled value for Slave 4 transaction interrupts.
khaledelmadawi 0:e63996fd7d3e 1246 * When set to 1, this bit enables the generation of an interrupt signal upon
khaledelmadawi 0:e63996fd7d3e 1247 * completion of a Slave 4 transaction. When cleared to 0, this bit disables the
khaledelmadawi 0:e63996fd7d3e 1248 * generation of an interrupt signal upon completion of a Slave 4 transaction.
khaledelmadawi 0:e63996fd7d3e 1249 * The interrupt status can be observed in Register 54.
khaledelmadawi 0:e63996fd7d3e 1250 *
khaledelmadawi 0:e63996fd7d3e 1251 * @return Current enabled value for Slave 4 transaction interrupts.
khaledelmadawi 0:e63996fd7d3e 1252 * @see MPU6050_RA_I2C_SLV4_CTRL
khaledelmadawi 0:e63996fd7d3e 1253 */
khaledelmadawi 0:e63996fd7d3e 1254 bool MPU6050::getSlave4InterruptEnabled()
khaledelmadawi 0:e63996fd7d3e 1255 {
khaledelmadawi 0:e63996fd7d3e 1256 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1257 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1258 }
khaledelmadawi 0:e63996fd7d3e 1259 /** Set the enabled value for Slave 4 transaction interrupts.
khaledelmadawi 0:e63996fd7d3e 1260 * @param enabled New enabled value for Slave 4 transaction interrupts.
khaledelmadawi 0:e63996fd7d3e 1261 * @see getSlave4InterruptEnabled()
khaledelmadawi 0:e63996fd7d3e 1262 * @see MPU6050_RA_I2C_SLV4_CTRL
khaledelmadawi 0:e63996fd7d3e 1263 */
khaledelmadawi 0:e63996fd7d3e 1264 void MPU6050::setSlave4InterruptEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1265 {
khaledelmadawi 0:e63996fd7d3e 1266 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1267 }
khaledelmadawi 0:e63996fd7d3e 1268 /** Get write mode for Slave 4.
khaledelmadawi 0:e63996fd7d3e 1269 * When set to 1, the transaction will read or write data only. When cleared to
khaledelmadawi 0:e63996fd7d3e 1270 * 0, the transaction will write a register address prior to reading or writing
khaledelmadawi 0:e63996fd7d3e 1271 * data. This should equal 0 when specifying the register address within the
khaledelmadawi 0:e63996fd7d3e 1272 * Slave device to/from which the ensuing data transaction will take place.
khaledelmadawi 0:e63996fd7d3e 1273 *
khaledelmadawi 0:e63996fd7d3e 1274 * @return Current write mode for Slave 4 (0 = register address + data, 1 = data only)
khaledelmadawi 0:e63996fd7d3e 1275 * @see MPU6050_RA_I2C_SLV4_CTRL
khaledelmadawi 0:e63996fd7d3e 1276 */
khaledelmadawi 0:e63996fd7d3e 1277 bool MPU6050::getSlave4WriteMode()
khaledelmadawi 0:e63996fd7d3e 1278 {
khaledelmadawi 0:e63996fd7d3e 1279 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1280 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1281 }
khaledelmadawi 0:e63996fd7d3e 1282 /** Set write mode for the Slave 4.
khaledelmadawi 0:e63996fd7d3e 1283 * @param mode New write mode for Slave 4 (0 = register address + data, 1 = data only)
khaledelmadawi 0:e63996fd7d3e 1284 * @see getSlave4WriteMode()
khaledelmadawi 0:e63996fd7d3e 1285 * @see MPU6050_RA_I2C_SLV4_CTRL
khaledelmadawi 0:e63996fd7d3e 1286 */
khaledelmadawi 0:e63996fd7d3e 1287 void MPU6050::setSlave4WriteMode(bool mode)
khaledelmadawi 0:e63996fd7d3e 1288 {
khaledelmadawi 0:e63996fd7d3e 1289 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, mode);
khaledelmadawi 0:e63996fd7d3e 1290 }
khaledelmadawi 0:e63996fd7d3e 1291 /** Get Slave 4 master delay value.
khaledelmadawi 0:e63996fd7d3e 1292 * This configures the reduced access rate of I2C slaves relative to the Sample
khaledelmadawi 0:e63996fd7d3e 1293 * Rate. When a slave's access rate is decreased relative to the Sample Rate,
khaledelmadawi 0:e63996fd7d3e 1294 * the slave is accessed every:
khaledelmadawi 0:e63996fd7d3e 1295 *
khaledelmadawi 0:e63996fd7d3e 1296 * 1 / (1 + I2C_MST_DLY) samples
khaledelmadawi 0:e63996fd7d3e 1297 *
khaledelmadawi 0:e63996fd7d3e 1298 * This base Sample Rate in turn is determined by SMPLRT_DIV (register 25) and
khaledelmadawi 0:e63996fd7d3e 1299 * DLPF_CFG (register 26). Whether a slave's access rate is reduced relative to
khaledelmadawi 0:e63996fd7d3e 1300 * the Sample Rate is determined by I2C_MST_DELAY_CTRL (register 103). For
khaledelmadawi 0:e63996fd7d3e 1301 * further information regarding the Sample Rate, please refer to register 25.
khaledelmadawi 0:e63996fd7d3e 1302 *
khaledelmadawi 0:e63996fd7d3e 1303 * @return Current Slave 4 master delay value
khaledelmadawi 0:e63996fd7d3e 1304 * @see MPU6050_RA_I2C_SLV4_CTRL
khaledelmadawi 0:e63996fd7d3e 1305 */
khaledelmadawi 0:e63996fd7d3e 1306 uint8_t MPU6050::getSlave4MasterDelay()
khaledelmadawi 0:e63996fd7d3e 1307 {
khaledelmadawi 0:e63996fd7d3e 1308 i2Cdev.readBits(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 1309 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1310 }
khaledelmadawi 0:e63996fd7d3e 1311 /** Set Slave 4 master delay value.
khaledelmadawi 0:e63996fd7d3e 1312 * @param delay New Slave 4 master delay value
khaledelmadawi 0:e63996fd7d3e 1313 * @see getSlave4MasterDelay()
khaledelmadawi 0:e63996fd7d3e 1314 * @see MPU6050_RA_I2C_SLV4_CTRL
khaledelmadawi 0:e63996fd7d3e 1315 */
khaledelmadawi 0:e63996fd7d3e 1316 void MPU6050::setSlave4MasterDelay(uint8_t delay)
khaledelmadawi 0:e63996fd7d3e 1317 {
khaledelmadawi 0:e63996fd7d3e 1318 i2Cdev.writeBits(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, delay);
khaledelmadawi 0:e63996fd7d3e 1319 }
khaledelmadawi 0:e63996fd7d3e 1320 /** Get last available byte read from Slave 4.
khaledelmadawi 0:e63996fd7d3e 1321 * This register stores the data read from Slave 4. This field is populated
khaledelmadawi 0:e63996fd7d3e 1322 * after a read transaction.
khaledelmadawi 0:e63996fd7d3e 1323 * @return Last available byte read from to Slave 4
khaledelmadawi 0:e63996fd7d3e 1324 * @see MPU6050_RA_I2C_SLV4_DI
khaledelmadawi 0:e63996fd7d3e 1325 */
khaledelmadawi 0:e63996fd7d3e 1326 uint8_t MPU6050::getSlate4InputByte()
khaledelmadawi 0:e63996fd7d3e 1327 {
khaledelmadawi 0:e63996fd7d3e 1328 i2Cdev.readByte(devAddr, MPU6050_RA_I2C_SLV4_DI, buffer);
khaledelmadawi 0:e63996fd7d3e 1329 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1330 }
khaledelmadawi 0:e63996fd7d3e 1331
khaledelmadawi 0:e63996fd7d3e 1332 // I2C_MST_STATUS register
khaledelmadawi 0:e63996fd7d3e 1333
khaledelmadawi 0:e63996fd7d3e 1334 /** Get FSYNC interrupt status.
khaledelmadawi 0:e63996fd7d3e 1335 * This bit reflects the status of the FSYNC interrupt from an external device
khaledelmadawi 0:e63996fd7d3e 1336 * into the MPU-60X0. This is used as a way to pass an external interrupt
khaledelmadawi 0:e63996fd7d3e 1337 * through the MPU-60X0 to the host application processor. When set to 1, this
khaledelmadawi 0:e63996fd7d3e 1338 * bit will cause an interrupt if FSYNC_INT_EN is asserted in INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1339 * (Register 55).
khaledelmadawi 0:e63996fd7d3e 1340 * @return FSYNC interrupt status
khaledelmadawi 0:e63996fd7d3e 1341 * @see MPU6050_RA_I2C_MST_STATUS
khaledelmadawi 0:e63996fd7d3e 1342 */
khaledelmadawi 0:e63996fd7d3e 1343 bool MPU6050::getPassthroughStatus()
khaledelmadawi 0:e63996fd7d3e 1344 {
khaledelmadawi 0:e63996fd7d3e 1345 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_PASS_THROUGH_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1346 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1347 }
khaledelmadawi 0:e63996fd7d3e 1348 /** Get Slave 4 transaction done status.
khaledelmadawi 0:e63996fd7d3e 1349 * Automatically sets to 1 when a Slave 4 transaction has completed. This
khaledelmadawi 0:e63996fd7d3e 1350 * triggers an interrupt if the I2C_MST_INT_EN bit in the INT_ENABLE register
khaledelmadawi 0:e63996fd7d3e 1351 * (Register 56) is asserted and if the SLV_4_DONE_INT bit is asserted in the
khaledelmadawi 0:e63996fd7d3e 1352 * I2C_SLV4_CTRL register (Register 52).
khaledelmadawi 0:e63996fd7d3e 1353 * @return Slave 4 transaction done status
khaledelmadawi 0:e63996fd7d3e 1354 * @see MPU6050_RA_I2C_MST_STATUS
khaledelmadawi 0:e63996fd7d3e 1355 */
khaledelmadawi 0:e63996fd7d3e 1356 bool MPU6050::getSlave4IsDone()
khaledelmadawi 0:e63996fd7d3e 1357 {
khaledelmadawi 0:e63996fd7d3e 1358 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_DONE_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1359 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1360 }
khaledelmadawi 0:e63996fd7d3e 1361 /** Get master arbitration lost status.
khaledelmadawi 0:e63996fd7d3e 1362 * This bit automatically sets to 1 when the I2C Master has lost arbitration of
khaledelmadawi 0:e63996fd7d3e 1363 * the auxiliary I2C bus (an error condition). This triggers an interrupt if the
khaledelmadawi 0:e63996fd7d3e 1364 * I2C_MST_INT_EN bit in the INT_ENABLE register (Register 56) is asserted.
khaledelmadawi 0:e63996fd7d3e 1365 * @return Master arbitration lost status
khaledelmadawi 0:e63996fd7d3e 1366 * @see MPU6050_RA_I2C_MST_STATUS
khaledelmadawi 0:e63996fd7d3e 1367 */
khaledelmadawi 0:e63996fd7d3e 1368 bool MPU6050::getLostArbitration()
khaledelmadawi 0:e63996fd7d3e 1369 {
khaledelmadawi 0:e63996fd7d3e 1370 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_LOST_ARB_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1371 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1372 }
khaledelmadawi 0:e63996fd7d3e 1373 /** Get Slave 4 NACK status.
khaledelmadawi 0:e63996fd7d3e 1374 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
khaledelmadawi 0:e63996fd7d3e 1375 * transaction with Slave 4. This triggers an interrupt if the I2C_MST_INT_EN
khaledelmadawi 0:e63996fd7d3e 1376 * bit in the INT_ENABLE register (Register 56) is asserted.
khaledelmadawi 0:e63996fd7d3e 1377 * @return Slave 4 NACK interrupt status
khaledelmadawi 0:e63996fd7d3e 1378 * @see MPU6050_RA_I2C_MST_STATUS
khaledelmadawi 0:e63996fd7d3e 1379 */
khaledelmadawi 0:e63996fd7d3e 1380 bool MPU6050::getSlave4Nack()
khaledelmadawi 0:e63996fd7d3e 1381 {
khaledelmadawi 0:e63996fd7d3e 1382 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_NACK_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1383 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1384 }
khaledelmadawi 0:e63996fd7d3e 1385 /** Get Slave 3 NACK status.
khaledelmadawi 0:e63996fd7d3e 1386 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
khaledelmadawi 0:e63996fd7d3e 1387 * transaction with Slave 3. This triggers an interrupt if the I2C_MST_INT_EN
khaledelmadawi 0:e63996fd7d3e 1388 * bit in the INT_ENABLE register (Register 56) is asserted.
khaledelmadawi 0:e63996fd7d3e 1389 * @return Slave 3 NACK interrupt status
khaledelmadawi 0:e63996fd7d3e 1390 * @see MPU6050_RA_I2C_MST_STATUS
khaledelmadawi 0:e63996fd7d3e 1391 */
khaledelmadawi 0:e63996fd7d3e 1392 bool MPU6050::getSlave3Nack()
khaledelmadawi 0:e63996fd7d3e 1393 {
khaledelmadawi 0:e63996fd7d3e 1394 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV3_NACK_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1395 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1396 }
khaledelmadawi 0:e63996fd7d3e 1397 /** Get Slave 2 NACK status.
khaledelmadawi 0:e63996fd7d3e 1398 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
khaledelmadawi 0:e63996fd7d3e 1399 * transaction with Slave 2. This triggers an interrupt if the I2C_MST_INT_EN
khaledelmadawi 0:e63996fd7d3e 1400 * bit in the INT_ENABLE register (Register 56) is asserted.
khaledelmadawi 0:e63996fd7d3e 1401 * @return Slave 2 NACK interrupt status
khaledelmadawi 0:e63996fd7d3e 1402 * @see MPU6050_RA_I2C_MST_STATUS
khaledelmadawi 0:e63996fd7d3e 1403 */
khaledelmadawi 0:e63996fd7d3e 1404 bool MPU6050::getSlave2Nack()
khaledelmadawi 0:e63996fd7d3e 1405 {
khaledelmadawi 0:e63996fd7d3e 1406 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV2_NACK_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1407 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1408 }
khaledelmadawi 0:e63996fd7d3e 1409 /** Get Slave 1 NACK status.
khaledelmadawi 0:e63996fd7d3e 1410 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
khaledelmadawi 0:e63996fd7d3e 1411 * transaction with Slave 1. This triggers an interrupt if the I2C_MST_INT_EN
khaledelmadawi 0:e63996fd7d3e 1412 * bit in the INT_ENABLE register (Register 56) is asserted.
khaledelmadawi 0:e63996fd7d3e 1413 * @return Slave 1 NACK interrupt status
khaledelmadawi 0:e63996fd7d3e 1414 * @see MPU6050_RA_I2C_MST_STATUS
khaledelmadawi 0:e63996fd7d3e 1415 */
khaledelmadawi 0:e63996fd7d3e 1416 bool MPU6050::getSlave1Nack()
khaledelmadawi 0:e63996fd7d3e 1417 {
khaledelmadawi 0:e63996fd7d3e 1418 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV1_NACK_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1419 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1420 }
khaledelmadawi 0:e63996fd7d3e 1421 /** Get Slave 0 NACK status.
khaledelmadawi 0:e63996fd7d3e 1422 * This bit automatically sets to 1 when the I2C Master receives a NACK in a
khaledelmadawi 0:e63996fd7d3e 1423 * transaction with Slave 0. This triggers an interrupt if the I2C_MST_INT_EN
khaledelmadawi 0:e63996fd7d3e 1424 * bit in the INT_ENABLE register (Register 56) is asserted.
khaledelmadawi 0:e63996fd7d3e 1425 * @return Slave 0 NACK interrupt status
khaledelmadawi 0:e63996fd7d3e 1426 * @see MPU6050_RA_I2C_MST_STATUS
khaledelmadawi 0:e63996fd7d3e 1427 */
khaledelmadawi 0:e63996fd7d3e 1428 bool MPU6050::getSlave0Nack()
khaledelmadawi 0:e63996fd7d3e 1429 {
khaledelmadawi 0:e63996fd7d3e 1430 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV0_NACK_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1431 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1432 }
khaledelmadawi 0:e63996fd7d3e 1433
khaledelmadawi 0:e63996fd7d3e 1434 // INT_PIN_CFG register
khaledelmadawi 0:e63996fd7d3e 1435
khaledelmadawi 0:e63996fd7d3e 1436 /** Get interrupt logic level mode.
khaledelmadawi 0:e63996fd7d3e 1437 * Will be set 0 for active-high, 1 for active-low.
khaledelmadawi 0:e63996fd7d3e 1438 * @return Current interrupt mode (0=active-high, 1=active-low)
khaledelmadawi 0:e63996fd7d3e 1439 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1440 * @see MPU6050_INTCFG_INT_LEVEL_BIT
khaledelmadawi 0:e63996fd7d3e 1441 */
khaledelmadawi 0:e63996fd7d3e 1442 bool MPU6050::getInterruptMode()
khaledelmadawi 0:e63996fd7d3e 1443 {
khaledelmadawi 0:e63996fd7d3e 1444 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1445 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1446 }
khaledelmadawi 0:e63996fd7d3e 1447 /** Set interrupt logic level mode.
khaledelmadawi 0:e63996fd7d3e 1448 * @param mode New interrupt mode (0=active-high, 1=active-low)
khaledelmadawi 0:e63996fd7d3e 1449 * @see getInterruptMode()
khaledelmadawi 0:e63996fd7d3e 1450 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1451 * @see MPU6050_INTCFG_INT_LEVEL_BIT
khaledelmadawi 0:e63996fd7d3e 1452 */
khaledelmadawi 0:e63996fd7d3e 1453 void MPU6050::setInterruptMode(bool mode)
khaledelmadawi 0:e63996fd7d3e 1454 {
khaledelmadawi 0:e63996fd7d3e 1455 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, mode);
khaledelmadawi 0:e63996fd7d3e 1456 }
khaledelmadawi 0:e63996fd7d3e 1457 /** Get interrupt drive mode.
khaledelmadawi 0:e63996fd7d3e 1458 * Will be set 0 for push-pull, 1 for open-drain.
khaledelmadawi 0:e63996fd7d3e 1459 * @return Current interrupt drive mode (0=push-pull, 1=open-drain)
khaledelmadawi 0:e63996fd7d3e 1460 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1461 * @see MPU6050_INTCFG_INT_OPEN_BIT
khaledelmadawi 0:e63996fd7d3e 1462 */
khaledelmadawi 0:e63996fd7d3e 1463 bool MPU6050::getInterruptDrive()
khaledelmadawi 0:e63996fd7d3e 1464 {
khaledelmadawi 0:e63996fd7d3e 1465 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1466 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1467 }
khaledelmadawi 0:e63996fd7d3e 1468 /** Set interrupt drive mode.
khaledelmadawi 0:e63996fd7d3e 1469 * @param drive New interrupt drive mode (0=push-pull, 1=open-drain)
khaledelmadawi 0:e63996fd7d3e 1470 * @see getInterruptDrive()
khaledelmadawi 0:e63996fd7d3e 1471 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1472 * @see MPU6050_INTCFG_INT_OPEN_BIT
khaledelmadawi 0:e63996fd7d3e 1473 */
khaledelmadawi 0:e63996fd7d3e 1474 void MPU6050::setInterruptDrive(bool drive)
khaledelmadawi 0:e63996fd7d3e 1475 {
khaledelmadawi 0:e63996fd7d3e 1476 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, drive);
khaledelmadawi 0:e63996fd7d3e 1477 }
khaledelmadawi 0:e63996fd7d3e 1478 /** Get interrupt latch mode.
khaledelmadawi 0:e63996fd7d3e 1479 * Will be set 0 for 50us-pulse, 1 for latch-until-int-cleared.
khaledelmadawi 0:e63996fd7d3e 1480 * @return Current latch mode (0=50us-pulse, 1=latch-until-int-cleared)
khaledelmadawi 0:e63996fd7d3e 1481 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1482 * @see MPU6050_INTCFG_LATCH_INT_EN_BIT
khaledelmadawi 0:e63996fd7d3e 1483 */
khaledelmadawi 0:e63996fd7d3e 1484 bool MPU6050::getInterruptLatch()
khaledelmadawi 0:e63996fd7d3e 1485 {
khaledelmadawi 0:e63996fd7d3e 1486 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1487 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1488 }
khaledelmadawi 0:e63996fd7d3e 1489 /** Set interrupt latch mode.
khaledelmadawi 0:e63996fd7d3e 1490 * @param latch New latch mode (0=50us-pulse, 1=latch-until-int-cleared)
khaledelmadawi 0:e63996fd7d3e 1491 * @see getInterruptLatch()
khaledelmadawi 0:e63996fd7d3e 1492 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1493 * @see MPU6050_INTCFG_LATCH_INT_EN_BIT
khaledelmadawi 0:e63996fd7d3e 1494 */
khaledelmadawi 0:e63996fd7d3e 1495 void MPU6050::setInterruptLatch(bool latch)
khaledelmadawi 0:e63996fd7d3e 1496 {
khaledelmadawi 0:e63996fd7d3e 1497 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, latch);
khaledelmadawi 0:e63996fd7d3e 1498 }
khaledelmadawi 0:e63996fd7d3e 1499 /** Get interrupt latch clear mode.
khaledelmadawi 0:e63996fd7d3e 1500 * Will be set 0 for status-read-only, 1 for any-register-read.
khaledelmadawi 0:e63996fd7d3e 1501 * @return Current latch clear mode (0=status-read-only, 1=any-register-read)
khaledelmadawi 0:e63996fd7d3e 1502 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1503 * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT
khaledelmadawi 0:e63996fd7d3e 1504 */
khaledelmadawi 0:e63996fd7d3e 1505 bool MPU6050::getInterruptLatchClear()
khaledelmadawi 0:e63996fd7d3e 1506 {
khaledelmadawi 0:e63996fd7d3e 1507 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1508 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1509 }
khaledelmadawi 0:e63996fd7d3e 1510 /** Set interrupt latch clear mode.
khaledelmadawi 0:e63996fd7d3e 1511 * @param clear New latch clear mode (0=status-read-only, 1=any-register-read)
khaledelmadawi 0:e63996fd7d3e 1512 * @see getInterruptLatchClear()
khaledelmadawi 0:e63996fd7d3e 1513 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1514 * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT
khaledelmadawi 0:e63996fd7d3e 1515 */
khaledelmadawi 0:e63996fd7d3e 1516 void MPU6050::setInterruptLatchClear(bool clear)
khaledelmadawi 0:e63996fd7d3e 1517 {
khaledelmadawi 0:e63996fd7d3e 1518 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, clear);
khaledelmadawi 0:e63996fd7d3e 1519 }
khaledelmadawi 0:e63996fd7d3e 1520 /** Get FSYNC interrupt logic level mode.
khaledelmadawi 0:e63996fd7d3e 1521 * @return Current FSYNC interrupt mode (0=active-high, 1=active-low)
khaledelmadawi 0:e63996fd7d3e 1522 * @see getFSyncInterruptMode()
khaledelmadawi 0:e63996fd7d3e 1523 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1524 * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT
khaledelmadawi 0:e63996fd7d3e 1525 */
khaledelmadawi 0:e63996fd7d3e 1526 bool MPU6050::getFSyncInterruptLevel()
khaledelmadawi 0:e63996fd7d3e 1527 {
khaledelmadawi 0:e63996fd7d3e 1528 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1529 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1530 }
khaledelmadawi 0:e63996fd7d3e 1531 /** Set FSYNC interrupt logic level mode.
khaledelmadawi 0:e63996fd7d3e 1532 * @param mode New FSYNC interrupt mode (0=active-high, 1=active-low)
khaledelmadawi 0:e63996fd7d3e 1533 * @see getFSyncInterruptMode()
khaledelmadawi 0:e63996fd7d3e 1534 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1535 * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT
khaledelmadawi 0:e63996fd7d3e 1536 */
khaledelmadawi 0:e63996fd7d3e 1537 void MPU6050::setFSyncInterruptLevel(bool level)
khaledelmadawi 0:e63996fd7d3e 1538 {
khaledelmadawi 0:e63996fd7d3e 1539 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, level);
khaledelmadawi 0:e63996fd7d3e 1540 }
khaledelmadawi 0:e63996fd7d3e 1541 /** Get FSYNC pin interrupt enabled setting.
khaledelmadawi 0:e63996fd7d3e 1542 * Will be set 0 for disabled, 1 for enabled.
khaledelmadawi 0:e63996fd7d3e 1543 * @return Current interrupt enabled setting
khaledelmadawi 0:e63996fd7d3e 1544 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1545 * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT
khaledelmadawi 0:e63996fd7d3e 1546 */
khaledelmadawi 0:e63996fd7d3e 1547 bool MPU6050::getFSyncInterruptEnabled()
khaledelmadawi 0:e63996fd7d3e 1548 {
khaledelmadawi 0:e63996fd7d3e 1549 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1550 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1551 }
khaledelmadawi 0:e63996fd7d3e 1552 /** Set FSYNC pin interrupt enabled setting.
khaledelmadawi 0:e63996fd7d3e 1553 * @param enabled New FSYNC pin interrupt enabled setting
khaledelmadawi 0:e63996fd7d3e 1554 * @see getFSyncInterruptEnabled()
khaledelmadawi 0:e63996fd7d3e 1555 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1556 * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT
khaledelmadawi 0:e63996fd7d3e 1557 */
khaledelmadawi 0:e63996fd7d3e 1558 void MPU6050::setFSyncInterruptEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1559 {
khaledelmadawi 0:e63996fd7d3e 1560 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1561 }
khaledelmadawi 0:e63996fd7d3e 1562 /** Get I2C bypass enabled status.
khaledelmadawi 0:e63996fd7d3e 1563 * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to
khaledelmadawi 0:e63996fd7d3e 1564 * 0, the host application processor will be able to directly access the
khaledelmadawi 0:e63996fd7d3e 1565 * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host
khaledelmadawi 0:e63996fd7d3e 1566 * application processor will not be able to directly access the auxiliary I2C
khaledelmadawi 0:e63996fd7d3e 1567 * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106
khaledelmadawi 0:e63996fd7d3e 1568 * bit[5]).
khaledelmadawi 0:e63996fd7d3e 1569 * @return Current I2C bypass enabled status
khaledelmadawi 0:e63996fd7d3e 1570 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1571 * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT
khaledelmadawi 0:e63996fd7d3e 1572 */
khaledelmadawi 0:e63996fd7d3e 1573 bool MPU6050::getI2CBypassEnabled()
khaledelmadawi 0:e63996fd7d3e 1574 {
khaledelmadawi 0:e63996fd7d3e 1575 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1576 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1577 }
khaledelmadawi 0:e63996fd7d3e 1578 /** Set I2C bypass enabled status.
khaledelmadawi 0:e63996fd7d3e 1579 * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to
khaledelmadawi 0:e63996fd7d3e 1580 * 0, the host application processor will be able to directly access the
khaledelmadawi 0:e63996fd7d3e 1581 * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host
khaledelmadawi 0:e63996fd7d3e 1582 * application processor will not be able to directly access the auxiliary I2C
khaledelmadawi 0:e63996fd7d3e 1583 * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106
khaledelmadawi 0:e63996fd7d3e 1584 * bit[5]).
khaledelmadawi 0:e63996fd7d3e 1585 * @param enabled New I2C bypass enabled status
khaledelmadawi 0:e63996fd7d3e 1586 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1587 * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT
khaledelmadawi 0:e63996fd7d3e 1588 */
khaledelmadawi 0:e63996fd7d3e 1589 void MPU6050::setI2CBypassEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1590 {
khaledelmadawi 0:e63996fd7d3e 1591 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1592 }
khaledelmadawi 0:e63996fd7d3e 1593 /** Get reference clock output enabled status.
khaledelmadawi 0:e63996fd7d3e 1594 * When this bit is equal to 1, a reference clock output is provided at the
khaledelmadawi 0:e63996fd7d3e 1595 * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For
khaledelmadawi 0:e63996fd7d3e 1596 * further information regarding CLKOUT, please refer to the MPU-60X0 Product
khaledelmadawi 0:e63996fd7d3e 1597 * Specification document.
khaledelmadawi 0:e63996fd7d3e 1598 * @return Current reference clock output enabled status
khaledelmadawi 0:e63996fd7d3e 1599 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1600 * @see MPU6050_INTCFG_CLKOUT_EN_BIT
khaledelmadawi 0:e63996fd7d3e 1601 */
khaledelmadawi 0:e63996fd7d3e 1602 bool MPU6050::getClockOutputEnabled()
khaledelmadawi 0:e63996fd7d3e 1603 {
khaledelmadawi 0:e63996fd7d3e 1604 i2Cdev.readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1605 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1606 }
khaledelmadawi 0:e63996fd7d3e 1607 /** Set reference clock output enabled status.
khaledelmadawi 0:e63996fd7d3e 1608 * When this bit is equal to 1, a reference clock output is provided at the
khaledelmadawi 0:e63996fd7d3e 1609 * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For
khaledelmadawi 0:e63996fd7d3e 1610 * further information regarding CLKOUT, please refer to the MPU-60X0 Product
khaledelmadawi 0:e63996fd7d3e 1611 * Specification document.
khaledelmadawi 0:e63996fd7d3e 1612 * @param enabled New reference clock output enabled status
khaledelmadawi 0:e63996fd7d3e 1613 * @see MPU6050_RA_INT_PIN_CFG
khaledelmadawi 0:e63996fd7d3e 1614 * @see MPU6050_INTCFG_CLKOUT_EN_BIT
khaledelmadawi 0:e63996fd7d3e 1615 */
khaledelmadawi 0:e63996fd7d3e 1616 void MPU6050::setClockOutputEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1617 {
khaledelmadawi 0:e63996fd7d3e 1618 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1619 }
khaledelmadawi 0:e63996fd7d3e 1620
khaledelmadawi 0:e63996fd7d3e 1621 // INT_ENABLE register
khaledelmadawi 0:e63996fd7d3e 1622
khaledelmadawi 0:e63996fd7d3e 1623 /** Get full interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1624 * Full register byte for all interrupts, for quick reading. Each bit will be
khaledelmadawi 0:e63996fd7d3e 1625 * set 0 for disabled, 1 for enabled.
khaledelmadawi 0:e63996fd7d3e 1626 * @return Current interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1627 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1628 * @see MPU6050_INTERRUPT_FF_BIT
khaledelmadawi 0:e63996fd7d3e 1629 **/
khaledelmadawi 0:e63996fd7d3e 1630 uint8_t MPU6050::getIntEnabled()
khaledelmadawi 0:e63996fd7d3e 1631 {
khaledelmadawi 0:e63996fd7d3e 1632 i2Cdev.readByte(devAddr, MPU6050_RA_INT_ENABLE, buffer);
khaledelmadawi 0:e63996fd7d3e 1633 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1634 }
khaledelmadawi 0:e63996fd7d3e 1635 /** Set full interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1636 * Full register byte for all interrupts, for quick reading. Each bit should be
khaledelmadawi 0:e63996fd7d3e 1637 * set 0 for disabled, 1 for enabled.
khaledelmadawi 0:e63996fd7d3e 1638 * @param enabled New interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1639 * @see getIntFreefallEnabled()
khaledelmadawi 0:e63996fd7d3e 1640 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1641 * @see MPU6050_INTERRUPT_FF_BIT
khaledelmadawi 0:e63996fd7d3e 1642 **/
khaledelmadawi 0:e63996fd7d3e 1643 void MPU6050::setIntEnabled(uint8_t enabled)
khaledelmadawi 0:e63996fd7d3e 1644 {
khaledelmadawi 0:e63996fd7d3e 1645 i2Cdev.writeByte(devAddr, MPU6050_RA_INT_ENABLE, enabled);
khaledelmadawi 0:e63996fd7d3e 1646 }
khaledelmadawi 0:e63996fd7d3e 1647 /** Get Free Fall interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1648 * Will be set 0 for disabled, 1 for enabled.
khaledelmadawi 0:e63996fd7d3e 1649 * @return Current interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1650 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1651 * @see MPU6050_INTERRUPT_FF_BIT
khaledelmadawi 0:e63996fd7d3e 1652 **/
khaledelmadawi 0:e63996fd7d3e 1653 bool MPU6050::getIntFreefallEnabled()
khaledelmadawi 0:e63996fd7d3e 1654 {
khaledelmadawi 0:e63996fd7d3e 1655 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1656 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1657 }
khaledelmadawi 0:e63996fd7d3e 1658 /** Set Free Fall interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1659 * @param enabled New interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1660 * @see getIntFreefallEnabled()
khaledelmadawi 0:e63996fd7d3e 1661 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1662 * @see MPU6050_INTERRUPT_FF_BIT
khaledelmadawi 0:e63996fd7d3e 1663 **/
khaledelmadawi 0:e63996fd7d3e 1664 void MPU6050::setIntFreefallEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1665 {
khaledelmadawi 0:e63996fd7d3e 1666 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1667 }
khaledelmadawi 0:e63996fd7d3e 1668 /** Get Motion Detection interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1669 * Will be set 0 for disabled, 1 for enabled.
khaledelmadawi 0:e63996fd7d3e 1670 * @return Current interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1671 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1672 * @see MPU6050_INTERRUPT_MOT_BIT
khaledelmadawi 0:e63996fd7d3e 1673 **/
khaledelmadawi 0:e63996fd7d3e 1674 bool MPU6050::getIntMotionEnabled()
khaledelmadawi 0:e63996fd7d3e 1675 {
khaledelmadawi 0:e63996fd7d3e 1676 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1677 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1678 }
khaledelmadawi 0:e63996fd7d3e 1679 /** Set Motion Detection interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1680 * @param enabled New interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1681 * @see getIntMotionEnabled()
khaledelmadawi 0:e63996fd7d3e 1682 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1683 * @see MPU6050_INTERRUPT_MOT_BIT
khaledelmadawi 0:e63996fd7d3e 1684 **/
khaledelmadawi 0:e63996fd7d3e 1685 void MPU6050::setIntMotionEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1686 {
khaledelmadawi 0:e63996fd7d3e 1687 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1688 }
khaledelmadawi 0:e63996fd7d3e 1689 /** Get Zero Motion Detection interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1690 * Will be set 0 for disabled, 1 for enabled.
khaledelmadawi 0:e63996fd7d3e 1691 * @return Current interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1692 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1693 * @see MPU6050_INTERRUPT_ZMOT_BIT
khaledelmadawi 0:e63996fd7d3e 1694 **/
khaledelmadawi 0:e63996fd7d3e 1695 bool MPU6050::getIntZeroMotionEnabled()
khaledelmadawi 0:e63996fd7d3e 1696 {
khaledelmadawi 0:e63996fd7d3e 1697 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1698 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1699 }
khaledelmadawi 0:e63996fd7d3e 1700 /** Set Zero Motion Detection interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1701 * @param enabled New interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1702 * @see getIntZeroMotionEnabled()
khaledelmadawi 0:e63996fd7d3e 1703 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1704 * @see MPU6050_INTERRUPT_ZMOT_BIT
khaledelmadawi 0:e63996fd7d3e 1705 **/
khaledelmadawi 0:e63996fd7d3e 1706 void MPU6050::setIntZeroMotionEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1707 {
khaledelmadawi 0:e63996fd7d3e 1708 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1709 }
khaledelmadawi 0:e63996fd7d3e 1710 /** Get FIFO Buffer Overflow interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1711 * Will be set 0 for disabled, 1 for enabled.
khaledelmadawi 0:e63996fd7d3e 1712 * @return Current interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1713 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1714 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
khaledelmadawi 0:e63996fd7d3e 1715 **/
khaledelmadawi 0:e63996fd7d3e 1716 bool MPU6050::getIntFIFOBufferOverflowEnabled()
khaledelmadawi 0:e63996fd7d3e 1717 {
khaledelmadawi 0:e63996fd7d3e 1718 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1719 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1720 }
khaledelmadawi 0:e63996fd7d3e 1721 /** Set FIFO Buffer Overflow interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1722 * @param enabled New interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1723 * @see getIntFIFOBufferOverflowEnabled()
khaledelmadawi 0:e63996fd7d3e 1724 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1725 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
khaledelmadawi 0:e63996fd7d3e 1726 **/
khaledelmadawi 0:e63996fd7d3e 1727 void MPU6050::setIntFIFOBufferOverflowEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1728 {
khaledelmadawi 0:e63996fd7d3e 1729 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1730 }
khaledelmadawi 0:e63996fd7d3e 1731 /** Get I2C Master interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1732 * This enables any of the I2C Master interrupt sources to generate an
khaledelmadawi 0:e63996fd7d3e 1733 * interrupt. Will be set 0 for disabled, 1 for enabled.
khaledelmadawi 0:e63996fd7d3e 1734 * @return Current interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1735 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1736 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
khaledelmadawi 0:e63996fd7d3e 1737 **/
khaledelmadawi 0:e63996fd7d3e 1738 bool MPU6050::getIntI2CMasterEnabled()
khaledelmadawi 0:e63996fd7d3e 1739 {
khaledelmadawi 0:e63996fd7d3e 1740 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1741 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1742 }
khaledelmadawi 0:e63996fd7d3e 1743 /** Set I2C Master interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1744 * @param enabled New interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1745 * @see getIntI2CMasterEnabled()
khaledelmadawi 0:e63996fd7d3e 1746 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1747 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
khaledelmadawi 0:e63996fd7d3e 1748 **/
khaledelmadawi 0:e63996fd7d3e 1749 void MPU6050::setIntI2CMasterEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1750 {
khaledelmadawi 0:e63996fd7d3e 1751 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1752 }
khaledelmadawi 0:e63996fd7d3e 1753 /** Get Data Ready interrupt enabled setting.
khaledelmadawi 0:e63996fd7d3e 1754 * This event occurs each time a write operation to all of the sensor registers
khaledelmadawi 0:e63996fd7d3e 1755 * has been completed. Will be set 0 for disabled, 1 for enabled.
khaledelmadawi 0:e63996fd7d3e 1756 * @return Current interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1757 * @see MPU6050_RA_INT_ENABLE
khaledelmadawi 0:e63996fd7d3e 1758 * @see MPU6050_INTERRUPT_DATA_RDY_BIT
khaledelmadawi 0:e63996fd7d3e 1759 */
khaledelmadawi 0:e63996fd7d3e 1760 bool MPU6050::getIntDataReadyEnabled()
khaledelmadawi 0:e63996fd7d3e 1761 {
khaledelmadawi 0:e63996fd7d3e 1762 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1763 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1764 }
khaledelmadawi 0:e63996fd7d3e 1765 /** Set Data Ready interrupt enabled status.
khaledelmadawi 0:e63996fd7d3e 1766 * @param enabled New interrupt enabled status
khaledelmadawi 0:e63996fd7d3e 1767 * @see getIntDataReadyEnabled()
khaledelmadawi 0:e63996fd7d3e 1768 * @see MPU6050_RA_INT_CFG
khaledelmadawi 0:e63996fd7d3e 1769 * @see MPU6050_INTERRUPT_DATA_RDY_BIT
khaledelmadawi 0:e63996fd7d3e 1770 */
khaledelmadawi 0:e63996fd7d3e 1771 void MPU6050::setIntDataReadyEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 1772 {
khaledelmadawi 0:e63996fd7d3e 1773 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 1774 }
khaledelmadawi 0:e63996fd7d3e 1775
khaledelmadawi 0:e63996fd7d3e 1776 // INT_STATUS register
khaledelmadawi 0:e63996fd7d3e 1777
khaledelmadawi 0:e63996fd7d3e 1778 /** Get full set of interrupt status bits.
khaledelmadawi 0:e63996fd7d3e 1779 * These bits clear to 0 after the register has been read. Very useful
khaledelmadawi 0:e63996fd7d3e 1780 * for getting multiple INT statuses, since each single bit read clears
khaledelmadawi 0:e63996fd7d3e 1781 * all of them because it has to read the whole byte.
khaledelmadawi 0:e63996fd7d3e 1782 * @return Current interrupt status
khaledelmadawi 0:e63996fd7d3e 1783 * @see MPU6050_RA_INT_STATUS
khaledelmadawi 0:e63996fd7d3e 1784 */
khaledelmadawi 0:e63996fd7d3e 1785 uint8_t MPU6050::getIntStatus()
khaledelmadawi 0:e63996fd7d3e 1786 {
khaledelmadawi 0:e63996fd7d3e 1787 i2Cdev.readByte(devAddr, MPU6050_RA_INT_STATUS, buffer);
khaledelmadawi 0:e63996fd7d3e 1788 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1789 }
khaledelmadawi 0:e63996fd7d3e 1790 /** Get Free Fall interrupt status.
khaledelmadawi 0:e63996fd7d3e 1791 * This bit automatically sets to 1 when a Free Fall interrupt has been
khaledelmadawi 0:e63996fd7d3e 1792 * generated. The bit clears to 0 after the register has been read.
khaledelmadawi 0:e63996fd7d3e 1793 * @return Current interrupt status
khaledelmadawi 0:e63996fd7d3e 1794 * @see MPU6050_RA_INT_STATUS
khaledelmadawi 0:e63996fd7d3e 1795 * @see MPU6050_INTERRUPT_FF_BIT
khaledelmadawi 0:e63996fd7d3e 1796 */
khaledelmadawi 0:e63996fd7d3e 1797 bool MPU6050::getIntFreefallStatus()
khaledelmadawi 0:e63996fd7d3e 1798 {
khaledelmadawi 0:e63996fd7d3e 1799 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FF_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1800 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1801 }
khaledelmadawi 0:e63996fd7d3e 1802 /** Get Motion Detection interrupt status.
khaledelmadawi 0:e63996fd7d3e 1803 * This bit automatically sets to 1 when a Motion Detection interrupt has been
khaledelmadawi 0:e63996fd7d3e 1804 * generated. The bit clears to 0 after the register has been read.
khaledelmadawi 0:e63996fd7d3e 1805 * @return Current interrupt status
khaledelmadawi 0:e63996fd7d3e 1806 * @see MPU6050_RA_INT_STATUS
khaledelmadawi 0:e63996fd7d3e 1807 * @see MPU6050_INTERRUPT_MOT_BIT
khaledelmadawi 0:e63996fd7d3e 1808 */
khaledelmadawi 0:e63996fd7d3e 1809 bool MPU6050::getIntMotionStatus()
khaledelmadawi 0:e63996fd7d3e 1810 {
khaledelmadawi 0:e63996fd7d3e 1811 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_MOT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1812 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1813 }
khaledelmadawi 0:e63996fd7d3e 1814 /** Get Zero Motion Detection interrupt status.
khaledelmadawi 0:e63996fd7d3e 1815 * This bit automatically sets to 1 when a Zero Motion Detection interrupt has
khaledelmadawi 0:e63996fd7d3e 1816 * been generated. The bit clears to 0 after the register has been read.
khaledelmadawi 0:e63996fd7d3e 1817 * @return Current interrupt status
khaledelmadawi 0:e63996fd7d3e 1818 * @see MPU6050_RA_INT_STATUS
khaledelmadawi 0:e63996fd7d3e 1819 * @see MPU6050_INTERRUPT_ZMOT_BIT
khaledelmadawi 0:e63996fd7d3e 1820 */
khaledelmadawi 0:e63996fd7d3e 1821 bool MPU6050::getIntZeroMotionStatus()
khaledelmadawi 0:e63996fd7d3e 1822 {
khaledelmadawi 0:e63996fd7d3e 1823 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_ZMOT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1824 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1825 }
khaledelmadawi 0:e63996fd7d3e 1826 /** Get FIFO Buffer Overflow interrupt status.
khaledelmadawi 0:e63996fd7d3e 1827 * This bit automatically sets to 1 when a Free Fall interrupt has been
khaledelmadawi 0:e63996fd7d3e 1828 * generated. The bit clears to 0 after the register has been read.
khaledelmadawi 0:e63996fd7d3e 1829 * @return Current interrupt status
khaledelmadawi 0:e63996fd7d3e 1830 * @see MPU6050_RA_INT_STATUS
khaledelmadawi 0:e63996fd7d3e 1831 * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
khaledelmadawi 0:e63996fd7d3e 1832 */
khaledelmadawi 0:e63996fd7d3e 1833 bool MPU6050::getIntFIFOBufferOverflowStatus()
khaledelmadawi 0:e63996fd7d3e 1834 {
khaledelmadawi 0:e63996fd7d3e 1835 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1836 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1837 }
khaledelmadawi 0:e63996fd7d3e 1838 /** Get I2C Master interrupt status.
khaledelmadawi 0:e63996fd7d3e 1839 * This bit automatically sets to 1 when an I2C Master interrupt has been
khaledelmadawi 0:e63996fd7d3e 1840 * generated. For a list of I2C Master interrupts, please refer to Register 54.
khaledelmadawi 0:e63996fd7d3e 1841 * The bit clears to 0 after the register has been read.
khaledelmadawi 0:e63996fd7d3e 1842 * @return Current interrupt status
khaledelmadawi 0:e63996fd7d3e 1843 * @see MPU6050_RA_INT_STATUS
khaledelmadawi 0:e63996fd7d3e 1844 * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
khaledelmadawi 0:e63996fd7d3e 1845 */
khaledelmadawi 0:e63996fd7d3e 1846 bool MPU6050::getIntI2CMasterStatus()
khaledelmadawi 0:e63996fd7d3e 1847 {
khaledelmadawi 0:e63996fd7d3e 1848 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_I2C_MST_INT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1849 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1850 }
khaledelmadawi 0:e63996fd7d3e 1851 /** Get Data Ready interrupt status.
khaledelmadawi 0:e63996fd7d3e 1852 * This bit automatically sets to 1 when a Data Ready interrupt has been
khaledelmadawi 0:e63996fd7d3e 1853 * generated. The bit clears to 0 after the register has been read.
khaledelmadawi 0:e63996fd7d3e 1854 * @return Current interrupt status
khaledelmadawi 0:e63996fd7d3e 1855 * @see MPU6050_RA_INT_STATUS
khaledelmadawi 0:e63996fd7d3e 1856 * @see MPU6050_INTERRUPT_DATA_RDY_BIT
khaledelmadawi 0:e63996fd7d3e 1857 */
khaledelmadawi 0:e63996fd7d3e 1858 bool MPU6050::getIntDataReadyStatus()
khaledelmadawi 0:e63996fd7d3e 1859 {
khaledelmadawi 0:e63996fd7d3e 1860 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DATA_RDY_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 1861 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 1862 }
khaledelmadawi 0:e63996fd7d3e 1863
khaledelmadawi 0:e63996fd7d3e 1864 // ACCEL_*OUT_* registers
khaledelmadawi 0:e63996fd7d3e 1865
khaledelmadawi 0:e63996fd7d3e 1866 /** Get raw 9-axis motion sensor readings (accel/gyro/compass).
khaledelmadawi 0:e63996fd7d3e 1867 * FUNCTION NOT FULLY IMPLEMENTED YET.
khaledelmadawi 0:e63996fd7d3e 1868 * @param ax 16-bit signed integer container for accelerometer X-axis value
khaledelmadawi 0:e63996fd7d3e 1869 * @param ay 16-bit signed integer container for accelerometer Y-axis value
khaledelmadawi 0:e63996fd7d3e 1870 * @param az 16-bit signed integer container for accelerometer Z-axis value
khaledelmadawi 0:e63996fd7d3e 1871 * @param gx 16-bit signed integer container for gyroscope X-axis value
khaledelmadawi 0:e63996fd7d3e 1872 * @param gy 16-bit signed integer container for gyroscope Y-axis value
khaledelmadawi 0:e63996fd7d3e 1873 * @param gz 16-bit signed integer container for gyroscope Z-axis value
khaledelmadawi 0:e63996fd7d3e 1874 * @param mx 16-bit signed integer container for magnetometer X-axis value
khaledelmadawi 0:e63996fd7d3e 1875 * @param my 16-bit signed integer container for magnetometer Y-axis value
khaledelmadawi 0:e63996fd7d3e 1876 * @param mz 16-bit signed integer container for magnetometer Z-axis value
khaledelmadawi 0:e63996fd7d3e 1877 * @see getMotion6()
khaledelmadawi 0:e63996fd7d3e 1878 * @see getAcceleration()
khaledelmadawi 0:e63996fd7d3e 1879 * @see getRotation()
khaledelmadawi 0:e63996fd7d3e 1880 * @see MPU6050_RA_ACCEL_XOUT_H
khaledelmadawi 0:e63996fd7d3e 1881 */
khaledelmadawi 0:e63996fd7d3e 1882 void MPU6050::getMotion9(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz, int16_t* mx, int16_t* my, int16_t* mz)
khaledelmadawi 0:e63996fd7d3e 1883 {
khaledelmadawi 0:e63996fd7d3e 1884 getMotion6(ax, ay, az, gx, gy, gz);
khaledelmadawi 0:e63996fd7d3e 1885
khaledelmadawi 0:e63996fd7d3e 1886 // magnetometer reading
khaledelmadawi 0:e63996fd7d3e 1887 i2Cdev.writeByte(devAddr, MPU6050_RA_INT_PIN_CFG, 0x02); //set i2c bypass enable pin to true to access magnetometer
khaledelmadawi 0:e63996fd7d3e 1888 wait_ms(10); // necessary wait >=6ms
khaledelmadawi 0:e63996fd7d3e 1889 i2Cdev.writeByte(MPU9150_RA_MAG_ADDRESS, 0x0A, 0x01); // enable the magnetometer
khaledelmadawi 0:e63996fd7d3e 1890 wait_ms(10); // necessary wait >=6ms
khaledelmadawi 0:e63996fd7d3e 1891 i2Cdev.readBytes(MPU9150_RA_MAG_ADDRESS, MPU9150_RA_MAG_XOUT_L, 6, buffer);
khaledelmadawi 0:e63996fd7d3e 1892 *mx = (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 1893 *my = (((int16_t)buffer[2]) << 8) | buffer[3];
khaledelmadawi 0:e63996fd7d3e 1894 *mz = (((int16_t)buffer[4]) << 8) | buffer[5];
khaledelmadawi 0:e63996fd7d3e 1895 }
khaledelmadawi 0:e63996fd7d3e 1896 /** Get raw 6-axis motion sensor readings (accel/gyro).
khaledelmadawi 0:e63996fd7d3e 1897 * Retrieves all currently available motion sensor values.
khaledelmadawi 0:e63996fd7d3e 1898 * @param ax 16-bit signed integer container for accelerometer X-axis value
khaledelmadawi 0:e63996fd7d3e 1899 * @param ay 16-bit signed integer container for accelerometer Y-axis value
khaledelmadawi 0:e63996fd7d3e 1900 * @param az 16-bit signed integer container for accelerometer Z-axis value
khaledelmadawi 0:e63996fd7d3e 1901 * @param gx 16-bit signed integer container for gyroscope X-axis value
khaledelmadawi 0:e63996fd7d3e 1902 * @param gy 16-bit signed integer container for gyroscope Y-axis value
khaledelmadawi 0:e63996fd7d3e 1903 * @param gz 16-bit signed integer container for gyroscope Z-axis value
khaledelmadawi 0:e63996fd7d3e 1904 * @see getAcceleration()
khaledelmadawi 0:e63996fd7d3e 1905 * @see getRotation()
khaledelmadawi 0:e63996fd7d3e 1906 * @see MPU6050_RA_ACCEL_XOUT_H
khaledelmadawi 0:e63996fd7d3e 1907 */
khaledelmadawi 0:e63996fd7d3e 1908 void MPU6050::getMotion6(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz)
khaledelmadawi 0:e63996fd7d3e 1909 {
khaledelmadawi 0:e63996fd7d3e 1910 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 14, buffer);
khaledelmadawi 0:e63996fd7d3e 1911 *ax = (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 1912 *ay = (((int16_t)buffer[2]) << 8) | buffer[3];
khaledelmadawi 0:e63996fd7d3e 1913 *az = (((int16_t)buffer[4]) << 8) | buffer[5];
khaledelmadawi 0:e63996fd7d3e 1914 *gx = (((int16_t)buffer[8]) << 8) | buffer[9];
khaledelmadawi 0:e63996fd7d3e 1915 *gy = (((int16_t)buffer[10]) << 8) | buffer[11];
khaledelmadawi 0:e63996fd7d3e 1916 *gz = (((int16_t)buffer[12]) << 8) | buffer[13];
khaledelmadawi 0:e63996fd7d3e 1917 }
khaledelmadawi 0:e63996fd7d3e 1918 /** Get 3-axis accelerometer readings.
khaledelmadawi 0:e63996fd7d3e 1919 * These registers store the most recent accelerometer measurements.
khaledelmadawi 0:e63996fd7d3e 1920 * Accelerometer measurements are written to these registers at the Sample Rate
khaledelmadawi 0:e63996fd7d3e 1921 * as defined in Register 25.
khaledelmadawi 0:e63996fd7d3e 1922 *
khaledelmadawi 0:e63996fd7d3e 1923 * The accelerometer measurement registers, along with the temperature
khaledelmadawi 0:e63996fd7d3e 1924 * measurement registers, gyroscope measurement registers, and external sensor
khaledelmadawi 0:e63996fd7d3e 1925 * data registers, are composed of two sets of registers: an internal register
khaledelmadawi 0:e63996fd7d3e 1926 * set and a user-facing read register set.
khaledelmadawi 0:e63996fd7d3e 1927 *
khaledelmadawi 0:e63996fd7d3e 1928 * The data within the accelerometer sensors' internal register set is always
khaledelmadawi 0:e63996fd7d3e 1929 * updated at the Sample Rate. Meanwhile, the user-facing read register set
khaledelmadawi 0:e63996fd7d3e 1930 * duplicates the internal register set's data values whenever the serial
khaledelmadawi 0:e63996fd7d3e 1931 * interface is idle. This guarantees that a burst read of sensor registers will
khaledelmadawi 0:e63996fd7d3e 1932 * read measurements from the same sampling instant. Note that if burst reads
khaledelmadawi 0:e63996fd7d3e 1933 * are not used, the user is responsible for ensuring a set of single byte reads
khaledelmadawi 0:e63996fd7d3e 1934 * correspond to a single sampling instant by checking the Data Ready interrupt.
khaledelmadawi 0:e63996fd7d3e 1935 *
khaledelmadawi 0:e63996fd7d3e 1936 * Each 16-bit accelerometer measurement has a full scale defined in ACCEL_FS
khaledelmadawi 0:e63996fd7d3e 1937 * (Register 28). For each full scale setting, the accelerometers' sensitivity
khaledelmadawi 0:e63996fd7d3e 1938 * per LSB in ACCEL_xOUT is shown in the table below:
khaledelmadawi 0:e63996fd7d3e 1939 *
khaledelmadawi 0:e63996fd7d3e 1940 * <pre>
khaledelmadawi 0:e63996fd7d3e 1941 * AFS_SEL | Full Scale Range | LSB Sensitivity
khaledelmadawi 0:e63996fd7d3e 1942 * --------+------------------+----------------
khaledelmadawi 0:e63996fd7d3e 1943 * 0 | +/- 2g | 8192 LSB/mg
khaledelmadawi 0:e63996fd7d3e 1944 * 1 | +/- 4g | 4096 LSB/mg
khaledelmadawi 0:e63996fd7d3e 1945 * 2 | +/- 8g | 2048 LSB/mg
khaledelmadawi 0:e63996fd7d3e 1946 * 3 | +/- 16g | 1024 LSB/mg
khaledelmadawi 0:e63996fd7d3e 1947 * </pre>
khaledelmadawi 0:e63996fd7d3e 1948 *
khaledelmadawi 0:e63996fd7d3e 1949 * @param x 16-bit signed integer container for X-axis acceleration
khaledelmadawi 0:e63996fd7d3e 1950 * @param y 16-bit signed integer container for Y-axis acceleration
khaledelmadawi 0:e63996fd7d3e 1951 * @param z 16-bit signed integer container for Z-axis acceleration
khaledelmadawi 0:e63996fd7d3e 1952 * @see MPU6050_RA_GYRO_XOUT_H
khaledelmadawi 0:e63996fd7d3e 1953 */
khaledelmadawi 0:e63996fd7d3e 1954 void MPU6050::getAcceleration(int16_t* x, int16_t* y, int16_t* z)
khaledelmadawi 0:e63996fd7d3e 1955 {
khaledelmadawi 0:e63996fd7d3e 1956 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 6, buffer);
khaledelmadawi 0:e63996fd7d3e 1957 *x = (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 1958 *y = (((int16_t)buffer[2]) << 8) | buffer[3];
khaledelmadawi 0:e63996fd7d3e 1959 *z = (((int16_t)buffer[4]) << 8) | buffer[5];
khaledelmadawi 0:e63996fd7d3e 1960 }
khaledelmadawi 0:e63996fd7d3e 1961 /** Get X-axis accelerometer reading.
khaledelmadawi 0:e63996fd7d3e 1962 * @return X-axis acceleration measurement in 16-bit 2's complement format
khaledelmadawi 0:e63996fd7d3e 1963 * @see getMotion6()
khaledelmadawi 0:e63996fd7d3e 1964 * @see MPU6050_RA_ACCEL_XOUT_H
khaledelmadawi 0:e63996fd7d3e 1965 */
khaledelmadawi 0:e63996fd7d3e 1966 int16_t MPU6050::getAccelerationX()
khaledelmadawi 0:e63996fd7d3e 1967 {
khaledelmadawi 0:e63996fd7d3e 1968 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 1969 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 1970 }
khaledelmadawi 0:e63996fd7d3e 1971 /** Get Y-axis accelerometer reading.
khaledelmadawi 0:e63996fd7d3e 1972 * @return Y-axis acceleration measurement in 16-bit 2's complement format
khaledelmadawi 0:e63996fd7d3e 1973 * @see getMotion6()
khaledelmadawi 0:e63996fd7d3e 1974 * @see MPU6050_RA_ACCEL_YOUT_H
khaledelmadawi 0:e63996fd7d3e 1975 */
khaledelmadawi 0:e63996fd7d3e 1976 int16_t MPU6050::getAccelerationY()
khaledelmadawi 0:e63996fd7d3e 1977 {
khaledelmadawi 0:e63996fd7d3e 1978 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_YOUT_H, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 1979 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 1980 }
khaledelmadawi 0:e63996fd7d3e 1981 /** Get Z-axis accelerometer reading.
khaledelmadawi 0:e63996fd7d3e 1982 * @return Z-axis acceleration measurement in 16-bit 2's complement format
khaledelmadawi 0:e63996fd7d3e 1983 * @see getMotion6()
khaledelmadawi 0:e63996fd7d3e 1984 * @see MPU6050_RA_ACCEL_ZOUT_H
khaledelmadawi 0:e63996fd7d3e 1985 */
khaledelmadawi 0:e63996fd7d3e 1986 int16_t MPU6050::getAccelerationZ()
khaledelmadawi 0:e63996fd7d3e 1987 {
khaledelmadawi 0:e63996fd7d3e 1988 i2Cdev.readBytes(devAddr, MPU6050_RA_ACCEL_ZOUT_H, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 1989 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 1990 }
khaledelmadawi 0:e63996fd7d3e 1991
khaledelmadawi 0:e63996fd7d3e 1992 // TEMP_OUT_* registers
khaledelmadawi 0:e63996fd7d3e 1993
khaledelmadawi 0:e63996fd7d3e 1994 /** Get current internal temperature.
khaledelmadawi 0:e63996fd7d3e 1995 * @return Temperature reading in 16-bit 2's complement format
khaledelmadawi 0:e63996fd7d3e 1996 * @see MPU6050_RA_TEMP_OUT_H
khaledelmadawi 0:e63996fd7d3e 1997 */
khaledelmadawi 0:e63996fd7d3e 1998 int16_t MPU6050::getTemperature()
khaledelmadawi 0:e63996fd7d3e 1999 {
khaledelmadawi 0:e63996fd7d3e 2000 i2Cdev.readBytes(devAddr, MPU6050_RA_TEMP_OUT_H, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 2001 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 2002 }
khaledelmadawi 0:e63996fd7d3e 2003
khaledelmadawi 0:e63996fd7d3e 2004 // GYRO_*OUT_* registers
khaledelmadawi 0:e63996fd7d3e 2005
khaledelmadawi 0:e63996fd7d3e 2006 /** Get 3-axis gyroscope readings.
khaledelmadawi 0:e63996fd7d3e 2007 * These gyroscope measurement registers, along with the accelerometer
khaledelmadawi 0:e63996fd7d3e 2008 * measurement registers, temperature measurement registers, and external sensor
khaledelmadawi 0:e63996fd7d3e 2009 * data registers, are composed of two sets of registers: an internal register
khaledelmadawi 0:e63996fd7d3e 2010 * set and a user-facing read register set.
khaledelmadawi 0:e63996fd7d3e 2011 * The data within the gyroscope sensors' internal register set is always
khaledelmadawi 0:e63996fd7d3e 2012 * updated at the Sample Rate. Meanwhile, the user-facing read register set
khaledelmadawi 0:e63996fd7d3e 2013 * duplicates the internal register set's data values whenever the serial
khaledelmadawi 0:e63996fd7d3e 2014 * interface is idle. This guarantees that a burst read of sensor registers will
khaledelmadawi 0:e63996fd7d3e 2015 * read measurements from the same sampling instant. Note that if burst reads
khaledelmadawi 0:e63996fd7d3e 2016 * are not used, the user is responsible for ensuring a set of single byte reads
khaledelmadawi 0:e63996fd7d3e 2017 * correspond to a single sampling instant by checking the Data Ready interrupt.
khaledelmadawi 0:e63996fd7d3e 2018 *
khaledelmadawi 0:e63996fd7d3e 2019 * Each 16-bit gyroscope measurement has a full scale defined in FS_SEL
khaledelmadawi 0:e63996fd7d3e 2020 * (Register 27). For each full scale setting, the gyroscopes' sensitivity per
khaledelmadawi 0:e63996fd7d3e 2021 * LSB in GYRO_xOUT is shown in the table below:
khaledelmadawi 0:e63996fd7d3e 2022 *
khaledelmadawi 0:e63996fd7d3e 2023 * <pre>
khaledelmadawi 0:e63996fd7d3e 2024 * FS_SEL | Full Scale Range | LSB Sensitivity
khaledelmadawi 0:e63996fd7d3e 2025 * -------+--------------------+----------------
khaledelmadawi 0:e63996fd7d3e 2026 * 0 | +/- 250 degrees/s | 131 LSB/deg/s
khaledelmadawi 0:e63996fd7d3e 2027 * 1 | +/- 500 degrees/s | 65.5 LSB/deg/s
khaledelmadawi 0:e63996fd7d3e 2028 * 2 | +/- 1000 degrees/s | 32.8 LSB/deg/s
khaledelmadawi 0:e63996fd7d3e 2029 * 3 | +/- 2000 degrees/s | 16.4 LSB/deg/s
khaledelmadawi 0:e63996fd7d3e 2030 * </pre>
khaledelmadawi 0:e63996fd7d3e 2031 *
khaledelmadawi 0:e63996fd7d3e 2032 * @param x 16-bit signed integer container for X-axis rotation
khaledelmadawi 0:e63996fd7d3e 2033 * @param y 16-bit signed integer container for Y-axis rotation
khaledelmadawi 0:e63996fd7d3e 2034 * @param z 16-bit signed integer container for Z-axis rotation
khaledelmadawi 0:e63996fd7d3e 2035 * @see getMotion6()
khaledelmadawi 0:e63996fd7d3e 2036 * @see MPU6050_RA_GYRO_XOUT_H
khaledelmadawi 0:e63996fd7d3e 2037 */
khaledelmadawi 0:e63996fd7d3e 2038 void MPU6050::getRotation(int16_t* x, int16_t* y, int16_t* z)
khaledelmadawi 0:e63996fd7d3e 2039 {
khaledelmadawi 0:e63996fd7d3e 2040 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_XOUT_H, 6, buffer);
khaledelmadawi 0:e63996fd7d3e 2041 *x = (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 2042 *y = (((int16_t)buffer[2]) << 8) | buffer[3];
khaledelmadawi 0:e63996fd7d3e 2043 *z = (((int16_t)buffer[4]) << 8) | buffer[5];
khaledelmadawi 0:e63996fd7d3e 2044 }
khaledelmadawi 0:e63996fd7d3e 2045 /** Get X-axis gyroscope reading.
khaledelmadawi 0:e63996fd7d3e 2046 * @return X-axis rotation measurement in 16-bit 2's complement format
khaledelmadawi 0:e63996fd7d3e 2047 * @see getMotion6()
khaledelmadawi 0:e63996fd7d3e 2048 * @see MPU6050_RA_GYRO_XOUT_H
khaledelmadawi 0:e63996fd7d3e 2049 */
khaledelmadawi 0:e63996fd7d3e 2050 int16_t MPU6050::getRotationX()
khaledelmadawi 0:e63996fd7d3e 2051 {
khaledelmadawi 0:e63996fd7d3e 2052 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_XOUT_H, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 2053 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 2054 }
khaledelmadawi 0:e63996fd7d3e 2055 /** Get Y-axis gyroscope reading.
khaledelmadawi 0:e63996fd7d3e 2056 * @return Y-axis rotation measurement in 16-bit 2's complement format
khaledelmadawi 0:e63996fd7d3e 2057 * @see getMotion6()
khaledelmadawi 0:e63996fd7d3e 2058 * @see MPU6050_RA_GYRO_YOUT_H
khaledelmadawi 0:e63996fd7d3e 2059 */
khaledelmadawi 0:e63996fd7d3e 2060 int16_t MPU6050::getRotationY()
khaledelmadawi 0:e63996fd7d3e 2061 {
khaledelmadawi 0:e63996fd7d3e 2062 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_YOUT_H, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 2063 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 2064 }
khaledelmadawi 0:e63996fd7d3e 2065 /** Get Z-axis gyroscope reading.
khaledelmadawi 0:e63996fd7d3e 2066 * @return Z-axis rotation measurement in 16-bit 2's complement format
khaledelmadawi 0:e63996fd7d3e 2067 * @see getMotion6()
khaledelmadawi 0:e63996fd7d3e 2068 * @see MPU6050_RA_GYRO_ZOUT_H
khaledelmadawi 0:e63996fd7d3e 2069 */
khaledelmadawi 0:e63996fd7d3e 2070 int16_t MPU6050::getRotationZ()
khaledelmadawi 0:e63996fd7d3e 2071 {
khaledelmadawi 0:e63996fd7d3e 2072 i2Cdev.readBytes(devAddr, MPU6050_RA_GYRO_ZOUT_H, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 2073 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 2074 }
khaledelmadawi 0:e63996fd7d3e 2075
khaledelmadawi 0:e63996fd7d3e 2076 // EXT_SENS_DATA_* registers
khaledelmadawi 0:e63996fd7d3e 2077
khaledelmadawi 0:e63996fd7d3e 2078 /** Read single byte from external sensor data register.
khaledelmadawi 0:e63996fd7d3e 2079 * These registers store data read from external sensors by the Slave 0, 1, 2,
khaledelmadawi 0:e63996fd7d3e 2080 * and 3 on the auxiliary I2C interface. Data read by Slave 4 is stored in
khaledelmadawi 0:e63996fd7d3e 2081 * I2C_SLV4_DI (Register 53).
khaledelmadawi 0:e63996fd7d3e 2082 *
khaledelmadawi 0:e63996fd7d3e 2083 * External sensor data is written to these registers at the Sample Rate as
khaledelmadawi 0:e63996fd7d3e 2084 * defined in Register 25. This access rate can be reduced by using the Slave
khaledelmadawi 0:e63996fd7d3e 2085 * Delay Enable registers (Register 103).
khaledelmadawi 0:e63996fd7d3e 2086 *
khaledelmadawi 0:e63996fd7d3e 2087 * External sensor data registers, along with the gyroscope measurement
khaledelmadawi 0:e63996fd7d3e 2088 * registers, accelerometer measurement registers, and temperature measurement
khaledelmadawi 0:e63996fd7d3e 2089 * registers, are composed of two sets of registers: an internal register set
khaledelmadawi 0:e63996fd7d3e 2090 * and a user-facing read register set.
khaledelmadawi 0:e63996fd7d3e 2091 *
khaledelmadawi 0:e63996fd7d3e 2092 * The data within the external sensors' internal register set is always updated
khaledelmadawi 0:e63996fd7d3e 2093 * at the Sample Rate (or the reduced access rate) whenever the serial interface
khaledelmadawi 0:e63996fd7d3e 2094 * is idle. This guarantees that a burst read of sensor registers will read
khaledelmadawi 0:e63996fd7d3e 2095 * measurements from the same sampling instant. Note that if burst reads are not
khaledelmadawi 0:e63996fd7d3e 2096 * used, the user is responsible for ensuring a set of single byte reads
khaledelmadawi 0:e63996fd7d3e 2097 * correspond to a single sampling instant by checking the Data Ready interrupt.
khaledelmadawi 0:e63996fd7d3e 2098 *
khaledelmadawi 0:e63996fd7d3e 2099 * Data is placed in these external sensor data registers according to
khaledelmadawi 0:e63996fd7d3e 2100 * I2C_SLV0_CTRL, I2C_SLV1_CTRL, I2C_SLV2_CTRL, and I2C_SLV3_CTRL (Registers 39,
khaledelmadawi 0:e63996fd7d3e 2101 * 42, 45, and 48). When more than zero bytes are read (I2C_SLVx_LEN > 0) from
khaledelmadawi 0:e63996fd7d3e 2102 * an enabled slave (I2C_SLVx_EN = 1), the slave is read at the Sample Rate (as
khaledelmadawi 0:e63996fd7d3e 2103 * defined in Register 25) or delayed rate (if specified in Register 52 and
khaledelmadawi 0:e63996fd7d3e 2104 * 103). During each Sample cycle, slave reads are performed in order of Slave
khaledelmadawi 0:e63996fd7d3e 2105 * number. If all slaves are enabled with more than zero bytes to be read, the
khaledelmadawi 0:e63996fd7d3e 2106 * order will be Slave 0, followed by Slave 1, Slave 2, and Slave 3.
khaledelmadawi 0:e63996fd7d3e 2107 *
khaledelmadawi 0:e63996fd7d3e 2108 * Each enabled slave will have EXT_SENS_DATA registers associated with it by
khaledelmadawi 0:e63996fd7d3e 2109 * number of bytes read (I2C_SLVx_LEN) in order of slave number, starting from
khaledelmadawi 0:e63996fd7d3e 2110 * EXT_SENS_DATA_00. Note that this means enabling or disabling a slave may
khaledelmadawi 0:e63996fd7d3e 2111 * change the higher numbered slaves' associated registers. Furthermore, if
khaledelmadawi 0:e63996fd7d3e 2112 * fewer total bytes are being read from the external sensors as a result of
khaledelmadawi 0:e63996fd7d3e 2113 * such a change, then the data remaining in the registers which no longer have
khaledelmadawi 0:e63996fd7d3e 2114 * an associated slave device (i.e. high numbered registers) will remain in
khaledelmadawi 0:e63996fd7d3e 2115 * these previously allocated registers unless reset.
khaledelmadawi 0:e63996fd7d3e 2116 *
khaledelmadawi 0:e63996fd7d3e 2117 * If the sum of the read lengths of all SLVx transactions exceed the number of
khaledelmadawi 0:e63996fd7d3e 2118 * available EXT_SENS_DATA registers, the excess bytes will be dropped. There
khaledelmadawi 0:e63996fd7d3e 2119 * are 24 EXT_SENS_DATA registers and hence the total read lengths between all
khaledelmadawi 0:e63996fd7d3e 2120 * the slaves cannot be greater than 24 or some bytes will be lost.
khaledelmadawi 0:e63996fd7d3e 2121 *
khaledelmadawi 0:e63996fd7d3e 2122 * Note: Slave 4's behavior is distinct from that of Slaves 0-3. For further
khaledelmadawi 0:e63996fd7d3e 2123 * information regarding the characteristics of Slave 4, please refer to
khaledelmadawi 0:e63996fd7d3e 2124 * Registers 49 to 53.
khaledelmadawi 0:e63996fd7d3e 2125 *
khaledelmadawi 0:e63996fd7d3e 2126 * EXAMPLE:
khaledelmadawi 0:e63996fd7d3e 2127 * Suppose that Slave 0 is enabled with 4 bytes to be read (I2C_SLV0_EN = 1 and
khaledelmadawi 0:e63996fd7d3e 2128 * I2C_SLV0_LEN = 4) while Slave 1 is enabled with 2 bytes to be read so that
khaledelmadawi 0:e63996fd7d3e 2129 * I2C_SLV1_EN = 1 and I2C_SLV1_LEN = 2. In such a situation, EXT_SENS_DATA _00
khaledelmadawi 0:e63996fd7d3e 2130 * through _03 will be associated with Slave 0, while EXT_SENS_DATA _04 and 05
khaledelmadawi 0:e63996fd7d3e 2131 * will be associated with Slave 1. If Slave 2 is enabled as well, registers
khaledelmadawi 0:e63996fd7d3e 2132 * starting from EXT_SENS_DATA_06 will be allocated to Slave 2.
khaledelmadawi 0:e63996fd7d3e 2133 *
khaledelmadawi 0:e63996fd7d3e 2134 * If Slave 2 is disabled while Slave 3 is enabled in this same situation, then
khaledelmadawi 0:e63996fd7d3e 2135 * registers starting from EXT_SENS_DATA_06 will be allocated to Slave 3
khaledelmadawi 0:e63996fd7d3e 2136 * instead.
khaledelmadawi 0:e63996fd7d3e 2137 *
khaledelmadawi 0:e63996fd7d3e 2138 * REGISTER ALLOCATION FOR DYNAMIC DISABLE VS. NORMAL DISABLE:
khaledelmadawi 0:e63996fd7d3e 2139 * If a slave is disabled at any time, the space initially allocated to the
khaledelmadawi 0:e63996fd7d3e 2140 * slave in the EXT_SENS_DATA register, will remain associated with that slave.
khaledelmadawi 0:e63996fd7d3e 2141 * This is to avoid dynamic adjustment of the register allocation.
khaledelmadawi 0:e63996fd7d3e 2142 *
khaledelmadawi 0:e63996fd7d3e 2143 * The allocation of the EXT_SENS_DATA registers is recomputed only when (1) all
khaledelmadawi 0:e63996fd7d3e 2144 * slaves are disabled, or (2) the I2C_MST_RST bit is set (Register 106).
khaledelmadawi 0:e63996fd7d3e 2145 *
khaledelmadawi 0:e63996fd7d3e 2146 * This above is also true if one of the slaves gets NACKed and stops
khaledelmadawi 0:e63996fd7d3e 2147 * functioning.
khaledelmadawi 0:e63996fd7d3e 2148 *
khaledelmadawi 0:e63996fd7d3e 2149 * @param position Starting position (0-23)
khaledelmadawi 0:e63996fd7d3e 2150 * @return Byte read from register
khaledelmadawi 0:e63996fd7d3e 2151 */
khaledelmadawi 0:e63996fd7d3e 2152 uint8_t MPU6050::getExternalSensorByte(int position)
khaledelmadawi 0:e63996fd7d3e 2153 {
khaledelmadawi 0:e63996fd7d3e 2154 i2Cdev.readByte(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, buffer);
khaledelmadawi 0:e63996fd7d3e 2155 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2156 }
khaledelmadawi 0:e63996fd7d3e 2157 /** Read word (2 bytes) from external sensor data registers.
khaledelmadawi 0:e63996fd7d3e 2158 * @param position Starting position (0-21)
khaledelmadawi 0:e63996fd7d3e 2159 * @return Word read from register
khaledelmadawi 0:e63996fd7d3e 2160 * @see getExternalSensorByte()
khaledelmadawi 0:e63996fd7d3e 2161 */
khaledelmadawi 0:e63996fd7d3e 2162 uint16_t MPU6050::getExternalSensorWord(int position)
khaledelmadawi 0:e63996fd7d3e 2163 {
khaledelmadawi 0:e63996fd7d3e 2164 i2Cdev.readBytes(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 2165 return (((uint16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 2166 }
khaledelmadawi 0:e63996fd7d3e 2167 /** Read double word (4 bytes) from external sensor data registers.
khaledelmadawi 0:e63996fd7d3e 2168 * @param position Starting position (0-20)
khaledelmadawi 0:e63996fd7d3e 2169 * @return Double word read from registers
khaledelmadawi 0:e63996fd7d3e 2170 * @see getExternalSensorByte()
khaledelmadawi 0:e63996fd7d3e 2171 */
khaledelmadawi 0:e63996fd7d3e 2172 uint32_t MPU6050::getExternalSensorDWord(int position)
khaledelmadawi 0:e63996fd7d3e 2173 {
khaledelmadawi 0:e63996fd7d3e 2174 i2Cdev.readBytes(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 4, buffer);
khaledelmadawi 0:e63996fd7d3e 2175 return (((uint32_t)buffer[0]) << 24) | (((uint32_t)buffer[1]) << 16) | (((uint16_t)buffer[2]) << 8) | buffer[3];
khaledelmadawi 0:e63996fd7d3e 2176 }
khaledelmadawi 0:e63996fd7d3e 2177
khaledelmadawi 0:e63996fd7d3e 2178 // MOT_DETECT_STATUS register
khaledelmadawi 0:e63996fd7d3e 2179
khaledelmadawi 0:e63996fd7d3e 2180 /** Get X-axis negative motion detection interrupt status.
khaledelmadawi 0:e63996fd7d3e 2181 * @return Motion detection status
khaledelmadawi 0:e63996fd7d3e 2182 * @see MPU6050_RA_MOT_DETECT_STATUS
khaledelmadawi 0:e63996fd7d3e 2183 * @see MPU6050_MOTION_MOT_XNEG_BIT
khaledelmadawi 0:e63996fd7d3e 2184 */
khaledelmadawi 0:e63996fd7d3e 2185 bool MPU6050::getXNegMotionDetected()
khaledelmadawi 0:e63996fd7d3e 2186 {
khaledelmadawi 0:e63996fd7d3e 2187 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XNEG_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2188 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2189 }
khaledelmadawi 0:e63996fd7d3e 2190 /** Get X-axis positive motion detection interrupt status.
khaledelmadawi 0:e63996fd7d3e 2191 * @return Motion detection status
khaledelmadawi 0:e63996fd7d3e 2192 * @see MPU6050_RA_MOT_DETECT_STATUS
khaledelmadawi 0:e63996fd7d3e 2193 * @see MPU6050_MOTION_MOT_XPOS_BIT
khaledelmadawi 0:e63996fd7d3e 2194 */
khaledelmadawi 0:e63996fd7d3e 2195 bool MPU6050::getXPosMotionDetected()
khaledelmadawi 0:e63996fd7d3e 2196 {
khaledelmadawi 0:e63996fd7d3e 2197 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XPOS_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2198 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2199 }
khaledelmadawi 0:e63996fd7d3e 2200 /** Get Y-axis negative motion detection interrupt status.
khaledelmadawi 0:e63996fd7d3e 2201 * @return Motion detection status
khaledelmadawi 0:e63996fd7d3e 2202 * @see MPU6050_RA_MOT_DETECT_STATUS
khaledelmadawi 0:e63996fd7d3e 2203 * @see MPU6050_MOTION_MOT_YNEG_BIT
khaledelmadawi 0:e63996fd7d3e 2204 */
khaledelmadawi 0:e63996fd7d3e 2205 bool MPU6050::getYNegMotionDetected()
khaledelmadawi 0:e63996fd7d3e 2206 {
khaledelmadawi 0:e63996fd7d3e 2207 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_YNEG_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2208 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2209 }
khaledelmadawi 0:e63996fd7d3e 2210 /** Get Y-axis positive motion detection interrupt status.
khaledelmadawi 0:e63996fd7d3e 2211 * @return Motion detection status
khaledelmadawi 0:e63996fd7d3e 2212 * @see MPU6050_RA_MOT_DETECT_STATUS
khaledelmadawi 0:e63996fd7d3e 2213 * @see MPU6050_MOTION_MOT_YPOS_BIT
khaledelmadawi 0:e63996fd7d3e 2214 */
khaledelmadawi 0:e63996fd7d3e 2215 bool MPU6050::getYPosMotionDetected()
khaledelmadawi 0:e63996fd7d3e 2216 {
khaledelmadawi 0:e63996fd7d3e 2217 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_YPOS_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2218 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2219 }
khaledelmadawi 0:e63996fd7d3e 2220 /** Get Z-axis negative motion detection interrupt status.
khaledelmadawi 0:e63996fd7d3e 2221 * @return Motion detection status
khaledelmadawi 0:e63996fd7d3e 2222 * @see MPU6050_RA_MOT_DETECT_STATUS
khaledelmadawi 0:e63996fd7d3e 2223 * @see MPU6050_MOTION_MOT_ZNEG_BIT
khaledelmadawi 0:e63996fd7d3e 2224 */
khaledelmadawi 0:e63996fd7d3e 2225 bool MPU6050::getZNegMotionDetected()
khaledelmadawi 0:e63996fd7d3e 2226 {
khaledelmadawi 0:e63996fd7d3e 2227 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZNEG_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2228 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2229 }
khaledelmadawi 0:e63996fd7d3e 2230 /** Get Z-axis positive motion detection interrupt status.
khaledelmadawi 0:e63996fd7d3e 2231 * @return Motion detection status
khaledelmadawi 0:e63996fd7d3e 2232 * @see MPU6050_RA_MOT_DETECT_STATUS
khaledelmadawi 0:e63996fd7d3e 2233 * @see MPU6050_MOTION_MOT_ZPOS_BIT
khaledelmadawi 0:e63996fd7d3e 2234 */
khaledelmadawi 0:e63996fd7d3e 2235 bool MPU6050::getZPosMotionDetected()
khaledelmadawi 0:e63996fd7d3e 2236 {
khaledelmadawi 0:e63996fd7d3e 2237 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZPOS_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2238 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2239 }
khaledelmadawi 0:e63996fd7d3e 2240 /** Get zero motion detection interrupt status.
khaledelmadawi 0:e63996fd7d3e 2241 * @return Motion detection status
khaledelmadawi 0:e63996fd7d3e 2242 * @see MPU6050_RA_MOT_DETECT_STATUS
khaledelmadawi 0:e63996fd7d3e 2243 * @see MPU6050_MOTION_MOT_ZRMOT_BIT
khaledelmadawi 0:e63996fd7d3e 2244 */
khaledelmadawi 0:e63996fd7d3e 2245 bool MPU6050::getZeroMotionDetected()
khaledelmadawi 0:e63996fd7d3e 2246 {
khaledelmadawi 0:e63996fd7d3e 2247 i2Cdev.readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_ZRMOT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2248 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2249 }
khaledelmadawi 0:e63996fd7d3e 2250
khaledelmadawi 0:e63996fd7d3e 2251 // I2C_SLV*_DO register
khaledelmadawi 0:e63996fd7d3e 2252
khaledelmadawi 0:e63996fd7d3e 2253 /** Write byte to Data Output container for specified slave.
khaledelmadawi 0:e63996fd7d3e 2254 * This register holds the output data written into Slave when Slave is set to
khaledelmadawi 0:e63996fd7d3e 2255 * write mode. For further information regarding Slave control, please
khaledelmadawi 0:e63996fd7d3e 2256 * refer to Registers 37 to 39 and immediately following.
khaledelmadawi 0:e63996fd7d3e 2257 * @param num Slave number (0-3)
khaledelmadawi 0:e63996fd7d3e 2258 * @param data Byte to write
khaledelmadawi 0:e63996fd7d3e 2259 * @see MPU6050_RA_I2C_SLV0_DO
khaledelmadawi 0:e63996fd7d3e 2260 */
khaledelmadawi 0:e63996fd7d3e 2261 void MPU6050::setSlaveOutputByte(uint8_t num, uint8_t data)
khaledelmadawi 0:e63996fd7d3e 2262 {
khaledelmadawi 0:e63996fd7d3e 2263 if (num > 3) return;
khaledelmadawi 0:e63996fd7d3e 2264 i2Cdev.writeByte(devAddr, MPU6050_RA_I2C_SLV0_DO + num, data);
khaledelmadawi 0:e63996fd7d3e 2265 }
khaledelmadawi 0:e63996fd7d3e 2266
khaledelmadawi 0:e63996fd7d3e 2267 // I2C_MST_DELAY_CTRL register
khaledelmadawi 0:e63996fd7d3e 2268
khaledelmadawi 0:e63996fd7d3e 2269 /** Get external data shadow delay enabled status.
khaledelmadawi 0:e63996fd7d3e 2270 * This register is used to specify the timing of external sensor data
khaledelmadawi 0:e63996fd7d3e 2271 * shadowing. When DELAY_ES_SHADOW is set to 1, shadowing of external
khaledelmadawi 0:e63996fd7d3e 2272 * sensor data is delayed until all data has been received.
khaledelmadawi 0:e63996fd7d3e 2273 * @return Current external data shadow delay enabled status.
khaledelmadawi 0:e63996fd7d3e 2274 * @see MPU6050_RA_I2C_MST_DELAY_CTRL
khaledelmadawi 0:e63996fd7d3e 2275 * @see MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT
khaledelmadawi 0:e63996fd7d3e 2276 */
khaledelmadawi 0:e63996fd7d3e 2277 bool MPU6050::getExternalShadowDelayEnabled()
khaledelmadawi 0:e63996fd7d3e 2278 {
khaledelmadawi 0:e63996fd7d3e 2279 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2280 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2281 }
khaledelmadawi 0:e63996fd7d3e 2282 /** Set external data shadow delay enabled status.
khaledelmadawi 0:e63996fd7d3e 2283 * @param enabled New external data shadow delay enabled status.
khaledelmadawi 0:e63996fd7d3e 2284 * @see getExternalShadowDelayEnabled()
khaledelmadawi 0:e63996fd7d3e 2285 * @see MPU6050_RA_I2C_MST_DELAY_CTRL
khaledelmadawi 0:e63996fd7d3e 2286 * @see MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT
khaledelmadawi 0:e63996fd7d3e 2287 */
khaledelmadawi 0:e63996fd7d3e 2288 void MPU6050::setExternalShadowDelayEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2289 {
khaledelmadawi 0:e63996fd7d3e 2290 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2291 }
khaledelmadawi 0:e63996fd7d3e 2292 /** Get slave delay enabled status.
khaledelmadawi 0:e63996fd7d3e 2293 * When a particular slave delay is enabled, the rate of access for the that
khaledelmadawi 0:e63996fd7d3e 2294 * slave device is reduced. When a slave's access rate is decreased relative to
khaledelmadawi 0:e63996fd7d3e 2295 * the Sample Rate, the slave is accessed every:
khaledelmadawi 0:e63996fd7d3e 2296 *
khaledelmadawi 0:e63996fd7d3e 2297 * 1 / (1 + I2C_MST_DLY) Samples
khaledelmadawi 0:e63996fd7d3e 2298 *
khaledelmadawi 0:e63996fd7d3e 2299 * This base Sample Rate in turn is determined by SMPLRT_DIV (register * 25)
khaledelmadawi 0:e63996fd7d3e 2300 * and DLPF_CFG (register 26).
khaledelmadawi 0:e63996fd7d3e 2301 *
khaledelmadawi 0:e63996fd7d3e 2302 * For further information regarding I2C_MST_DLY, please refer to register 52.
khaledelmadawi 0:e63996fd7d3e 2303 * For further information regarding the Sample Rate, please refer to register 25.
khaledelmadawi 0:e63996fd7d3e 2304 *
khaledelmadawi 0:e63996fd7d3e 2305 * @param num Slave number (0-4)
khaledelmadawi 0:e63996fd7d3e 2306 * @return Current slave delay enabled status.
khaledelmadawi 0:e63996fd7d3e 2307 * @see MPU6050_RA_I2C_MST_DELAY_CTRL
khaledelmadawi 0:e63996fd7d3e 2308 * @see MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT
khaledelmadawi 0:e63996fd7d3e 2309 */
khaledelmadawi 0:e63996fd7d3e 2310 bool MPU6050::getSlaveDelayEnabled(uint8_t num)
khaledelmadawi 0:e63996fd7d3e 2311 {
khaledelmadawi 0:e63996fd7d3e 2312 // MPU6050_DELAYCTRL_I2C_SLV4_DLY_EN_BIT is 4, SLV3 is 3, etc.
khaledelmadawi 0:e63996fd7d3e 2313 if (num > 4) return 0;
khaledelmadawi 0:e63996fd7d3e 2314 i2Cdev.readBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, num, buffer);
khaledelmadawi 0:e63996fd7d3e 2315 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2316 }
khaledelmadawi 0:e63996fd7d3e 2317 /** Set slave delay enabled status.
khaledelmadawi 0:e63996fd7d3e 2318 * @param num Slave number (0-4)
khaledelmadawi 0:e63996fd7d3e 2319 * @param enabled New slave delay enabled status.
khaledelmadawi 0:e63996fd7d3e 2320 * @see MPU6050_RA_I2C_MST_DELAY_CTRL
khaledelmadawi 0:e63996fd7d3e 2321 * @see MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT
khaledelmadawi 0:e63996fd7d3e 2322 */
khaledelmadawi 0:e63996fd7d3e 2323 void MPU6050::setSlaveDelayEnabled(uint8_t num, bool enabled)
khaledelmadawi 0:e63996fd7d3e 2324 {
khaledelmadawi 0:e63996fd7d3e 2325 i2Cdev.writeBit(devAddr, MPU6050_RA_I2C_MST_DELAY_CTRL, num, enabled);
khaledelmadawi 0:e63996fd7d3e 2326 }
khaledelmadawi 0:e63996fd7d3e 2327
khaledelmadawi 0:e63996fd7d3e 2328 // SIGNAL_PATH_RESET register
khaledelmadawi 0:e63996fd7d3e 2329
khaledelmadawi 0:e63996fd7d3e 2330 /** Reset gyroscope signal path.
khaledelmadawi 0:e63996fd7d3e 2331 * The reset will revert the signal path analog to digital converters and
khaledelmadawi 0:e63996fd7d3e 2332 * filters to their power up configurations.
khaledelmadawi 0:e63996fd7d3e 2333 * @see MPU6050_RA_SIGNAL_PATH_RESET
khaledelmadawi 0:e63996fd7d3e 2334 * @see MPU6050_PATHRESET_GYRO_RESET_BIT
khaledelmadawi 0:e63996fd7d3e 2335 */
khaledelmadawi 0:e63996fd7d3e 2336 void MPU6050::resetGyroscopePath()
khaledelmadawi 0:e63996fd7d3e 2337 {
khaledelmadawi 0:e63996fd7d3e 2338 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_GYRO_RESET_BIT, true);
khaledelmadawi 0:e63996fd7d3e 2339 }
khaledelmadawi 0:e63996fd7d3e 2340 /** Reset accelerometer signal path.
khaledelmadawi 0:e63996fd7d3e 2341 * The reset will revert the signal path analog to digital converters and
khaledelmadawi 0:e63996fd7d3e 2342 * filters to their power up configurations.
khaledelmadawi 0:e63996fd7d3e 2343 * @see MPU6050_RA_SIGNAL_PATH_RESET
khaledelmadawi 0:e63996fd7d3e 2344 * @see MPU6050_PATHRESET_ACCEL_RESET_BIT
khaledelmadawi 0:e63996fd7d3e 2345 */
khaledelmadawi 0:e63996fd7d3e 2346 void MPU6050::resetAccelerometerPath()
khaledelmadawi 0:e63996fd7d3e 2347 {
khaledelmadawi 0:e63996fd7d3e 2348 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_ACCEL_RESET_BIT, true);
khaledelmadawi 0:e63996fd7d3e 2349 }
khaledelmadawi 0:e63996fd7d3e 2350 /** Reset temperature sensor signal path.
khaledelmadawi 0:e63996fd7d3e 2351 * The reset will revert the signal path analog to digital converters and
khaledelmadawi 0:e63996fd7d3e 2352 * filters to their power up configurations.
khaledelmadawi 0:e63996fd7d3e 2353 * @see MPU6050_RA_SIGNAL_PATH_RESET
khaledelmadawi 0:e63996fd7d3e 2354 * @see MPU6050_PATHRESET_TEMP_RESET_BIT
khaledelmadawi 0:e63996fd7d3e 2355 */
khaledelmadawi 0:e63996fd7d3e 2356 void MPU6050::resetTemperaturePath()
khaledelmadawi 0:e63996fd7d3e 2357 {
khaledelmadawi 0:e63996fd7d3e 2358 i2Cdev.writeBit(devAddr, MPU6050_RA_SIGNAL_PATH_RESET, MPU6050_PATHRESET_TEMP_RESET_BIT, true);
khaledelmadawi 0:e63996fd7d3e 2359 }
khaledelmadawi 0:e63996fd7d3e 2360
khaledelmadawi 0:e63996fd7d3e 2361 // MOT_DETECT_CTRL register
khaledelmadawi 0:e63996fd7d3e 2362
khaledelmadawi 0:e63996fd7d3e 2363 /** Get accelerometer power-on delay.
khaledelmadawi 0:e63996fd7d3e 2364 * The accelerometer data path provides samples to the sensor registers, Motion
khaledelmadawi 0:e63996fd7d3e 2365 * detection, Zero Motion detection, and Free Fall detection modules. The
khaledelmadawi 0:e63996fd7d3e 2366 * signal path contains filters which must be flushed on wake-up with new
khaledelmadawi 0:e63996fd7d3e 2367 * samples before the detection modules begin operations. The default wake-up
khaledelmadawi 0:e63996fd7d3e 2368 * delay, of 4ms can be lengthened by up to 3ms. This additional delay is
khaledelmadawi 0:e63996fd7d3e 2369 * specified in ACCEL_ON_DELAY in units of 1 LSB = 1 ms. The user may select
khaledelmadawi 0:e63996fd7d3e 2370 * any value above zero unless instructed otherwise by InvenSense. Please refer
khaledelmadawi 0:e63996fd7d3e 2371 * to Section 8 of the MPU-6000/MPU-6050 Product Specification document for
khaledelmadawi 0:e63996fd7d3e 2372 * further information regarding the detection modules.
khaledelmadawi 0:e63996fd7d3e 2373 * @return Current accelerometer power-on delay
khaledelmadawi 0:e63996fd7d3e 2374 * @see MPU6050_RA_MOT_DETECT_CTRL
khaledelmadawi 0:e63996fd7d3e 2375 * @see MPU6050_DETECT_ACCEL_ON_DELAY_BIT
khaledelmadawi 0:e63996fd7d3e 2376 */
khaledelmadawi 0:e63996fd7d3e 2377 uint8_t MPU6050::getAccelerometerPowerOnDelay()
khaledelmadawi 0:e63996fd7d3e 2378 {
khaledelmadawi 0:e63996fd7d3e 2379 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_ACCEL_ON_DELAY_BIT, MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 2380 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2381 }
khaledelmadawi 0:e63996fd7d3e 2382 /** Set accelerometer power-on delay.
khaledelmadawi 0:e63996fd7d3e 2383 * @param delay New accelerometer power-on delay (0-3)
khaledelmadawi 0:e63996fd7d3e 2384 * @see getAccelerometerPowerOnDelay()
khaledelmadawi 0:e63996fd7d3e 2385 * @see MPU6050_RA_MOT_DETECT_CTRL
khaledelmadawi 0:e63996fd7d3e 2386 * @see MPU6050_DETECT_ACCEL_ON_DELAY_BIT
khaledelmadawi 0:e63996fd7d3e 2387 */
khaledelmadawi 0:e63996fd7d3e 2388 void MPU6050::setAccelerometerPowerOnDelay(uint8_t delay)
khaledelmadawi 0:e63996fd7d3e 2389 {
khaledelmadawi 0:e63996fd7d3e 2390 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_ACCEL_ON_DELAY_BIT, MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH, delay);
khaledelmadawi 0:e63996fd7d3e 2391 }
khaledelmadawi 0:e63996fd7d3e 2392 /** Get Free Fall detection counter decrement configuration.
khaledelmadawi 0:e63996fd7d3e 2393 * Detection is registered by the Free Fall detection module after accelerometer
khaledelmadawi 0:e63996fd7d3e 2394 * measurements meet their respective threshold conditions over a specified
khaledelmadawi 0:e63996fd7d3e 2395 * number of samples. When the threshold conditions are met, the corresponding
khaledelmadawi 0:e63996fd7d3e 2396 * detection counter increments by 1. The user may control the rate at which the
khaledelmadawi 0:e63996fd7d3e 2397 * detection counter decrements when the threshold condition is not met by
khaledelmadawi 0:e63996fd7d3e 2398 * configuring FF_COUNT. The decrement rate can be set according to the
khaledelmadawi 0:e63996fd7d3e 2399 * following table:
khaledelmadawi 0:e63996fd7d3e 2400 *
khaledelmadawi 0:e63996fd7d3e 2401 * <pre>
khaledelmadawi 0:e63996fd7d3e 2402 * FF_COUNT | Counter Decrement
khaledelmadawi 0:e63996fd7d3e 2403 * ---------+------------------
khaledelmadawi 0:e63996fd7d3e 2404 * 0 | Reset
khaledelmadawi 0:e63996fd7d3e 2405 * 1 | 1
khaledelmadawi 0:e63996fd7d3e 2406 * 2 | 2
khaledelmadawi 0:e63996fd7d3e 2407 * 3 | 4
khaledelmadawi 0:e63996fd7d3e 2408 * </pre>
khaledelmadawi 0:e63996fd7d3e 2409 *
khaledelmadawi 0:e63996fd7d3e 2410 * When FF_COUNT is configured to 0 (reset), any non-qualifying sample will
khaledelmadawi 0:e63996fd7d3e 2411 * reset the counter to 0. For further information on Free Fall detection,
khaledelmadawi 0:e63996fd7d3e 2412 * please refer to Registers 29 to 32.
khaledelmadawi 0:e63996fd7d3e 2413 *
khaledelmadawi 0:e63996fd7d3e 2414 * @return Current decrement configuration
khaledelmadawi 0:e63996fd7d3e 2415 * @see MPU6050_RA_MOT_DETECT_CTRL
khaledelmadawi 0:e63996fd7d3e 2416 * @see MPU6050_DETECT_FF_COUNT_BIT
khaledelmadawi 0:e63996fd7d3e 2417 */
khaledelmadawi 0:e63996fd7d3e 2418 uint8_t MPU6050::getFreefallDetectionCounterDecrement()
khaledelmadawi 0:e63996fd7d3e 2419 {
khaledelmadawi 0:e63996fd7d3e 2420 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_FF_COUNT_BIT, MPU6050_DETECT_FF_COUNT_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 2421 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2422 }
khaledelmadawi 0:e63996fd7d3e 2423 /** Set Free Fall detection counter decrement configuration.
khaledelmadawi 0:e63996fd7d3e 2424 * @param decrement New decrement configuration value
khaledelmadawi 0:e63996fd7d3e 2425 * @see getFreefallDetectionCounterDecrement()
khaledelmadawi 0:e63996fd7d3e 2426 * @see MPU6050_RA_MOT_DETECT_CTRL
khaledelmadawi 0:e63996fd7d3e 2427 * @see MPU6050_DETECT_FF_COUNT_BIT
khaledelmadawi 0:e63996fd7d3e 2428 */
khaledelmadawi 0:e63996fd7d3e 2429 void MPU6050::setFreefallDetectionCounterDecrement(uint8_t decrement)
khaledelmadawi 0:e63996fd7d3e 2430 {
khaledelmadawi 0:e63996fd7d3e 2431 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_FF_COUNT_BIT, MPU6050_DETECT_FF_COUNT_LENGTH, decrement);
khaledelmadawi 0:e63996fd7d3e 2432 }
khaledelmadawi 0:e63996fd7d3e 2433 /** Get Motion detection counter decrement configuration.
khaledelmadawi 0:e63996fd7d3e 2434 * Detection is registered by the Motion detection module after accelerometer
khaledelmadawi 0:e63996fd7d3e 2435 * measurements meet their respective threshold conditions over a specified
khaledelmadawi 0:e63996fd7d3e 2436 * number of samples. When the threshold conditions are met, the corresponding
khaledelmadawi 0:e63996fd7d3e 2437 * detection counter increments by 1. The user may control the rate at which the
khaledelmadawi 0:e63996fd7d3e 2438 * detection counter decrements when the threshold condition is not met by
khaledelmadawi 0:e63996fd7d3e 2439 * configuring MOT_COUNT. The decrement rate can be set according to the
khaledelmadawi 0:e63996fd7d3e 2440 * following table:
khaledelmadawi 0:e63996fd7d3e 2441 *
khaledelmadawi 0:e63996fd7d3e 2442 * <pre>
khaledelmadawi 0:e63996fd7d3e 2443 * MOT_COUNT | Counter Decrement
khaledelmadawi 0:e63996fd7d3e 2444 * ----------+------------------
khaledelmadawi 0:e63996fd7d3e 2445 * 0 | Reset
khaledelmadawi 0:e63996fd7d3e 2446 * 1 | 1
khaledelmadawi 0:e63996fd7d3e 2447 * 2 | 2
khaledelmadawi 0:e63996fd7d3e 2448 * 3 | 4
khaledelmadawi 0:e63996fd7d3e 2449 * </pre>
khaledelmadawi 0:e63996fd7d3e 2450 *
khaledelmadawi 0:e63996fd7d3e 2451 * When MOT_COUNT is configured to 0 (reset), any non-qualifying sample will
khaledelmadawi 0:e63996fd7d3e 2452 * reset the counter to 0. For further information on Motion detection,
khaledelmadawi 0:e63996fd7d3e 2453 * please refer to Registers 29 to 32.
khaledelmadawi 0:e63996fd7d3e 2454 *
khaledelmadawi 0:e63996fd7d3e 2455 */
khaledelmadawi 0:e63996fd7d3e 2456 uint8_t MPU6050::getMotionDetectionCounterDecrement()
khaledelmadawi 0:e63996fd7d3e 2457 {
khaledelmadawi 0:e63996fd7d3e 2458 i2Cdev.readBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_MOT_COUNT_BIT, MPU6050_DETECT_MOT_COUNT_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 2459 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2460 }
khaledelmadawi 0:e63996fd7d3e 2461 /** Set Motion detection counter decrement configuration.
khaledelmadawi 0:e63996fd7d3e 2462 * @param decrement New decrement configuration value
khaledelmadawi 0:e63996fd7d3e 2463 * @see getMotionDetectionCounterDecrement()
khaledelmadawi 0:e63996fd7d3e 2464 * @see MPU6050_RA_MOT_DETECT_CTRL
khaledelmadawi 0:e63996fd7d3e 2465 * @see MPU6050_DETECT_MOT_COUNT_BIT
khaledelmadawi 0:e63996fd7d3e 2466 */
khaledelmadawi 0:e63996fd7d3e 2467 void MPU6050::setMotionDetectionCounterDecrement(uint8_t decrement)
khaledelmadawi 0:e63996fd7d3e 2468 {
khaledelmadawi 0:e63996fd7d3e 2469 i2Cdev.writeBits(devAddr, MPU6050_RA_MOT_DETECT_CTRL, MPU6050_DETECT_MOT_COUNT_BIT, MPU6050_DETECT_MOT_COUNT_LENGTH, decrement);
khaledelmadawi 0:e63996fd7d3e 2470 }
khaledelmadawi 0:e63996fd7d3e 2471
khaledelmadawi 0:e63996fd7d3e 2472 // USER_CTRL register
khaledelmadawi 0:e63996fd7d3e 2473
khaledelmadawi 0:e63996fd7d3e 2474 /** Get FIFO enabled status.
khaledelmadawi 0:e63996fd7d3e 2475 * When this bit is set to 0, the FIFO buffer is disabled. The FIFO buffer
khaledelmadawi 0:e63996fd7d3e 2476 * cannot be written to or read from while disabled. The FIFO buffer's state
khaledelmadawi 0:e63996fd7d3e 2477 * does not change unless the MPU-60X0 is power cycled.
khaledelmadawi 0:e63996fd7d3e 2478 * @return Current FIFO enabled status
khaledelmadawi 0:e63996fd7d3e 2479 * @see MPU6050_RA_USER_CTRL
khaledelmadawi 0:e63996fd7d3e 2480 * @see MPU6050_USERCTRL_FIFO_EN_BIT
khaledelmadawi 0:e63996fd7d3e 2481 */
khaledelmadawi 0:e63996fd7d3e 2482 bool MPU6050::getFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 2483 {
khaledelmadawi 0:e63996fd7d3e 2484 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2485 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2486 }
khaledelmadawi 0:e63996fd7d3e 2487 /** Set FIFO enabled status.
khaledelmadawi 0:e63996fd7d3e 2488 * @param enabled New FIFO enabled status
khaledelmadawi 0:e63996fd7d3e 2489 * @see getFIFOEnabled()
khaledelmadawi 0:e63996fd7d3e 2490 * @see MPU6050_RA_USER_CTRL
khaledelmadawi 0:e63996fd7d3e 2491 * @see MPU6050_USERCTRL_FIFO_EN_BIT
khaledelmadawi 0:e63996fd7d3e 2492 */
khaledelmadawi 0:e63996fd7d3e 2493 void MPU6050::setFIFOEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2494 {
khaledelmadawi 0:e63996fd7d3e 2495 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2496 }
khaledelmadawi 0:e63996fd7d3e 2497 /** Get I2C Master Mode enabled status.
khaledelmadawi 0:e63996fd7d3e 2498 * When this mode is enabled, the MPU-60X0 acts as the I2C Master to the
khaledelmadawi 0:e63996fd7d3e 2499 * external sensor slave devices on the auxiliary I2C bus. When this bit is
khaledelmadawi 0:e63996fd7d3e 2500 * cleared to 0, the auxiliary I2C bus lines (AUX_DA and AUX_CL) are logically
khaledelmadawi 0:e63996fd7d3e 2501 * driven by the primary I2C bus (SDA and SCL). This is a precondition to
khaledelmadawi 0:e63996fd7d3e 2502 * enabling Bypass Mode. For further information regarding Bypass Mode, please
khaledelmadawi 0:e63996fd7d3e 2503 * refer to Register 55.
khaledelmadawi 0:e63996fd7d3e 2504 * @return Current I2C Master Mode enabled status
khaledelmadawi 0:e63996fd7d3e 2505 * @see MPU6050_RA_USER_CTRL
khaledelmadawi 0:e63996fd7d3e 2506 * @see MPU6050_USERCTRL_I2C_MST_EN_BIT
khaledelmadawi 0:e63996fd7d3e 2507 */
khaledelmadawi 0:e63996fd7d3e 2508 bool MPU6050::getI2CMasterModeEnabled()
khaledelmadawi 0:e63996fd7d3e 2509 {
khaledelmadawi 0:e63996fd7d3e 2510 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2511 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2512 }
khaledelmadawi 0:e63996fd7d3e 2513 /** Set I2C Master Mode enabled status.
khaledelmadawi 0:e63996fd7d3e 2514 * @param enabled New I2C Master Mode enabled status
khaledelmadawi 0:e63996fd7d3e 2515 * @see getI2CMasterModeEnabled()
khaledelmadawi 0:e63996fd7d3e 2516 * @see MPU6050_RA_USER_CTRL
khaledelmadawi 0:e63996fd7d3e 2517 * @see MPU6050_USERCTRL_I2C_MST_EN_BIT
khaledelmadawi 0:e63996fd7d3e 2518 */
khaledelmadawi 0:e63996fd7d3e 2519 void MPU6050::setI2CMasterModeEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2520 {
khaledelmadawi 0:e63996fd7d3e 2521 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2522 }
khaledelmadawi 0:e63996fd7d3e 2523 /** Switch from I2C to SPI mode (MPU-6000 only)
khaledelmadawi 0:e63996fd7d3e 2524 * If this is set, the primary SPI interface will be enabled in place of the
khaledelmadawi 0:e63996fd7d3e 2525 * disabled primary I2C interface.
khaledelmadawi 0:e63996fd7d3e 2526 */
khaledelmadawi 0:e63996fd7d3e 2527 void MPU6050::switchSPIEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2528 {
khaledelmadawi 0:e63996fd7d3e 2529 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_IF_DIS_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2530 }
khaledelmadawi 0:e63996fd7d3e 2531 /** Reset the FIFO.
khaledelmadawi 0:e63996fd7d3e 2532 * This bit resets the FIFO buffer when set to 1 while FIFO_EN equals 0. This
khaledelmadawi 0:e63996fd7d3e 2533 * bit automatically clears to 0 after the reset has been triggered.
khaledelmadawi 0:e63996fd7d3e 2534 * @see MPU6050_RA_USER_CTRL
khaledelmadawi 0:e63996fd7d3e 2535 * @see MPU6050_USERCTRL_FIFO_RESET_BIT
khaledelmadawi 0:e63996fd7d3e 2536 */
khaledelmadawi 0:e63996fd7d3e 2537 void MPU6050::resetFIFO()
khaledelmadawi 0:e63996fd7d3e 2538 {
khaledelmadawi 0:e63996fd7d3e 2539 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_FIFO_RESET_BIT, true);
khaledelmadawi 0:e63996fd7d3e 2540 }
khaledelmadawi 0:e63996fd7d3e 2541 /** Reset the I2C Master.
khaledelmadawi 0:e63996fd7d3e 2542 * This bit resets the I2C Master when set to 1 while I2C_MST_EN equals 0.
khaledelmadawi 0:e63996fd7d3e 2543 * This bit automatically clears to 0 after the reset has been triggered.
khaledelmadawi 0:e63996fd7d3e 2544 * @see MPU6050_RA_USER_CTRL
khaledelmadawi 0:e63996fd7d3e 2545 * @see MPU6050_USERCTRL_I2C_MST_RESET_BIT
khaledelmadawi 0:e63996fd7d3e 2546 */
khaledelmadawi 0:e63996fd7d3e 2547 void MPU6050::resetI2CMaster()
khaledelmadawi 0:e63996fd7d3e 2548 {
khaledelmadawi 0:e63996fd7d3e 2549 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_I2C_MST_RESET_BIT, true);
khaledelmadawi 0:e63996fd7d3e 2550 }
khaledelmadawi 0:e63996fd7d3e 2551 /** Reset all sensor registers and signal paths.
khaledelmadawi 0:e63996fd7d3e 2552 * When set to 1, this bit resets the signal paths for all sensors (gyroscopes,
khaledelmadawi 0:e63996fd7d3e 2553 * accelerometers, and temperature sensor). This operation will also clear the
khaledelmadawi 0:e63996fd7d3e 2554 * sensor registers. This bit automatically clears to 0 after the reset has been
khaledelmadawi 0:e63996fd7d3e 2555 * triggered.
khaledelmadawi 0:e63996fd7d3e 2556 *
khaledelmadawi 0:e63996fd7d3e 2557 * When resetting only the signal path (and not the sensor registers), please
khaledelmadawi 0:e63996fd7d3e 2558 * use Register 104, SIGNAL_PATH_RESET.
khaledelmadawi 0:e63996fd7d3e 2559 *
khaledelmadawi 0:e63996fd7d3e 2560 * @see MPU6050_RA_USER_CTRL
khaledelmadawi 0:e63996fd7d3e 2561 * @see MPU6050_USERCTRL_SIG_COND_RESET_BIT
khaledelmadawi 0:e63996fd7d3e 2562 */
khaledelmadawi 0:e63996fd7d3e 2563 void MPU6050::resetSensors()
khaledelmadawi 0:e63996fd7d3e 2564 {
khaledelmadawi 0:e63996fd7d3e 2565 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_SIG_COND_RESET_BIT, true);
khaledelmadawi 0:e63996fd7d3e 2566 }
khaledelmadawi 0:e63996fd7d3e 2567
khaledelmadawi 0:e63996fd7d3e 2568 // PWR_MGMT_1 register
khaledelmadawi 0:e63996fd7d3e 2569
khaledelmadawi 0:e63996fd7d3e 2570 /** Trigger a full device reset.
khaledelmadawi 0:e63996fd7d3e 2571 * A small delay of ~50ms may be desirable after triggering a reset.
khaledelmadawi 0:e63996fd7d3e 2572 * @see MPU6050_RA_PWR_MGMT_1
khaledelmadawi 0:e63996fd7d3e 2573 * @see MPU6050_PWR1_DEVICE_RESET_BIT
khaledelmadawi 0:e63996fd7d3e 2574 */
khaledelmadawi 0:e63996fd7d3e 2575 void MPU6050::reset()
khaledelmadawi 0:e63996fd7d3e 2576 {
khaledelmadawi 0:e63996fd7d3e 2577 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_DEVICE_RESET_BIT, true);
khaledelmadawi 0:e63996fd7d3e 2578 }
khaledelmadawi 0:e63996fd7d3e 2579 /** Get sleep mode status.
khaledelmadawi 0:e63996fd7d3e 2580 * Setting the SLEEP bit in the register puts the device into very low power
khaledelmadawi 0:e63996fd7d3e 2581 * sleep mode. In this mode, only the serial interface and internal registers
khaledelmadawi 0:e63996fd7d3e 2582 * remain active, allowing for a very low standby current. Clearing this bit
khaledelmadawi 0:e63996fd7d3e 2583 * puts the device back into normal mode. To save power, the individual standby
khaledelmadawi 0:e63996fd7d3e 2584 * selections for each of the gyros should be used if any gyro axis is not used
khaledelmadawi 0:e63996fd7d3e 2585 * by the application.
khaledelmadawi 0:e63996fd7d3e 2586 * @return Current sleep mode enabled status
khaledelmadawi 0:e63996fd7d3e 2587 * @see MPU6050_RA_PWR_MGMT_1
khaledelmadawi 0:e63996fd7d3e 2588 * @see MPU6050_PWR1_SLEEP_BIT
khaledelmadawi 0:e63996fd7d3e 2589 */
khaledelmadawi 0:e63996fd7d3e 2590 bool MPU6050::getSleepEnabled()
khaledelmadawi 0:e63996fd7d3e 2591 {
khaledelmadawi 0:e63996fd7d3e 2592 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2593 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2594 }
khaledelmadawi 0:e63996fd7d3e 2595 /** Set sleep mode status.
khaledelmadawi 0:e63996fd7d3e 2596 * @param enabled New sleep mode enabled status
khaledelmadawi 0:e63996fd7d3e 2597 * @see getSleepEnabled()
khaledelmadawi 0:e63996fd7d3e 2598 * @see MPU6050_RA_PWR_MGMT_1
khaledelmadawi 0:e63996fd7d3e 2599 * @see MPU6050_PWR1_SLEEP_BIT
khaledelmadawi 0:e63996fd7d3e 2600 */
khaledelmadawi 0:e63996fd7d3e 2601 void MPU6050::setSleepEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2602 {
khaledelmadawi 0:e63996fd7d3e 2603 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2604 }
khaledelmadawi 0:e63996fd7d3e 2605 /** Get wake cycle enabled status.
khaledelmadawi 0:e63996fd7d3e 2606 * When this bit is set to 1 and SLEEP is disabled, the MPU-60X0 will cycle
khaledelmadawi 0:e63996fd7d3e 2607 * between sleep mode and waking up to take a single sample of data from active
khaledelmadawi 0:e63996fd7d3e 2608 * sensors at a rate determined by LP_WAKE_CTRL (register 108).
khaledelmadawi 0:e63996fd7d3e 2609 * @return Current sleep mode enabled status
khaledelmadawi 0:e63996fd7d3e 2610 * @see MPU6050_RA_PWR_MGMT_1
khaledelmadawi 0:e63996fd7d3e 2611 * @see MPU6050_PWR1_CYCLE_BIT
khaledelmadawi 0:e63996fd7d3e 2612 */
khaledelmadawi 0:e63996fd7d3e 2613 bool MPU6050::getWakeCycleEnabled()
khaledelmadawi 0:e63996fd7d3e 2614 {
khaledelmadawi 0:e63996fd7d3e 2615 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CYCLE_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2616 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2617 }
khaledelmadawi 0:e63996fd7d3e 2618 /** Set wake cycle enabled status.
khaledelmadawi 0:e63996fd7d3e 2619 * @param enabled New sleep mode enabled status
khaledelmadawi 0:e63996fd7d3e 2620 * @see getWakeCycleEnabled()
khaledelmadawi 0:e63996fd7d3e 2621 * @see MPU6050_RA_PWR_MGMT_1
khaledelmadawi 0:e63996fd7d3e 2622 * @see MPU6050_PWR1_CYCLE_BIT
khaledelmadawi 0:e63996fd7d3e 2623 */
khaledelmadawi 0:e63996fd7d3e 2624 void MPU6050::setWakeCycleEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2625 {
khaledelmadawi 0:e63996fd7d3e 2626 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CYCLE_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2627 }
khaledelmadawi 0:e63996fd7d3e 2628 /** Get temperature sensor enabled status.
khaledelmadawi 0:e63996fd7d3e 2629 * Control the usage of the internal temperature sensor.
khaledelmadawi 0:e63996fd7d3e 2630 *
khaledelmadawi 0:e63996fd7d3e 2631 * Note: this register stores the *disabled* value, but for consistency with the
khaledelmadawi 0:e63996fd7d3e 2632 * rest of the code, the function is named and used with standard true/false
khaledelmadawi 0:e63996fd7d3e 2633 * values to indicate whether the sensor is enabled or disabled, respectively.
khaledelmadawi 0:e63996fd7d3e 2634 *
khaledelmadawi 0:e63996fd7d3e 2635 * @return Current temperature sensor enabled status
khaledelmadawi 0:e63996fd7d3e 2636 * @see MPU6050_RA_PWR_MGMT_1
khaledelmadawi 0:e63996fd7d3e 2637 * @see MPU6050_PWR1_TEMP_DIS_BIT
khaledelmadawi 0:e63996fd7d3e 2638 */
khaledelmadawi 0:e63996fd7d3e 2639 bool MPU6050::getTempSensorEnabled()
khaledelmadawi 0:e63996fd7d3e 2640 {
khaledelmadawi 0:e63996fd7d3e 2641 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2642 return buffer[0] == 0; // 1 is actually disabled here
khaledelmadawi 0:e63996fd7d3e 2643 }
khaledelmadawi 0:e63996fd7d3e 2644 /** Set temperature sensor enabled status.
khaledelmadawi 0:e63996fd7d3e 2645 * Note: this register stores the *disabled* value, but for consistency with the
khaledelmadawi 0:e63996fd7d3e 2646 * rest of the code, the function is named and used with standard true/false
khaledelmadawi 0:e63996fd7d3e 2647 * values to indicate whether the sensor is enabled or disabled, respectively.
khaledelmadawi 0:e63996fd7d3e 2648 *
khaledelmadawi 0:e63996fd7d3e 2649 * @param enabled New temperature sensor enabled status
khaledelmadawi 0:e63996fd7d3e 2650 * @see getTempSensorEnabled()
khaledelmadawi 0:e63996fd7d3e 2651 * @see MPU6050_RA_PWR_MGMT_1
khaledelmadawi 0:e63996fd7d3e 2652 * @see MPU6050_PWR1_TEMP_DIS_BIT
khaledelmadawi 0:e63996fd7d3e 2653 */
khaledelmadawi 0:e63996fd7d3e 2654 void MPU6050::setTempSensorEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2655 {
khaledelmadawi 0:e63996fd7d3e 2656 // 1 is actually disabled here
khaledelmadawi 0:e63996fd7d3e 2657 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, !enabled);
khaledelmadawi 0:e63996fd7d3e 2658 }
khaledelmadawi 0:e63996fd7d3e 2659 /** Get clock source setting.
khaledelmadawi 0:e63996fd7d3e 2660 * @return Current clock source setting
khaledelmadawi 0:e63996fd7d3e 2661 * @see MPU6050_RA_PWR_MGMT_1
khaledelmadawi 0:e63996fd7d3e 2662 * @see MPU6050_PWR1_CLKSEL_BIT
khaledelmadawi 0:e63996fd7d3e 2663 * @see MPU6050_PWR1_CLKSEL_LENGTH
khaledelmadawi 0:e63996fd7d3e 2664 */
khaledelmadawi 0:e63996fd7d3e 2665 uint8_t MPU6050::getClockSource()
khaledelmadawi 0:e63996fd7d3e 2666 {
khaledelmadawi 0:e63996fd7d3e 2667 i2Cdev.readBits(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT, MPU6050_PWR1_CLKSEL_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 2668 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2669 }
khaledelmadawi 0:e63996fd7d3e 2670 /** Set clock source setting.
khaledelmadawi 0:e63996fd7d3e 2671 * An internal 8MHz oscillator, gyroscope based clock, or external sources can
khaledelmadawi 0:e63996fd7d3e 2672 * be selected as the MPU-60X0 clock source. When the internal 8 MHz oscillator
khaledelmadawi 0:e63996fd7d3e 2673 * or an external source is chosen as the clock source, the MPU-60X0 can operate
khaledelmadawi 0:e63996fd7d3e 2674 * in low power modes with the gyroscopes disabled.
khaledelmadawi 0:e63996fd7d3e 2675 *
khaledelmadawi 0:e63996fd7d3e 2676 * Upon power up, the MPU-60X0 clock source defaults to the internal oscillator.
khaledelmadawi 0:e63996fd7d3e 2677 * However, it is highly recommended that the device be configured to use one of
khaledelmadawi 0:e63996fd7d3e 2678 * the gyroscopes (or an external clock source) as the clock reference for
khaledelmadawi 0:e63996fd7d3e 2679 * improved stability. The clock source can be selected according to the following table:
khaledelmadawi 0:e63996fd7d3e 2680 *
khaledelmadawi 0:e63996fd7d3e 2681 * <pre>
khaledelmadawi 0:e63996fd7d3e 2682 * CLK_SEL | Clock Source
khaledelmadawi 0:e63996fd7d3e 2683 * --------+--------------------------------------
khaledelmadawi 0:e63996fd7d3e 2684 * 0 | Internal oscillator
khaledelmadawi 0:e63996fd7d3e 2685 * 1 | PLL with X Gyro reference
khaledelmadawi 0:e63996fd7d3e 2686 * 2 | PLL with Y Gyro reference
khaledelmadawi 0:e63996fd7d3e 2687 * 3 | PLL with Z Gyro reference
khaledelmadawi 0:e63996fd7d3e 2688 * 4 | PLL with external 32.768kHz reference
khaledelmadawi 0:e63996fd7d3e 2689 * 5 | PLL with external 19.2MHz reference
khaledelmadawi 0:e63996fd7d3e 2690 * 6 | Reserved
khaledelmadawi 0:e63996fd7d3e 2691 * 7 | Stops the clock and keeps the timing generator in reset
khaledelmadawi 0:e63996fd7d3e 2692 * </pre>
khaledelmadawi 0:e63996fd7d3e 2693 *
khaledelmadawi 0:e63996fd7d3e 2694 * @param source New clock source setting
khaledelmadawi 0:e63996fd7d3e 2695 * @see getClockSource()
khaledelmadawi 0:e63996fd7d3e 2696 * @see MPU6050_RA_PWR_MGMT_1
khaledelmadawi 0:e63996fd7d3e 2697 * @see MPU6050_PWR1_CLKSEL_BIT
khaledelmadawi 0:e63996fd7d3e 2698 * @see MPU6050_PWR1_CLKSEL_LENGTH
khaledelmadawi 0:e63996fd7d3e 2699 */
khaledelmadawi 0:e63996fd7d3e 2700 void MPU6050::setClockSource(uint8_t source)
khaledelmadawi 0:e63996fd7d3e 2701 {
khaledelmadawi 0:e63996fd7d3e 2702 i2Cdev.writeBits(devAddr, MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT, MPU6050_PWR1_CLKSEL_LENGTH, source);
khaledelmadawi 0:e63996fd7d3e 2703 }
khaledelmadawi 0:e63996fd7d3e 2704
khaledelmadawi 0:e63996fd7d3e 2705 // PWR_MGMT_2 register
khaledelmadawi 0:e63996fd7d3e 2706
khaledelmadawi 0:e63996fd7d3e 2707 /** Get wake frequency in Accel-Only Low Power Mode.
khaledelmadawi 0:e63996fd7d3e 2708 * The MPU-60X0 can be put into Accerlerometer Only Low Power Mode by setting
khaledelmadawi 0:e63996fd7d3e 2709 * PWRSEL to 1 in the Power Management 1 register (Register 107). In this mode,
khaledelmadawi 0:e63996fd7d3e 2710 * the device will power off all devices except for the primary I2C interface,
khaledelmadawi 0:e63996fd7d3e 2711 * waking only the accelerometer at fixed intervals to take a single
khaledelmadawi 0:e63996fd7d3e 2712 * measurement. The frequency of wake-ups can be configured with LP_WAKE_CTRL
khaledelmadawi 0:e63996fd7d3e 2713 * as shown below:
khaledelmadawi 0:e63996fd7d3e 2714 *
khaledelmadawi 0:e63996fd7d3e 2715 * <pre>
khaledelmadawi 0:e63996fd7d3e 2716 * LP_WAKE_CTRL | Wake-up Frequency
khaledelmadawi 0:e63996fd7d3e 2717 * -------------+------------------
khaledelmadawi 0:e63996fd7d3e 2718 * 0 | 1.25 Hz
khaledelmadawi 0:e63996fd7d3e 2719 * 1 | 2.5 Hz
khaledelmadawi 0:e63996fd7d3e 2720 * 2 | 5 Hz
khaledelmadawi 0:e63996fd7d3e 2721 * 3 | 10 Hz
khaledelmadawi 0:e63996fd7d3e 2722 * <pre>
khaledelmadawi 0:e63996fd7d3e 2723 *
khaledelmadawi 0:e63996fd7d3e 2724 * For further information regarding the MPU-60X0's power modes, please refer to
khaledelmadawi 0:e63996fd7d3e 2725 * Register 107.
khaledelmadawi 0:e63996fd7d3e 2726 *
khaledelmadawi 0:e63996fd7d3e 2727 * @return Current wake frequency
khaledelmadawi 0:e63996fd7d3e 2728 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2729 */
khaledelmadawi 0:e63996fd7d3e 2730 uint8_t MPU6050::getWakeFrequency()
khaledelmadawi 0:e63996fd7d3e 2731 {
khaledelmadawi 0:e63996fd7d3e 2732 i2Cdev.readBits(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_LP_WAKE_CTRL_BIT, MPU6050_PWR2_LP_WAKE_CTRL_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 2733 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2734 }
khaledelmadawi 0:e63996fd7d3e 2735 /** Set wake frequency in Accel-Only Low Power Mode.
khaledelmadawi 0:e63996fd7d3e 2736 * @param frequency New wake frequency
khaledelmadawi 0:e63996fd7d3e 2737 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2738 */
khaledelmadawi 0:e63996fd7d3e 2739 void MPU6050::setWakeFrequency(uint8_t frequency)
khaledelmadawi 0:e63996fd7d3e 2740 {
khaledelmadawi 0:e63996fd7d3e 2741 i2Cdev.writeBits(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_LP_WAKE_CTRL_BIT, MPU6050_PWR2_LP_WAKE_CTRL_LENGTH, frequency);
khaledelmadawi 0:e63996fd7d3e 2742 }
khaledelmadawi 0:e63996fd7d3e 2743
khaledelmadawi 0:e63996fd7d3e 2744 /** Get X-axis accelerometer standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2745 * If enabled, the X-axis will not gather or report data (or use power).
khaledelmadawi 0:e63996fd7d3e 2746 * @return Current X-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2747 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2748 * @see MPU6050_PWR2_STBY_XA_BIT
khaledelmadawi 0:e63996fd7d3e 2749 */
khaledelmadawi 0:e63996fd7d3e 2750 bool MPU6050::getStandbyXAccelEnabled()
khaledelmadawi 0:e63996fd7d3e 2751 {
khaledelmadawi 0:e63996fd7d3e 2752 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XA_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2753 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2754 }
khaledelmadawi 0:e63996fd7d3e 2755 /** Set X-axis accelerometer standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2756 * @param New X-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2757 * @see getStandbyXAccelEnabled()
khaledelmadawi 0:e63996fd7d3e 2758 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2759 * @see MPU6050_PWR2_STBY_XA_BIT
khaledelmadawi 0:e63996fd7d3e 2760 */
khaledelmadawi 0:e63996fd7d3e 2761 void MPU6050::setStandbyXAccelEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2762 {
khaledelmadawi 0:e63996fd7d3e 2763 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XA_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2764 }
khaledelmadawi 0:e63996fd7d3e 2765 /** Get Y-axis accelerometer standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2766 * If enabled, the Y-axis will not gather or report data (or use power).
khaledelmadawi 0:e63996fd7d3e 2767 * @return Current Y-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2768 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2769 * @see MPU6050_PWR2_STBY_YA_BIT
khaledelmadawi 0:e63996fd7d3e 2770 */
khaledelmadawi 0:e63996fd7d3e 2771 bool MPU6050::getStandbyYAccelEnabled()
khaledelmadawi 0:e63996fd7d3e 2772 {
khaledelmadawi 0:e63996fd7d3e 2773 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YA_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2774 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2775 }
khaledelmadawi 0:e63996fd7d3e 2776 /** Set Y-axis accelerometer standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2777 * @param New Y-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2778 * @see getStandbyYAccelEnabled()
khaledelmadawi 0:e63996fd7d3e 2779 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2780 * @see MPU6050_PWR2_STBY_YA_BIT
khaledelmadawi 0:e63996fd7d3e 2781 */
khaledelmadawi 0:e63996fd7d3e 2782 void MPU6050::setStandbyYAccelEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2783 {
khaledelmadawi 0:e63996fd7d3e 2784 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YA_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2785 }
khaledelmadawi 0:e63996fd7d3e 2786 /** Get Z-axis accelerometer standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2787 * If enabled, the Z-axis will not gather or report data (or use power).
khaledelmadawi 0:e63996fd7d3e 2788 * @return Current Z-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2789 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2790 * @see MPU6050_PWR2_STBY_ZA_BIT
khaledelmadawi 0:e63996fd7d3e 2791 */
khaledelmadawi 0:e63996fd7d3e 2792 bool MPU6050::getStandbyZAccelEnabled()
khaledelmadawi 0:e63996fd7d3e 2793 {
khaledelmadawi 0:e63996fd7d3e 2794 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZA_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2795 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2796 }
khaledelmadawi 0:e63996fd7d3e 2797 /** Set Z-axis accelerometer standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2798 * @param New Z-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2799 * @see getStandbyZAccelEnabled()
khaledelmadawi 0:e63996fd7d3e 2800 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2801 * @see MPU6050_PWR2_STBY_ZA_BIT
khaledelmadawi 0:e63996fd7d3e 2802 */
khaledelmadawi 0:e63996fd7d3e 2803 void MPU6050::setStandbyZAccelEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2804 {
khaledelmadawi 0:e63996fd7d3e 2805 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZA_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2806 }
khaledelmadawi 0:e63996fd7d3e 2807 /** Get X-axis gyroscope standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2808 * If enabled, the X-axis will not gather or report data (or use power).
khaledelmadawi 0:e63996fd7d3e 2809 * @return Current X-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2810 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2811 * @see MPU6050_PWR2_STBY_XG_BIT
khaledelmadawi 0:e63996fd7d3e 2812 */
khaledelmadawi 0:e63996fd7d3e 2813 bool MPU6050::getStandbyXGyroEnabled()
khaledelmadawi 0:e63996fd7d3e 2814 {
khaledelmadawi 0:e63996fd7d3e 2815 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XG_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2816 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2817 }
khaledelmadawi 0:e63996fd7d3e 2818 /** Set X-axis gyroscope standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2819 * @param New X-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2820 * @see getStandbyXGyroEnabled()
khaledelmadawi 0:e63996fd7d3e 2821 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2822 * @see MPU6050_PWR2_STBY_XG_BIT
khaledelmadawi 0:e63996fd7d3e 2823 */
khaledelmadawi 0:e63996fd7d3e 2824 void MPU6050::setStandbyXGyroEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2825 {
khaledelmadawi 0:e63996fd7d3e 2826 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_XG_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2827 }
khaledelmadawi 0:e63996fd7d3e 2828 /** Get Y-axis gyroscope standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2829 * If enabled, the Y-axis will not gather or report data (or use power).
khaledelmadawi 0:e63996fd7d3e 2830 * @return Current Y-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2831 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2832 * @see MPU6050_PWR2_STBY_YG_BIT
khaledelmadawi 0:e63996fd7d3e 2833 */
khaledelmadawi 0:e63996fd7d3e 2834 bool MPU6050::getStandbyYGyroEnabled()
khaledelmadawi 0:e63996fd7d3e 2835 {
khaledelmadawi 0:e63996fd7d3e 2836 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YG_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2837 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2838 }
khaledelmadawi 0:e63996fd7d3e 2839 /** Set Y-axis gyroscope standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2840 * @param New Y-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2841 * @see getStandbyYGyroEnabled()
khaledelmadawi 0:e63996fd7d3e 2842 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2843 * @see MPU6050_PWR2_STBY_YG_BIT
khaledelmadawi 0:e63996fd7d3e 2844 */
khaledelmadawi 0:e63996fd7d3e 2845 void MPU6050::setStandbyYGyroEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2846 {
khaledelmadawi 0:e63996fd7d3e 2847 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_YG_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2848 }
khaledelmadawi 0:e63996fd7d3e 2849 /** Get Z-axis gyroscope standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2850 * If enabled, the Z-axis will not gather or report data (or use power).
khaledelmadawi 0:e63996fd7d3e 2851 * @return Current Z-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2852 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2853 * @see MPU6050_PWR2_STBY_ZG_BIT
khaledelmadawi 0:e63996fd7d3e 2854 */
khaledelmadawi 0:e63996fd7d3e 2855 bool MPU6050::getStandbyZGyroEnabled()
khaledelmadawi 0:e63996fd7d3e 2856 {
khaledelmadawi 0:e63996fd7d3e 2857 i2Cdev.readBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZG_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2858 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2859 }
khaledelmadawi 0:e63996fd7d3e 2860 /** Set Z-axis gyroscope standby enabled status.
khaledelmadawi 0:e63996fd7d3e 2861 * @param New Z-axis standby enabled status
khaledelmadawi 0:e63996fd7d3e 2862 * @see getStandbyZGyroEnabled()
khaledelmadawi 0:e63996fd7d3e 2863 * @see MPU6050_RA_PWR_MGMT_2
khaledelmadawi 0:e63996fd7d3e 2864 * @see MPU6050_PWR2_STBY_ZG_BIT
khaledelmadawi 0:e63996fd7d3e 2865 */
khaledelmadawi 0:e63996fd7d3e 2866 void MPU6050::setStandbyZGyroEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2867 {
khaledelmadawi 0:e63996fd7d3e 2868 i2Cdev.writeBit(devAddr, MPU6050_RA_PWR_MGMT_2, MPU6050_PWR2_STBY_ZG_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2869 }
khaledelmadawi 0:e63996fd7d3e 2870
khaledelmadawi 0:e63996fd7d3e 2871 // FIFO_COUNT* registers
khaledelmadawi 0:e63996fd7d3e 2872
khaledelmadawi 0:e63996fd7d3e 2873 /** Get current FIFO buffer size.
khaledelmadawi 0:e63996fd7d3e 2874 * This value indicates the number of bytes stored in the FIFO buffer. This
khaledelmadawi 0:e63996fd7d3e 2875 * number is in turn the number of bytes that can be read from the FIFO buffer
khaledelmadawi 0:e63996fd7d3e 2876 * and it is directly proportional to the number of samples available given the
khaledelmadawi 0:e63996fd7d3e 2877 * set of sensor data bound to be stored in the FIFO (register 35 and 36).
khaledelmadawi 0:e63996fd7d3e 2878 * @return Current FIFO buffer size
khaledelmadawi 0:e63996fd7d3e 2879 */
khaledelmadawi 0:e63996fd7d3e 2880 uint16_t MPU6050::getFIFOCount()
khaledelmadawi 0:e63996fd7d3e 2881 {
khaledelmadawi 0:e63996fd7d3e 2882 i2Cdev.readBytes(devAddr, MPU6050_RA_FIFO_COUNTH, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 2883 return (((uint16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 2884 }
khaledelmadawi 0:e63996fd7d3e 2885
khaledelmadawi 0:e63996fd7d3e 2886 // FIFO_R_W register
khaledelmadawi 0:e63996fd7d3e 2887
khaledelmadawi 0:e63996fd7d3e 2888 /** Get byte from FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 2889 * This register is used to read and write data from the FIFO buffer. Data is
khaledelmadawi 0:e63996fd7d3e 2890 * written to the FIFO in order of register number (from lowest to highest). If
khaledelmadawi 0:e63996fd7d3e 2891 * all the FIFO enable flags (see below) are enabled and all External Sensor
khaledelmadawi 0:e63996fd7d3e 2892 * Data registers (Registers 73 to 96) are associated with a Slave device, the
khaledelmadawi 0:e63996fd7d3e 2893 * contents of registers 59 through 96 will be written in order at the Sample
khaledelmadawi 0:e63996fd7d3e 2894 * Rate.
khaledelmadawi 0:e63996fd7d3e 2895 *
khaledelmadawi 0:e63996fd7d3e 2896 * The contents of the sensor data registers (Registers 59 to 96) are written
khaledelmadawi 0:e63996fd7d3e 2897 * into the FIFO buffer when their corresponding FIFO enable flags are set to 1
khaledelmadawi 0:e63996fd7d3e 2898 * in FIFO_EN (Register 35). An additional flag for the sensor data registers
khaledelmadawi 0:e63996fd7d3e 2899 * associated with I2C Slave 3 can be found in I2C_MST_CTRL (Register 36).
khaledelmadawi 0:e63996fd7d3e 2900 *
khaledelmadawi 0:e63996fd7d3e 2901 * If the FIFO buffer has overflowed, the status bit FIFO_OFLOW_INT is
khaledelmadawi 0:e63996fd7d3e 2902 * automatically set to 1. This bit is located in INT_STATUS (Register 58).
khaledelmadawi 0:e63996fd7d3e 2903 * When the FIFO buffer has overflowed, the oldest data will be lost and new
khaledelmadawi 0:e63996fd7d3e 2904 * data will be written to the FIFO.
khaledelmadawi 0:e63996fd7d3e 2905 *
khaledelmadawi 0:e63996fd7d3e 2906 * If the FIFO buffer is empty, reading this register will return the last byte
khaledelmadawi 0:e63996fd7d3e 2907 * that was previously read from the FIFO until new data is available. The user
khaledelmadawi 0:e63996fd7d3e 2908 * should check FIFO_COUNT to ensure that the FIFO buffer is not read when
khaledelmadawi 0:e63996fd7d3e 2909 * empty.
khaledelmadawi 0:e63996fd7d3e 2910 *
khaledelmadawi 0:e63996fd7d3e 2911 * @return Byte from FIFO buffer
khaledelmadawi 0:e63996fd7d3e 2912 */
khaledelmadawi 0:e63996fd7d3e 2913 uint8_t MPU6050::getFIFOByte()
khaledelmadawi 0:e63996fd7d3e 2914 {
khaledelmadawi 0:e63996fd7d3e 2915 i2Cdev.readByte(devAddr, MPU6050_RA_FIFO_R_W, buffer);
khaledelmadawi 0:e63996fd7d3e 2916 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2917 }
khaledelmadawi 0:e63996fd7d3e 2918 void MPU6050::getFIFOBytes(uint8_t *data, uint8_t length)
khaledelmadawi 0:e63996fd7d3e 2919 {
khaledelmadawi 0:e63996fd7d3e 2920 i2Cdev.readBytes(devAddr, MPU6050_RA_FIFO_R_W, length, data);
khaledelmadawi 0:e63996fd7d3e 2921 }
khaledelmadawi 0:e63996fd7d3e 2922 /** Write byte to FIFO buffer.
khaledelmadawi 0:e63996fd7d3e 2923 * @see getFIFOByte()
khaledelmadawi 0:e63996fd7d3e 2924 * @see MPU6050_RA_FIFO_R_W
khaledelmadawi 0:e63996fd7d3e 2925 */
khaledelmadawi 0:e63996fd7d3e 2926 void MPU6050::setFIFOByte(uint8_t data)
khaledelmadawi 0:e63996fd7d3e 2927 {
khaledelmadawi 0:e63996fd7d3e 2928 i2Cdev.writeByte(devAddr, MPU6050_RA_FIFO_R_W, data);
khaledelmadawi 0:e63996fd7d3e 2929 }
khaledelmadawi 0:e63996fd7d3e 2930
khaledelmadawi 0:e63996fd7d3e 2931 // WHO_AM_I register
khaledelmadawi 0:e63996fd7d3e 2932
khaledelmadawi 0:e63996fd7d3e 2933 /** Get Device ID.
khaledelmadawi 0:e63996fd7d3e 2934 * This register is used to verify the identity of the device (0b110100, 0x34).
khaledelmadawi 0:e63996fd7d3e 2935 * @return Device ID (6 bits only! should be 0x34)
khaledelmadawi 0:e63996fd7d3e 2936 * @see MPU6050_RA_WHO_AM_I
khaledelmadawi 0:e63996fd7d3e 2937 * @see MPU6050_WHO_AM_I_BIT
khaledelmadawi 0:e63996fd7d3e 2938 * @see MPU6050_WHO_AM_I_LENGTH
khaledelmadawi 0:e63996fd7d3e 2939 */
khaledelmadawi 0:e63996fd7d3e 2940 uint8_t MPU6050::getDeviceID()
khaledelmadawi 0:e63996fd7d3e 2941 {
khaledelmadawi 0:e63996fd7d3e 2942 i2Cdev.readBits(devAddr, MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 2943 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2944 }
khaledelmadawi 0:e63996fd7d3e 2945 /** Set Device ID.
khaledelmadawi 0:e63996fd7d3e 2946 * Write a new ID into the WHO_AM_I register (no idea why this should ever be
khaledelmadawi 0:e63996fd7d3e 2947 * necessary though).
khaledelmadawi 0:e63996fd7d3e 2948 * @param id New device ID to set.
khaledelmadawi 0:e63996fd7d3e 2949 * @see getDeviceID()
khaledelmadawi 0:e63996fd7d3e 2950 * @see MPU6050_RA_WHO_AM_I
khaledelmadawi 0:e63996fd7d3e 2951 * @see MPU6050_WHO_AM_I_BIT
khaledelmadawi 0:e63996fd7d3e 2952 * @see MPU6050_WHO_AM_I_LENGTH
khaledelmadawi 0:e63996fd7d3e 2953 */
khaledelmadawi 0:e63996fd7d3e 2954 void MPU6050::setDeviceID(uint8_t id)
khaledelmadawi 0:e63996fd7d3e 2955 {
khaledelmadawi 0:e63996fd7d3e 2956 i2Cdev.writeBits(devAddr, MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, id);
khaledelmadawi 0:e63996fd7d3e 2957 }
khaledelmadawi 0:e63996fd7d3e 2958
khaledelmadawi 0:e63996fd7d3e 2959 // ======== UNDOCUMENTED/DMP REGISTERS/METHODS ========
khaledelmadawi 0:e63996fd7d3e 2960
khaledelmadawi 0:e63996fd7d3e 2961 // XG_OFFS_TC register
khaledelmadawi 0:e63996fd7d3e 2962
khaledelmadawi 0:e63996fd7d3e 2963 uint8_t MPU6050::getOTPBankValid()
khaledelmadawi 0:e63996fd7d3e 2964 {
khaledelmadawi 0:e63996fd7d3e 2965 i2Cdev.readBit(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OTP_BNK_VLD_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 2966 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2967 }
khaledelmadawi 0:e63996fd7d3e 2968 void MPU6050::setOTPBankValid(bool enabled)
khaledelmadawi 0:e63996fd7d3e 2969 {
khaledelmadawi 0:e63996fd7d3e 2970 i2Cdev.writeBit(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OTP_BNK_VLD_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 2971 }
khaledelmadawi 0:e63996fd7d3e 2972 int8_t MPU6050::getXGyroOffset()
khaledelmadawi 0:e63996fd7d3e 2973 {
khaledelmadawi 0:e63996fd7d3e 2974 i2Cdev.readBits(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 2975 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2976 }
khaledelmadawi 0:e63996fd7d3e 2977 void MPU6050::setXGyroOffset(int8_t offset)
khaledelmadawi 0:e63996fd7d3e 2978 {
khaledelmadawi 0:e63996fd7d3e 2979 i2Cdev.writeBits(devAddr, MPU6050_RA_XG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset);
khaledelmadawi 0:e63996fd7d3e 2980 }
khaledelmadawi 0:e63996fd7d3e 2981
khaledelmadawi 0:e63996fd7d3e 2982 // YG_OFFS_TC register
khaledelmadawi 0:e63996fd7d3e 2983
khaledelmadawi 0:e63996fd7d3e 2984 int8_t MPU6050::getYGyroOffset()
khaledelmadawi 0:e63996fd7d3e 2985 {
khaledelmadawi 0:e63996fd7d3e 2986 i2Cdev.readBits(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 2987 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 2988 }
khaledelmadawi 0:e63996fd7d3e 2989 void MPU6050::setYGyroOffset(int8_t offset)
khaledelmadawi 0:e63996fd7d3e 2990 {
khaledelmadawi 0:e63996fd7d3e 2991 i2Cdev.writeBits(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset);
khaledelmadawi 0:e63996fd7d3e 2992 }
khaledelmadawi 0:e63996fd7d3e 2993
khaledelmadawi 0:e63996fd7d3e 2994 // ZG_OFFS_TC register
khaledelmadawi 0:e63996fd7d3e 2995
khaledelmadawi 0:e63996fd7d3e 2996 int8_t MPU6050::getZGyroOffset()
khaledelmadawi 0:e63996fd7d3e 2997 {
khaledelmadawi 0:e63996fd7d3e 2998 i2Cdev.readBits(devAddr, MPU6050_RA_ZG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, buffer);
khaledelmadawi 0:e63996fd7d3e 2999 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3000 }
khaledelmadawi 0:e63996fd7d3e 3001 void MPU6050::setZGyroOffset(int8_t offset)
khaledelmadawi 0:e63996fd7d3e 3002 {
khaledelmadawi 0:e63996fd7d3e 3003 i2Cdev.writeBits(devAddr, MPU6050_RA_ZG_OFFS_TC, MPU6050_TC_OFFSET_BIT, MPU6050_TC_OFFSET_LENGTH, offset);
khaledelmadawi 0:e63996fd7d3e 3004 }
khaledelmadawi 0:e63996fd7d3e 3005
khaledelmadawi 0:e63996fd7d3e 3006 // X_FINE_GAIN register
khaledelmadawi 0:e63996fd7d3e 3007
khaledelmadawi 0:e63996fd7d3e 3008 int8_t MPU6050::getXFineGain()
khaledelmadawi 0:e63996fd7d3e 3009 {
khaledelmadawi 0:e63996fd7d3e 3010 i2Cdev.readByte(devAddr, MPU6050_RA_X_FINE_GAIN, buffer);
khaledelmadawi 0:e63996fd7d3e 3011 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3012 }
khaledelmadawi 0:e63996fd7d3e 3013 void MPU6050::setXFineGain(int8_t gain)
khaledelmadawi 0:e63996fd7d3e 3014 {
khaledelmadawi 0:e63996fd7d3e 3015 i2Cdev.writeByte(devAddr, MPU6050_RA_X_FINE_GAIN, gain);
khaledelmadawi 0:e63996fd7d3e 3016 }
khaledelmadawi 0:e63996fd7d3e 3017
khaledelmadawi 0:e63996fd7d3e 3018 // Y_FINE_GAIN register
khaledelmadawi 0:e63996fd7d3e 3019
khaledelmadawi 0:e63996fd7d3e 3020 int8_t MPU6050::getYFineGain()
khaledelmadawi 0:e63996fd7d3e 3021 {
khaledelmadawi 0:e63996fd7d3e 3022 i2Cdev.readByte(devAddr, MPU6050_RA_Y_FINE_GAIN, buffer);
khaledelmadawi 0:e63996fd7d3e 3023 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3024 }
khaledelmadawi 0:e63996fd7d3e 3025 void MPU6050::setYFineGain(int8_t gain)
khaledelmadawi 0:e63996fd7d3e 3026 {
khaledelmadawi 0:e63996fd7d3e 3027 i2Cdev.writeByte(devAddr, MPU6050_RA_Y_FINE_GAIN, gain);
khaledelmadawi 0:e63996fd7d3e 3028 }
khaledelmadawi 0:e63996fd7d3e 3029
khaledelmadawi 0:e63996fd7d3e 3030 // Z_FINE_GAIN register
khaledelmadawi 0:e63996fd7d3e 3031
khaledelmadawi 0:e63996fd7d3e 3032 int8_t MPU6050::getZFineGain()
khaledelmadawi 0:e63996fd7d3e 3033 {
khaledelmadawi 0:e63996fd7d3e 3034 i2Cdev.readByte(devAddr, MPU6050_RA_Z_FINE_GAIN, buffer);
khaledelmadawi 0:e63996fd7d3e 3035 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3036 }
khaledelmadawi 0:e63996fd7d3e 3037 void MPU6050::setZFineGain(int8_t gain)
khaledelmadawi 0:e63996fd7d3e 3038 {
khaledelmadawi 0:e63996fd7d3e 3039 i2Cdev.writeByte(devAddr, MPU6050_RA_Z_FINE_GAIN, gain);
khaledelmadawi 0:e63996fd7d3e 3040 }
khaledelmadawi 0:e63996fd7d3e 3041
khaledelmadawi 0:e63996fd7d3e 3042 // XA_OFFS_* registers
khaledelmadawi 0:e63996fd7d3e 3043
khaledelmadawi 0:e63996fd7d3e 3044 int16_t MPU6050::getXAccelOffset()
khaledelmadawi 0:e63996fd7d3e 3045 {
khaledelmadawi 0:e63996fd7d3e 3046 i2Cdev.readBytes(devAddr, MPU6050_RA_XA_OFFS_H, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 3047 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 3048 }
khaledelmadawi 0:e63996fd7d3e 3049 void MPU6050::setXAccelOffset(int16_t offset)
khaledelmadawi 0:e63996fd7d3e 3050 {
khaledelmadawi 0:e63996fd7d3e 3051 i2Cdev.writeWord(devAddr, MPU6050_RA_XA_OFFS_H, offset);
khaledelmadawi 0:e63996fd7d3e 3052 }
khaledelmadawi 0:e63996fd7d3e 3053
khaledelmadawi 0:e63996fd7d3e 3054 // YA_OFFS_* register
khaledelmadawi 0:e63996fd7d3e 3055
khaledelmadawi 0:e63996fd7d3e 3056 int16_t MPU6050::getYAccelOffset()
khaledelmadawi 0:e63996fd7d3e 3057 {
khaledelmadawi 0:e63996fd7d3e 3058 i2Cdev.readBytes(devAddr, MPU6050_RA_YA_OFFS_H, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 3059 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 3060 }
khaledelmadawi 0:e63996fd7d3e 3061 void MPU6050::setYAccelOffset(int16_t offset)
khaledelmadawi 0:e63996fd7d3e 3062 {
khaledelmadawi 0:e63996fd7d3e 3063 i2Cdev.writeWord(devAddr, MPU6050_RA_YA_OFFS_H, offset);
khaledelmadawi 0:e63996fd7d3e 3064 }
khaledelmadawi 0:e63996fd7d3e 3065
khaledelmadawi 0:e63996fd7d3e 3066 // ZA_OFFS_* register
khaledelmadawi 0:e63996fd7d3e 3067
khaledelmadawi 0:e63996fd7d3e 3068 int16_t MPU6050::getZAccelOffset()
khaledelmadawi 0:e63996fd7d3e 3069 {
khaledelmadawi 0:e63996fd7d3e 3070 i2Cdev.readBytes(devAddr, MPU6050_RA_ZA_OFFS_H, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 3071 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 3072 }
khaledelmadawi 0:e63996fd7d3e 3073 void MPU6050::setZAccelOffset(int16_t offset)
khaledelmadawi 0:e63996fd7d3e 3074 {
khaledelmadawi 0:e63996fd7d3e 3075 i2Cdev.writeWord(devAddr, MPU6050_RA_ZA_OFFS_H, offset);
khaledelmadawi 0:e63996fd7d3e 3076 }
khaledelmadawi 0:e63996fd7d3e 3077
khaledelmadawi 0:e63996fd7d3e 3078 // XG_OFFS_USR* registers
khaledelmadawi 0:e63996fd7d3e 3079
khaledelmadawi 0:e63996fd7d3e 3080 int16_t MPU6050::getXGyroOffsetUser()
khaledelmadawi 0:e63996fd7d3e 3081 {
khaledelmadawi 0:e63996fd7d3e 3082 i2Cdev.readBytes(devAddr, MPU6050_RA_XG_OFFS_USRH, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 3083 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 3084 }
khaledelmadawi 0:e63996fd7d3e 3085 void MPU6050::setXGyroOffsetUser(int16_t offset)
khaledelmadawi 0:e63996fd7d3e 3086 {
khaledelmadawi 0:e63996fd7d3e 3087 i2Cdev.writeWord(devAddr, MPU6050_RA_XG_OFFS_USRH, offset);
khaledelmadawi 0:e63996fd7d3e 3088 }
khaledelmadawi 0:e63996fd7d3e 3089
khaledelmadawi 0:e63996fd7d3e 3090 // YG_OFFS_USR* register
khaledelmadawi 0:e63996fd7d3e 3091
khaledelmadawi 0:e63996fd7d3e 3092 int16_t MPU6050::getYGyroOffsetUser()
khaledelmadawi 0:e63996fd7d3e 3093 {
khaledelmadawi 0:e63996fd7d3e 3094 i2Cdev.readBytes(devAddr, MPU6050_RA_YG_OFFS_USRH, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 3095 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 3096 }
khaledelmadawi 0:e63996fd7d3e 3097 void MPU6050::setYGyroOffsetUser(int16_t offset)
khaledelmadawi 0:e63996fd7d3e 3098 {
khaledelmadawi 0:e63996fd7d3e 3099 i2Cdev.writeWord(devAddr, MPU6050_RA_YG_OFFS_USRH, offset);
khaledelmadawi 0:e63996fd7d3e 3100 }
khaledelmadawi 0:e63996fd7d3e 3101
khaledelmadawi 0:e63996fd7d3e 3102 // ZG_OFFS_USR* register
khaledelmadawi 0:e63996fd7d3e 3103
khaledelmadawi 0:e63996fd7d3e 3104 int16_t MPU6050::getZGyroOffsetUser()
khaledelmadawi 0:e63996fd7d3e 3105 {
khaledelmadawi 0:e63996fd7d3e 3106 i2Cdev.readBytes(devAddr, MPU6050_RA_ZG_OFFS_USRH, 2, buffer);
khaledelmadawi 0:e63996fd7d3e 3107 return (((int16_t)buffer[0]) << 8) | buffer[1];
khaledelmadawi 0:e63996fd7d3e 3108 }
khaledelmadawi 0:e63996fd7d3e 3109 void MPU6050::setZGyroOffsetUser(int16_t offset)
khaledelmadawi 0:e63996fd7d3e 3110 {
khaledelmadawi 0:e63996fd7d3e 3111 i2Cdev.writeWord(devAddr, MPU6050_RA_ZG_OFFS_USRH, offset);
khaledelmadawi 0:e63996fd7d3e 3112 }
khaledelmadawi 0:e63996fd7d3e 3113
khaledelmadawi 0:e63996fd7d3e 3114 // INT_ENABLE register (DMP functions)
khaledelmadawi 0:e63996fd7d3e 3115
khaledelmadawi 0:e63996fd7d3e 3116 bool MPU6050::getIntPLLReadyEnabled()
khaledelmadawi 0:e63996fd7d3e 3117 {
khaledelmadawi 0:e63996fd7d3e 3118 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3119 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3120 }
khaledelmadawi 0:e63996fd7d3e 3121 void MPU6050::setIntPLLReadyEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 3122 {
khaledelmadawi 0:e63996fd7d3e 3123 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 3124 }
khaledelmadawi 0:e63996fd7d3e 3125 bool MPU6050::getIntDMPEnabled()
khaledelmadawi 0:e63996fd7d3e 3126 {
khaledelmadawi 0:e63996fd7d3e 3127 i2Cdev.readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DMP_INT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3128 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3129 }
khaledelmadawi 0:e63996fd7d3e 3130 void MPU6050::setIntDMPEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 3131 {
khaledelmadawi 0:e63996fd7d3e 3132 i2Cdev.writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DMP_INT_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 3133 }
khaledelmadawi 0:e63996fd7d3e 3134
khaledelmadawi 0:e63996fd7d3e 3135 // DMP_INT_STATUS
khaledelmadawi 0:e63996fd7d3e 3136
khaledelmadawi 0:e63996fd7d3e 3137 bool MPU6050::getDMPInt5Status()
khaledelmadawi 0:e63996fd7d3e 3138 {
khaledelmadawi 0:e63996fd7d3e 3139 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_5_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3140 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3141 }
khaledelmadawi 0:e63996fd7d3e 3142 bool MPU6050::getDMPInt4Status()
khaledelmadawi 0:e63996fd7d3e 3143 {
khaledelmadawi 0:e63996fd7d3e 3144 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_4_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3145 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3146 }
khaledelmadawi 0:e63996fd7d3e 3147 bool MPU6050::getDMPInt3Status()
khaledelmadawi 0:e63996fd7d3e 3148 {
khaledelmadawi 0:e63996fd7d3e 3149 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_3_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3150 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3151 }
khaledelmadawi 0:e63996fd7d3e 3152 bool MPU6050::getDMPInt2Status()
khaledelmadawi 0:e63996fd7d3e 3153 {
khaledelmadawi 0:e63996fd7d3e 3154 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_2_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3155 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3156 }
khaledelmadawi 0:e63996fd7d3e 3157 bool MPU6050::getDMPInt1Status()
khaledelmadawi 0:e63996fd7d3e 3158 {
khaledelmadawi 0:e63996fd7d3e 3159 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_1_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3160 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3161 }
khaledelmadawi 0:e63996fd7d3e 3162 bool MPU6050::getDMPInt0Status()
khaledelmadawi 0:e63996fd7d3e 3163 {
khaledelmadawi 0:e63996fd7d3e 3164 i2Cdev.readBit(devAddr, MPU6050_RA_DMP_INT_STATUS, MPU6050_DMPINT_0_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3165 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3166 }
khaledelmadawi 0:e63996fd7d3e 3167
khaledelmadawi 0:e63996fd7d3e 3168 // INT_STATUS register (DMP functions)
khaledelmadawi 0:e63996fd7d3e 3169
khaledelmadawi 0:e63996fd7d3e 3170 bool MPU6050::getIntPLLReadyStatus()
khaledelmadawi 0:e63996fd7d3e 3171 {
khaledelmadawi 0:e63996fd7d3e 3172 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_PLL_RDY_INT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3173 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3174 }
khaledelmadawi 0:e63996fd7d3e 3175 bool MPU6050::getIntDMPStatus()
khaledelmadawi 0:e63996fd7d3e 3176 {
khaledelmadawi 0:e63996fd7d3e 3177 i2Cdev.readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DMP_INT_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3178 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3179 }
khaledelmadawi 0:e63996fd7d3e 3180
khaledelmadawi 0:e63996fd7d3e 3181 // USER_CTRL register (DMP functions)
khaledelmadawi 0:e63996fd7d3e 3182
khaledelmadawi 0:e63996fd7d3e 3183 bool MPU6050::getDMPEnabled()
khaledelmadawi 0:e63996fd7d3e 3184 {
khaledelmadawi 0:e63996fd7d3e 3185 i2Cdev.readBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_EN_BIT, buffer);
khaledelmadawi 0:e63996fd7d3e 3186 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3187 }
khaledelmadawi 0:e63996fd7d3e 3188 void MPU6050::setDMPEnabled(bool enabled)
khaledelmadawi 0:e63996fd7d3e 3189 {
khaledelmadawi 0:e63996fd7d3e 3190 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_EN_BIT, enabled);
khaledelmadawi 0:e63996fd7d3e 3191 }
khaledelmadawi 0:e63996fd7d3e 3192 void MPU6050::resetDMP()
khaledelmadawi 0:e63996fd7d3e 3193 {
khaledelmadawi 0:e63996fd7d3e 3194 i2Cdev.writeBit(devAddr, MPU6050_RA_USER_CTRL, MPU6050_USERCTRL_DMP_RESET_BIT, true);
khaledelmadawi 0:e63996fd7d3e 3195 }
khaledelmadawi 0:e63996fd7d3e 3196
khaledelmadawi 0:e63996fd7d3e 3197 // BANK_SEL register
khaledelmadawi 0:e63996fd7d3e 3198
khaledelmadawi 0:e63996fd7d3e 3199 void MPU6050::setMemoryBank(uint8_t bank, bool prefetchEnabled, bool userBank)
khaledelmadawi 0:e63996fd7d3e 3200 {
khaledelmadawi 0:e63996fd7d3e 3201 bank &= 0x1F;
khaledelmadawi 0:e63996fd7d3e 3202 if (userBank) bank |= 0x20;
khaledelmadawi 0:e63996fd7d3e 3203 if (prefetchEnabled) bank |= 0x40;
khaledelmadawi 0:e63996fd7d3e 3204 i2Cdev.writeByte(devAddr, MPU6050_RA_BANK_SEL, bank);
khaledelmadawi 0:e63996fd7d3e 3205 }
khaledelmadawi 0:e63996fd7d3e 3206
khaledelmadawi 0:e63996fd7d3e 3207 // MEM_START_ADDR register
khaledelmadawi 0:e63996fd7d3e 3208
khaledelmadawi 0:e63996fd7d3e 3209 void MPU6050::setMemoryStartAddress(uint8_t address)
khaledelmadawi 0:e63996fd7d3e 3210 {
khaledelmadawi 0:e63996fd7d3e 3211 i2Cdev.writeByte(devAddr, MPU6050_RA_MEM_START_ADDR, address);
khaledelmadawi 0:e63996fd7d3e 3212 }
khaledelmadawi 0:e63996fd7d3e 3213
khaledelmadawi 0:e63996fd7d3e 3214 // MEM_R_W register
khaledelmadawi 0:e63996fd7d3e 3215
khaledelmadawi 0:e63996fd7d3e 3216 uint8_t MPU6050::readMemoryByte()
khaledelmadawi 0:e63996fd7d3e 3217 {
khaledelmadawi 0:e63996fd7d3e 3218 i2Cdev.readByte(devAddr, MPU6050_RA_MEM_R_W, buffer);
khaledelmadawi 0:e63996fd7d3e 3219 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3220 }
khaledelmadawi 0:e63996fd7d3e 3221 void MPU6050::writeMemoryByte(uint8_t data)
khaledelmadawi 0:e63996fd7d3e 3222 {
khaledelmadawi 0:e63996fd7d3e 3223 i2Cdev.writeByte(devAddr, MPU6050_RA_MEM_R_W, data);
khaledelmadawi 0:e63996fd7d3e 3224 }
khaledelmadawi 0:e63996fd7d3e 3225 void MPU6050::readMemoryBlock(uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address)
khaledelmadawi 0:e63996fd7d3e 3226 {
khaledelmadawi 0:e63996fd7d3e 3227 setMemoryBank(bank);
khaledelmadawi 0:e63996fd7d3e 3228 setMemoryStartAddress(address);
khaledelmadawi 0:e63996fd7d3e 3229 uint8_t chunkSize;
khaledelmadawi 0:e63996fd7d3e 3230 for (uint16_t i = 0; i < dataSize;) {
khaledelmadawi 0:e63996fd7d3e 3231 // determine correct chunk size according to bank position and data size
khaledelmadawi 0:e63996fd7d3e 3232 chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE;
khaledelmadawi 0:e63996fd7d3e 3233
khaledelmadawi 0:e63996fd7d3e 3234 // make sure we don't go past the data size
khaledelmadawi 0:e63996fd7d3e 3235 if (i + chunkSize > dataSize) chunkSize = dataSize - i;
khaledelmadawi 0:e63996fd7d3e 3236
khaledelmadawi 0:e63996fd7d3e 3237 // make sure this chunk doesn't go past the bank boundary (256 bytes)
khaledelmadawi 0:e63996fd7d3e 3238 if (chunkSize > 256 - address) chunkSize = 256 - address;
khaledelmadawi 0:e63996fd7d3e 3239
khaledelmadawi 0:e63996fd7d3e 3240 // read the chunk of data as specified
khaledelmadawi 0:e63996fd7d3e 3241 i2Cdev.readBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, data + i);
khaledelmadawi 0:e63996fd7d3e 3242
khaledelmadawi 0:e63996fd7d3e 3243 // increase byte index by [chunkSize]
khaledelmadawi 0:e63996fd7d3e 3244 i += chunkSize;
khaledelmadawi 0:e63996fd7d3e 3245
khaledelmadawi 0:e63996fd7d3e 3246 // uint8_t automatically wraps to 0 at 256
khaledelmadawi 0:e63996fd7d3e 3247 address += chunkSize;
khaledelmadawi 0:e63996fd7d3e 3248
khaledelmadawi 0:e63996fd7d3e 3249 // if we aren't done, update bank (if necessary) and address
khaledelmadawi 0:e63996fd7d3e 3250 if (i < dataSize) {
khaledelmadawi 0:e63996fd7d3e 3251 if (address == 0) bank++;
khaledelmadawi 0:e63996fd7d3e 3252 setMemoryBank(bank);
khaledelmadawi 0:e63996fd7d3e 3253 setMemoryStartAddress(address);
khaledelmadawi 0:e63996fd7d3e 3254 }
khaledelmadawi 0:e63996fd7d3e 3255 }
khaledelmadawi 0:e63996fd7d3e 3256 }
khaledelmadawi 0:e63996fd7d3e 3257 bool MPU6050::writeMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool verify, bool useProgMem)
khaledelmadawi 0:e63996fd7d3e 3258 {
khaledelmadawi 0:e63996fd7d3e 3259 setMemoryBank(bank);
khaledelmadawi 0:e63996fd7d3e 3260 setMemoryStartAddress(address);
khaledelmadawi 0:e63996fd7d3e 3261 uint8_t chunkSize;
khaledelmadawi 0:e63996fd7d3e 3262 uint8_t *verifyBuffer = NULL;
khaledelmadawi 0:e63996fd7d3e 3263 uint8_t *progBuffer = NULL;
khaledelmadawi 0:e63996fd7d3e 3264 uint16_t i;
khaledelmadawi 0:e63996fd7d3e 3265 uint8_t j;
khaledelmadawi 0:e63996fd7d3e 3266 if (verify) verifyBuffer = (uint8_t *)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE);
khaledelmadawi 0:e63996fd7d3e 3267 if (useProgMem) progBuffer = (uint8_t *)malloc(MPU6050_DMP_MEMORY_CHUNK_SIZE);
khaledelmadawi 0:e63996fd7d3e 3268 for (i = 0; i < dataSize;) {
khaledelmadawi 0:e63996fd7d3e 3269 // determine correct chunk size according to bank position and data size
khaledelmadawi 0:e63996fd7d3e 3270 chunkSize = MPU6050_DMP_MEMORY_CHUNK_SIZE;
khaledelmadawi 0:e63996fd7d3e 3271
khaledelmadawi 0:e63996fd7d3e 3272 // make sure we don't go past the data size
khaledelmadawi 0:e63996fd7d3e 3273 if (i + chunkSize > dataSize) chunkSize = dataSize - i;
khaledelmadawi 0:e63996fd7d3e 3274
khaledelmadawi 0:e63996fd7d3e 3275 // make sure this chunk doesn't go past the bank boundary (256 bytes)
khaledelmadawi 0:e63996fd7d3e 3276 if (chunkSize > 256 - address) chunkSize = 256 - address;
khaledelmadawi 0:e63996fd7d3e 3277
khaledelmadawi 0:e63996fd7d3e 3278 if (useProgMem) {
khaledelmadawi 0:e63996fd7d3e 3279 // write the chunk of data as specified
khaledelmadawi 0:e63996fd7d3e 3280 for (j = 0; j < chunkSize; j++) progBuffer[j] = pgm_read_byte(data + i + j);
khaledelmadawi 0:e63996fd7d3e 3281 } else {
khaledelmadawi 0:e63996fd7d3e 3282 // write the chunk of data as specified
khaledelmadawi 0:e63996fd7d3e 3283 progBuffer = (uint8_t *)data + i;
khaledelmadawi 0:e63996fd7d3e 3284 }
khaledelmadawi 0:e63996fd7d3e 3285
khaledelmadawi 0:e63996fd7d3e 3286 i2Cdev.writeBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, progBuffer);
khaledelmadawi 0:e63996fd7d3e 3287
khaledelmadawi 0:e63996fd7d3e 3288 // verify data if needed
khaledelmadawi 0:e63996fd7d3e 3289 if (verify && verifyBuffer) {
khaledelmadawi 0:e63996fd7d3e 3290 setMemoryBank(bank);
khaledelmadawi 0:e63996fd7d3e 3291 setMemoryStartAddress(address);
khaledelmadawi 0:e63996fd7d3e 3292 i2Cdev.readBytes(devAddr, MPU6050_RA_MEM_R_W, chunkSize, verifyBuffer);
khaledelmadawi 0:e63996fd7d3e 3293 if (memcmp(progBuffer, verifyBuffer, chunkSize) != 0) {
khaledelmadawi 0:e63996fd7d3e 3294 /*Serial.print("Block write verification error, bank ");
khaledelmadawi 0:e63996fd7d3e 3295 Serial.print(bank, DEC);
khaledelmadawi 0:e63996fd7d3e 3296 Serial.print(", address ");
khaledelmadawi 0:e63996fd7d3e 3297 Serial.print(address, DEC);
khaledelmadawi 0:e63996fd7d3e 3298 Serial.print("!\nExpected:");
khaledelmadawi 0:e63996fd7d3e 3299 for (j = 0; j < chunkSize; j++) {
khaledelmadawi 0:e63996fd7d3e 3300 Serial.print(" 0x");
khaledelmadawi 0:e63996fd7d3e 3301 if (progBuffer[j] < 16) Serial.print("0");
khaledelmadawi 0:e63996fd7d3e 3302 Serial.print(progBuffer[j], HEX);
khaledelmadawi 0:e63996fd7d3e 3303 }
khaledelmadawi 0:e63996fd7d3e 3304 Serial.print("\nReceived:");
khaledelmadawi 0:e63996fd7d3e 3305 for (uint8_t j = 0; j < chunkSize; j++) {
khaledelmadawi 0:e63996fd7d3e 3306 Serial.print(" 0x");
khaledelmadawi 0:e63996fd7d3e 3307 if (verifyBuffer[i + j] < 16) Serial.print("0");
khaledelmadawi 0:e63996fd7d3e 3308 Serial.print(verifyBuffer[i + j], HEX);
khaledelmadawi 0:e63996fd7d3e 3309 }
khaledelmadawi 0:e63996fd7d3e 3310 Serial.print("\n");*/
khaledelmadawi 0:e63996fd7d3e 3311 free(verifyBuffer);
khaledelmadawi 0:e63996fd7d3e 3312 if (useProgMem) free(progBuffer);
khaledelmadawi 0:e63996fd7d3e 3313 return false; // uh oh.
khaledelmadawi 0:e63996fd7d3e 3314 }
khaledelmadawi 0:e63996fd7d3e 3315 }
khaledelmadawi 0:e63996fd7d3e 3316
khaledelmadawi 0:e63996fd7d3e 3317 // increase byte index by [chunkSize]
khaledelmadawi 0:e63996fd7d3e 3318 i += chunkSize;
khaledelmadawi 0:e63996fd7d3e 3319
khaledelmadawi 0:e63996fd7d3e 3320 // uint8_t automatically wraps to 0 at 256
khaledelmadawi 0:e63996fd7d3e 3321 address += chunkSize;
khaledelmadawi 0:e63996fd7d3e 3322
khaledelmadawi 0:e63996fd7d3e 3323 // if we aren't done, update bank (if necessary) and address
khaledelmadawi 0:e63996fd7d3e 3324 if (i < dataSize) {
khaledelmadawi 0:e63996fd7d3e 3325 if (address == 0) bank++;
khaledelmadawi 0:e63996fd7d3e 3326 setMemoryBank(bank);
khaledelmadawi 0:e63996fd7d3e 3327 setMemoryStartAddress(address);
khaledelmadawi 0:e63996fd7d3e 3328 }
khaledelmadawi 0:e63996fd7d3e 3329 }
khaledelmadawi 0:e63996fd7d3e 3330 if (verify) free(verifyBuffer);
khaledelmadawi 0:e63996fd7d3e 3331 if (useProgMem) free(progBuffer);
khaledelmadawi 0:e63996fd7d3e 3332 return true;
khaledelmadawi 0:e63996fd7d3e 3333 }
khaledelmadawi 0:e63996fd7d3e 3334 bool MPU6050::writeProgMemoryBlock(const uint8_t *data, uint16_t dataSize, uint8_t bank, uint8_t address, bool verify)
khaledelmadawi 0:e63996fd7d3e 3335 {
khaledelmadawi 0:e63996fd7d3e 3336 return writeMemoryBlock(data, dataSize, bank, address, verify, true);
khaledelmadawi 0:e63996fd7d3e 3337 }
khaledelmadawi 0:e63996fd7d3e 3338 bool MPU6050::writeDMPConfigurationSet(const uint8_t *data, uint16_t dataSize, bool useProgMem)
khaledelmadawi 0:e63996fd7d3e 3339 {
khaledelmadawi 0:e63996fd7d3e 3340 uint8_t success, special;
khaledelmadawi 0:e63996fd7d3e 3341 uint8_t *progBuffer = NULL;
khaledelmadawi 0:e63996fd7d3e 3342 uint16_t i, j;
khaledelmadawi 0:e63996fd7d3e 3343 if (useProgMem) {
khaledelmadawi 0:e63996fd7d3e 3344 progBuffer = (uint8_t *)malloc(8); // assume 8-byte blocks, realloc later if necessary
khaledelmadawi 0:e63996fd7d3e 3345 }
khaledelmadawi 0:e63996fd7d3e 3346
khaledelmadawi 0:e63996fd7d3e 3347 // config set data is a long string of blocks with the following structure:
khaledelmadawi 0:e63996fd7d3e 3348 // [bank] [offset] [length] [byte[0], byte[1], ..., byte[length]]
khaledelmadawi 0:e63996fd7d3e 3349 uint8_t bank, offset, length;
khaledelmadawi 0:e63996fd7d3e 3350 for (i = 0; i < dataSize;) {
khaledelmadawi 0:e63996fd7d3e 3351 if (useProgMem) {
khaledelmadawi 0:e63996fd7d3e 3352 bank = pgm_read_byte(data + i++);
khaledelmadawi 0:e63996fd7d3e 3353 offset = pgm_read_byte(data + i++);
khaledelmadawi 0:e63996fd7d3e 3354 length = pgm_read_byte(data + i++);
khaledelmadawi 0:e63996fd7d3e 3355 } else {
khaledelmadawi 0:e63996fd7d3e 3356 bank = data[i++];
khaledelmadawi 0:e63996fd7d3e 3357 offset = data[i++];
khaledelmadawi 0:e63996fd7d3e 3358 length = data[i++];
khaledelmadawi 0:e63996fd7d3e 3359 }
khaledelmadawi 0:e63996fd7d3e 3360
khaledelmadawi 0:e63996fd7d3e 3361 // write data or perform special action
khaledelmadawi 0:e63996fd7d3e 3362 if (length > 0) {
khaledelmadawi 0:e63996fd7d3e 3363 // regular block of data to write
khaledelmadawi 0:e63996fd7d3e 3364 /*Serial.print("Writing config block to bank ");
khaledelmadawi 0:e63996fd7d3e 3365 Serial.print(bank);
khaledelmadawi 0:e63996fd7d3e 3366 Serial.print(", offset ");
khaledelmadawi 0:e63996fd7d3e 3367 Serial.print(offset);
khaledelmadawi 0:e63996fd7d3e 3368 Serial.print(", length=");
khaledelmadawi 0:e63996fd7d3e 3369 Serial.println(length);*/
khaledelmadawi 0:e63996fd7d3e 3370 if (useProgMem) {
khaledelmadawi 0:e63996fd7d3e 3371 if (sizeof(progBuffer) < length) progBuffer = (uint8_t *)realloc(progBuffer, length);
khaledelmadawi 0:e63996fd7d3e 3372 for (j = 0; j < length; j++) progBuffer[j] = pgm_read_byte(data + i + j);
khaledelmadawi 0:e63996fd7d3e 3373 } else {
khaledelmadawi 0:e63996fd7d3e 3374 progBuffer = (uint8_t *)data + i;
khaledelmadawi 0:e63996fd7d3e 3375 }
khaledelmadawi 0:e63996fd7d3e 3376 success = writeMemoryBlock(progBuffer, length, bank, offset, true);
khaledelmadawi 0:e63996fd7d3e 3377 i += length;
khaledelmadawi 0:e63996fd7d3e 3378 } else {
khaledelmadawi 0:e63996fd7d3e 3379 // special instruction
khaledelmadawi 0:e63996fd7d3e 3380 // NOTE: this kind of behavior (what and when to do certain things)
khaledelmadawi 0:e63996fd7d3e 3381 // is totally undocumented. This code is in here based on observed
khaledelmadawi 0:e63996fd7d3e 3382 // behavior only, and exactly why (or even whether) it has to be here
khaledelmadawi 0:e63996fd7d3e 3383 // is anybody's guess for now.
khaledelmadawi 0:e63996fd7d3e 3384 if (useProgMem) {
khaledelmadawi 0:e63996fd7d3e 3385 special = pgm_read_byte(data + i++);
khaledelmadawi 0:e63996fd7d3e 3386 } else {
khaledelmadawi 0:e63996fd7d3e 3387 special = data[i++];
khaledelmadawi 0:e63996fd7d3e 3388 }
khaledelmadawi 0:e63996fd7d3e 3389 /*Serial.print("Special command code ");
khaledelmadawi 0:e63996fd7d3e 3390 Serial.print(special, HEX);
khaledelmadawi 0:e63996fd7d3e 3391 Serial.println(" found...");*/
khaledelmadawi 0:e63996fd7d3e 3392 if (special == 0x01) {
khaledelmadawi 0:e63996fd7d3e 3393 // enable DMP-related interrupts
khaledelmadawi 0:e63996fd7d3e 3394
khaledelmadawi 0:e63996fd7d3e 3395 //setIntZeroMotionEnabled(true);
khaledelmadawi 0:e63996fd7d3e 3396 //setIntFIFOBufferOverflowEnabled(true);
khaledelmadawi 0:e63996fd7d3e 3397 //setIntDMPEnabled(true);
khaledelmadawi 0:e63996fd7d3e 3398 i2Cdev.writeByte(devAddr, MPU6050_RA_INT_ENABLE, 0x32); // single operation
khaledelmadawi 0:e63996fd7d3e 3399
khaledelmadawi 0:e63996fd7d3e 3400 success = true;
khaledelmadawi 0:e63996fd7d3e 3401 } else {
khaledelmadawi 0:e63996fd7d3e 3402 // unknown special command
khaledelmadawi 0:e63996fd7d3e 3403 success = false;
khaledelmadawi 0:e63996fd7d3e 3404 }
khaledelmadawi 0:e63996fd7d3e 3405 }
khaledelmadawi 0:e63996fd7d3e 3406
khaledelmadawi 0:e63996fd7d3e 3407 if (!success) {
khaledelmadawi 0:e63996fd7d3e 3408 if (useProgMem) free(progBuffer);
khaledelmadawi 0:e63996fd7d3e 3409 return false; // uh oh
khaledelmadawi 0:e63996fd7d3e 3410 }
khaledelmadawi 0:e63996fd7d3e 3411 }
khaledelmadawi 0:e63996fd7d3e 3412 if (useProgMem) free(progBuffer);
khaledelmadawi 0:e63996fd7d3e 3413 return true;
khaledelmadawi 0:e63996fd7d3e 3414 }
khaledelmadawi 0:e63996fd7d3e 3415 bool MPU6050::writeProgDMPConfigurationSet(const uint8_t *data, uint16_t dataSize)
khaledelmadawi 0:e63996fd7d3e 3416 {
khaledelmadawi 0:e63996fd7d3e 3417 return writeDMPConfigurationSet(data, dataSize, false);
khaledelmadawi 0:e63996fd7d3e 3418 }
khaledelmadawi 0:e63996fd7d3e 3419
khaledelmadawi 0:e63996fd7d3e 3420 // DMP_CFG_1 register
khaledelmadawi 0:e63996fd7d3e 3421
khaledelmadawi 0:e63996fd7d3e 3422 uint8_t MPU6050::getDMPConfig1()
khaledelmadawi 0:e63996fd7d3e 3423 {
khaledelmadawi 0:e63996fd7d3e 3424 i2Cdev.readByte(devAddr, MPU6050_RA_DMP_CFG_1, buffer);
khaledelmadawi 0:e63996fd7d3e 3425 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3426 }
khaledelmadawi 0:e63996fd7d3e 3427 void MPU6050::setDMPConfig1(uint8_t config)
khaledelmadawi 0:e63996fd7d3e 3428 {
khaledelmadawi 0:e63996fd7d3e 3429 i2Cdev.writeByte(devAddr, MPU6050_RA_DMP_CFG_1, config);
khaledelmadawi 0:e63996fd7d3e 3430 }
khaledelmadawi 0:e63996fd7d3e 3431
khaledelmadawi 0:e63996fd7d3e 3432 // DMP_CFG_2 register
khaledelmadawi 0:e63996fd7d3e 3433
khaledelmadawi 0:e63996fd7d3e 3434 uint8_t MPU6050::getDMPConfig2()
khaledelmadawi 0:e63996fd7d3e 3435 {
khaledelmadawi 0:e63996fd7d3e 3436 i2Cdev.readByte(devAddr, MPU6050_RA_DMP_CFG_2, buffer);
khaledelmadawi 0:e63996fd7d3e 3437 return buffer[0];
khaledelmadawi 0:e63996fd7d3e 3438 }
khaledelmadawi 0:e63996fd7d3e 3439 void MPU6050::setDMPConfig2(uint8_t config)
khaledelmadawi 0:e63996fd7d3e 3440 {
khaledelmadawi 0:e63996fd7d3e 3441 i2Cdev.writeByte(devAddr, MPU6050_RA_DMP_CFG_2, config);
khaledelmadawi 0:e63996fd7d3e 3442 }