-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_i2c_eeprom.cpp
341 lines (308 loc) · 8.4 KB
/
logger_i2c_eeprom.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "logger_i2c_eeprom.h"
#include "IC2extEEPROM.h"
extEEPROM eep(kbits_512, 1, 64);
logger_I2C_eeprom::logger_I2C_eeprom(uint8_t deviceAddress)
{
//_deviceAddress = deviceAddress;
//logger_I2C_eeprom(deviceAddress, LOGGER_I2C_EEPROM_PAGESIZE);
}
void logger_I2C_eeprom::begin()
{
Wire.begin();
//initialize Flight structure
}
/*
clearFlightList()
Clear the flight list. Rather than clearing the entire eeprom
let's just reset addresses 0 to 200 which contains the flights addresses
*/
void logger_I2C_eeprom::clearFlightList()
{
int i;
for (i = 0; i < 25; i++)
{
_FlightConfig[i].flight_start = 0;
_FlightConfig[i].flight_stop = 0;
}
}
/*
readFlightList()
*/
int logger_I2C_eeprom::readFlightList()
{
eep.read(0, ((byte*)&_FlightConfig), sizeof(_FlightConfig));
return FLIGHT_LIST_START + sizeof(_FlightConfig) ;
}
/*
readFlight(int eeaddress)
*/
unsigned long logger_I2C_eeprom::readFlight(unsigned long eeaddress)
{
eep.read(eeaddress, ((byte*)&_FlightData), sizeof(_FlightData));
return eeaddress + sizeof(_FlightData);
}
/*
writeFlightList()
*/
int logger_I2C_eeprom::writeFlightList()
{
eep.write(FLIGHT_LIST_START, ((byte*)&_FlightConfig), sizeof(_FlightConfig));
return FLIGHT_LIST_START + sizeof(_FlightConfig);
}
/*
writeFastFlight(int eeaddress)
*/
unsigned long logger_I2C_eeprom::writeFastFlight(unsigned long eeaddress)
{
eep.write(eeaddress, ((byte*)&_FlightData), sizeof(_FlightData));
return eeaddress + sizeof(_FlightData);
}
/*
getLastFlightNbr()
Parse the flight index end check if the flight_start address is > 0
return -1 if no flight have been recorded else return the flight number
*/
int logger_I2C_eeprom::getLastFlightNbr()
{
int i;
for (i = 0; i < 25; i++)
{
if (_FlightConfig[i].flight_start == 0)
{
break;
}
}
i--;
return i;
}
/*
eraseLastFlight()
*/
bool logger_I2C_eeprom::eraseLastFlight() {
int i;
for (i = 0; i < 25; i++)
{
if (_FlightConfig[i].flight_start == 0)
{
if (i > 0) {
_FlightConfig[i - 1].flight_start = 0;
_FlightConfig[i - 1].flight_stop = 0;
writeFlightList();
return true;
}
}
}
return false;
}
/*
getLastFlightEndAddress()
Parse the flight index end check if the flight_start address is > 0
return -1 if no flight have been recorded else return the flight number
*/
long logger_I2C_eeprom::getLastFlightEndAddress()
{
int i;
for (i = 0; i < 25; i++)
{
if (_FlightConfig[i].flight_start == 0)
{
break;
}
}
i--;
return _FlightConfig[i].flight_stop;
}
/*
printFlightList()
*/
int logger_I2C_eeprom::printFlightList()
{
//retrieve from the eeprom
int v_ret = readFlightList();
//Read the stucture
int i;
for (i = 0; i < 25; i++)
{
if (_FlightConfig[i].flight_start == 0)
break;
SerialCom.print("Flight Nbr: ");
SerialCom.println(i);
SerialCom.print("Start: ");
SerialCom.println(_FlightConfig[i].flight_start);
SerialCom.print("End: ");
SerialCom.println(_FlightConfig[i].flight_stop);
}
return i;
}
void logger_I2C_eeprom::setFlightStartAddress(int flightNbr, long startAddress)
{
_FlightConfig[flightNbr].flight_start = startAddress;
}
void logger_I2C_eeprom::setFlightEndAddress(int flightNbr, long endAddress)
{
_FlightConfig[flightNbr].flight_stop = endAddress;
}
void logger_I2C_eeprom::setFlightTimeData( long difftime)
{
_FlightData.diffTime = difftime;
}
void logger_I2C_eeprom::setFlightAltitudeData( long altitude)
{
_FlightData.altitude = altitude;
}
void logger_I2C_eeprom::setFlightTemperatureData( long temperature)
{
_FlightData.temperature = temperature;
}
void logger_I2C_eeprom::setFlightPressureData( long pressure) {
_FlightData.pressure = pressure;
}
#ifdef LOG_VOLTAGE
void logger_I2C_eeprom::setFlightVoltageData( long voltage) {
_FlightData.voltage = voltage;
}
#endif
#if defined ALTIMULTIESP32_ACCELERO || defined ALTIMULTIESP32_ACCELERO_375 || defined ALTIMULTIESP32_ACCELERO_345
void logger_I2C_eeprom::setADXL375accelX(long accelX)
{
_FlightData.ADXL375accelX = accelX;
}
void logger_I2C_eeprom::setADXL375accelY(long accelY)
{
_FlightData.ADXL375accelY = accelY;
}
void logger_I2C_eeprom::setADXL375accelZ(long accelZ)
{
_FlightData.ADXL375accelZ = accelZ;
}
void logger_I2C_eeprom::setADXL345accelX(long accelX)
{
_FlightData.ADXL345accelX = accelX;
}
void logger_I2C_eeprom::setADXL345accelY(long accelY)
{
_FlightData.ADXL345accelY = accelY;
}
void logger_I2C_eeprom::setADXL345accelZ(long accelZ)
{
_FlightData.ADXL345accelZ = accelZ;
}
#endif
long logger_I2C_eeprom::getFlightStart(int flightNbr)
{
return _FlightConfig[flightNbr].flight_start;
}
long logger_I2C_eeprom::getFlightStop(int flightNbr)
{
return _FlightConfig[flightNbr].flight_stop;
}
long logger_I2C_eeprom::getFlightTimeData()
{
return _FlightData.diffTime;
}
long logger_I2C_eeprom::getFlightAltitudeData()
{
return _FlightData.altitude;
}
long logger_I2C_eeprom::getSizeOfFlightData()
{
return sizeof(_FlightData);
}
void logger_I2C_eeprom::printFlightData(int flightNbr)
{
unsigned long startaddress;
unsigned long endaddress;
startaddress = getFlightStart(flightNbr);
endaddress = getFlightStop(flightNbr);
if (startaddress > 200)
{
unsigned long i = startaddress;
unsigned long currentTime = 0;
while (i < (endaddress + 1))
{
i = readFlight(i) + 1;
char flightData[120] = "";
char temp[20] = "";
currentTime = currentTime + getFlightTimeData();
strcat(flightData, "data,");
sprintf(temp, "%i,", flightNbr );
strcat(flightData, temp);
sprintf(temp, "%i,", currentTime );
strcat(flightData, temp);
sprintf(temp, "%i,", getFlightAltitudeData() );
strcat(flightData, temp);
sprintf(temp, "%i,", _FlightData.temperature );
strcat(flightData, temp);
sprintf(temp, "%i,", _FlightData.pressure );
strcat(flightData, temp);
#ifdef LOG_VOLTAGE
sprintf(temp, "%i,", _FlightData.voltage );
strcat(flightData, temp);
#endif
#if defined ALTIMULTIESP32_ACCELERO || defined ALTIMULTIESP32_ACCELERO_375 || defined ALTIMULTIESP32_ACCELERO_345
sprintf(temp, "%i,", _FlightData.ADXL375accelX );
strcat(flightData, temp);
sprintf(temp, "%i,", _FlightData.ADXL375accelY );
strcat(flightData, temp);
sprintf(temp, "%i,", _FlightData.ADXL375accelZ );
strcat(flightData, temp);
sprintf(temp, "%i,", _FlightData.ADXL345accelX );
strcat(flightData, temp);
sprintf(temp, "%i,", _FlightData.ADXL345accelY );
strcat(flightData, temp);
sprintf(temp, "%i,", _FlightData.ADXL345accelZ );
strcat(flightData, temp);
#endif
unsigned int chk = msgChk(flightData, sizeof(flightData));
sprintf(temp, "%i", chk);
strcat(flightData, temp);
strcat(flightData, ";\n");
//#ifdef ALTIMULTIESP32
#if defined ALTIMULTIESP32 || defined ALTIMULTIESP32_ACCELERO || defined ALTIMULTIESP32_ACCELERO_375 || defined ALTIMULTIESP32_ACCELERO_345
Serial.print("$");
Serial.print(flightData);
#endif
SerialCom.print("$");
SerialCom.print(flightData);
//This will slow down the data
// this is for telemetry modules without enought buffer
if (config.telemetryType == 0)
delay(0);
else if (config.telemetryType == 1)
delay(20);
else if (config.telemetryType == 2)
delay(50);
else if (config.telemetryType == 3)
delay(100);
}
}
}
/*
CanRecord()
First count the number of flights. It cannot be greter than 25
if last flight end address is greater than the max possible
address then the EEprom is full
*/
boolean logger_I2C_eeprom::CanRecord()
{
long lastFlight;
lastFlight = getLastFlightNbr();
if (lastFlight == -1)
return true;
if (lastFlight == 24)
{
//#ifdef SERIAL_DEBUG
//Serial.println("25 flights");
//#endif
return false;
}
// Check if eeprom is full
if (getFlightStop(lastFlight) > 65500 )
{
//#ifdef SERIAL_DEBUG
//Serial.println("memory is full");
//#endif
return false;
}
return true;
}