From fbe6c1a9a54c7ae902921b056791cbc16620ff69 Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Thu, 21 Aug 2025 10:34:58 +0200 Subject: [PATCH] Prepared formatter class for boat values. E.g. for better config handling --- lib/obp60task/OBP60Extensions.h | 1 + lib/obp60task/OBP60Formatter.cpp | 16 ++++ lib/obp60task/OBP60Formatter.h | 123 +++++++++++++++++++++++++++++++ lib/obp60task/Pagedata.h | 48 +++++------- lib/obp60task/README | 1 + lib/obp60task/TODO | 2 + lib/obp60task/obp60task.cpp | 8 +- 7 files changed, 165 insertions(+), 34 deletions(-) create mode 100644 lib/obp60task/OBP60Formatter.h diff --git a/lib/obp60task/OBP60Extensions.h b/lib/obp60task/OBP60Extensions.h index d619a64..4caf5ed 100644 --- a/lib/obp60task/OBP60Extensions.h +++ b/lib/obp60task/OBP60Extensions.h @@ -4,6 +4,7 @@ #include #include "OBP60Hardware.h" +#include "OBP60Formatter.h" #include "LedSpiTask.h" #include "Graphics.h" #include // E-paper lib V2 diff --git a/lib/obp60task/OBP60Formatter.cpp b/lib/obp60task/OBP60Formatter.cpp index 8079bc3..8f4f26f 100644 --- a/lib/obp60task/OBP60Formatter.cpp +++ b/lib/obp60task/OBP60Formatter.cpp @@ -3,11 +3,27 @@ #include #include "GwApi.h" #include "Pagedata.h" +#include "OBP60Formatter.h" // ToDo // simulation data // hold values by missing data +Formatter::Formatter(GwConfigHandler *config) { + // Load configuration values + // TODO do not use strings but enums, see header file + stimeZone = config->getString(config->timeZone); + timeZone = stimeZone.toDouble(); + lengthFormat = config->getString(config->lengthFormat); + distanceFormat = config->getString(config->distanceFormat); + speedFormat = config->getString(config->speedFormat); + windspeedFormat = config->getString(config->windspeedFormat); + tempFormat = config->getString(config->tempFormat); + dateFormat = config->getString(config->dateFormat); + usesimudata = config->getBool(config->useSimuData); + precision = config->getString(config->valueprecision); +} + String formatDate(String fmttype, uint16_t year, uint8_t month, uint8_t day) { char buffer[12]; if (fmttype == "GB") { diff --git a/lib/obp60task/OBP60Formatter.h b/lib/obp60task/OBP60Formatter.h new file mode 100644 index 0000000..72360f2 --- /dev/null +++ b/lib/obp60task/OBP60Formatter.h @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#ifndef _OBP60FORMATTER_H +#define _OBP60FORMATTER_H + +/* + +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 + D Linear displacement + F Frequency + G Generic + H Humidity + I Current + L Salinity + N Force + P Pressure + R Flow + S Switch or valve + T Tachometer + U Voltage + V Volume + +XDR units + A Ampere + B Bar + C Celsius + D Degrees + H Hertz + I Liter per second? + M Meter / Cubic meter + N Newton + P Percent + R RPM + V Volt + + */ + +// Possible formats as scoped enums +enum class fmtDate {DE, EN, GB, ISO}; +enum class fmtTime {MMHH, MMHHSS}; +enum class fmtLength {METER, FEET, FATHOM, CABLE}; +enum class fmtDepth {METER, FEET, FATHOM}; +enum class fmtWind {KMH, MS, KN, BFT}; +enum class fmtCourse {DEG, RAD}; +enum class fmtRot {DEGS, RADS}; +enum class fmtXte {M, KM, NM, CABLE}; +enum class fmtPress {PA, BAR}; +enum class fmtTemp {KELVIN, CELSUIS, FAHRENHEIT}; + +// Conversion factors +#define CONV_M_FT 3.2808399 // meter too feet +#define CONV_M_FM 0.5468 // meter to fathom +#define CONV_M_CBL 0.0053961182483768 // meter to cable +#define CONV_CBL_FT 608 // cable to feet +#define CONV_FM_FT 6 // fathom to feet + +// Structure for formatted boat values +typedef struct { + double value; + String svalue; + String unit; +} FormattedData; + +// Formatter for boat values +FormattedData formatValue(GwApi::BoatValue *value, CommonData &commondata); + +class Formatter { +private: + String stimeZone = "0"; + double timeZone = 0.0; // [UTC -14.00...+12.00] + String lengthFormat = "m"; // [m|ft] + String distanceFormat = "nm"; // [m|km|nm] + String speedFormat = "kn"; // [m/s|km/h|kn] + String windspeedFormat = "kn"; // [m/s|km/h|kn|bft] + String tempFormat = "C"; // [K|°C|°F] + String dateFormat = "ISO"; // [DE|GB|US|ISO] + bool usesimudata = false; // [on|off] + String precision = "2"; // [1|2] +public: + Formatter(GwConfigHandler *config); +}; + +// 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); +String formatLatitude(double lat); +String formatLongitude(double lon); + +#endif diff --git a/lib/obp60task/Pagedata.h b/lib/obp60task/Pagedata.h index cbf738d..7a331d0 100644 --- a/lib/obp60task/Pagedata.h +++ b/lib/obp60task/Pagedata.h @@ -105,22 +105,24 @@ typedef struct{ int voltage = 0; } AvgData; +class Formatter; // forward declaration typedef struct{ - GwApi::Status status; - GwLog *logger=NULL; - GwConfigHandler *config=NULL; - SensorData data; - SunData sundata; - TouchKeyData keydata[6]; - BacklightData backlight; - AlarmData alarm; - AvgData avgdata; - GwApi::BoatValue *time=NULL; - GwApi::BoatValue *date=NULL; - uint16_t fgcolor; - uint16_t bgcolor; - bool keylock = false; - String powermode; + GwApi::Status status; + GwLog *logger = nullptr; + GwConfigHandler *config = nullptr; + Formatter *fmt = nullptr; + SensorData data; + SunData sundata; + TouchKeyData keydata[6]; + BacklightData backlight; + AlarmData alarm; + AvgData avgdata; + GwApi::BoatValue *time = nullptr; + GwApi::BoatValue *date = nullptr; + uint16_t fgcolor; + uint16_t bgcolor; + bool keylock = false; + String powermode; } CommonData; //a base class that all pages must inherit from @@ -210,19 +212,3 @@ class PageStruct{ PageData parameters; PageDescription *description=NULL; }; - -// 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); -String formatLatitude(double lat); -String formatLongitude(double lon); - -// Structure for formatted boat values -typedef struct{ - double value; - String svalue; - String unit; -} FormattedData; - -// Formatter for boat values -FormattedData formatValue(GwApi::BoatValue *value, CommonData &commondata); diff --git a/lib/obp60task/README b/lib/obp60task/README index 02dca63..9a2c2cc 100644 --- a/lib/obp60task/README +++ b/lib/obp60task/README @@ -10,6 +10,7 @@ WIP Please format your new code the same as already existing code. Preprocessor directives go to column zero. +Identation is 4 spaces Git commands ------------ diff --git a/lib/obp60task/TODO b/lib/obp60task/TODO index 451a4b5..d458f5d 100644 --- a/lib/obp60task/TODO +++ b/lib/obp60task/TODO @@ -15,3 +15,5 @@ - page clock: sunrise / sunset in local time or UTC - implement alerts + +- implement formatter as class diff --git a/lib/obp60task/obp60task.cpp b/lib/obp60task/obp60task.cpp index 84a2085..a26c7b9 100644 --- a/lib/obp60task/obp60task.cpp +++ b/lib/obp60task/obp60task.cpp @@ -2,6 +2,7 @@ #if defined BOARD_OBP60S3 || defined BOARD_OBP40S3 #include "obp60task.h" #include "Pagedata.h" // Data exchange for pages +#include "OBP60Formatter.h" // Data formatting for boat values #include "OBP60Hardware.h" // PIN definitions #include // I2C connections #include // MCP23017 extension Port @@ -274,7 +275,7 @@ void registerAllPages(GwLog *logger, PageList &list){ extern PageDescription registerPageAIS; list.add(®isterPageAIS); extern PageDescription registerPageBarograph; - list.add(®isterPageBarograph); + list.add(®isterPageBarograph); logger->logDebug(GwLog::LOG,"Memory after registering pages: stack=%d, heap=%d", uxTaskGetStackHighWaterMark(NULL), ESP.getFreeHeap()); } @@ -548,8 +549,9 @@ void OBP60Task(GwApi *api){ PageList allPages; registerAllPages(logger, allPages); CommonData commonData; - commonData.logger=logger; - commonData.config=config; + commonData.logger = logger; + commonData.config = config; + commonData.fmt = new Formatter(config); #ifdef HARDWARE_V21 // Keyboard coordinates for page footer