-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathLogBuffer.cpp
More file actions
102 lines (83 loc) · 3.01 KB
/
LogBuffer.cpp
File metadata and controls
102 lines (83 loc) · 3.01 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
/**
* @file LogBuffer.cpp
* @copyright 2023 Thomas Watson
* Part of the EnviroDIY ModularSensors library for Arduino
* @author Thomas Watson <twatson52@icloud.com>
*
* @brief Implements the LogBuffer class.
*
* This class buffers logged timestamps and variable values for transmission.
*/
#include "LogBuffer.h"
#include <string.h>
// Constructor
LogBuffer::LogBuffer() {}
// Destructor
LogBuffer::~LogBuffer() {}
void LogBuffer::setNumVariables(uint8_t numVariables_) {
// each record is one uint32_t to hold the timestamp, plus N floats to hold
// each variable's value
recordSize = sizeof(uint32_t) + sizeof(float) * numVariables_;
numVariables = numVariables_;
// this scrambles all the data in the buffer so clear it out
clear();
}
void LogBuffer::clear(void) {
// clear out the buffer
numRecords = 0;
dataBufferTail = 0;
dataBufferHead = 0;
_bufferOverflow = false;
}
uint8_t LogBuffer::getNumVariables(void) {
return numVariables;
}
int LogBuffer::getNumRecords(void) {
return numRecords;
}
uint8_t LogBuffer::getPercentFull(void) {
uint32_t bytesFull = (uint32_t)numRecords * (uint32_t)recordSize;
uint32_t bytesTotal = MS_LOG_DATA_BUFFER_SIZE;
return (uint8_t)((bytesFull * (uint32_t)100) / bytesTotal);
}
int LogBuffer::addRecord(uint32_t timestamp) {
// check how many records currently exist
int record = numRecords;
// compute position of the new record's timestamp in the buffer
// (the timestamp is the first data in the record)
size_t pos = record * recordSize;
// verify we have sufficient space for the record and bail if not
if (MS_LOG_DATA_BUFFER_SIZE - pos < recordSize) { return -1; }
// write the timestamp to the record
memcpy(static_cast<void*>(&dataBuffer[pos]), static_cast<void*>(×tamp),
sizeof(uint32_t));
numRecords += 1; // just added another record
// return the index of the record number just created
return record;
}
void LogBuffer::setRecordValue(int record, uint8_t variable, float value) {
// compute position of this value in the buffer
size_t pos = record * recordSize + sizeof(uint32_t) +
variable * sizeof(float);
// write the value to the record
memcpy(static_cast<void*>(&dataBuffer[pos]), static_cast<void*>(&value),
sizeof(float));
}
uint32_t LogBuffer::getRecordTimestamp(int record) {
// read the timestamp from the record (which is the first data in it)
uint32_t timestamp;
memcpy(static_cast<void*>(×tamp),
static_cast<void*>(&dataBuffer[record * recordSize]),
sizeof(uint32_t));
return timestamp;
}
float LogBuffer::getRecordValue(int record, uint8_t variable) {
// compute position of this value in the buffer
size_t pos = record * recordSize + sizeof(uint32_t) +
variable * sizeof(float);
// read the value from the record
float value;
memcpy(static_cast<void*>(&value), static_cast<void*>(&dataBuffer[pos]),
sizeof(float));
return value;
}