forked from gaelj/thermostat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.cpp
53 lines (49 loc) · 1.48 KB
/
settings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "settings.h"
#include <EEPROM.h>
/**
* @brief Calculate the CRC8 value of a structure
*
* @param data
* @param count
* @return byte
*/
byte SettingsClass::GetCrc8(byte* data, byte count) {
byte result = 0xDF;
while (count--) {
result ^= *data;
data++;
}
return result;
}
/**
* @brief Calculate the CRC8 and persist the settings struct to E2P
*
*/
void SettingsClass::PersistSettings() {
TheSettings.crc8 = GetCrc8((byte*)&TheSettings, sizeof(settings_s) - 1);
EEPROM.put(E2P_START_ADDRESS, &TheSettings, sizeof(settings_s));
}
/**
* @brief Restore the settings struct from E2P. Checks the CRC8 and version values. Resets to default values in case of error
*
*/
void SettingsClass::RestoreSettings() {
EEPROM.get(E2P_START_ADDRESS, &TheSettings, sizeof(settings_s));
// Check data
if (GetCrc8((byte*)&TheSettings, sizeof(settings_s) - 1) != TheSettings.crc8 || TheSettings.E2PVersionNr != E2P_VERSION_NUMBER) {
// Invalid data - reset all
Serial.println("Applying default settings");
TheSettings.E2PVersionNr = E2P_VERSION_NUMBER;
TheSettings.DesiredTemperature = 18.0;
TheSettings.Kp = 2;
TheSettings.Ki = 0.5;
TheSettings.Kd = 2;
TheSettings.ATuneStep = 50;
TheSettings.ATuneNoise = 1;
TheSettings.ATuneStartValue = 100;
TheSettings.ATuneLookBack = 20;
PersistSettings();
RestoreSettings();
}
}
SettingsClass SETTINGS;