From 23d56edbdd5c38c10a3c676a4fd9c82fb456033d Mon Sep 17 00:00:00 2001 From: James Devine Date: Tue, 2 Feb 2016 20:59:01 +0000 Subject: [PATCH] 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. --- source/MicroBitCompass.cpp | 18 ++++++++++++++++++ source/MicroBitSuperMain.cpp | 13 +++++++++++++ 2 files changed, 31 insertions(+) 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;