More work on formatter

This commit is contained in:
Thomas Hooge 2025-08-22 15:17:54 +02:00
parent 672a3843d1
commit c932724473
5 changed files with 131 additions and 53 deletions

View File

@ -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(" ");

View File

@ -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);
}

View File

@ -2,41 +2,10 @@
#ifndef _OBP60FORMATTER_H
#define _OBP60FORMATTER_H
#include <unordered_map>
/*
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<const char*, fmtType> 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);

View File

@ -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);

View File

@ -208,7 +208,7 @@ class PageDescription{
class PageStruct{
public:
Page *page=NULL;
Page *page = nullptr;
PageData parameters;
PageDescription *description=NULL;
PageDescription *description = nullptr;
};