Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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()
, thenbegin()
will reset the MPU and erase the interesting data.This PR attempts to show one workaround.
begin()
is split into a private functioninitI2C()
and there is also aninit()
call added. Normally, users will usebegin()
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
RTC_DATA_ATTR int bootCount = 0;
that is incremented bysetup()
. 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 usebegin()
when it is zero andinit()
when it is non-zero.init()
call, check the MPU6050's registers for known values you had set before and if you don't find those, then callbegin()
.