mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2026-09-11 19:55:14 +02:00
Extend history buffer for flexible data storage frequency
This commit is contained in:
@@ -228,6 +228,7 @@ void HstryBuf::init(const String& format, int updFreq, int mltplr, double minVal
|
||||
hstryBuf.setMetaData(boatDataName, format, updFreq, mltplr, minVal, maxVal);
|
||||
hstryMin = minVal;
|
||||
hstryMax = maxVal;
|
||||
bufUpdateTime = 0;
|
||||
if (!boatValue->valid) {
|
||||
boatValue->setFormat(format);
|
||||
boatValue->value = std::numeric_limits<double>::max(); // mark current value invalid
|
||||
@@ -244,19 +245,24 @@ void HstryBuf::add(double value)
|
||||
|
||||
void HstryBuf::handle(bool useSimuData, CommonData& common)
|
||||
{
|
||||
std::unique_ptr<GwApi::BoatValue> tmpBVal; // Temp variable to get formatted and converted data value from OBP60Formatter
|
||||
if (millis() >= (bufUpdateTime + hstryBuf.getUpdFreq())) {
|
||||
bufUpdateTime = millis();
|
||||
LOG_DEBUG(GwLog::DEBUG, "HstryBuf::handle: name: %s, frequency: %d, bufUpdateTime: %d, value: %.3f", hstryBuf.getName(), hstryBuf.getUpdFreq(), bufUpdateTime, boatValue->value);
|
||||
|
||||
if (boatValue->valid) {
|
||||
add(boatValue->value);
|
||||
} else if (useSimuData) { // add simulated value to history buffer
|
||||
tmpBVal = std::unique_ptr<GwApi::BoatValue>(new GwApi::BoatValue(boatDataName)); // create temporary boat value for retrieval of simulation value
|
||||
tmpBVal->setFormat(boatValue->getFormat());
|
||||
tmpBVal->value = boatValue->value;
|
||||
tmpBVal->valid = boatValue->valid;
|
||||
double simSIValue = formatValue(tmpBVal.get(), common).value; // simulated value is generated at <formatValue>; here: retreive SI value
|
||||
add(simSIValue);
|
||||
} else {
|
||||
// here we will add invalid (DBL_MAX) value; this will mark periods of missing data in buffer together with a timestamp
|
||||
if (boatValue->valid) {
|
||||
add(boatValue->value);
|
||||
|
||||
} else if (useSimuData) { // add simulated value to history buffer
|
||||
std::unique_ptr<GwApi::BoatValue> tmpBVal; // Temp variable to get formatted and converted data value from OBP60Formatter
|
||||
tmpBVal = std::unique_ptr<GwApi::BoatValue>(new GwApi::BoatValue(boatDataName)); // create temporary boat value for retrieval of simulation value
|
||||
tmpBVal->setFormat(boatValue->getFormat());
|
||||
tmpBVal->value = boatValue->value;
|
||||
tmpBVal->valid = boatValue->valid;
|
||||
double simSIValue = formatValue(tmpBVal.get(), common).value; // simulated value is generated at <formatValue>; here: retreive SI value
|
||||
add(simSIValue);
|
||||
} else {
|
||||
// here we will add invalid (DBL_MAX) value; this will mark periods of missing data in buffer together with a timestamp
|
||||
}
|
||||
}
|
||||
}
|
||||
// --- End Class HstryBuf ---------------
|
||||
@@ -287,7 +293,8 @@ void HstryBuffers::addBuffer(const String& name)
|
||||
|
||||
hstryBuffers[name] = std::unique_ptr<HstryBuf>(new HstryBuf(name, size, boatValueList, logger));
|
||||
hstryBuffers[name]->init(valueFormat, hstryUpdFreq, mltplr, bufferMinVal, bufferMaxVal);
|
||||
LOG_DEBUG(GwLog::DEBUG, "HstryBuffers: new buffer added: name: %s, format: %s, multiplier: %d, min value: %.2f, max value: %.2f", name, valueFormat, mltplr, bufferMinVal, bufferMaxVal);
|
||||
LOG_DEBUG(GwLog::DEBUG, "HstryBuffers: new buffer added: name: %s, format: %s, frequency: %d, multiplier: %d, min value: %.2f, max value: %.2f", name, valueFormat, hstryUpdFreq,
|
||||
mltplr, bufferMinVal, bufferMaxVal);
|
||||
}
|
||||
|
||||
// Handle all registered history buffers
|
||||
|
||||
@@ -39,6 +39,7 @@ private:
|
||||
String boatDataName;
|
||||
double hstryMin;
|
||||
double hstryMax;
|
||||
unsigned long bufUpdateTime;
|
||||
GwApi::BoatValue* boatValue;
|
||||
GwLog* logger;
|
||||
|
||||
@@ -62,17 +63,19 @@ private:
|
||||
struct HistoryParams {
|
||||
int hstryUpdFreq; // update frequency of history buffer (documentation only)
|
||||
int mltplr; // specifies actual value precision being storable:
|
||||
// [10000: 0 - 6.5535 | 1000: 0 - 65.535 | 100: 0 - 650.35 | 10: 0 - 6503.5
|
||||
// [10000: 0 - 6.5535 | 1000: 0 - 65.535 | 100: 0 - 655.35 | 10: 0 - 6553.5 | 1: 0 - 65535]
|
||||
double bufferMinVal; // minimum valid data value
|
||||
double bufferMaxVal; // maximum valid data value
|
||||
String format; // format of data type
|
||||
};
|
||||
|
||||
// Define buffer parameters for supported boat data type
|
||||
// Structure: name, frequency, multiplier, minVal, maxVal, format
|
||||
std::map<String, HistoryParams> bufferParams = {
|
||||
{ "AWA", { 1000, 10000, 0.0, M_TWOPI, "formatWind" } },
|
||||
{ "AWD", { 1000, 10000, 0.0, M_TWOPI, "formatCourse" } },
|
||||
{ "AWS", { 1000, 1000, 0.0, 65.0, "formatKnots" } },
|
||||
{ "BARO", {60000, 10, 0.0, 6553, "formatXdr:P:B"}},
|
||||
{ "COG", { 1000, 10000, 0.0, M_TWOPI, "formatCourse" } },
|
||||
{ "DBS", { 1000, 100, 0.0, 650.0, "formatDepth" } },
|
||||
{ "DBT", { 1000, 100, 0.0, 650.0, "formatDepth" } },
|
||||
|
||||
@@ -68,6 +68,7 @@ public:
|
||||
bool getMetaData(String& name, String& format);
|
||||
String getName() const; // Get buffer name
|
||||
String getFormat() const; // Get buffer data format
|
||||
int getUpdFreq() const; // Get buffer update frequency
|
||||
void add(const double& value); // Add a new value to buffer
|
||||
double get(size_t index) const; // Get value at specific position (0-based index from oldest to newest)
|
||||
double getFirst() const; // Get the first (oldest) value in buffer
|
||||
|
||||
@@ -106,6 +106,13 @@ String RingBuffer<T>::getFormat() const
|
||||
return dataFmt;
|
||||
}
|
||||
|
||||
// Get buffer update frequency
|
||||
template <typename T>
|
||||
int RingBuffer<T>::getUpdFreq() const
|
||||
{
|
||||
return updFreq;
|
||||
}
|
||||
|
||||
// Add a new value to buffer
|
||||
template <typename T>
|
||||
void RingBuffer<T>::add(const double& value)
|
||||
|
||||
Reference in New Issue
Block a user