diff --git a/lib/obp60task/OBP60Extensions.cpp b/lib/obp60task/OBP60Extensions.cpp index e45c90b..de153db 100644 --- a/lib/obp60task/OBP60Extensions.cpp +++ b/lib/obp60task/OBP60Extensions.cpp @@ -434,7 +434,7 @@ void drawTextRalign(int16_t x, int16_t y, String text) { int16_t x1, y1; uint16_t w, h; getdisplay().getTextBounds(text, 0, 150, &x1, &y1, &w, &h); - getdisplay().setCursor(x - w, y); + getdisplay().setCursor(x - w - 1, y); // '-1' required since some strings wrap around w/o it getdisplay().print(text); } diff --git a/lib/obp60task/OBPDataOperations.cpp b/lib/obp60task/OBPDataOperations.cpp index 769ddbf..3d81fe0 100644 --- a/lib/obp60task/OBPDataOperations.cpp +++ b/lib/obp60task/OBPDataOperations.cpp @@ -259,44 +259,6 @@ bool WindUtils::calcWinds(const double* awaVal, const double* awsVal, } } -/* // we don't need this -> AWD is calculated in calcTwdSA -// Calc AWD from existing AWA and HDT/HDM -bool WindUtils::calcATWD(const double* waVal, const double* hdtVal, const double* hdmVal, const double* varVal, const double* cogVal, const double* sogVal, double* wdVal) -{ - double wd, hdt; - GwApi::BoatValue* calBVal; // temp variable just for data calibration - bool isCalculated = false; - - if (*waVal == DBL_MAX) { - return false; - } - - if (*hdtVal != DBL_MAX) { - hdt = *hdtVal; // Use HDT if available - } else { - hdt = calcHDT(hdmVal, varVal, cogVal, sogVal); - } - - if (hdt != DBL_MAX) { - wd = *waVal + hdt; - wd = to2PI(wd); - isCalculated = true; - } - - // Calibrate AWD/TWD if required - calBVal = new GwApi::BoatValue("AWD"); // temporary solution for calibration of history buffer values - calBVal->value = wd; - calBVal->setFormat(awdBVal->getFormat()); - calBVal->valid = true; - calibrationData.calibrateInstance(calBVal, logger); // Check if boat data value is to be calibrated - *wdVal = calBVal->value; - - delete calBVal; - calBVal = nullptr; - - return isCalculated; -} */ - // Calculate true wind data and add to obp60task boat data list bool WindUtils::addWinds() { diff --git a/lib/obp60task/OBPDataOperations.h b/lib/obp60task/OBPDataOperations.h index 0a4faaf..53cae41 100644 --- a/lib/obp60task/OBPDataOperations.h +++ b/lib/obp60task/OBPDataOperations.h @@ -3,9 +3,6 @@ #include "OBPRingBuffer.h" #include "obp60task.h" #include -#include -#include -#include class HstryBuf { private: @@ -46,16 +43,18 @@ private: {"AWA", {1000, 10000, -M_PI, M_PI, "formatWind"}}, {"AWD", {1000, 10000, 0.0, M_TWOPI, "formatCourse"}}, {"AWS", {1000, 1000, 0.0, 65.0, "formatKnots"}}, - {"DBS", {1000, 100, 0.0, 650, "formatDepth"}}, - {"DBT", {1000, 100, 0.0, 650, "formatDepth"}}, - {"DPT", {1000, 100, 0.0, 650, "formatDepth"}}, - {"HDT", {1000, 10000, 0.0, M_TWOPI, "formatCourse"}}, + {"COG", {1000, 10000, 0.0, M_TWOPI, "formatCourse"}}, + {"DBS", {1000, 100, 0.0, 650.0, "formatDepth"}}, + {"DBT", {1000, 100, 0.0, 650.0, "formatDepth"}}, + {"DPT", {1000, 100, 0.0, 650.0, "formatDepth"}}, {"HDM", {1000, 10000, 0.0, M_TWOPI, "formatCourse"}}, + {"HDT", {1000, 10000, 0.0, M_TWOPI, "formatCourse"}}, + {"ROT", {1000, 10000, -M_PI / 180.0 * 99.0, M_PI / 180.0 * 99.0, "formatRot"}}, // min/max is -/+ 99 degrees for rotational angle + {"SOG", {1000, 1000, 0.0, 65.0, "formatKnots"}}, + {"STW", {1000, 1000, 0.0, 65.0, "formatKnots"}}, {"TWA", {1000, 10000, -M_PI, M_PI, "formatWind"}}, {"TWD", {1000, 10000, 0.0, M_TWOPI, "formatCourse"}}, {"TWS", {1000, 1000, 0.0, 65.0, "formatKnots"}}, - {"SOG", {1000, 1000, 0.0, 65.0, "formatKnots"}}, - {"STW", {1000, 1000, 0.0, 65.0, "formatKnots"}}, {"WTemp", {1000, 100, 0.0, 650.0, "kelvinToC"}} }; @@ -106,6 +105,5 @@ public: bool calcWinds(const double* awaVal, const double* awsVal, const double* cogVal, const double* stwVal, const double* sogVal, const double* hdtVal, const double* hdmVal, const double* varVal, double* twdVal, double* twsVal, double* twaVal, double* awdVal); - bool calcATWD(const double* waVal, const double* hdtVal, const double* hdmVal, const double* varVal, const double* cogVal, const double* sogVal, double* wdVal); bool addWinds(); }; \ No newline at end of file diff --git a/lib/obp60task/OBPcharts.cpp b/lib/obp60task/OBPcharts.cpp index b78bc6e..97bd71a 100644 --- a/lib/obp60task/OBPcharts.cpp +++ b/lib/obp60task/OBPcharts.cpp @@ -4,6 +4,13 @@ #include "OBPRingBuffer.h" // --- Class Chart --------------- + +// Chart - object holding the actual chart, incl. data buffer and format definition +// Parameters: chart timeline direction: 'H' = horizontal, 'V' = vertical; +// chart size: [0] = full size, [1] = half size left/top, [2] half size right/bottom; +// default range of chart, e.g. 30 = [0..30]; +// common program data; required for logger and color data +// flag to indicate if simulation data is active template Chart::Chart(RingBuffer& dataBuf, char chrtDir, int8_t chrtSz, double dfltRng, CommonData& common, bool useSimuData) : dataBuf(dataBuf) @@ -23,7 +30,7 @@ Chart::Chart(RingBuffer& dataBuf, char chrtDir, int8_t chrtSz, double dflt if (chrtDir == 'H') { // horizontal chart timeline direction - timAxis = dWidth; + timAxis = dWidth - 1; switch (chrtSz) { case 0: valAxis = dHeight - top - bottom; @@ -41,12 +48,13 @@ Chart::Chart(RingBuffer& dataBuf, char chrtDir, int8_t chrtSz, double dflt LOG_DEBUG(GwLog::ERROR, "displayChart: wrong init parameter"); return; } + } else if (chrtDir == 'V') { // vertical chart timeline direction timAxis = dHeight - top - bottom; switch (chrtSz) { case 0: - valAxis = dWidth; + valAxis = dWidth - 1; cStart = { 0, top - 1 }; break; case 1: @@ -106,21 +114,39 @@ Chart::~Chart() } // Perform all actions to draw chart -// Parameters are chart time interval, and the current boat data value to be printed +// Parameters: chart time interval, current boat data value to be printed, current boat data shall be shown yes/no template -void Chart::showChrt(int8_t chrtIntv, GwApi::BoatValue currValue) +void Chart::showChrt(int8_t chrtIntv, GwApi::BoatValue currValue, bool showCurrValue) { drawChrt(chrtIntv, currValue); drawChrtTimeAxis(chrtIntv); drawChrtValAxis(); if (bufDataValid) { - // uses BoatValue temp variable to format latest buffer value - // doesn't work unfortunately when 'simulation data' is active, because OBP60Formatter generates own simulation value in that case - currValue.value = dataBuf.getLast(); - currValue.valid = currValue.value != dbMAX_VAL; - Chart::prntCurrValue(currValue); - LOG_DEBUG(GwLog::DEBUG, "Chart drawChrt: currValue-value: %.1f, Valid: %d, Name: %s, Address: %p", currValue.value, currValue.valid, currValue.getName(), (void*)&currValue); + if (showCurrValue) { + // uses BoatValue temp variable to format latest buffer value + // doesn't work unfortunately when 'simulation data' is active, because OBP60Formatter generates own simulation value in that case + currValue.value = dataBuf.getLast(); + currValue.valid = currValue.value != dbMAX_VAL; + Chart::prntCurrValue(currValue); + LOG_DEBUG(GwLog::DEBUG, "OBPcharts showChrt: currValue-value: %.1f, Valid: %d, Name: %s, Address: %p", currValue.value, currValue.valid, currValue.getName(), (void*)&currValue); + } + + } else { // No valid data available -> print message + getdisplay().setFont(&Ubuntu_Bold10pt8b); + + int pX, pY; + if (chrtDir == 'H') { + pX = cStart.x + (timAxis / 2); + pY = cStart.y + (valAxis / 2) - 10; + } else { + pX = cStart.x + (valAxis / 2); + pY = cStart.y + (timAxis / 2) - 10; + } + + getdisplay().fillRect(pX - 37, pY - 10, 78, 24, bgColor); // Clear area for message + drawTextCenter(pX, pY, "No data"); + LOG_DEBUG(GwLog::LOG, "Page chart: No valid data available"); } } @@ -131,7 +157,6 @@ void Chart::drawChrt(int8_t chrtIntv, GwApi::BoatValue& currValue) double chrtVal; // Current data value double chrtScl; // Scale for data values in pixels per value static double chrtPrevVal; // Last data value in chart area - // bool bufDataValid = false; // Flag to indicate if buffer data is valid static int numNoData; // Counter for multiple invalid data values in a row int x, y; // x and y coordinates for drawing @@ -144,7 +169,6 @@ void Chart::drawChrt(int8_t chrtIntv, GwApi::BoatValue& currValue) if (chrtIntv != oldChrtIntv || count == 1) { // new data interval selected by user; this is only x * 230 values instead of 240 seconds (4 minutes) per interval step - // intvBufSize = timAxis * chrtIntv; // obsolete numBufVals = min(count, (timAxis - 60) * chrtIntv); // keep free or release 60 values on chart for plotting of new values bufStart = max(0, count - numBufVals); lastAddedIdx = currIdx; @@ -186,7 +210,8 @@ void Chart::drawChrt(int8_t chrtIntv, GwApi::BoatValue& currValue) if (chrtDir == 'H') { // horizontal chart x = cStart.x + i; // Position in chart area - if (chrtDataFmt == 'S') { + + if (chrtDataFmt == 'S') { // speed data format -> print low values at bottom // y = cStart.y + static_cast(((chrtVal - chrtMin) * chrtScl) + 0.5); // calculate chart point and round y = cStart.y + valAxis - static_cast(((chrtVal - chrtMin) * chrtScl) + 0.5); // calculate chart point and round } else if (chrtDataFmt == 'D') { @@ -194,8 +219,10 @@ void Chart::drawChrt(int8_t chrtIntv, GwApi::BoatValue& currValue) } else { // degree type value y = cStart.y + static_cast((WindUtils::to2PI(chrtVal - chrtMin) * chrtScl) + 0.5); // calculate chart point and round } + } else { // vertical chart y = cStart.y + timAxis - i; // Position in chart area + if (chrtDataFmt == 'S' || chrtDataFmt == 'D') { x = cStart.x + static_cast(((chrtVal - chrtMin) * chrtScl) + 0.5); // calculate chart point and round } else { // degree type value @@ -266,27 +293,11 @@ void Chart::drawChrt(int8_t chrtIntv, GwApi::BoatValue& currValue) if (chrtDataFmt == 'W') { // degree of course or wind recalcRngCntr = true; - LOG_DEBUG(GwLog::DEBUG, "PageWindPlot FreeTop: timAxis: %d, i: %d, bufStart: %d, numBufVals: %d, recalcRngCntr: %d", timAxis, i, bufStart, numBufVals, recalcRngCntr); + LOG_DEBUG(GwLog::DEBUG, "PageWindPlot: chart end: timAxis: %d, i: %d, bufStart: %d, numBufVals: %d, recalcRngCntr: %d", timAxis, i, bufStart, numBufVals, recalcRngCntr); } break; } } - } else { - // No valid data available - getdisplay().setFont(&Ubuntu_Bold10pt8b); - - int pX, pY; - if (chrtDir == 'H') { - pX = cStart.x + (timAxis / 2); - pY = cStart.y + (valAxis / 2) - 10; - } else { - pX = cStart.x + (valAxis / 2); - pY = cStart.y + (timAxis / 2) - 10; - } - - getdisplay().fillRect(pX - 33, pY - 10, 66, 24, bgColor); // Clear area for message - drawTextCenter(pX, pY, "No data"); - LOG_DEBUG(GwLog::LOG, "PageWindPlot: No valid data available"); } } @@ -330,38 +341,8 @@ double Chart::getRng(double center, size_t amount) template void Chart::calcChrtBorders(double& rngMid, double& rngMin, double& rngMax, double& rng) { - if (chrtDataFmt == 'S' || chrtDataFmt == 'D') { - // Chart data is of any type but 'degree' - - double oldRngMin = rngMin; - double oldRngMax = rngMax; - - // Chart starts at lowest range value, but at least '0' or includes even negative values - double currMinVal = dataBuf.getMin(numBufVals); - LOG_DEBUG(GwLog::DEBUG, "calcChrtRange0a: currMinVal: %.1f, currMaxVal: %.1f, rngMin: %.1f, rngMid: %.1f, rngMax: %.1f, rng: %.1f, rngStep: %.1f, oldRngMin: %.1f, oldRngMax: %.1f, dfltRng: %.1f, numBufVals: %d", - currMinVal, dataBuf.getMax(numBufVals), rngMin, rngMid, rngMax, rng, rngStep, oldRngMin, oldRngMax, dfltRng, numBufVals); - - if (currMinVal != dbMAX_VAL) { // current min value is valid - if (currMinVal > 0 && dbMIN_VAL == 0) { // Chart range starts at least at '0' or includes negative values - rngMin = 0; - } else if (currMinVal < oldRngMin || (oldRngMin < 0 && (currMinVal > (oldRngMin + rngStep)))) { // decrease rngMin if required or increase if lowest value is higher than old rngMin - rngMin = std::floor(currMinVal / rngStep) * rngStep; - } - } // otherwise keep rngMin unchanged - - double currMaxVal = dataBuf.getMax(numBufVals); - if (currMaxVal != dbMAX_VAL) { // current max value is valid - if ((currMaxVal > oldRngMax) || (currMaxVal < (oldRngMax - rngStep))) { // increase rngMax if required or decrease if lowest value is lower than old rngMax - rngMax = std::ceil(currMaxVal / rngStep) * rngStep; - rngMax = std::max(rngMax, rngMin + dfltRng); // keep at least default chart range - } - } // otherwise keep rngMax unchanged - - rngMid = (rngMin + rngMax) / 2.0; - rng = rngMax - rngMin; - LOG_DEBUG(GwLog::DEBUG, "calcChrtRange1a: currMinVal: %.1f, currMaxVal: %.1f, rngMin: %.1f, rngMid: %.1f, rngMax: %.1f, rng: %.1f, rngStep: %.1f, oldRngMin: %.1f, oldRngMax: %.1f, dfltRng: %.1f, numBufVals: %d", - currMinVal, currMaxVal, rngMin, rngMid, rngMax, rng, rngStep, oldRngMin, oldRngMax, dfltRng, numBufVals); - } else { + if (chrtDataFmt == 'W' || chrtDataFmt == 'R') { + // Chart data is of type 'course', 'wind' or 'rot' if (chrtDataFmt == 'W') { // Chart data is of type 'course' or 'wind' @@ -416,8 +397,38 @@ void Chart::calcChrtBorders(double& rngMid, double& rngMin, double& rngMax, d // LOG_DEBUG(GwLog::DEBUG, "calcChrtRange2: diffRng: %.1f°, halfRng: %.1f°", diffRng * RAD_TO_DEG, halfRng * RAD_TO_DEG); rng = halfRng * 2.0; - LOG_DEBUG(GwLog::DEBUG, "calcChrtRange2b: rngMid: %.1f°, rngMin: %.1f°, rngMax: %.1f°, diffRng: %.1f°, rng: %.1f°, rngStep: %.1f°", rngMid * RAD_TO_DEG, rngMin * RAD_TO_DEG, rngMax * RAD_TO_DEG, - diffRng * RAD_TO_DEG, rng * RAD_TO_DEG, rngStep * RAD_TO_DEG); + // LOG_DEBUG(GwLog::DEBUG, "calcChrtRange2b: rngMid: %.1f°, rngMin: %.1f°, rngMax: %.1f°, diffRng: %.1f°, rng: %.1f°, rngStep: %.1f°", rngMid * RAD_TO_DEG, rngMin * RAD_TO_DEG, rngMax * RAD_TO_DEG, + // diffRng * RAD_TO_DEG, rng * RAD_TO_DEG, rngStep * RAD_TO_DEG); + + } else { + double oldRngMin = rngMin; + double oldRngMax = rngMax; + + // Chart starts at lowest range value, but at least '0' or includes even negative values + double currMinVal = dataBuf.getMin(numBufVals); + // LOG_DEBUG(GwLog::DEBUG, "calcChrtRange0a: currMinVal: %.1f, currMaxVal: %.1f, rngMin: %.1f, rngMid: %.1f, rngMax: %.1f, rng: %.1f, rngStep: %.1f, oldRngMin: %.1f, oldRngMax: %.1f, dfltRng: %.1f, numBufVals: %d", + // currMinVal, dataBuf.getMax(numBufVals), rngMin, rngMid, rngMax, rng, rngStep, oldRngMin, oldRngMax, dfltRng, numBufVals); + + if (currMinVal != dbMAX_VAL) { // current min value is valid + if (currMinVal > 0 && dbMIN_VAL == 0) { // Chart range starts at least at '0' or includes negative values + rngMin = 0; + } else if (currMinVal < oldRngMin || (oldRngMin < 0 && (currMinVal > (oldRngMin + rngStep)))) { // decrease rngMin if required or increase if lowest value is higher than old rngMin + rngMin = std::floor(currMinVal / rngStep) * rngStep; + } + } // otherwise keep rngMin unchanged + + double currMaxVal = dataBuf.getMax(numBufVals); + if (currMaxVal != dbMAX_VAL) { // current max value is valid + if ((currMaxVal > oldRngMax) || (currMaxVal < (oldRngMax - rngStep))) { // increase rngMax if required or decrease if lowest value is lower than old rngMax + rngMax = std::ceil(currMaxVal / rngStep) * rngStep; + rngMax = std::max(rngMax, rngMin + dfltRng); // keep at least default chart range + } + } // otherwise keep rngMax unchanged + + rngMid = (rngMin + rngMax) / 2.0; + rng = rngMax - rngMin; + // LOG_DEBUG(GwLog::DEBUG, "calcChrtRange1a: currMinVal: %.1f, currMaxVal: %.1f, rngMin: %.1f, rngMid: %.1f, rngMax: %.1f, rng: %.1f, rngStep: %.1f, oldRngMin: %.1f, oldRngMax: %.1f, dfltRng: %.1f, numBufVals: %d", + // currMinVal, currMaxVal, rngMin, rngMid, rngMax, rng, rngStep, oldRngMin, oldRngMax, dfltRng, numBufVals); } } @@ -425,52 +436,40 @@ void Chart::calcChrtBorders(double& rngMid, double& rngMin, double& rngMax, d template void Chart::drawChrtTimeAxis(int8_t chrtIntv) { - int timeRng; float slots, intv, i; char sTime[6]; + int timeRng = chrtIntv * 4; // chart time interval: [1] 4 min., [2] 8 min., [3] 12 min., [4] 16 min., [8] 32 min. + getdisplay().setFont(&Ubuntu_Bold8pt8b); getdisplay().setTextColor(fgColor); if (chrtDir == 'H') { // horizontal chart getdisplay().fillRect(0, cStart.y, dWidth, 2, fgColor); - timeRng = chrtIntv * 4; // Chart time interval: [1] 4 min., [2] 8 min., [3] 12 min., [4] 16 min., [8] 32 min. - slots = timAxis / 80.0; // number of axis labels - intv = timeRng / slots; // minutes per chart axis interval + slots = 5; // number of axis labels + intv = timAxis / (slots - 1); // minutes per chart axis interval (interval is 1 less than slots) i = timeRng; // Chart axis label start at -32, -16, -12, ... minutes - for (int j = 0; j < timAxis - 30; j += 80) { // fill time axis with values but keep area free on right hand side for value label - // LOG_DEBUG(GwLog::DEBUG, "ChartTimeAxis: timAxis: %d, {x,y}: {%d,%d}, i: %.1f, j: %d, chrtIntv: %d, intv: %.1f, slots: %.1f", timAxis, cStart.x, cStart.y, i, j, chrtIntv, intv, slots); - - // Format time label based on interval - if (chrtIntv < 3) { - snprintf(sTime, sizeof(sTime), "-%.1f", i); - } else { - snprintf(sTime, sizeof(sTime), "-%.0f", std::round(i)); - } + for (float j = 0; j < timAxis - 1; j += intv) { // fill time axis with values but keep area free on right hand side for value label // draw text with appropriate offset - // int tOffset = (j == 0) ? 13 : (chrtIntv < 3 ? -4 : -4); int tOffset = j == 0 ? 13 : -4; + snprintf(sTime, sizeof(sTime), "-%.0f", i); drawTextCenter(cStart.x + j + tOffset, cStart.y - 8, sTime); getdisplay().drawLine(cStart.x + j, cStart.y, cStart.x + j, cStart.y + 5, fgColor); // draw short vertical time mark - i -= intv; + i -= chrtIntv; } } else { // vertical chart - timeRng = chrtIntv * 4; // chart time interval: [1] 4 min., [2] 8 min., [3] 12 min., [4] 16 min., [8] 32 min. - slots = timAxis / 75.0; // number of axis labels - intv = timeRng / slots; // minutes per chart axis interval - i = -intv; // chart axis label start at -32, -16, -12, ... minutes + slots = 5; // number of axis labels + intv = timAxis / (slots - 1); // minutes per chart axis interval (interval is 1 less than slots) + i = timeRng; // Chart axis label start at -32, -16, -12, ... minutes - for (int j = 75; j < (timAxis - 75); j += 75) { // don't print time label at upper and lower end of time axis - if (chrtIntv < 3) { // print 1 decimal if time range is single digit (4 or 8 minutes) - snprintf(sTime, sizeof(sTime), "%.1f", i); - } else { - snprintf(sTime, sizeof(sTime), "%.0f", std::floor(i)); - } + for (float j = intv; j < timAxis - 1; j += intv) { // don't print time label at upper and lower end of time axis + i -= chrtIntv; // we start not at top chart position + snprintf(sTime, sizeof(sTime), "-%.0f", i); getdisplay().drawLine(cStart.x, cStart.y + j, cStart.x + valAxis, cStart.y + j, fgColor); // Grid line if (chrtSz == 0) { // full size chart @@ -480,8 +479,6 @@ void Chart::drawChrtTimeAxis(int8_t chrtIntv) } else if (chrtSz == 2) { // half size chart; right side drawTextCenter(dWidth / 2, cStart.y + j, sTime); // time value; print mid screen } - - i -= intv; } } } @@ -509,8 +506,21 @@ void Chart::drawChrtValAxis() if (chrtSz == 0) { // full size chart -> print multiple value lines getdisplay().setFont(&Ubuntu_Bold12pt8b); -// for (int j = 60; j < valAxis - 30; j += 60) { - for (int j = valAxis - 60; j > 30; j -= 60) { + + int loopStrt, loopEnd, loopStp; + if (chrtDataFmt == 'S') { + loopStrt = valAxis - 60; + loopEnd = 30; + loopStp = -60; + } else { + loopStrt = 60; + loopEnd = valAxis - 30; + loopStp = 60; + } + LOG_DEBUG(GwLog::DEBUG, "Chart drawValAxis: chrtDataFmt: %c, loopStrt: %d, loopEnd: %d, loopIntv: %d", chrtDataFmt, loopStrt, loopEnd, loopStp); + + for (int j = loopStrt; (loopStp > 0) ? (j < loopEnd) : (j > loopEnd); j += loopStp) { + LOG_DEBUG(GwLog::DEBUG, "Chart drawValAxis2: j: %d, i: %d", j, i); getdisplay().drawLine(cStart.x, cStart.y + j, cStart.x + timAxis, cStart.y + j, fgColor); getdisplay().fillRect(cStart.x, cStart.y + j - 11, 42, 21, bgColor); // Clear small area to remove potential chart lines @@ -523,7 +533,7 @@ void Chart::drawChrtValAxis() } else { // half size chart -> print just edge values + middle chart line getdisplay().setFont(&Ubuntu_Bold10pt8b); - tmpBVal->value = chrtMin; + tmpBVal->value = (chrtDataFmt == 'D') ? chrtMin : chrtMax; cVal = formatValue(tmpBVal.get(), *commonData).cvalue; // value (converted) sLen = snprintf(sVal, sizeof(sVal), "%.0f", round(cVal)); getdisplay().fillRect(cStart.x, cStart.y + 2, 42, 16, bgColor); // Clear small area to remove potential chart lines @@ -538,7 +548,7 @@ void Chart::drawChrtValAxis() getdisplay().printf("%s", sVal); // Range mid value getdisplay().drawLine(cStart.x + 43, cStart.y + (valAxis / 2), cStart.x + timAxis, cStart.y + (valAxis / 2), fgColor); - tmpBVal->value = chrtMax; + tmpBVal->value = (chrtDataFmt == 'D') ? chrtMax : chrtMin; cVal = formatValue(tmpBVal.get(), *commonData).cvalue; // value (converted) sLen = snprintf(sVal, sizeof(sVal), "%.0f", round(cVal)); getdisplay().fillRect(cStart.x, cStart.y + valAxis - 16, 42, 16, bgColor); // Clear small area to remove potential chart lines @@ -586,7 +596,7 @@ template void Chart::prntCurrValue(GwApi::BoatValue& currValue) { const int xPosVal = (chrtDir == 'H') ? cStart.x + (timAxis / 2) - 56 : cStart.x + 32; - const int yPosVal = (chrtDir == 'H') ? cStart.y + valAxis - 5 : cStart.y + timAxis - 5; + const int yPosVal = (chrtDir == 'H') ? cStart.y + valAxis - 7 : cStart.y + timAxis - 7; FormattedData frmtDbData = formatValue(&currValue, *commonData); double testdbValue = frmtDbData.value; @@ -595,8 +605,8 @@ void Chart::prntCurrValue(GwApi::BoatValue& currValue) // LOG_DEBUG(GwLog::DEBUG, "Chart CurrValue: dbValue: %.2f, sdbValue: %s, fmrtDbValue: %.2f, dbFormat: %s, dbUnit: %s, Valid: %d, Name: %s, Address: %p", currValue.value, sdbValue, // testdbValue, currValue.getFormat(), dbUnit, currValue.valid, currValue.getName(), currValue); - getdisplay().fillRect(xPosVal - 1, yPosVal - 34, 125, 40, bgColor); // Clear area for TWS value - getdisplay().drawRect(xPosVal, yPosVal - 33, 123, 39, fgColor); // Draw box for TWS value + getdisplay().fillRect(xPosVal - 1, yPosVal - 35, 125, 41, bgColor); // Clear area for TWS value + getdisplay().drawRect(xPosVal, yPosVal - 34, 123, 40, fgColor); // Draw box for TWS value getdisplay().setFont(&DSEG7Classic_BoldItalic16pt7b); getdisplay().setCursor(xPosVal + 1, yPosVal); if (useSimuData) { diff --git a/lib/obp60task/OBPcharts.h b/lib/obp60task/OBPcharts.h index f160dd4..2ffa141 100644 --- a/lib/obp60task/OBPcharts.h +++ b/lib/obp60task/OBPcharts.h @@ -6,16 +6,16 @@ struct Pos { int x; int y; }; + template class RingBuffer; class GwLog; -template -class Chart { +template class Chart { protected: - CommonData *commonData; - GwLog *logger; + CommonData* commonData; + GwLog* logger; - RingBuffer &dataBuf; // Buffer to display + RingBuffer& dataBuf; // Buffer to display char chrtDir; // Chart timeline direction: 'H' = horizontal, 'V' = vertical int8_t chrtSz; // Chart size: [0] = full size, [1] = half size left/top, [2] half size right/bottom double dfltRng; // Default range of chart, e.g. 30 = [0..30] @@ -23,7 +23,6 @@ protected: uint16_t bgColor; // color code for screen background bool useSimuData; // flag to indicate if simulation data is active - // int top = 48; // display top header lines int top = 44; // chart gap at top of display (25 lines for standard gap + 19 lines for axis labels) int bottom = 25; // chart gap at bottom of display to keep space for status line int hGap = 11; // gap between 2 horizontal charts; actual gap is 2x @@ -59,11 +58,22 @@ protected: void calcChrtBorders(double& rngMid, double& rngMin, double& rngMax, double& rng); // Calculate chart points for value axis and return range between and void drawChrtTimeAxis(int8_t chrtIntv); // Draw time axis of chart, value and lines void drawChrtValAxis(); // Draw value axis of chart, value and lines - void prntCurrValue(GwApi::BoatValue& currValue); // Add current boat data value to chart + void prntCurrValue(GwApi::BoatValue& currValue); // Add current boat data value to chart public: + // Define default chart range for each boat data type + static std::map dfltChartRng; + Chart(RingBuffer& dataBuf, char chrtDir, int8_t chrtSz, double dfltRng, CommonData& common, bool useSimuData); // Chart object of data chart ~Chart(); - void showChrt(int8_t chrtIntv, GwApi::BoatValue currValue); // Perform all actions to draw chart + void showChrt(int8_t chrtIntv, GwApi::BoatValue currValue, bool showCurrValue); // Perform all actions to draw chart +}; +template +std::map Chart::dfltChartRng = { + { "formatWind", 60.0 * DEG_TO_RAD }, // default course range 60 degrees + { "formatCourse", 60.0 * DEG_TO_RAD }, // default course range 60 degrees + { "formatKnots", 5.1 }, // default speed range in m/s + { "formatDepth", 15 }, // default depth range in m + { "kelvinToC", 30 } // default temp range in °C/K }; \ No newline at end of file diff --git a/lib/obp60task/PageOneValue.cpp b/lib/obp60task/PageOneValue.cpp index 6f33597..fe2ac93 100644 --- a/lib/obp60task/PageOneValue.cpp +++ b/lib/obp60task/PageOneValue.cpp @@ -3,112 +3,254 @@ #include "Pagedata.h" #include "OBP60Extensions.h" #include "BoatDataCalibration.h" +#include "OBPcharts.h" -class PageOneValue : public Page -{ - public: - PageOneValue(CommonData &common){ - commonData = &common; - common.logger->logDebug(GwLog::LOG,"Instantiate PageOneValue"); +class PageOneValue : public Page { +private: + GwLog* logger; + + int width; // Screen width + int height; // Screen height + + bool keylock = false; // Keylock + char pageMode = 'V'; // Page mode: 'V' for value, 'C' for chart, 'B' for both + int dataIntv = 1; // Update interval for wind history chart: + // (1)|(2)|(3)|(4)|(8) x 240 seconds for 4, 8, 12, 16, 32 min. history chart + + String lengthformat; + bool useSimuData; + bool holdValues; + String flashLED; + String backlightMode; + + // Old values for hold function + String sValue1Old = ""; + String unit1Old = ""; + + // Data buffer pointer (owned by HstryBuffers) + RingBuffer* dataHstryBuf = nullptr; + std::unique_ptr> dataFlChart, dataHfChart; // Chart object, full and half size + // Active chart and value + Chart* dataChart = nullptr; + + void showData(GwApi::BoatValue* bValue1, char size) + { + int nameXoff, nameYoff, unitXoff, unitYoff, value1Xoff, value1Yoff; + const GFXfont *nameFnt, *unitFnt, *valueFnt1, *valueFnt2, *valueFnt3; + + if (size == 'F') { // full size data display + nameXoff = 0; + nameYoff = 0; + nameFnt = &Ubuntu_Bold32pt8b; + unitXoff = 0; + unitYoff = 0; + unitFnt = &Ubuntu_Bold20pt8b; + value1Xoff = 0; + value1Yoff = 0; + valueFnt1 = &Ubuntu_Bold20pt8b; + valueFnt2 = &Ubuntu_Bold32pt8b; + valueFnt3 = &DSEG7Classic_BoldItalic60pt7b; + } else { // half size data and chart display + nameXoff = 105; + nameYoff = -40; + nameFnt = &Ubuntu_Bold20pt8b; + unitXoff = -33; + unitYoff = -40; + unitFnt = &Ubuntu_Bold12pt8b; + valueFnt1 = &Ubuntu_Bold12pt8b; + value1Xoff = 105; + value1Yoff = -105; + valueFnt2 = &Ubuntu_Bold20pt8b; + valueFnt3 = &DSEG7Classic_BoldItalic30pt7b; + } + + String name1 = xdrDelete(bValue1->getName()); // Value name + name1 = name1.substring(0, 6); // String length limit for value name + calibrationData.calibrateInstance(bValue1, logger); // Check if boat data value is to be calibrated + double value1 = bValue1->value; // Value as double in SI unit + bool valid1 = bValue1->valid; // Valid information + String sValue1 = formatValue(bValue1, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places + String unit1 = formatValue(bValue1, *commonData).unit; // Unit of value + + // Show name + getdisplay().setTextColor(commonData->fgcolor); + getdisplay().setFont(nameFnt); + getdisplay().setCursor(20 + nameXoff, 100 + nameYoff); + getdisplay().print(name1); // name + + // Show unit + getdisplay().setFont(unitFnt); + getdisplay().setCursor(270 + unitXoff, 100 + unitYoff); + if (holdValues == false) { + //getdisplay().print(unit1); // Unit + drawTextRalign(298 + unitXoff, 100 + unitYoff, unit1); // Unit + + } else { + // getdisplay().print(unit1Old); + drawTextRalign(298 + unitXoff, 100 + unitYoff, unit1Old); + } + + // Switch font if format for any values + if (bValue1->getFormat() == "formatLatitude" || bValue1->getFormat() == "formatLongitude") { + getdisplay().setFont(valueFnt1); + getdisplay().setCursor(20 + value1Xoff, 180 + value1Yoff); + } else if (bValue1->getFormat() == "formatTime" || bValue1->getFormat() == "formatDate") { + getdisplay().setFont(valueFnt2); + getdisplay().setCursor(20 + value1Xoff, 200 + value1Yoff); + } else { + getdisplay().setFont(valueFnt3); + getdisplay().setCursor(20 + value1Xoff, 240 + value1Yoff); + } + + // Show bus data + if (holdValues == false) { + getdisplay().print(sValue1); // Real value as formated string + } else { + getdisplay().print(sValue1Old); // Old value as formated string + } + if (valid1 == true) { + sValue1Old = sValue1; // Save the old value + unit1Old = unit1; // Save the old unit + } } - virtual int handleKey(int key){ - // Code for keylock - if(key == 11){ +public: + PageOneValue(CommonData& common) + { + commonData = &common; + logger = commonData->logger; + LOG_DEBUG(GwLog::LOG, "Instantiate PageOneValue"); + + width = getdisplay().width(); // Screen width + height = getdisplay().height(); // Screen height + + // Get config data + lengthformat = common.config->getString(common.config->lengthFormat); + useSimuData = common.config->getBool(common.config->useSimuData); + holdValues = common.config->getBool(common.config->holdvalues); + flashLED = common.config->getString(common.config->flashLED); + backlightMode = common.config->getString(common.config->backlight); + } + + virtual void setupKeys() + { + Page::setupKeys(); + commonData->keydata[0].label = "MODE"; +#if defined BOARD_OBP60S3 + commonData->keydata[4].label = "ZOOM"; +#elif defined BOARD_OBP40S3 + commonData->keydata[1].label = "ZOOM"; +#endif + } + + // Key functions + virtual int handleKey(int key) + { + // Set page mode value | full chart | value/half chart + if (key == 1) { + if (pageMode == 'V') { + pageMode = 'C'; + } else if (pageMode == 'C') { + pageMode = 'B'; + } else { + pageMode = 'V'; + } + return 0; // Commit the key + } + + // Set interval for history chart update time (interval) +#if defined BOARD_OBP60S3 + if (key == 5) { +#elif defined BOARD_OBP40S3 + if (key == 2) { +#endif + if (dataIntv == 1) { + dataIntv = 2; + } else if (dataIntv == 2) { + dataIntv = 3; + } else if (dataIntv == 3) { + dataIntv = 4; + } else if (dataIntv == 4) { + dataIntv = 8; + } else { + dataIntv = 1; + } + return 0; // Commit the key + } + + // Keylock function + if (key == 11) { // Code for keylock commonData->keylock = !commonData->keylock; - return 0; // Commit the key + return 0; // Commit the key } return key; } - int displayPage(PageData &pageData){ - GwConfigHandler *config = commonData->config; - GwLog *logger = commonData->logger; + virtual void displayNew(PageData& pageData) + { +#ifdef BOARD_OBP60S3 + // Clear optical warning + if (flashLED == "Limit Violation") { + setBlinkingLED(false); + setFlashLED(false); + } +#endif + if (!dataFlChart) { // Create chart objects if they don't exist + GwApi::BoatValue* bValue1 = pageData.values[0]; // Page boat data element + String bValName1 = bValue1->getName(); // Value name + String bValFormat = bValue1->getFormat(); // Value format - // Old values for hold function - static String svalue1old = ""; - static String unit1old = ""; + dataHstryBuf = pageData.hstryBuffers->getBuffer(bValName1); - // Get config data - String lengthformat = config->getString(config->lengthFormat); - // bool simulation = config->getBool(config->useSimuData); - bool holdvalues = config->getBool(config->holdvalues); - String flashLED = config->getString(config->flashLED); - String backlightMode = config->getString(config->backlight); - - // Get boat values - GwApi::BoatValue *bvalue1 = pageData.values[0]; // First element in list (only one value by PageOneValue) - String name1 = xdrDelete(bvalue1->getName()); // Value name - name1 = name1.substring(0, 6); // String length limit for value name - calibrationData.calibrateInstance(bvalue1, logger); // Check if boat data value is to be calibrated - double value1 = bvalue1->value; // Value as double in SI unit - bool valid1 = bvalue1->valid; // Valid information - String svalue1 = formatValue(bvalue1, *commonData).svalue; // Formatted value as string including unit conversion and switching decimal places - String unit1 = formatValue(bvalue1, *commonData).unit; // Unit of value + dataFlChart.reset(new Chart(*dataHstryBuf, 'H', 0, Chart::dfltChartRng[bValFormat], *commonData, useSimuData)); + dataHfChart.reset(new Chart(*dataHstryBuf, 'H', 2, Chart::dfltChartRng[bValFormat], *commonData, useSimuData)); + LOG_DEBUG(GwLog::DEBUG, "PageOneValue: Created chart objects for %s", bValName1); + } + } + + int displayPage(PageData& pageData) + { + + LOG_DEBUG(GwLog::LOG, "Display PageOneValue"); + GwConfigHandler* config = commonData->config; + GwLog* logger = commonData->logger; + + // Get boat value for page + GwApi::BoatValue* bValue1 = pageData.values[0]; // Page boat data element // Optical warning by limit violation (unused) - if(String(flashLED) == "Limit Violation"){ + if (String(flashLED) == "Limit Violation") { setBlinkingLED(false); - setFlashLED(false); + setFlashLED(false); } // Logging boat values - if (bvalue1 == NULL) return PAGE_OK; // WTF why this statement? - LOG_DEBUG(GwLog::LOG,"Drawing at PageOneValue, %s: %f", name1.c_str(), value1); + if (bValue1 == NULL) + return PAGE_OK; // WTF why this statement? + LOG_DEBUG(GwLog::LOG, "Drawing at PageOneValue, %s: %f", bValue1->getName().c_str(), bValue1->value); // Draw page //*********************************************************** - /// Set display in partial refresh mode - getdisplay().setPartialWindow(0, 0, getdisplay().width(), getdisplay().height()); // Set partial update + getdisplay().setPartialWindow(0, 0, width, height); // Set partial update - // Show name - getdisplay().setTextColor(commonData->fgcolor); - getdisplay().setFont(&Ubuntu_Bold32pt8b); - getdisplay().setCursor(20, 100); - getdisplay().print(name1); // Page name + if (pageMode == 'V') { // show only data value + showData(bValue1, 'F'); - // Show unit - getdisplay().setFont(&Ubuntu_Bold20pt8b); - getdisplay().setCursor(270, 100); - if(holdvalues == false){ - getdisplay().print(unit1); // Unit - } - else{ - getdisplay().print(unit1old); - } + } else if (pageMode == 'C') { // show only data chart + dataFlChart->showChrt(dataIntv, *bValue1, true); - // Switch font if format for any values - if(bvalue1->getFormat() == "formatLatitude" || bvalue1->getFormat() == "formatLongitude"){ - getdisplay().setFont(&Ubuntu_Bold20pt8b); - getdisplay().setCursor(20, 180); - } - else if(bvalue1->getFormat() == "formatTime" || bvalue1->getFormat() == "formatDate"){ - getdisplay().setFont(&Ubuntu_Bold32pt8b); - getdisplay().setCursor(20, 200); - } - else{ - getdisplay().setFont(&DSEG7Classic_BoldItalic60pt7b); - getdisplay().setCursor(20, 240); - } - - // Show bus data - if(holdvalues == false){ - getdisplay().print(svalue1); // Real value as formated string - } - else{ - getdisplay().print(svalue1old); // Old value as formated string - } - if(valid1 == true){ - svalue1old = svalue1; // Save the old value - unit1old = unit1; // Save the old unit + } else if (pageMode == 'B') { // show data value and chart + showData(bValue1, 'H'); + dataHfChart->showChrt(dataIntv, *bValue1, false); } return PAGE_UPDATE; }; }; -static Page* createPage(CommonData &common){ +static Page* createPage(CommonData& common) +{ return new PageOneValue(common); } @@ -120,10 +262,10 @@ static Page* createPage(CommonData &common){ * this will be number of BoatValue pointers in pageData.values */ PageDescription registerPageOneValue( - "OneValue", // Page name - createPage, // Action - 1, // Number of bus values depends on selection in Web configuration - true // Show display header on/off + "OneValue", // Page name + createPage, // Action + 1, // Number of bus values depends on selection in Web configuration + true // Show display header on/off ); #endif diff --git a/lib/obp60task/PageWindPlot.cpp b/lib/obp60task/PageWindPlot.cpp index 636d8f9..5042d7c 100644 --- a/lib/obp60task/PageWindPlot.cpp +++ b/lib/obp60task/PageWindPlot.cpp @@ -2,7 +2,6 @@ #include "Pagedata.h" #include "OBP60Extensions.h" -#include "OBPDataOperations.h" #include "OBPcharts.h" // **************************************************************** @@ -77,9 +76,9 @@ public: commonData->keydata[0].label = "MODE"; #if defined BOARD_OBP60S3 commonData->keydata[1].label = "SRC"; - commonData->keydata[4].label = "INTV"; + commonData->keydata[4].label = "ZOOM"; #elif defined BOARD_OBP40S3 - commonData->keydata[1].label = "INTV"; + commonData->keydata[1].label = "ZOOM"; #endif } @@ -209,14 +208,14 @@ public: getdisplay().setTextColor(commonData->fgcolor); if (chrtMode == 'D') { - wdFlChart->showChrt(dataIntv, *wdBVal); + wdFlChart->showChrt(dataIntv, *wdBVal, true); } else if (chrtMode == 'S') { - wsFlChart->showChrt(dataIntv, *wsBVal); + wsFlChart->showChrt(dataIntv, *wsBVal, true); } else if (chrtMode == 'B') { - wdHfChart->showChrt(dataIntv, *wdBVal); - wsHfChart->showChrt(dataIntv, *wsBVal); + wdHfChart->showChrt(dataIntv, *wdBVal, true); + wsHfChart->showChrt(dataIntv, *wsBVal, true); } LOG_DEBUG(GwLog::DEBUG, "PageWindPlot: page time %ldms", millis() - pageTime);