From c932724473c2b231931a7e7e73db0dc77d986a31 Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Fri, 22 Aug 2025 15:17:54 +0200 Subject: [PATCH] More work on formatter --- lib/obp60task/OBP60Extensions.cpp | 4 +- lib/obp60task/OBP60Formatter.cpp | 54 +++++++++++--- lib/obp60task/OBP60Formatter.h | 114 ++++++++++++++++++++---------- lib/obp60task/PageClock.cpp | 8 +-- lib/obp60task/Pagedata.h | 4 +- 5 files changed, 131 insertions(+), 53 deletions(-) diff --git a/lib/obp60task/OBP60Extensions.cpp b/lib/obp60task/OBP60Extensions.cpp index 911c0d7..8baefe6 100644 --- a/lib/obp60task/OBP60Extensions.cpp +++ b/lib/obp60task/OBP60Extensions.cpp @@ -584,7 +584,7 @@ void displayHeader(CommonData &commonData, bool symbolmode, GwApi::BoatValue *da heartbeat = !heartbeat; // Date and time - String fmttype = commonData.config->getString(commonData.config->dateFormat); + fmtDate fmttype = commonData.fmt->getDateFormat(commonData.config->getString(commonData.config->dateFormat)); String timesource = commonData.config->getString(commonData.config->timeSource); double tz = commonData.config->getString(commonData.config->timeZone).toDouble(); epd->setTextColor(commonData.fgcolor); @@ -595,7 +595,7 @@ void displayHeader(CommonData &commonData, bool symbolmode, GwApi::BoatValue *da if (commonData.data.rtcValid) { time_t tv = mktime(&commonData.data.rtcTime) + (int)(tz * 3600); struct tm *local_tm = localtime(&tv); - epd->print(formatTime('m', local_tm->tm_hour, local_tm->tm_min, 0)); + epd->print(formatTime(fmtTime::MMHH, local_tm->tm_hour, local_tm->tm_min, 0)); epd->print(" "); epd->print(formatDate(fmttype, local_tm->tm_year + 1900, local_tm->tm_mon + 1, local_tm->tm_mday)); epd->print(" "); diff --git a/lib/obp60task/OBP60Formatter.cpp b/lib/obp60task/OBP60Formatter.cpp index 49021c3..795dfbc 100644 --- a/lib/obp60task/OBP60Formatter.cpp +++ b/lib/obp60task/OBP60Formatter.cpp @@ -20,6 +20,7 @@ Formatter::Formatter(GwConfigHandler *config) { windspeedFormat = config->getString(config->windspeedFormat); tempFormat = config->getString(config->tempFormat); dateFormat = config->getString(config->dateFormat); + dateFmt = getDateFormat(dateFormat); usesimudata = config->getBool(config->useSimuData); precision = config->getString(config->valueprecision); @@ -35,6 +36,37 @@ Formatter::Formatter(GwConfigHandler *config) { } +fmtType Formatter::stringToFormat(const char* formatStr) { + auto it = formatMap.find(formatStr); + if (it != formatMap.end()) { + return it->second; + } + return fmtType::XDR_G; // generic as default +} + +fmtDate Formatter::getDateFormat(String sformat) { + if (sformat == "DE") { + return fmtDate::DE; + } + if (sformat == "GB") { + return fmtDate::GB; + } + if (sformat == "US") { + return fmtDate::US; + } + return fmtDate::ISO; // default +} + +fmtTime Formatter::getTimeFormat(String sformat) { + if (sformat == "MMHH") { + return fmtTime::MMHH; + } + if (sformat == "MMHHSS") { + return fmtTime::MMHHSS; + } + return fmtTime::MMHH; // default +} + FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &commondata){ GwLog *logger = commondata.logger; FormattedData result; @@ -782,32 +814,36 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common return result; } -String formatDate(String fmttype, uint16_t year, uint8_t month, uint8_t day) { +String formatDate(fmtDate fmttype, uint16_t year, uint8_t month, uint8_t day) { char buffer[12]; - if (fmttype == "GB") { + if (fmttype == fmtDate::GB) { snprintf(buffer, 12, "%02d/%02d/%04d", day , month, year); } - else if (fmttype == "US") { + else if (fmttype == fmtDate::US) { snprintf(buffer, 12, "%02d/%02d/%04d", month, day, year); } - else if (fmttype == "ISO") { + else if (fmttype == fmtDate::ISO) { snprintf(buffer, 12, "%04d-%02d-%02d", year, month, day); } - else { + else if (fmttype == fmtDate::DE) { snprintf(buffer, 12, "%02d.%02d.%04d", day, month, year); + } else { + snprintf(buffer, 12, "%04d-%02d-%02d", year, month, day); } return String(buffer); } -String formatTime(char fmttype, uint8_t hour, uint8_t minute, uint8_t second) { - // fmttype: s: with seconds, m: only minutes +String formatTime(fmtTime fmttype, uint8_t hour, uint8_t minute, uint8_t second) { char buffer[10]; - if (fmttype == 'm') { + if (fmttype == fmtTime::MMHH) { snprintf(buffer, 10, "%02d:%02d", hour , minute); } - else { + else if (fmttype == fmtTime::MMHHSS) { snprintf(buffer, 10, "%02d:%02d:%02d", hour, minute, second); } + else { + snprintf(buffer, 10, "%02d%02d%02d", hour, minute, second); + } return String(buffer); } diff --git a/lib/obp60task/OBP60Formatter.h b/lib/obp60task/OBP60Formatter.h index 55a69b4..cc7c14f 100644 --- a/lib/obp60task/OBP60Formatter.h +++ b/lib/obp60task/OBP60Formatter.h @@ -2,41 +2,10 @@ #ifndef _OBP60FORMATTER_H #define _OBP60FORMATTER_H +#include + /* -Formatter names as defined in BoatItemBase - formatCourse - formatKnots - formatWind - formatLatitude - formatLongitude - formatXte - formatFixed0 - formatDepth - kelvinToC TODO not a format but conversion - mtr2nm TODO not a format but conversion - formatDop dilution of precision - formatRot - formatDate - formatTime - formatName - -XDR Formatter names - formatXdr:P:P // pressure percent - formatXdr:P:B // pressure bar - formatXdr:U:V // voltage volt - formatXdr:I:A // current ampere - formatXdr:C:K // temperature kelvin - formatXdr:C:C // temperature celsius - formatXdr:H:P // humidity percent - formatXdr:V:P // volume percent - formatXdr:V:M // volume cubic meters - formatXdr:R:I // flow liter per second? - formatXdr:G: // generic - formatXdr:A:P // angle percent - formatXdr:A:D // angle degrees - formatXdr:T:R // tachometer rpm - XDR types A Angular displacement C Temperature @@ -69,6 +38,75 @@ XDR units */ +enum class fmtType { + // Formatter names as defined in BoatItemBase + COURSE, + KNOTS, + WIND, + LATITUDE, + LONGITUDE, + XTE, + FIXED0, + DEPTH, + DOP, // dilution of precision + ROT, + DATE, + TIME, + NAME, + + kelvinToC, // TODO not a format but conversion + mtr2nm, // TODO not a format but conversion + + // XDR Formatter names + XDR_PP, // pressure percent + XDR_PB, // pressure bar + XDR_UV, // voltage volt + XDR_IA, // current ampere + XDR_CK, // temperature kelvin + XDR_CC, // temperature celsius + XDR_HP, // humidity percent + XDR_VP, // volume percent + XDR_VM, // volume cubic meters + XDR_RI, // flow liter per second? + XDR_G, // generic + XDR_AP, // angle percent + XDR_AD, // angle degrees + XDR_TR // tachometer rpm +}; + +// Hint: String is not supported +static std::unordered_map formatMap PROGMEM = { + {"formatCourse", fmtType::COURSE}, + {"formatKnots", fmtType::KNOTS}, + {"formatWind", fmtType::WIND}, + {"formatLatitude", fmtType::LATITUDE}, + {"formatLongitude", fmtType::LONGITUDE}, + {"formatXte", fmtType::XTE}, + {"formatFixed0", fmtType::FIXED0}, + {"formatDepth", fmtType::DEPTH}, + {"formatDop", fmtType::DOP}, + {"formatRot", fmtType::ROT}, + {"formatDate", fmtType::DATE}, + {"formatTime", fmtType::TIME}, + {"formatName", fmtType::NAME}, + {"kelvinToC", fmtType::kelvinToC}, + {"mtr2nm", fmtType::mtr2nm}, + {"formatXdr:P:P", fmtType::XDR_PP}, + {"formatXdr:P:B", fmtType::XDR_PB}, + {"formatXdr:U:V", fmtType::XDR_UV}, + {"formatXdr:I:A", fmtType::XDR_IA}, + {"formatXdr:C:K", fmtType::XDR_CK}, + {"formatXdr:C:C", fmtType::XDR_CC}, + {"formatXdr:H:P", fmtType::XDR_HP}, + {"formatXdr:V:P", fmtType::XDR_VP}, + {"formatXdr:V:M", fmtType::XDR_VM}, + {"formatXdr:R:I", fmtType::XDR_RI}, + {"formatXdr:G:", fmtType::XDR_G}, + {"formatXdr:A:P", fmtType::XDR_AP}, + {"formatXdr:A:D", fmtType::XDR_AD}, + {"formatXdr:T:R", fmtType::XDR_TR} +}; + // Possible formats as scoped enums enum class fmtDate {DE, GB, US, ISO}; enum class fmtTime {MMHH, MMHHSS}; @@ -106,6 +144,7 @@ private: String windspeedFormat = "kn"; // [m/s|km/h|kn|bft] String tempFormat = "C"; // [K|°C|°F] String dateFormat = "ISO"; // [DE|GB|US|ISO] + fmtDate dateFmt; bool usesimudata = false; // [on|off] String precision = "2"; // [1|2] @@ -115,13 +154,16 @@ private: public: Formatter(GwConfigHandler *config); + fmtType stringToFormat(const char* formatStr); + fmtDate getDateFormat(String sformat); + fmtTime getTimeFormat(String sformat); FormattedData formatValue(GwApi::BoatValue *value, CommonData &commondata); String placeholder = "---"; }; -// Standard format functions without overhead -String formatDate(String fmttype, uint16_t year, uint8_t month, uint8_t day); -String formatTime(char fmttype, uint8_t hour, uint8_t minute, uint8_t second); +// Standard format functions without class and overhead +String formatDate(fmtDate fmttype, uint16_t year, uint8_t month, uint8_t day); +String formatTime(fmtTime fmttype, uint8_t hour, uint8_t minute, uint8_t second); String formatLatitude(double lat); String formatLongitude(double lon); diff --git a/lib/obp60task/PageClock.cpp b/lib/obp60task/PageClock.cpp index f043198..0bd0b20 100644 --- a/lib/obp60task/PageClock.cpp +++ b/lib/obp60task/PageClock.cpp @@ -18,7 +18,7 @@ class PageClock : public Page { private: - String dateformat; + fmtDate dateformat; int simtime; bool keylock = false; char source = 'R'; // time source (R)TC | (G)PS | (N)TP @@ -35,7 +35,7 @@ public: logger->logDebug(GwLog::LOG, "Instantiate PageClock"); // Get config data - dateformat = config->getString(config->dateFormat); + dateformat = common.fmt->getDateFormat(config->getString(config->dateFormat)); timezone = config->getString(config->timeZone).toDouble(); homelat = config->getString(config->homeLAT).toDouble(); homelon = config->getString(config->homeLON).toDouble(); @@ -229,10 +229,10 @@ public: } else if (commonData->data.rtcValid) { if (tz == 'L') { - epd->print(formatTime('s', local_tm->tm_hour, local_tm->tm_min, local_tm->tm_sec)); + epd->print(formatTime(fmtTime::MMHHSS, local_tm->tm_hour, local_tm->tm_min, local_tm->tm_sec)); } else { - epd->print(formatTime('s', commonData->data.rtcTime.tm_hour, commonData->data.rtcTime.tm_min, commonData->data.rtcTime.tm_sec)); + epd->print(formatTime(fmtTime::MMHHSS, commonData->data.rtcTime.tm_hour, commonData->data.rtcTime.tm_min, commonData->data.rtcTime.tm_sec)); } } else { epd->print(commonData->fmt->placeholder); diff --git a/lib/obp60task/Pagedata.h b/lib/obp60task/Pagedata.h index 7a331d0..b2f7d1e 100644 --- a/lib/obp60task/Pagedata.h +++ b/lib/obp60task/Pagedata.h @@ -208,7 +208,7 @@ class PageDescription{ class PageStruct{ public: - Page *page=NULL; + Page *page = nullptr; PageData parameters; - PageDescription *description=NULL; + PageDescription *description = nullptr; };