swimtracker-firmware/session/SessionChunk.h

110 lines
2.7 KiB
C
Raw Normal View History

#include "StreamingMsgPackEncoder.h"
template<typename Measurement_T, uint_t SIZE>
class SessionChunk
{
public:
SessionChunk()
: nextFree(0), sessionStartTime(0), startIndex(0)
{}
void init(uint32_t epochStartTime, uint32_t startIdx)
{
nextFree = 0;
sessionStartTime = epochStartTime;
startIndex = startIdx;
}
uint32_t getStartTime() const {
return sessionStartTime;
}
uint32_t getStartIndex() const {
return startIndex;
}
uint32_t numMeasurements() const {
return nextFree;
}
bool addPoint(Measurement_T measurement)
{
if( nextFree >= SIZE)
return false;
values[nextFree] = measurement;
nextFree++;
return true;
}
template<typename T>
void serialize(StreamingMsgPackEncoder<T> & encoder) const
{
sendHeader(encoder, sessionStartTime, startIndex);
2019-08-18 19:41:19 +02:00
encoder.sendArray(values, nextFree);
}
2019-08-18 19:41:19 +02:00
/*
template<typename T>
void serialize(StreamingMsgPackEncoder<T> & encoder, uint32_t start, uint32_t end) const
{
if( start < this->startIndex )
start = this->startIndex;
if( end == 0 ) {
end = start + this->nextFree;
}
sendHeader(encoder, sessionStartTime, start);
bool sendEmpty =
(start >= end) ||
(end <= this->startIndex) ||
(start >= (this->startIndex + this->nextFree));
if( sendEmpty ) {
encoder.sendArray(nullptr, 0);
} else {
const uint32_t idxStart = (start - this->startIndex);
const uint32_t length = min(nextFree, end - start);
encoder.sendArray(values + idxStart, length);
}
2019-08-18 19:41:19 +02:00
}*/
template<typename T>
static uint32_t valueOffset()
{
StreamingMsgPackEncoder<T> encoder(nullptr);
encoder.setSizeCountMode(true);
sendHeader(encoder, 0, 0);
2019-08-18 19:58:13 +02:00
encoder.template sendArrayHeader<Measurement_T>(0);
return encoder.getContentLength();
}
Measurement_T * getDataPointer() {
return values;
}
const Measurement_T * getDataPointer() const {
return values;
}
template<typename T>
static void sendHeader(StreamingMsgPackEncoder<T> & encoder, uint32_t sessionStartTime, uint32_t startIndex)
{
encoder.sendMap16(3);
encoder.sendString255("sessionStartTime");
encoder.sendInt(sessionStartTime);
encoder.sendString255("startIndex");
encoder.sendInt(startIndex);
encoder.sendString255("values");
}
private:
uint32_t nextFree = 0;
uint32_t sessionStartTime;
uint32_t startIndex;
Measurement_T values[SIZE];
};