-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChronoData.cpp
More file actions
307 lines (274 loc) · 6.79 KB
/
Copy pathChronoData.cpp
File metadata and controls
307 lines (274 loc) · 6.79 KB
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
//
// Copyright (c) 2013 - Allen Bauer - http://blog.thereadoracleatdelphi.com
// Under MIT License
//
#include <math.h>
#include <avr/eeprom.h>
#include <Print.h>
#include "ChronoData.h"
ChronoData::ShotData::ShotData(float velocity1, float velocity2)
{
Velocity[0] = velocity1;
Velocity[1] = velocity2;
}
void ChronoData::init(uint8_t *eeAddress)
{
_eeAddress = eeAddress;
if (eeprom_read_byte(_eeAddress) != 'c' || eeprom_read_byte(_eeAddress + 1) != 'r')
{
_savedSeqCount = 0;
eeprom_write_byte(_eeAddress, 'c');
eeprom_write_byte(_eeAddress + 1, 'r');
eeprom_write_byte(_eeAddress + 2, _savedSeqCount);
}
else
_savedSeqCount = eeprom_read_byte(_eeAddress + 2);
_thisSeqNum = _savedSeqCount;
_readOnly = false;
}
void ChronoData::ClearAllSequences()
{
ClearCurrentSequence();
_savedSeqCount = 0;
_thisSeqNum = 0;
_readOnly = false;
eeprom_write_byte(_eeAddress + 2, _savedSeqCount);
}
void ChronoData::ClearCurrentSequence()
{
_shotCount = 0;
}
int8_t ChronoData::NewSequence(bool saveCurrent)
{
if ((_shotCount > 0) && !_readOnly && saveCurrent)
SaveCurrentSequence();
_thisSeqNum = _savedSeqCount;
_readOnly = false;
_shotCount = 0;
return _thisSeqNum;
}
int8_t ChronoData::SequencesCount()
{
return _savedSeqCount;
}
bool ChronoData::LoadSequence(uint8_t seq)
{
uint8_t *eeAddress = GetSeqAddress(seq);
_shotCount = eeprom_read_byte(eeAddress++);
eeprom_read_block((void*)&_sequenceShotData[0], (void *)eeAddress, _shotCount * sizeof(ShotData));
_thisSeqNum = seq;
_readOnly = true;
return true;
}
bool ChronoData::SaveCurrentSequence()
{
if (!_readOnly && (_shotCount > 0))
{
uint8_t *eeAddress = GetSeqAddress(_thisSeqNum);
eeprom_write_byte(_eeAddress + 2, ++_savedSeqCount);
eeprom_write_byte(eeAddress++, _shotCount);
eeprom_write_block((void*)&_sequenceShotData[0], (void *)eeAddress, _shotCount * sizeof(ShotData));
_readOnly = true;
return true;
} else
return false;
}
int8_t ChronoData::CurrentSequence()
{
return _thisSeqNum;
}
int8_t ChronoData::ShotsInSequence(int8_t seq) const
{
if (seq < 0)
return _shotCount;
uint8_t *eeAddress = GetSeqAddress(seq);
return eeprom_read_byte(eeAddress);
}
struct MinMax
{
float _min, _max;
MinMax() : _min(3.4e38), _max(0.0) {};
};
void getSequenceMinMax(const ChronoData::ShotData &data, void *param)
{
MinMax *minMax = (MinMax *)param;
float cur = data.Velocity[0];
minMax->_max = fmax(cur, minMax->_max);
minMax->_min = fmin(cur, minMax->_min);
}
float ChronoData::SequenceMax(int8_t seq) const
{
MinMax minMax;
ForEachShot(getSequenceMinMax, &minMax, seq);
return minMax._max;
}
float ChronoData::SequenceMin(int8_t seq) const
{
MinMax minMax;
ForEachShot(getSequenceMinMax, &minMax, seq);
return minMax._min;
}
struct AvgData
{
float Avg;
int8_t items;
AvgData() : Avg(0.0), items(0) {};
};
void getSequenceAvg(const ChronoData::ShotData &data, void *param)
{
AvgData *avg = (AvgData *)param;
float cur = data.Velocity[0];
avg->items++;
avg->Avg += cur;
}
float ChronoData::SequenceAverage(int8_t seq) const
{
AvgData avg;
ForEachShot(getSequenceAvg, &avg, seq);
return avg.Avg / avg.items;
}
float ChronoData::SequenceSpread(int8_t seq) const
{
MinMax minMax;
ForEachShot(getSequenceMinMax, &minMax, seq);
return minMax._max - minMax._min;
}
struct DeviationData
{
float sumSq;
float sum;
DeviationData() : sumSq(0.0), sum(0.0) {};
};
void getDeviationData(const ChronoData::ShotData &data, void *param)
{
DeviationData *deviationData = (DeviationData *)param;
float cur = data.Velocity[0];
deviationData->sumSq += cur * cur;
deviationData->sum += cur;
}
float ChronoData::SequenceDeviation(int8_t seq) const
{
DeviationData deviation;
float sd;
uint8_t shotCount = ShotsInSequence(seq);
if (shotCount < 2)
return 0.0;
ForEachShot(getDeviationData, &deviation, seq);
sd = sqrt((deviation.sumSq - (square(deviation.sum) / shotCount)) / (shotCount - 1));
return sd;
}
ChronoData::ShotData ChronoData::SequenceShotData(int8_t shot, int8_t seq) const
{
if (seq < 0)
return _sequenceShotData[shot];
else
{
ShotData data;
uint8_t *eeAddress = GetSeqAddress(seq);
uint8_t count = eeprom_read_byte(eeAddress++);
shot = min(shot, count - 1);
eeprom_read_block((void*)&data, (void*)(eeAddress + shot * sizeof(ShotData)), sizeof(ShotData));
return data;
}
}
bool ChronoData::ReadOnly()
{
return _readOnly;
}
int8_t ChronoData::AddShot(ShotData shotData)
{
if (!_readOnly)
{
_sequenceShotData[_shotCount++] = shotData;
return _shotCount - 1;
}
else
return -1;
}
bool ChronoData::DeleteShot(int8_t shot)
{
if (!_readOnly && _shotCount > 0)
{
if (shot < 0)
shot = 0;
else if (shot >= _shotCount)
shot = _shotCount - 1;
if (shot < _shotCount - 1)
memmove((void*)&_sequenceShotData[shot], (void *)&_sequenceShotData[shot + 1], (_shotCount - shot - 1) * sizeof(ShotData));
_shotCount--;
return true;
}
else
return false;
}
void ChronoData::ForEachShot(ProcessData processData, void * param, int8_t seq) const
{
int i;
if (seq < 0)
{
for (i = 0; i < _shotCount; i++)
processData(_sequenceShotData[i], param);
}
else
{
uint8_t *eeAddress = GetSeqAddress(seq);
uint8_t shotCount = eeprom_read_byte(eeAddress++);
for (i = 0; i < shotCount; i++)
{
ShotData data;
eeprom_read_block((void*)&data, (void*)eeAddress, sizeof(ShotData));
processData(data, param);
eeAddress += sizeof(ShotData);
}
}
}
uint8_t *ChronoData::GetSeqAddress(uint8_t seq) const
{
uint8_t *eeAddress = _eeAddress + 3;
while (seq-- > 0)
{
uint8_t shotCount = eeprom_read_byte(eeAddress++);
eeAddress += shotCount * sizeof(ShotData);
}
return eeAddress;
}
size_t ChronoData::printStatsTo(Print& p) const
{
size_t size = 0;
if (_shotCount != 0)
{
size += p.print(F("Min/Max: ")); size += p.print(SequenceMin()); size += p.print("/"); size += p.println(SequenceMax());
size += p.print(F("Spread: ")); size += p.println(SequenceSpread());
size += p.print(F("Average: ")); size += p.println(SequenceAverage());
size += p.print(F("Deviation: ")); size += p.print(SequenceDeviation());
}
else
size += p.print(F("<empty>"));
return size;
}
size_t ChronoData::printSeqShotTo(Print& p) const
{
size_t size = p.print(F("Series #")); size += p.println(_thisSeqNum);
size += p.print(F("# Shots: ")); size += p.println(_shotCount);
return size;
}
size_t ChronoData::printTo(Print& p) const
{
int i;
const ShotData *data;
size_t size = p.println(F("---ChronoData---"));
size += printSeqShotTo(p);
data = _sequenceShotData;
for (i = 0; i < _shotCount; i++)
{
size += p.print(i); size += p.print(": ");
size += p.print(data->Velocity[0]); size += p.print(", ");
size += p.print(data->Velocity[1]); size += p.print(", ");
size += p.println(data->AvgVelocity());
data++;
}
size += p.println(F("--Statistics--"));
size += printStatsTo(p);
return size;
}
ChronoData chronoData;