MPU9250

Dependents:   FreeIMU

Fork of MPU6050 by Yifei Teng

Revision:
11:9549be34fa7f
Parent:
9:d879deb55ae1
Child:
12:e32a6beb0a41
--- a/MPU6050.cpp	Mon Dec 23 08:34:58 2013 +0000
+++ b/MPU6050.cpp	Wed Sep 24 01:10:42 2014 +0000
@@ -54,7 +54,7 @@
 /** Default constructor, uses default I2C address.
  * @see MPU6050_DEFAULT_ADDRESS
  */
-MPU6050::MPU6050() : debugSerial(USBTX, USBRX)
+MPU6050::MPU6050() : debugSerial(USBTX, USBRX), sampling(false)
 {
     devAddr = MPU6050_DEFAULT_ADDRESS << 1;
     debugSerial.baud(115200);
@@ -66,7 +66,7 @@
  * @see MPU6050_ADDRESS_AD0_LOW
  * @see MPU6050_ADDRESS_AD0_HIGH
  */
-MPU6050::MPU6050(uint8_t address) : debugSerial(USBTX, USBRX)
+MPU6050::MPU6050(uint8_t address) : debugSerial(USBTX, USBRX), sampling(false)
 {
     devAddr = address << 1;
     debugSerial.baud(115200);
@@ -1910,6 +1910,9 @@
     *gz = gz_cache;
 }
 
+/*
+ * Populate readings cache when finished reading from I2C
+ */
 uint32_t mpureadfin(uint32_t param){
     MPU6050* ins = (MPU6050*)param;
     ins->ax_cache = (((int16_t)ins->mpu_buffer[0]) << 8) | ins->mpu_buffer[1];
@@ -1921,11 +1924,20 @@
     return 0;
 }
 
+void MPU6050::sample(bool on){
+   if (sampling && !on)
+      mpu_sampling.detach();
+   if (!sampling && on)
+      start_sampling();
+   sampling = on;
+}
+
 void MPU6050::mpu_sample_func(){
     i2Cdev.readBytes_nb(devAddr, &mpu_cmd, 14, (uint8_t*)mpu_buffer, &mpureadfin, this);
 }
 
 void MPU6050::start_sampling(){
+    sampling = true;
     mpu_cmd = MPU6050_RA_ACCEL_XOUT_H;
     mpu_sampling.attach_us(this, &MPU6050::mpu_sample_func, 2000);
 }