Fixes for TWS flip, scale calculation, chart range overflow; add axis lines

This commit is contained in:
Ulrich Meine 2025-06-12 23:41:15 +02:00
parent f0aba89301
commit 9b504469bc
1 changed files with 94 additions and 75 deletions

View File

@ -24,16 +24,13 @@ public:
return false;
}
SIZE = size;
buffer.resize(size); // allocate buffer
buffer.resize(size, INT_MIN); // allocate buffer
return true;
}
void add(int value)
// Add a new value
// Add a new value; store in [0..360 deg] format
{
// if (value > 180) {
// value -= 360; // Normalize value to -180..180 to make min/max calculations working
// }
buffer[head] = value;
head = (head + 1) % SIZE;
if (count < SIZE) {
@ -45,7 +42,6 @@ public:
int get(int index) const
// Get value by index in [0..360 deg] format (0 = oldest, count-1 = newest)
// **** Get value by index in [-180..180 deg] format (0 = oldest, count-1 = newest)
{
int realIndex;
@ -54,7 +50,6 @@ public:
}
realIndex = (first + index) % SIZE;
return buffer[realIndex];
// }
}
int get(int index, int deg) const
@ -125,19 +120,20 @@ public:
return (getMin() + getMax()) / 2;
}
int getRng(int center) const
int getRng(int center)
// Get range of values in the buffer relative to a center value
{
if (count == 0) {
return -1; // Buffer is empty
}
int min = getMin();
int max = getMax();
int rng = std::max(abs((min - center + 540) % 360 - 180), abs((max - center + 540) % 360 - 180));
// if (rng < -180) {
// wind value crosses 180 degree line, so we need to adjust the chart range
// ********************** hier an der Skalierung arbeiten ********************
// }
// int rng = std::max(abs((min - center + 540) % 360 - 180), abs((max - center + 540) % 360 - 180));
int rng = std::max(abs(min - center), abs(max - center));
if (rng > 180) { // should never happen, but just in case
rng = 180;
}
return rng;
}
@ -176,6 +172,7 @@ class PageWindPlot : public Page {
char mode = 'D'; // Chart mode: 'D' for TWD, 'S' for TWS
int updTime = 1; // Update interval for wind history chart:
// (1)|(2)|(3)|(5) seconds for 3, 7, 10, 15 min. history chart
bool showTWS = true; // Show TWS value in chart area
public:
PageWindPlot(CommonData& common)
@ -189,6 +186,7 @@ public:
Page::setupKeys();
commonData->keydata[0].label = "MODE";
commonData->keydata[1].label = "INTV";
commonData->keydata[4].label = "TWS";
}
// Key functions
@ -216,7 +214,12 @@ public:
} else {
updTime = 1;
}
setupKeys(); // Update key labels
return 0; // Commit the key
}
// Switch TWS on/off
if (key == 5) {
showTWS = !showTWS;
return 0; // Commit the key
}
@ -250,9 +253,9 @@ public:
int width = getdisplay().width(); // Get screen width
int height = getdisplay().height(); // Get screen height
static const int yOffset = 48; // Offset for y coordinates of chart area 76
int cHeight = height - yOffset - 22; // height of chart area 98
// int cHeight = 80; // height of chart area
static const int yOffset = 48; // Offset for y coordinates of chart area
int cHeight = height - yOffset - 22; // height of chart area
// int cHeight = 60; // height of chart area
int xCenter = width / 2; // Center of screen in x direction
// static bool plotShift = false; // Flag to indicate if chartplot data have been shifted
static const float radToDeg = 180.0 / M_PI; // Conversion factor from radians to degrees
@ -262,19 +265,23 @@ public:
static int wndRight; // chart wind right value position
static int chrtRng; // Range of wind values from mid wind value to min/max wind value in degrees
int diffRng; // Difference between mid and current wind value
bool rngFlipped = false; // Flag to indicate if range exceeds 180 degrees
static int simWnd = 0; // Simulation value for wind data
static float simTWS = 0; // Simulation value for TWS data
static const int simStep = 10; // Simulation step for wind data
int x, y; // x and y coordinates for drawing
static int prevX, prevY; // Last x and y coordinates for drawing
static int chrtScl; // Scale for wind values in pixels per degree
static float chrtScl; // Scale for wind values in pixels per degree
int chrtVal; // Current wind value
static int chrtPrevVal; // Last wind value in chart area for check if value crosses 180 degree line
int count; // index for next wind value in buffer
static int updCnt = 0; // update counter for wind history chart in seconds
bool isTimeforUpd = true; // Flag to indicate if it is time for chart update
bool TwsFlipped = false; // Flag to indicate if TWS value flipped
LOG_DEBUG(GwLog::LOG, "Display page WindPlot");
if (windValues.getSize() == 0) {
if (!windValues.begin(cHeight)) {
@ -283,8 +290,6 @@ public:
}
}
LOG_DEBUG(GwLog::LOG, "Display page WindPlot");
if (updCnt < updTime) {
// Next update interval not reached yet
updCnt++;
@ -327,12 +332,10 @@ public:
// wndDataValid = windValues.calcTWD(&twdValue, dataValue[1], dataValue[2], dataValue[3], dataValue[4], dataValue[5], dataValue[6]);
}
// ************* falsche Position <isTimeforUpd> ****************
if (isTimeforUpd) {
if (wndDataValid) {
windValues.add(twdValue);
LOG_DEBUG(GwLog::DEBUG, "PageWindPlot: Data 0 valid - dataValue[0]: %f, TWD: %d, cnt: %d, valid0: %d", dataValue[0] * radToDeg, twdValue, count, dataValid[0]);
}
if (simulation) {
@ -346,6 +349,7 @@ public:
}
}
count = windValues.getSize(); // Get number of valid elements in buffer; maximum is cHeight
LOG_DEBUG(GwLog::ERROR, "PageWindPlot: Data 0 valid - dataValue[0]: %f, TWD: %d, cnt: %d, valid0: %d", dataValue[0] * radToDeg, twdValue, count, dataValid[0]);
// Optical warning by limit violation (unused)
if (String(flashLED) == "Limit Violation") {
@ -361,28 +365,31 @@ public:
// initialize chart range values
if (wndCenter == -400) {
wndCenter = windValues.get(0);
wndCenter = (windValues.get(0) < 0 ? 0 : windValues.get(0));
wndCenter = int((wndCenter + (wndCenter >= 0 ? 5 : -5)) / 10) * 10; // Set new center value; round to nearest 10 degree value
diffRng = 30;
chrtRng = 30;
LOG_DEBUG(GwLog::DEBUG, "PageWindPlot initialized. wndCenter: %d, chrtRng: %d ", wndCenter, chrtRng);
} else {
diffRng = windValues.getRng(wndCenter);
// diffRng = max(abs(((windValues.getMax() - wndCenter + 540) % 360) - 180), abs(((windValues.getMin() - wndCenter + 540) % 360) - 180)); // check necessary range size
diffRng = (diffRng < 0 ? 0 : diffRng); // If no data in buffer, set range to 0
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
chrtRng = max(30, 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
chrtRng = max(30, int((diffRng + (diffRng >= 0 ? 9 : -1)) / 10) * 10);
}
LOG_DEBUG(GwLog::ERROR, "PageWindPlot range adjusted. wndCenter: %d, chrtRng: %d ", wndCenter, chrtRng);
}
chrtScl = float(width) / float(chrtRng) / 2.0; // chart scale: pixels per degree
wndLeft = wndCenter - chrtRng;
if (wndLeft < -180)
if (wndLeft < 0)
wndLeft += 360;
wndRight = wndCenter + chrtRng;
if (wndRight >= 180)
if (wndRight >= 360)
wndRight -= 360;
LOG_DEBUG(GwLog::LOG, "PageWindPlot dataValue[0]: %f, windValue: %d, count: %d, diffRng: %d, chartRng: %d", float(dataValue[0] * radToDeg), windValues.get(count - 1), count, diffRng, chrtRng);
LOG_DEBUG(GwLog::ERROR, "PageWindPlot dataValue[0]: %f, windValue: %d, count: %d, diffRng: %d, chartRng: %d, Center: %d, scale: %f", double(dataValue[0] * radToDeg),
(!windValues.get(count - 1) < 0 ? 0 : windValues.get(count - 1)), count, diffRng, chrtRng, wndCenter, chrtScl);
// Draw page
//***********************************************************
@ -422,80 +429,92 @@ public:
// Draw wind values in chart
//***********************************************************
if (wndDataValid || holdValues || simulation) {
prevX = xCenter + ((windValues.get(0) - wndCenter) * chrtScl);
prevY = yOffset + cHeight; // Reset lastY to bottom of chart
LOG_DEBUG(GwLog::ERROR, "PageWindPlot Draw: prevX: %d, chrtPrevVal: %d, wndLeft: %d, chrtScl: %f, count: %d", prevX, chrtPrevVal, wndLeft, chrtScl, count);
for (int i = 0; i < count; i++) {
chrtVal = windValues.get(i); // Get value from buffer
// chrtScl = xCenter / chrtRng; // current scale: pixels per degree
chrtScl = width / chrtRng / 2; // current scale: pixels per degree
// x = xCenter + ((chrtVal - wndCenter) * chrtScl); // Scale to chart width
x = ((chrtVal - wndLeft + 360) % 360) * chrtScl; // Scale to chart width
chrtVal = windValues.get(i);
x = ((chrtVal - wndLeft + 360) % 360) * chrtScl;
y = yOffset + cHeight - i; // Position in chart area
// LOG_DEBUG(GwLog::DEBUG, "PageWindPlot: chrtVal: %d, wndCenter: %d, chrtScl: %d, x: %d, y: %d", chrtVal, wndCenter, chrtScl, x, y);
// Draw line with 2 pixels width; make sure vertical line are drawn correctly
getdisplay().drawLine(prevX, prevY, x, y, commonData->fgcolor);
getdisplay().drawLine(prevX, prevY - 1, (x != prevX) ? x : x - 1, (x != prevX) ? y - 1 : y, commonData->fgcolor);
chrtPrevVal = chrtVal;
if ((abs(chrtVal - wndCenter) > 180) && !rngFlipped) { // If range exceeds 180 degrees, value plotted on other side of chart
rngFlipped = true;
prevX = x; // don't print connecting line to previous value
prevY = y;
}
if (i == 0) { // just a dot for 1st chart point
prevX = x;
prevY = y;
if (i == (cHeight - 1)) {
// Reaching chart area top end
windValues.mvStart(40);
// virtually delete 40 values from buffer
}
// if (i < 30)
// LOG_DEBUG(GwLog::ERROR, "PageWindPlot Chart: x: %d, y: %d prevX: %d, prevY: %d, loop-Counter: %d", x, y, prevX, prevY, count);
// Draw line with 2 pixels width + make sure vertical line are drawn correctly
getdisplay().drawLine(prevX, prevY, x, y, commonData->fgcolor);
getdisplay().drawLine(prevX, prevY - 1, ((x != prevX) ? x : x - 1), ((x != prevX) ? y - 1 : y), commonData->fgcolor);
// chrtPrevVal = chrtVal;
prevX = x;
prevY = y;
if (i == (cHeight - 1)) { // Reaching chart area top end
windValues.mvStart(40); // virtually delete 40 values from buffer
if ((windValues.getMin() > wndCenter) || (windValues.getMax() < wndCenter)) {
// Check if all wind value are left or right of center value -> optimize chart range
int mid = windValues.getMid();
wndCenter = int((mid + (mid >= 0 ? 5 : -5)) / 10) * 10; // Set new center value; round to nearest 10 degree value
rngFlipped = false; // chart value within standard 180 degree range
chrtPrevVal = (windValues.get(0) < 0 ? 0 : windValues.get(0));
}
LOG_DEBUG(GwLog::DEBUG, "PageWindPlot Shift: Min: %d, Max: %d, Mid: %d, new Center: %d", windValues.getMin(), windValues.getMax(), windValues.getMid(), wndCenter);
LOG_DEBUG(GwLog::ERROR, "PageWindPlot Shift: Min: %d, Max: %d, Mid: %d, new Center: %d", windValues.getMin(), windValues.getMax(), windValues.getMid(), wndCenter);
break;
}
}
LOG_DEBUG(GwLog::DEBUG, "PageWindPlot End: prevX: %d, prevY: %d, loop-Counter: %d", prevX, prevY, count);
LOG_DEBUG(GwLog::ERROR, "PageWindPlot chart end: x: %d, y: %d prevX: %d, prevY: %d, chrtPrevVal: %d, loop-Counter: %d", x, y, prevX, prevY, chrtPrevVal, count);
} else if (!wndDataValid) {
// No valid data available
LOG_DEBUG(GwLog::LOG, "PageWindPlot: No valid data available");
getdisplay().setFont(&Ubuntu_Bold10pt7b);
getdisplay().fillRect(xCenter - 66, height / 2 - 18, 146, 24, commonData->bgcolor); // Clear area for TWS value
getdisplay().setCursor(xCenter - 66, height / 2);
getdisplay().print("No sensor data");
}
// Print TWS value
int yPosTws = yOffset + 40; // Y position for TWS value
int xPosTws = width - 145; // X position for TWS value
if ((prevY < yPosTws) && prevX > xPosTws) {
// If chart line enters TWS value area, move TWS value to the left side
xPosTws = 20 ;
if (showTWS) {
int xPosTws = width - 145;
int yPosTws = yOffset + 40;
if ((prevY > yPosTws - 36) && (prevY < yPosTws) && (prevX > xPosTws) && !TwsFlipped) {
// If chart line enters TWS value area, move TWS value to the other side
xPosTws = (xPosTws == width - 145) ? 30 : width - 145;
TwsFlipped = true;
}
getdisplay().fillRect(xPosTws - 3, yPosTws - 35, 138, 40, commonData->bgcolor); // Clear area for TWS value
TwsFlipped = false; // Reset flag for next display update
getdisplay().fillRect(xPosTws - 4, yPosTws - 38, 142, 44, commonData->bgcolor); // Clear area for TWS value
getdisplay().setFont(&DSEG7Classic_BoldItalic16pt7b);
getdisplay().setCursor(xPosTws, yPosTws); // 252, 52
getdisplay().setCursor(xPosTws, yPosTws);
getdisplay().print(dataSValue[2]); // Value
getdisplay().setFont(&Ubuntu_Bold12pt7b);
getdisplay().setCursor(xPosTws + 82, yPosTws - 14); // 334, 38
getdisplay().setCursor(xPosTws + 82, yPosTws - 14);
getdisplay().print(dataName[2]); // Name
getdisplay().setFont(&Ubuntu_Bold8pt7b);
getdisplay().setCursor(xPosTws + 78, yPosTws + 1); // 330, 53
getdisplay().setCursor(xPosTws + 78, yPosTws + 1);
getdisplay().print(" ");
if (holdValues == false) {
getdisplay().print(dataUnit[2]); // Unit
} else {
getdisplay().print(dataUnitOld[2]); // Unit
}
}
// chart Y axis labels
// chart Y axis labels; print last to overwrite potential chart lines in label area
char sWndYAx[4]; // char buffer for wind Y axis labels
int yPos; // Y position for label
int yPos;
getdisplay().setFont(&Ubuntu_Bold8pt7b);
for (int i = 4; i > 0; i--) {
yPos = yOffset + cHeight - (i * 59) + 14; // Y position for label
getdisplay().fillRect(0, yPos - 6, 28, 15, commonData->bgcolor); // Clear small area to remove potential chart lines
for (int i = 3; i > 0; i--) {
yPos = yOffset + cHeight - (i * 60) + 14; // Y position for label
getdisplay().fillRect(0, yPos, width, 1, commonData->fgcolor);
getdisplay().fillRect(0, yPos - 6, 26, 15, commonData->bgcolor); // Clear small area to remove potential chart lines
getdisplay().fillRect(0, yPos, 8, 2, commonData->fgcolor);
getdisplay().setCursor(9, yPos + 5);
getdisplay().setCursor(9, yPos + 4);
snprintf(sWndYAx, 4, "%2d", i * updTime);
getdisplay().print(sWndYAx); // Wind value label
}