Adafruit_MPU6050 icon indicating copy to clipboard operation
Adafruit_MPU6050 copied to clipboard

begin() was split up

Open mahboud opened this issue 2 years ago • 0 comments

The ESP32 has a deep sleep mode, which returns, not to where deep sleep was initiated, but to setup() all over again. A difference between that and a regular reset or power-up is that the RTC variables are not zeroed out.

An MPU6050 can be connected to the ESP32 and be allowed to continue looking for motion, while the ESP32 is asleep. When the MPU6050 wakes the ESP32 back, it is good to be able to access the MPU6050's interrupt and other registers. However, if the only way to regain communication with the MPU6050 through I2C, is by calling a mpu.begin(), then begin() will reset the MPU and erase the interesting data.

This PR attempts to show one workaround. begin() is split into a private function initI2C() and there is also an init() call added. Normally, users will use begin() as before. The behavior should be unchanged.

Users who wish to preserve the contents of the MPU can call init(), which only does the bare minimum to restore communication with the MPU via i2c.

There are two ways (at least) to know whether the MPU has already been set up

  • a RTC_DATA_ATTR int bootCount = 0; that is incremented by setup(). This will always be 0 if the ESP32 has been reset or is powered up, or non-zero if it is waking up. You can use begin() when it is zero and init() when it is non-zero.
  • do an init() call, check the MPU6050's registers for known values you had set before and if you don't find those, then call begin().

mahboud avatar Mar 07 '22 20:03 mahboud