diff --git a/source/MicroBitCompass.cpp b/source/MicroBitCompass.cpp index d89b6a5..5d15c1f 100644 --- a/source/MicroBitCompass.cpp +++ b/source/MicroBitCompass.cpp @@ -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; } diff --git a/source/MicroBitSuperMain.cpp b/source/MicroBitSuperMain.cpp index 85c5017..b6a4e8a 100644 --- a/source/MicroBitSuperMain.cpp +++ b/source/MicroBitSuperMain.cpp @@ -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;