Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: BLE_Nano_MPU6050Service
Diff: MPU6050.cpp
- Revision:
- 3:a6e53ab2c8c0
- Parent:
- 2:32b13cc64cb0
- Child:
- 4:5a52b575fbc6
--- a/MPU6050.cpp Mon Jul 13 15:23:35 2015 +0000 +++ b/MPU6050.cpp Wed Jul 22 12:20:10 2015 +0000 @@ -1062,4 +1062,389 @@ { uint8_t tmp; return (this->read(REG_SIGNAL_PATH_RESET, &tmp) && this->write(REG_SIGNAL_PATH_RESET, tmp | TEMP_PATH_RESET_MASK)); +} + +bool MPU6050::getAccelerometerPowerOnDelay(PowerOnDelay* delay, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_MOT_DETECT_CTRL, &tmp, timeout_secs)) + { + *delay = static_cast<PowerOnDelay>(tmp & POWER_ON_DELAY_MASK); + return true; + } + + return false; +} + +bool MPU6050::setAccelerometerPowerOnDelay(PowerOnDelay delay, float timeout_secs) +{ + uint8_t tmp; + return (this->read(REG_MOT_DETECT_CTRL, &tmp, timeout_secs)) && (this->write(REG_MOT_DETECT_CTRL, (tmp & (~POWER_ON_DELAY_MASK)) | delay, timeout_secs)); +} + +bool MPU6050::getFreefallDetectionDecrement(FreefallDetectionDecrement* dec, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_MOT_DETECT_CTRL, &tmp, timeout_secs)) + { + *dec = static_cast<FreefallDetectionDecrement>(tmp & FF_DETECTION_DEC_MASK); + return true; + } + + return false; +} + +bool MPU6050::setFreefallDetectionDecrement(FreefallDetectionDecrement dec, float timeout_secs) +{ + uint8_t tmp; + return (this->read(REG_MOT_DETECT_CTRL, &tmp, timeout_secs)) && (this->write(REG_MOT_DETECT_CTRL, (tmp & (~FF_DETECTION_DEC_MASK)) | dec, timeout_secs)); +} + +bool MPU6050::getMotionDetectionDecrement(MotionDetectionDecrement* dec, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_MOT_DETECT_CTRL, &tmp, timeout_secs)) + { + *dec = static_cast<MotionDetectionDecrement>(tmp & MOT_DETECTION_DEC_MASK); + return true; + } + + return false; +} + +bool MPU6050::setMotionDetectionDecrement(MotionDetectionDecrement dec, float timeout_secs) +{ + uint8_t tmp; + return (this->read(REG_MOT_DETECT_CTRL, &tmp, timeout_secs)) && (this->write(REG_MOT_DETECT_CTRL, (tmp & (~MOT_DETECTION_DEC_MASK)) | dec, timeout_secs)); +} + +bool MPU6050::getFIFOEnabled(bool *enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_USER_CTRL, &tmp, timeout_secs)) + { + *enabled = ((tmp & FIFO_EN_MASK) != 0); + return true; + } + + return false; +} + +bool MPU6050::setFIFOEnabled(bool enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_USER_CTRL, &tmp, timeout_secs)) + { + tmp &= ~(FIFO_EN_MASK); + return this->write(REG_USER_CTRL, enabled ? (tmp | FIFO_EN_MASK) : tmp, timeout_secs); + } + return false; +} + +bool MPU6050::resetFIFO(float timeout_secs) +{ + uint8_t tmp; + return (this->read(REG_USER_CTRL, &tmp) && this->write(REG_USER_CTRL, tmp | FIFO_RESET_MASK)); +} + +bool MPU6050::resetSensors(float timeout_secs) +{ + uint8_t tmp; + return (this->read(REG_USER_CTRL, &tmp) && this->write(REG_USER_CTRL, tmp | SIG_COND_RESET_MASK)); +} + +bool MPU6050::reset(float timeout_secs) +{ + uint8_t tmp; + return (this->read(REG_PWR_MGMT_1, &tmp) && this->write(REG_PWR_MGMT_1, tmp | RESET_MASK)); +} + +bool MPU6050::getSleepEnabled(bool *enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_1, &tmp, timeout_secs)) + { + *enabled = ((tmp & SLEEP_EN_MASK) != 0); + return true; + } + + return false; +} + +bool MPU6050::setSleepEnabled(bool enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_1, &tmp, timeout_secs)) + { + tmp &= ~(SLEEP_EN_MASK); + return this->write(REG_PWR_MGMT_1, enabled ? (tmp | SLEEP_EN_MASK) : tmp, timeout_secs); + } + return false; +} + +bool MPU6050::getWakeCycleEnabled(bool *enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_1, &tmp, timeout_secs)) + { + *enabled = ((tmp & WAKE_CYCLE_EN_MASK) != 0); + return true; + } + + return false; +} + +bool MPU6050::setWakeCycleEnabled(bool enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_1, &tmp, timeout_secs)) + { + tmp &= ~(WAKE_CYCLE_EN_MASK); + return this->write(REG_PWR_MGMT_1, enabled ? (tmp | WAKE_CYCLE_EN_MASK) : tmp, timeout_secs); + } + return false; +} + +bool MPU6050::getTemperatureSensorEnabled(bool *enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_1, &tmp, timeout_secs)) + { + // this is an inverted state + *enabled = ((tmp & TEMP_EN_MASK) == 0); + return true; + } + + return false; +} + +bool MPU6050::setTemperatureSensorEnabled(bool enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_1, &tmp, timeout_secs)) + { + tmp &= ~(TEMP_EN_MASK); + //this is an incverted state + return this->write(REG_PWR_MGMT_1, enabled ? tmp : (tmp | TEMP_EN_MASK), timeout_secs); + } + return false; +} + +bool MPU6050::getClockSource(ClockSource* clockSource, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_1, &tmp, timeout_secs)) + { + *clockSource = static_cast<ClockSource>(tmp & CLOCK_SOURCE_MASK); + return true; + } + + return false; +} + +bool MPU6050::setClockSource(ClockSource clockSource, float timeout_secs) +{ + uint8_t tmp; + return (this->read(REG_PWR_MGMT_1, &tmp, timeout_secs)) && (this->write(REG_PWR_MGMT_1, (tmp & (~CLOCK_SOURCE_MASK)) | clockSource, timeout_secs)); +} + +bool MPU6050::getWakeFrequency(WakeFrequency* wakeFrequency, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + *clockSource = static_cast<ClockSource>(tmp & WAKE_FREQ_MASK); + return true; + } + + return false; +} + +bool MPU6050::setWakeFrequency(WakeFrequency wakeFrequency, float timeout_secs) +{ + uint8_t tmp; + return (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) && (this->write(REG_PWR_MGMT_2, (tmp & (~WAKE_FREQ_MASK)) | clockSource, timeout_secs)); +} + +bool MPU6050::getGyroXStandbyEnabled(bool *enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + *enabled = ((tmp & GYRO_X_STB_EN_MASK) != 0); + return true; + } + + return false; +} + +bool MPU6050::setGyroXStandbyEnabled(bool enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + tmp &= ~(GYRO_X_STB_EN_MASK); + return this->write(REG_PWR_MGMT_2, enabled ? (tmp | GYRO_X_STB_EN_MASK) : tmp, timeout_secs); + } + return false; +} + +bool MPU6050::getGyroYStandbyEnabled(bool *enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + *enabled = ((tmp & GYRO_Y_STB_EN_MASK) != 0); + return true; + } + + return false; +} + +bool MPU6050::setGyroYStandbyEnabled(bool enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + tmp &= ~(GYRO_Y_STB_EN_MASK); + return this->write(REG_PWR_MGMT_2, enabled ? (tmp | GYRO_Y_STB_EN_MASK) : tmp, timeout_secs); + } + return false; +} + +bool MPU6050::getGyroZStandbyEnabled(bool *enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + *enabled = ((tmp & GYRO_Z_STB_EN_MASK) != 0); + return true; + } + + return false; +} + +bool MPU6050::setGyroZStandbyEnabled(bool enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + tmp &= ~(GYRO_Z_STB_EN_MASK); + return this->write(REG_PWR_MGMT_2, enabled ? (tmp | GYRO_Z_STB_EN_MASK) : tmp, timeout_secs); + } + return false; +} + +bool MPU6050::getAccelXStandbyEnabled(bool *enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + *enabled = ((tmp & ACCEL_X_STB_EN_MASK) != 0); + return true; + } + + return false; +} + +bool MPU6050::setAccelXStandbyEnabled(bool enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + tmp &= ~(ACCEL_X_STB_EN_MASK); + return this->write(REG_PWR_MGMT_2, enabled ? (tmp | ACCEL_X_STB_EN_MASK) : tmp, timeout_secs); + } + return false; +} + +bool MPU6050::getAccelYStandbyEnabled(bool *enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + *enabled = ((tmp & ACCEL_Y_STB_EN_MASK) != 0); + return true; + } + + return false; +} + +bool MPU6050::setAccelYStandbyEnabled(bool enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + tmp &= ~(ACCEL_Y_STB_EN_MASK); + return this->write(REG_PWR_MGMT_2, enabled ? (tmp | ACCEL_Y_STB_EN_MASK) : tmp, timeout_secs); + } + return false; +} + +bool MPU6050::getAccelZStandbyEnabled(bool *enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + *enabled = ((tmp & ACCEL_Z_STB_EN_MASK) != 0); + return true; + } + + return false; +} + +bool MPU6050::setAccelZStandbyEnabled(bool enabled, float timeout_secs) +{ + uint8_t tmp; + if (this->read(REG_PWR_MGMT_2, &tmp, timeout_secs)) + { + tmp &= ~(ACCEL_Z_STB_EN_MASK); + return this->write(REG_PWR_MGMT_2, enabled ? (tmp | ACCEL_Z_STB_EN_MASK) : tmp, timeout_secs); + } + return false; +} + +bool MPU6050::getFIFOCount(uint16_t *count, float timeout_secs) +{ + uint8_t tmp[2]; + if (this->read(REG_FIFO_COUNTH, tmp, 2, timeout_secs)) + { + *count = (((int16_t)tmp[0]) << 8) | tmp[1]; + return true; + } + + return false; +} + +bool MPU6050::readFIFO(uint8_t *data, float timeout_secs) +{ + return this->read(REG_FIFO_R_W, data, timeout_secs); +} + +bool MPU6050::writeFIFO(uint8_t data, float timeout_secs) +{ + return this->write(REG_FIFO_R_W, data, timeout_secs); +} + +bool MPU6050::readFIFO(uint8_t *data, size_t lenght, float timeout_secs) +{ + return this->read(REG_FIFO_R_W, data, lenght, timeout_secs); +} + +bool MPU6050::writeFIFO(uint8_t data, size_t lenght, float timeout_secs) +{ + return this->write(REG_FIFO_R_W, data, lenght, timeout_secs); +} + +bool MPU6050::getDeviceId(uint8_t *id, float timeout_secs) +{ + + uint8_t tmp; + if (this->read(REG_WHO_AM_I, &tmp, timeout_secs)) + { + *id = tmp & DEVICE_ID_MASK; + return true; + } + + return false; } \ No newline at end of file