diff --git a/lib/calibrateCompass/calibrateCompass.cpp b/lib/calibrateCompass/calibrateCompass.cpp index a1c38d3..1b21ae6 100644 --- a/lib/calibrateCompass/calibrateCompass.cpp +++ b/lib/calibrateCompass/calibrateCompass.cpp @@ -61,8 +61,10 @@ void CalibrateCompass::loop() { if (changed) this->lastChange = millis(); - if (millis() - this->lastChange > this->maxTimeWithoutChange) + if (millis() - this->lastChange > this->maxTimeWithoutChange) { this->state = State::Finished; + this->newData = true; + } } void CalibrateCompass::start() { @@ -98,6 +100,37 @@ void CalibrateCompass::reset() { this->state = State::Ready; } +void CalibrateCompass::saveData() { + if (!this->newData) + return; + + Preferences preferences; + preferences.begin("compassCalibration", false); + + preferences.putInt("xLow", this->data.data[0][0]); + preferences.putInt("xHigh", this->data.data[0][1]); + preferences.putInt("yLow", this->data.data[1][0]); + preferences.putInt("yHigh", this->data.data[1][1]); + preferences.putInt("zLow", this->data.data[2][0]); + preferences.putInt("zHigh", this->data.data[2][1]); + + preferences.end(); +} + +void CalibrateCompass::loadData() { + Preferences preferences; + preferences.begin("compassCalibration", true); + + this->data.data[0][0] = preferences.getInt("xLow", 0); + this->data.data[0][1] = preferences.getInt("xHigh", 0); + this->data.data[1][0] = preferences.getInt("yLow", 0); + this->data.data[1][1] = preferences.getInt("yHigh", 0); + this->data.data[2][0] = preferences.getInt("zLow", 0); + this->data.data[2][1] = preferences.getInt("zHigh", 0); + + preferences.end(); +} + void CalibrateCompass::clearData() { for (uint8_t i = 0; i < 3; i++) { this->data.data[i][0] = 0; diff --git a/lib/calibrateCompass/calibrateCompass.h b/lib/calibrateCompass/calibrateCompass.h index ad7d6f5..4483a79 100644 --- a/lib/calibrateCompass/calibrateCompass.h +++ b/lib/calibrateCompass/calibrateCompass.h @@ -12,6 +12,7 @@ #pragma once #include +#include class CalibrateCompass { public: @@ -32,6 +33,8 @@ class CalibrateCompass { void useData(); void resetData(); void reset(); + void saveData(); + void loadData(); State getState() const { return this->state; } CallibrationData getCallibrationData() const { return this->data; } @@ -43,6 +46,7 @@ class CalibrateCompass { void clearData(); + bool newData = false; const uint16_t maxTimeWithoutChange = 5000; uint32_t lastChange = 0; };