added possibility to save and load the data

from flash for compass calibration
This commit is contained in:
2023-05-24 11:41:07 +02:00
parent 434f5a48c0
commit 1b25f59719
2 changed files with 38 additions and 1 deletions
+34 -1
View File
@@ -61,8 +61,10 @@ void CalibrateCompass::loop() {
if (changed) if (changed)
this->lastChange = millis(); this->lastChange = millis();
if (millis() - this->lastChange > this->maxTimeWithoutChange) if (millis() - this->lastChange > this->maxTimeWithoutChange) {
this->state = State::Finished; this->state = State::Finished;
this->newData = true;
}
} }
void CalibrateCompass::start() { void CalibrateCompass::start() {
@@ -98,6 +100,37 @@ void CalibrateCompass::reset() {
this->state = State::Ready; 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() { void CalibrateCompass::clearData() {
for (uint8_t i = 0; i < 3; i++) { for (uint8_t i = 0; i < 3; i++) {
this->data.data[i][0] = 0; this->data.data[i][0] = 0;
+4
View File
@@ -12,6 +12,7 @@
#pragma once #pragma once
#include <QMC5883LCompass.h> #include <QMC5883LCompass.h>
#include <Preferences.h>
class CalibrateCompass { class CalibrateCompass {
public: public:
@@ -32,6 +33,8 @@ class CalibrateCompass {
void useData(); void useData();
void resetData(); void resetData();
void reset(); void reset();
void saveData();
void loadData();
State getState() const { return this->state; } State getState() const { return this->state; }
CallibrationData getCallibrationData() const { return this->data; } CallibrationData getCallibrationData() const { return this->data; }
@@ -43,6 +46,7 @@ class CalibrateCompass {
void clearData(); void clearData();
bool newData = false;
const uint16_t maxTimeWithoutChange = 5000; const uint16_t maxTimeWithoutChange = 5000;
uint32_t lastChange = 0; uint32_t lastChange = 0;
}; };