-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
146 lines (125 loc) · 3.79 KB
/
Config.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <Arduino.h>
#include "Config.h"
Config::Config() {
// Constructor
// Takes an int as offset
_needSave = false;
}
void Config::setGSMPhone(const char* gsmPhone) {
strcpy( _config.gsmPhone, gsmPhone);
Serial.print("_config.gsmPhone is: '");
Serial.print(_config.gsmPhone);
Serial.println("'");
_needSave = true;
}
char* Config::getGSMPhone() {
return _config.gsmPhone;
}
void Config::setSavedLat(const double* Lat) {
_config.savedLat = *Lat;
_needSave = true;
}
double* Config::getSavedLat(void) {
return &_config.savedLat;
}
void Config::setSavedLng(const double* Lng) {
_needSave = true;
_config.savedLng = *Lng;
}
double* Config::getSavedLng(void) {
return &_config.savedLng;
}
boolean Config::load(void) {
// load config
_NOW = millis();
_crcCheckSumLoaded = 0;
_tempBuffer = "";
// Serial.print("chars from eeProm: '");
if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2])
for (unsigned int t = 0; t < sizeof(_config); t++) {
_c = EEPROM.read(CONFIG_START + t);
//*((char*)&_config + t) = EEPROM.read(CONFIG_START + t);
*((char*)&_config + t) = _c;
Serial.print("'");
// http://stackoverflow.com/questions/17158890/transform-char-array-into-string
_tempBuffer += _c;
Serial.print(EEPROM.read(CONFIG_START + t));
} else {
Serial.print("LOAD ERROR");
}
Serial.println("'");
_crcCheckSumLoaded = crc.checksum(_tempBuffer);
// Serial.println(_tempBuffer);
// Serial.print("checksum is: ");
// Serial.println(_crcChecksum, HEX);
// if ( _crcChecksum == _config.crc ) {
// // what was loaded is what what saved...
// Serial.print("serial is: ");
// Serial.println(_config.crc, HEX);
// Serial.print("checksum is: ");
// Serial.println(_crcChecksum, HEX);
// return true;
// } else {
// // Ops summat went wrong with load or save!!!!!
// Serial.println("Something went wrong with the checksum");
// return false;
// }
_timeTaken = millis() - _NOW;
Serial.print("Time taken to load: ");
Serial.println(_timeTaken);
}
boolean Config::save(void) {
// config
if ( _needSave == false ) {
return true;
}
// ok, we need to save something
_NOW = millis();
_tempBuffer = "";
_crcCheckSumSaved = 0;
_config.serial ++;
Serial.print("_config.serial is: ");
Serial.println(_config.serial);
Serial.print("Size of _config: ");
Serial.println(sizeof(_config));
Serial.print("About to write: '");
//unsigned int t = 0;
for (unsigned int t = 0; t < sizeof(_config); t++) {
//EEPROM.write(CONFIG_START + t, *((char*)&_config + t));
//for (unsigned int t = 0; t < sizeof(_config); t++)
// EEPROM.write does not return anything
// so we need re-read in to check if it wrote ... grh
Serial.print("'");
_c = *((char*)&_config + t);
// EEPROM.write(CONFIG_START + t, *((char*)&_config + t));
EEPROM.write(CONFIG_START + t, _c);
_tempBuffer += _c;
}
Serial.println("'");
Serial.print("_tempBuffer is: ");
Serial.println(_tempBuffer);
// checksum what was sent
_crcCheckSumSaved = crc.checksum(_tempBuffer);
// load it back in
load();
if ( _crcCheckSumSaved == _crcCheckSumLoaded ) {
Serial.print("Yay checksums match, _crcCheckSumSaved ='");
Serial.print(_crcCheckSumSaved);
Serial.print("', _crcCheckSumLoaded ='");
Serial.print(_crcCheckSumLoaded);
Serial.println("'");
_timeTaken = millis() - _NOW;
Serial.print("Time taken go save: ");
Serial.println(_timeTaken);
return true;
} else {
Serial.println("Load checksum error");
_timeTaken = millis() - _NOW;
Serial.print("Time taken go save: ");
Serial.println(_timeTaken);
return false;
}
// done!
}