microbit: integrated MicroBitStorage with MicroBitCompass

This commit introduces persistent storage for the compass between
device resets.

setCalibration now also stores the data in FLASH if it differs from
what it contains.

In source/MicroBitSuperMain.cpp, the data is restored if there is
persistent data available that is non-zero.

(i.e. x, y and z are NOT zero).

This ensures that users can code without interference from persistent
storage.
master
James Devine 2016-02-02 20:59:01 +00:00
parent 4c9f32d79a
commit 23d56edbdd
2 changed files with 31 additions and 0 deletions

View File

@ -488,6 +488,24 @@ void MicroBitCompass::calibrateEnd()
*/
void MicroBitCompass::setCalibration(CompassSample calibration)
{
MicroBitStorage s = MicroBitStorage();
MicroBitConfigurationBlock *b = s.getConfigurationBlock();
//check we are not storing our restored calibration data.
if(b->compassCalibrationData != calibration)
{
b->magic = MICROBIT_STORAGE_CONFIG_MAGIC;
b->compassCalibrationData.x = calibration.x;
b->compassCalibrationData.y = calibration.y;
b->compassCalibrationData.z = calibration.z;
s.setConfigurationBlock(b);
}
delete b;
average = calibration;
status |= MICROBIT_COMPASS_STATUS_CALIBRATED;
}

View File

@ -36,6 +36,19 @@ int main()
// Provide time for all threaded initialisers to complete.
uBit.sleep(100);
//check our persistent storage for compassCalibrationData
MicroBitStorage s = MicroBitStorage();
MicroBitConfigurationBlock *b = s.getConfigurationBlock();
//if we have some calibrated data, calibrate the compass!
if(b->magic == MICROBIT_STORAGE_CONFIG_MAGIC)
{
if(b->compassCalibrationData != CompassSample(0,0,0))
uBit.compass.setCalibration(b->compassCalibrationData);
}
delete b;
#if CONFIG_ENABLED(MICROBIT_BLE_PAIRING_MODE)
// Test if we need to enter BLE pairing mode...
int i=0;