mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-15 15:03:07 +01:00
Handling of missing data
This commit is contained in:
@@ -52,11 +52,12 @@ public:
|
||||
|
||||
int get(int index) const
|
||||
// Get value by index in [0..360 deg] format (0 = oldest, count-1 = newest)
|
||||
// INT_MIN indicates missing value or wrong index
|
||||
{
|
||||
int realIndex;
|
||||
|
||||
if (index < 0 || index >= count) {
|
||||
return -1; // Invalid index
|
||||
return INT_MIN; // Invalid index
|
||||
}
|
||||
realIndex = (first + index) % SIZE;
|
||||
return buffer[realIndex];
|
||||
@@ -78,7 +79,6 @@ public:
|
||||
return value;
|
||||
}
|
||||
default:
|
||||
// Return value in [-180..180 deg] format
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -105,42 +105,23 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/* int getMin(int amount) const
|
||||
// Get minimum value of the last <amount> values of buffer
|
||||
{
|
||||
if (count == 0 || amount <= 0) {
|
||||
return -1;
|
||||
} else if (amount > count) {
|
||||
amount = count; // Limit to available values
|
||||
}
|
||||
|
||||
if (last + amount <= SIZE) {
|
||||
// No wrap-around
|
||||
return *std::min_element(buffer.begin() + last, buffer.begin() + (last + amount));
|
||||
} else {
|
||||
// Wrap-around
|
||||
int min1 = *std::min_element(buffer.begin() + ((last - amount) % last), buffer.begin() + last);
|
||||
int min2 = *std::min_element(buffer.end() - (count - amount - last > 0 ? amount - last : 0), buffer.end());
|
||||
return std::min(min1, min2);
|
||||
}
|
||||
} */
|
||||
|
||||
int getMin(int amount) const
|
||||
// Get minimum value of the last <amount> values of buffer
|
||||
// INT_MIN indicates missing value or wrong index
|
||||
|
||||
{
|
||||
if (count == 0 || amount <= 0)
|
||||
return -1;
|
||||
return INT_MIN;
|
||||
if (amount > count)
|
||||
amount = count;
|
||||
|
||||
int minVal = INT_MAX;
|
||||
int value = 0;
|
||||
// Start from the newest value (last) and go backwards x times
|
||||
for (int i = 0; i < amount; ++i) {
|
||||
// int idx = (last - i + SIZE) % SIZE;
|
||||
// if (buffer[idx] < minVal)
|
||||
// minVal = buffer[idx];
|
||||
if (get(i) > minVal)
|
||||
minVal = get(i);
|
||||
value = get(i);
|
||||
if (value < minVal)
|
||||
minVal = value;
|
||||
}
|
||||
return minVal;
|
||||
}
|
||||
@@ -163,6 +144,8 @@ public:
|
||||
|
||||
int getMax(int amount) const
|
||||
// Get maximum value of the last <amount> values of buffer
|
||||
// INT_MIN indicates missing value or wrong index
|
||||
|
||||
{
|
||||
if (count == 0 || amount <= 0)
|
||||
return -1;
|
||||
@@ -170,42 +153,21 @@ public:
|
||||
amount = count;
|
||||
|
||||
int maxVal = INT_MIN;
|
||||
int value = 0;
|
||||
// Start from the newest value (last) and go backwards x times
|
||||
for (int i = 0; i < amount; ++i) {
|
||||
// int idx = (last - i + SIZE) % SIZE;
|
||||
// if (buffer[idx] > maxVal)
|
||||
// maxVal = buffer[idx];
|
||||
if (get(i) > maxVal)
|
||||
maxVal = get(i);
|
||||
value = get(i);
|
||||
if (value > maxVal)
|
||||
maxVal = value;
|
||||
}
|
||||
return maxVal;
|
||||
}
|
||||
|
||||
/* int getMax(int amount) const
|
||||
// Get maximum value of the last <amount> values of buffer
|
||||
{
|
||||
if (count == 0 || amount <= 0) {
|
||||
return -1;
|
||||
} else if (amount > count) {
|
||||
amount = count; // Limit to available values
|
||||
}
|
||||
|
||||
if (first + count <= SIZE) {
|
||||
// No wrap-around
|
||||
return *std::max_element(buffer.begin() + last, buffer.begin() + (last + amount));
|
||||
} else {
|
||||
// Wrap-around
|
||||
int max1 = *std::max_element(buffer.begin() + ((last - amount) % last), buffer.begin() + last);
|
||||
int max2 = *std::max_element(buffer.end(), buffer.end() - (amount - last > 0 ? amount - last : 0);
|
||||
return std::max(max1, max2);
|
||||
}
|
||||
} */
|
||||
|
||||
int getMid(int amount) const
|
||||
// Get middle value in the buffer
|
||||
{
|
||||
if (count == 0) {
|
||||
return -1; // Buffer is empty
|
||||
return INT_MIN; // Buffer is empty
|
||||
}
|
||||
return (getMin(amount) + getMax(amount)) / 2;
|
||||
}
|
||||
@@ -214,15 +176,20 @@ public:
|
||||
// Get maximum difference of last <amount> of buffer values to center value
|
||||
{
|
||||
if (count == 0 || amount <= 0)
|
||||
return -1;
|
||||
return INT_MIN;
|
||||
if (amount > count)
|
||||
amount = count;
|
||||
|
||||
int value = 0;
|
||||
int maxRng = INT_MIN;
|
||||
int rng = 0;
|
||||
// Start from the newest value (last) and go backwards x times
|
||||
for (int i = 0; i < amount; ++i) {
|
||||
rng = abs(((get(i) - center + 540) % 360) - 180);
|
||||
value = get(i);
|
||||
if (value == INT_MIN) {
|
||||
continue;
|
||||
}
|
||||
rng = abs(((value - center + 540) % 360) - 180);
|
||||
if (rng > maxRng)
|
||||
maxRng = rng;
|
||||
}
|
||||
@@ -231,20 +198,6 @@ public:
|
||||
}
|
||||
return maxRng;
|
||||
}
|
||||
|
||||
void mvStart(int delta)
|
||||
// Move the start index of buffer forward by <delta> positions -> virtually delete oldest <delta> data entries
|
||||
{
|
||||
first = (first + delta) % SIZE;
|
||||
if (count > delta)
|
||||
count -= delta;
|
||||
else
|
||||
count = 0;
|
||||
if (first == 0)
|
||||
last = count;
|
||||
else
|
||||
last = first - 1;
|
||||
}
|
||||
};
|
||||
|
||||
// ****************************************************************
|
||||
@@ -253,8 +206,8 @@ class PageWindPlot : public Page {
|
||||
bool keylock = false; // Keylock
|
||||
// int16_t lp = 80; // Pointer length
|
||||
char chrtMode = 'D'; // Chart mode: 'D' for TWD, 'S' for TWS, 'B' for both06121990
|
||||
int dataInterv = 1; // Update interval for wind history chart:
|
||||
// (1)|(2)|(3)|(4) seconds for approx. 4, 8, 12, 16 min. history chart
|
||||
int dataIntv = 1; // Update interval for wind history chart:
|
||||
// (1)|(2)|(3)|(4) seconds for approx. 4, 8, 12, 16 min. history chart
|
||||
bool showTWS = true; // Show TWS value in chart area
|
||||
|
||||
public:
|
||||
@@ -289,14 +242,14 @@ public:
|
||||
|
||||
// Set interval for wind history chart update time
|
||||
if (key == 2) {
|
||||
if (dataInterv == 1) {
|
||||
dataInterv = 2;
|
||||
} else if (dataInterv == 2) {
|
||||
dataInterv = 3;
|
||||
} else if (dataInterv == 3) {
|
||||
dataInterv = 4;
|
||||
if (dataIntv == 1) {
|
||||
dataIntv = 2;
|
||||
} else if (dataIntv == 2) {
|
||||
dataIntv = 3;
|
||||
} else if (dataIntv == 3) {
|
||||
dataIntv = 4;
|
||||
} else {
|
||||
dataInterv = 1;
|
||||
dataIntv = 1;
|
||||
}
|
||||
return 0; // Commit the key
|
||||
}
|
||||
@@ -351,7 +304,7 @@ public:
|
||||
int numWndValues; // number of wind values available for current interval selection
|
||||
static int linesToShow; // current number of lines to display on chart
|
||||
static int bufStart; // 1st data value in buffer to show
|
||||
static int oldDataInterv; // remember recent user selection of data interval
|
||||
static int oldDataIntv; // remember recent user selection of data interval
|
||||
static int newDate; // indicates for higher time intervals that new date is available
|
||||
|
||||
static int wndCenter = INT_MIN; // chart wind center value position; init value indicates that wndCenter is not set yet
|
||||
@@ -386,7 +339,7 @@ public:
|
||||
twdValue = 0;
|
||||
bufStart = 0;
|
||||
linesToShow = 0;
|
||||
oldDataInterv = dataInterv;
|
||||
oldDataIntv = dataIntv;
|
||||
newDate = 0;
|
||||
}
|
||||
|
||||
@@ -442,21 +395,22 @@ public:
|
||||
}
|
||||
|
||||
// Identify buffer sizes and buffer position to print on the chart
|
||||
intvBufSize = cHeight * dataInterv;
|
||||
intvBufSize = cHeight * dataIntv;
|
||||
count = windDirHstry.getSize();
|
||||
numWndValues = min(count, intvBufSize);
|
||||
newDate++;
|
||||
if (dataInterv != oldDataInterv) {
|
||||
linesToShow = min(numWndValues / dataInterv, max(0, cHeight - 40));
|
||||
bufStart = max(0, (count - (linesToShow * dataInterv)));
|
||||
oldDataInterv = dataInterv;
|
||||
} else if (newDate >= dataInterv) {
|
||||
linesToShow = min(numWndValues / dataInterv, linesToShow + 1);
|
||||
if (dataIntv != oldDataIntv) {
|
||||
linesToShow = min(numWndValues / dataIntv, max(0, cHeight - 40));
|
||||
bufStart = max(0, (count - (linesToShow * dataIntv)));
|
||||
oldDataIntv = dataIntv;
|
||||
} else if (newDate >= dataIntv) {
|
||||
linesToShow = min(numWndValues / dataIntv, linesToShow + 1);
|
||||
newDate = 0;
|
||||
}
|
||||
if (count == bufSize)
|
||||
if (count == bufSize) {
|
||||
bufStart--; // show the latest wind values in buffer; keep 1st value constant in a rolling buffer when new data is added
|
||||
LOG_DEBUG(GwLog::ERROR, "PageWindPlot DateSet: TWD: %d, count: %d, bufStart: %d, linesToShow: %d, newDate: %d", twdValue, count, bufStart, linesToShow, newDate);
|
||||
}
|
||||
LOG_DEBUG(GwLog::ERROR, "PageWindPlot Dataset: TWD: %d, count: %d, intvBufSize: %d, numWndValues: %d, bufStart: %d, linesToShow: %d, newDate: %d", twdValue, count, intvBufSize, numWndValues, bufStart, linesToShow, newDate);
|
||||
|
||||
if (bvalue == NULL)
|
||||
return;
|
||||
@@ -466,14 +420,14 @@ public:
|
||||
|
||||
// initialize chart range values
|
||||
if (wndCenter == INT_MIN) {
|
||||
wndCenter = windDirHstry.get(max(0, numWndValues - intvBufSize)); // get 1st value of current data interval
|
||||
wndCenter = max(0, windDirHstry.get(numWndValues - intvBufSize)); // get 1st value of current data interval
|
||||
wndCenter = (int((wndCenter + (wndCenter >= 0 ? 5 : -5)) / 10) * 10) % 360; // Set new center value; round to nearest 10 degree value; 360° -> 0°
|
||||
diffRng = dfltRng;
|
||||
chrtRng = dfltRng;
|
||||
} else {
|
||||
// check and adjust range between left, center, and right chart limit
|
||||
diffRng = windDirHstry.getRng(wndCenter, numWndValues);
|
||||
diffRng = (diffRng < 0 ? 0 : diffRng); // If no data in buffer, set range to 0
|
||||
diffRng = (diffRng == INT_MIN ? 0 : diffRng);
|
||||
if (diffRng > chrtRng) {
|
||||
chrtRng = int((diffRng + (diffRng >= 0 ? 9 : -1)) / 10) * 10; // Round up to next 10 degree value
|
||||
} else if (diffRng + 10 < chrtRng) { // Reduce chart range for higher resolution if possible
|
||||
@@ -484,12 +438,10 @@ public:
|
||||
LOG_DEBUG(GwLog::ERROR, "PageWindPlot Range. wndCenter: %d, numWndValues: %d, min: %d, max: %d, diffrng: %d, chrtRng: %d ", wndCenter, numWndValues, debugMin, debugMax, diffRng, chrtRng);
|
||||
}
|
||||
chrtScl = float(width) / float(chrtRng) / 2.0; // Chart scale: pixels per degree
|
||||
// wndCenter = windDirHstry.get(max(0, numWndValues - intvBufSize)); // Get 1st value of current data interval
|
||||
// wndCenter = int((wndCenter + (wndCenter >= 0 ? 5 : -5)) / 10) * 10; // Round to nearest 10 degree value
|
||||
wndLeft = wndCenter - chrtRng;
|
||||
if (wndLeft < 0)
|
||||
wndLeft += 360;
|
||||
wndRight = wndCenter + chrtRng - 1;
|
||||
wndRight = (chrtRng < 180 ? wndCenter + chrtRng : wndCenter + chrtRng - 1);
|
||||
if (wndRight >= 360)
|
||||
wndRight -= 360;
|
||||
// LOG_DEBUG(GwLog::DEBUG, "PageWindPlot dataValue[0]: %f, windValue: %d, count: %d, diffRng: %d, chartRng: %d, Center: %d, scale: %f", double(dataValue[0] * radToDeg),
|
||||
@@ -502,9 +454,6 @@ public:
|
||||
getdisplay().setPartialWindow(0, 0, width, height); // Set partial update
|
||||
getdisplay().setTextColor(commonData->fgcolor);
|
||||
|
||||
// Horizontal top line for orientation -> to be deleted
|
||||
// getdisplay().fillRect(0, 20, width, 1, commonData->fgcolor);
|
||||
|
||||
// chart lines
|
||||
getdisplay().fillRect(0, yOffset, width, 2, commonData->fgcolor);
|
||||
getdisplay().fillRect(xCenter, yOffset, 1, cHeight, commonData->fgcolor);
|
||||
@@ -514,21 +463,22 @@ public:
|
||||
getdisplay().setFont(&Ubuntu_Bold12pt7b);
|
||||
getdisplay().setCursor(xCenter - 88, yOffset - 3);
|
||||
getdisplay().print("TWD"); // Wind name
|
||||
getdisplay().setCursor(xCenter - 20, yOffset - 3);
|
||||
// getdisplay().setCursor(xCenter - 20, yOffset - 3);
|
||||
snprintf(sWndLbl, 4, "%03d", (wndCenter < 0) ? (wndCenter + 360) : wndCenter);
|
||||
getdisplay().print(sWndLbl); // Wind center value
|
||||
getdisplay().drawCircle(xCenter + 25, yOffset - 16, 2, commonData->fgcolor); // <degree> symbol
|
||||
getdisplay().drawCircle(xCenter + 25, yOffset - 16, 3, commonData->fgcolor); // <degree> symbol
|
||||
drawTextCenter(xCenter, yOffset - 11, sWndLbl);
|
||||
// getdisplay().print(sWndLbl); // Wind center value
|
||||
getdisplay().drawCircle(xCenter + 25, yOffset - 17, 2, commonData->fgcolor); // <degree> symbol
|
||||
getdisplay().drawCircle(xCenter + 25, yOffset - 17, 3, commonData->fgcolor); // <degree> symbol
|
||||
getdisplay().setCursor(1, yOffset - 3);
|
||||
snprintf(sWndLbl, 4, "%03d", (wndLeft < 0) ? (wndLeft + 360) : wndLeft);
|
||||
getdisplay().print(sWndLbl); // Wind left value
|
||||
getdisplay().drawCircle(46, yOffset - 16, 2, commonData->fgcolor); // <degree> symbol
|
||||
getdisplay().drawCircle(46, yOffset - 16, 3, commonData->fgcolor); // <degree> symbol
|
||||
getdisplay().setCursor(width - 51, yOffset - 3);
|
||||
getdisplay().drawCircle(46, yOffset - 17, 2, commonData->fgcolor); // <degree> symbol
|
||||
getdisplay().drawCircle(46, yOffset - 17, 3, commonData->fgcolor); // <degree> symbol
|
||||
getdisplay().setCursor(width - 50, yOffset - 3);
|
||||
snprintf(sWndLbl, 4, "%03d", (wndRight < 0) ? (wndRight + 360) : wndRight);
|
||||
getdisplay().print(sWndLbl); // Wind right value
|
||||
getdisplay().drawCircle(width - 5, yOffset - 16, 2, commonData->fgcolor); // <degree> symbol
|
||||
getdisplay().drawCircle(width - 5, yOffset - 16, 3, commonData->fgcolor); // <degree> symbol
|
||||
getdisplay().drawCircle(width - 5, yOffset - 17, 2, commonData->fgcolor); // <degree> symbol
|
||||
getdisplay().drawCircle(width - 5, yOffset - 17, 3, commonData->fgcolor); // <degree> symbol
|
||||
|
||||
// Draw wind values in chart
|
||||
//***********************************************************
|
||||
@@ -536,10 +486,11 @@ public:
|
||||
// if (count == bufSize)
|
||||
// bufStart--; // show the latest wind values in buffer; keep 1st value constant in a rolling buffer
|
||||
for (int i = 0; i < linesToShow; i++) {
|
||||
chrtVal = windDirHstry.get(bufStart + (i * dataInterv)); // show the latest wind values in buffer; keep 1st value constant in a rolling buffer
|
||||
chrtVal = windDirHstry.get(bufStart + (i * dataIntv)); // show the latest wind values in buffer; keep 1st value constant in a rolling buffer
|
||||
x = ((chrtVal - wndLeft + 360) % 360) * chrtScl;
|
||||
y = yOffset + cHeight - i; // Position in chart area
|
||||
LOG_DEBUG(GwLog::ERROR, "PageWindPlot Chart: i: %d, chrtVal: %d, chrtPrevVal: %d, bufStart: %d count: %d, linesToShow: %d", i, chrtVal, chrtPrevVal, bufStart, count, linesToShow);
|
||||
if (i > linesToShow - 15)
|
||||
LOG_DEBUG(GwLog::ERROR, "PageWindPlot Chart: i: %d, chrtVal: %d, chrtPrevVal: %d, bufStart: %d count: %d, linesToShow: %d", i, chrtVal, chrtPrevVal, bufStart, count, linesToShow);
|
||||
|
||||
if (i == 0) {
|
||||
prevX = x; // just a dot for 1st chart point
|
||||
@@ -570,12 +521,14 @@ public:
|
||||
|
||||
if (i == (cHeight - 1)) { // Reaching chart area top end ()
|
||||
linesToShow -= min(40, cHeight); // free top 40 lines of chart for new values
|
||||
bufStart = max(0, count - (linesToShow * dataInterv)); // next start value in buffer to show
|
||||
bufStart = max(0, count - (linesToShow * dataIntv)); // next start value in buffer to show
|
||||
// windDirHstry.mvStart(); // virtually delete 40 values from buffer
|
||||
if ((windDirHstry.getMin(numWndValues) > wndCenter) || (windDirHstry.getMax(numWndValues) < wndCenter)) {
|
||||
// Check if all wind value are left or right of center value -> optimize chart range
|
||||
int mid = windDirHstry.getMid(numWndValues);
|
||||
wndCenter = int((mid + (mid >= 0 ? 5 : -5)) / 10) * 10; // Set new center value; round to nearest 10 degree value
|
||||
if (mid != INT_MIN) {
|
||||
wndCenter = int((mid + (mid >= 0 ? 5 : -5)) / 10) * 10; // Set new center value; round to nearest 10 degree value
|
||||
}
|
||||
}
|
||||
LOG_DEBUG(GwLog::ERROR, "PageWindPlot FreeTop: cHeight: %d, LinesToShow: %d, numWndValues: %d, wndCenter: %d, bufStart: %d", cHeight, linesToShow, numWndValues, wndCenter, bufStart);
|
||||
break;
|
||||
@@ -637,13 +590,16 @@ public:
|
||||
int yPos;
|
||||
getdisplay().setFont(&Ubuntu_Bold8pt7b);
|
||||
for (int i = 1; i <= 3; i++) {
|
||||
yPos = yOffset + (i * 60); // Y position for label
|
||||
// yPos = yOffset + cHeight - (i * 60); // Y position for label
|
||||
if (numWndValues < intvBufSize) {
|
||||
// chart initially filled from botton to top -> revert minute labels
|
||||
yPos = yOffset + cHeight - (i * 60);
|
||||
} else {
|
||||
yPos = yOffset + (i * 60);
|
||||
}
|
||||
getdisplay().fillRect(0, yPos, width, 1, commonData->fgcolor);
|
||||
getdisplay().fillRect(0, yPos - 9, 26, 16, commonData->bgcolor); // Clear small area to remove potential chart lines
|
||||
// getdisplay().fillRect(0, yPos, 8, 2, commonData->fgcolor);
|
||||
getdisplay().fillRect(0, yPos - 8, 26, 16, commonData->bgcolor); // Clear small area to remove potential chart lines
|
||||
getdisplay().setCursor(1, yPos + 4);
|
||||
snprintf(sWndYAx, 4, "%3d", i * dataInterv * -1);
|
||||
snprintf(sWndYAx, 4, "%3d", i * dataIntv * -1);
|
||||
getdisplay().print(sWndYAx); // Wind value label
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user