-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogSystem.cpp
285 lines (229 loc) · 7.23 KB
/
LogSystem.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
//
// Lucky Resistor's Deluxe Data Logger
// ---------------------------------------------------------------------------
// (c)2015 by Lucky Resistor. See LICENSE for details.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "LogSystem.h"
#include "Storage.h"
#include <util/crc16.h>
namespace lr {
LogRecord::LogRecord()
: _dateTime(), _temperature(0.0f), _humidity(0.0f)
{
}
LogRecord::~LogRecord()
{
}
LogRecord::LogRecord(const DateTime &dateTime, float temperature, float humidity)
: _dateTime(dateTime), _temperature(temperature), _humidity(humidity)
{
if (_temperature > 100.0f) {
_temperature = 100.0f;
}
if (_temperature < -273.15f) {
_temperature = -273.15f;
}
if (_humidity > 100.0f) {
_humidity = 100.0f;
}
if (_humidity < 0.0f) {
_humidity = 0.0f;
}
}
bool LogRecord::isNull() const
{
return _dateTime.isFirst() && _humidity == 0.0f && _temperature == 0.0f;
}
void LogRecord::writeToSerial() const
{
Serial.print(_dateTime.toString(DateTime::FormatLong));
Serial.print(",");
Serial.print(_temperature, 2);
Serial.print(",");
Serial.println(_humidity, 2);
}
namespace LogSystem {
static uint32_t gReservedForConfig; ///< The number of bytes reserved for the settings.
static uint32_t gCurrentNumberOfRecords; ///< The current number of records.
static uint32_t gMaximumNumberOfRecords; ///< The maximum number of records.
// The internal representation of a log record.
//
struct InternalLogRecord
{
uint32_t time; // The time as seconds since 2000-01-01 00:00:00.
float humidity; // The humidity value from the sensor.
float temperature; // The humidity value from the sensor.
uint16_t crc; // The CRC-16 of the record.
};
// Calculate the start of a record.
//
inline uint32_t getRecordStart(uint32_t index)
{
return gReservedForConfig + (sizeof(InternalLogRecord) * index);
}
// Read one single internal record from the storage.
//
// @param index The index of the record.
// @return A copy of the internal record.
//
inline InternalLogRecord getInternalRecord(uint32_t index)
{
InternalLogRecord record;
Storage::readBytes(getRecordStart(index), reinterpret_cast<uint8_t*>(&record), sizeof(InternalLogRecord));
return record;
}
// Write a single internal record to the storage.
//
// @param record The record to store.
// @param index The index of the record.
//
inline void setInternalRecord(const InternalLogRecord *record, uint32_t index)
{
Storage::writeBytes(getRecordStart(index), reinterpret_cast<const uint8_t*>(record), sizeof(InternalLogRecord));
}
// Set a record in the storage to zero.
//
// @param index The index of the record to zero.
//
void zeroInternalRecord(uint32_t index)
{
uint32_t storageIndex = getRecordStart(index);
for (uint8_t i = 0; i < sizeof(InternalLogRecord); ++i) {
Storage::writeByte(storageIndex, 0);
++storageIndex;
}
}
// Check if an internal record is null.
//
// This is true if all bytes of the records are null.
//
// @param record The record to check.
// @return true if the record is null.
//
bool isInternalRecordNull(InternalLogRecord *record)
{
uint8_t *recordPtr = reinterpret_cast<uint8_t*>(record);
for (uint8_t i = 0; i < sizeof(InternalLogRecord); ++i) {
if (*recordPtr != 0) {
return false;
}
++recordPtr;
}
return true;
}
// Calculate the CRC for the record.
//
// The CRC is calculated as CRC-16 while the CRC field is set to 0.
// All nibbles of the CRC-16 combined by XOR.
//
// @param record The record to calculate the CRC for.
// @return The 4-bit CRC
//
uint16_t getCRCForInternalRecord(InternalLogRecord *record)
{
uint16_t crc = 0xFFFF;
InternalLogRecord recordForCRC = *record;
recordForCRC.crc = 0;
uint8_t *recordPtr = reinterpret_cast<uint8_t*>(&recordForCRC);
for (uint8_t i = 0; i < sizeof(InternalLogRecord); ++i) {
crc = _crc16_update(crc, *recordPtr);
++recordPtr;
}
return crc;
}
// Check if an internal record is valid.
//
// This is true if all values of the record are in a valid range
// and the CRC code is valid.
//
// @param record The record to check.
// @return true if the record is valid.
//
bool isInternalRecordValid(InternalLogRecord *record)
{
if (record->humidity < 0.0f ||
record->humidity > 100.0f ||
record->temperature < -273.15f ||
record->temperature > 100.0f) {
return false; // out of range.
}
const uint16_t crc = getCRCForInternalRecord(record);
return crc == record->crc;
}
void begin(uint32_t reservedForConfig)
{
gReservedForConfig = reservedForConfig;
gCurrentNumberOfRecords = 0;
gMaximumNumberOfRecords = 0;
// Calculate the maximum number of records.
gMaximumNumberOfRecords = (Storage::size() - gReservedForConfig) / sizeof(InternalLogRecord);
// Scan the storage for valid records.
uint32_t index = 0;
InternalLogRecord record = getInternalRecord(index);
while (!isInternalRecordNull(&record)) {
if (!isInternalRecordValid(&record)) {
break;
}
++index;
record = getInternalRecord(index);
}
gCurrentNumberOfRecords = index;
}
LogRecord getLogRecord(uint32_t index)
{
if (index >= gCurrentNumberOfRecords) {
return LogRecord();
}
const InternalLogRecord record = getInternalRecord(index);
return LogRecord(DateTime::fromSecondsSince2000(record.time), record.temperature, record.humidity);
}
bool appendRecord(const LogRecord &logRecord)
{
if (gCurrentNumberOfRecords >= gMaximumNumberOfRecords) {
return false;
}
// zero the following record if possible
if (gCurrentNumberOfRecords+1 < gMaximumNumberOfRecords) {
zeroInternalRecord(gCurrentNumberOfRecords+1);
}
// convert the record into the internal structure.
InternalLogRecord internalRecord;
memset(&internalRecord, 0, sizeof(InternalLogRecord));
internalRecord.time = logRecord.getDateTime().toSecondsSince2000();
internalRecord.humidity = logRecord.getHumidity();
internalRecord.temperature = logRecord.getTemperature();
internalRecord.crc = getCRCForInternalRecord(&internalRecord);
setInternalRecord(&internalRecord, gCurrentNumberOfRecords);
gCurrentNumberOfRecords++;
return true;
}
void format()
{
zeroInternalRecord(0);
zeroInternalRecord(1);
gCurrentNumberOfRecords = 0;
}
uint32_t maximumNumberOfRecords()
{
return gMaximumNumberOfRecords;
}
uint32_t currentNumberOfRecords()
{
return gCurrentNumberOfRecords;
}
}
}