Add formater for values

This commit is contained in:
norbert-walter 2022-02-20 19:21:15 +01:00
parent 0b17b96900
commit f30bd27ad6
4 changed files with 228 additions and 50 deletions

View File

@ -0,0 +1,180 @@
#include <Arduino.h>
#include "GwApi.h"
#include "Pagedata.h"
// ToDo
// simulation data
// hold values by missing data
// different unit convertion
FormatedData formatValue(GwApi::BoatValue *value){
FormatedData result;
if (! value->valid){
result.svalue = "---";
result.unit = "";
return result;
}
static const int bsize=30;
char buffer[bsize+1];
buffer[0]=0;
if (value->getFormat() == "formatDate"){
time_t tv=tNMEA0183Msg::daysToTime_t(value->value);
tmElements_t parts;
tNMEA0183Msg::breakTime(tv,parts);
snprintf(buffer,bsize,"%04d/%02d/%02d",parts.tm_year+1900,parts.tm_mon+1,parts.tm_mday);
result.unit = "";
}
else if(value->getFormat() == "formatTime"){
double inthr;
double intmin;
double intsec;
double val;
val=modf(value->value/3600.0,&inthr);
val=modf(val*3600.0/60.0,&intmin);
modf(val*60.0,&intsec);
snprintf(buffer,bsize,"%02.0f:%02.0f:%02.0f",inthr,intmin,intsec);
result.unit = "";
}
else if (value->getFormat() == "formatFixed0"){
snprintf(buffer,bsize,"%.0f",value->value);
result.unit = "";
}
else if (value->getFormat() == "formatCourse"){
double course = value->value;
course = course * 57.2958; // Unit conversion form rad to deg
result.unit = "Deg";
if(course < 10){
snprintf(buffer,bsize,"%2.1f",course);
}
if(course >= 10 && course < 100){
snprintf(buffer,bsize,"%3.1f",course);
}
if(course >= 100){
snprintf(buffer,bsize,"%3.0f",course);
}
}
else if (value->getFormat() == "formatKnots" || value->getFormat() == "formatWind"){
double speed = value->value;
speed = speed * 1.94384; // Unit conversion form m/s to kn
result.unit = "kn";
if(speed < 10){
snprintf(buffer,bsize,"%2.1f",speed);
}
if(speed >= 10 && speed < 100){
snprintf(buffer,bsize,"%3.1f",speed);
}
if(speed >= 100){
snprintf(buffer,bsize,"%3.0f",speed);
}
}
else if (value->getFormat() == "formatRot"){
double rotation = value->value;
rotation = rotation * 57.2958; // Unit conversion form rad/s to deg/s
result.unit = "deg/s";
if(rotation < 10){
snprintf(buffer,bsize,"%2.1f",rotation);
}
if(rotation >= 10 && rotation < 100){
snprintf(buffer,bsize,"%3.1f",rotation);
}
if(rotation >= 100){
snprintf(buffer,bsize,"%3.0f",rotation);
}
}
else if (value->getFormat() == "formatDop"){
double dop = value->value;
result.unit = "m";
if(dop < 10){
snprintf(buffer,bsize,"%2.1f",dop);
}
if(dop >= 10 && dop < 100){
snprintf(buffer,bsize,"%3.1f",dop);
}
if(dop >= 100){
snprintf(buffer,bsize,"%3.0f",dop);
}
}
else if (value->getFormat() == "formatLatitude"){
double lat = value->value;
String latitude = "";
String latdir = "";
float degree = int(lat);
float minute = (lat - degree) * 60;
float secound = (minute - int(minute)) * 60;
if(lat > 0){
latdir = "N";
}
else{
latdir = "S";
}
latitude = String(degree,0) + "\" " + String(minute,0) + "' " + String(secound, 4) + " " + latdir;
result.unit = "";
strcpy(buffer, latitude.c_str());
}
else if (value->getFormat() == "formatLongitude"){
double lon = value->value;
String longitude = "";
String londir = "";
float degree = int(lon);
float minute = (lon - degree) * 60;
float secound = (minute - int(minute)) * 60;
if(lon > 0){
londir = "E";
}
else{
londir = "W";
}
longitude = String(degree,0) + "\" " + String(minute,0) + "' " + String(secound, 4) + " " + londir;
result.unit = "";
strcpy(buffer, longitude.c_str());
}
else if (value->getFormat() == "formatDepth"){
double depth = value->value;
result.unit = "m";
if(depth < 10){
snprintf(buffer,bsize,"%2.1f",depth);
}
if(depth >= 10 && depth < 100){
snprintf(buffer,bsize,"%3.1f",depth);
}
if(depth >= 100){
snprintf(buffer,bsize,"%3.0f",depth);
}
}
else if (value->getFormat() == "kelvinToC"){
double temp = value->value;
temp = temp - 273.15;
result.unit = "degree";
if(temp < 10){
snprintf(buffer,bsize,"%2.1f",temp);
}
if(temp >= 10 && temp < 100){
snprintf(buffer,bsize,"%3.1f",temp);
}
if(temp >= 100){
snprintf(buffer,bsize,"%3.0f",temp);
}
}
else if (value->getFormat() == "mtr2nm"){
double distance = value->value;
distance = distance * 0.000539957;
result.unit = "nm";
if(distance < 10){
snprintf(buffer,bsize,"%2.1f",distance);
}
if(distance >= 10 && distance < 100){
snprintf(buffer,bsize,"%3.1f",distance);
}
if(distance >= 100){
snprintf(buffer,bsize,"%3.0f",distance);
}
}
else{
snprintf(buffer,bsize,"%3.0f",value->value);
result.unit = "";
}
buffer[bsize]=0;
result.svalue = String(buffer);
return result;
}

View File

@ -14,14 +14,15 @@ class PageOneValue : public Page{
bool holdvalues = config->getBool(config->holdvalues);
// Get boat values
GwApi::BoatValue *value=pageData.values[0];
String name1 = value->getName().c_str();
double value1 = value->value;
bool valid1 = value->valid;
String format1 = value->getFormat().c_str();
GwApi::BoatValue *bvalue=pageData.values[0]; // First element in list (only one value by PageOneValue)
String name1 = bvalue->getName().c_str(); // Value name
double value1 = bvalue->value; // Value as double in SI unit
String svalue1 = formatValue(bvalue).svalue; // Formatted value as string including unit conversion and switching decimal places
String unit1 = formatValue(bvalue).unit; // Unit of value
bool valid1 = bvalue->valid; // Valid flag of value
// Logging boat values
if (value == NULL) return;
if (bvalue == NULL) return;
LOG_DEBUG(GwLog::LOG,"Drawing at PageOneValue, p=%s, v=%f", name1, value1);
// Draw page
@ -48,17 +49,21 @@ class PageOneValue : public Page{
display.setFont(&Ubuntu_Bold32pt7b);
display.setCursor(20, 100);
display.print(name1); // Page name
// Show unit
display.setFont(&Ubuntu_Bold20pt7b);
display.setCursor(270, 100);
// Show unit
if(String(lengthformat) == "m"){
display.print("m");
display.print(unit1);
// Switch font if format latitude or longitude
if(bvalue->getFormat() == "formatLatitude" || bvalue->getFormat() == "formatLongitude"){
display.setFont(&Ubuntu_Bold20pt7b);
display.setCursor(20, 180);
}
if(String(lengthformat) == "ft"){
display.print("ft");
else{
display.setFont(&DSEG7Classic_BoldItalic60pt7b);
display.setCursor(20, 240);
}
display.setFont(&DSEG7Classic_BoldItalic60pt7b);
display.setCursor(20, 240);
// Reading bus data or using simulation data
if(simulation == true){
@ -67,26 +72,7 @@ class PageOneValue : public Page{
display.print(value1,1);
}
else{
// Check vor valid real data, display also if hold values activated
if(valid1 == true || holdvalues == true){
// Unit conversion
if(String(lengthformat) == "m"){
value1 = value1; // Real bus data m
}
if(String(lengthformat) == "ft"){
value1 = value1 * 3.28084; // Bus data in ft
}
// Resolution switching
if(value1 <= 99.9){
display.print(value1,1);
}
else{
display.print(value1,0);
}
}
else{
display.print("---"); // Missing bus data
}
display.print(svalue1); // Real value as formated string
}
// Key Layout
@ -112,7 +98,8 @@ static Page* createPage(CommonData &common){return new PageOneValue();}
* this will be number of BoatValue pointers in pageData.values
*/
PageDescription registerPageOneValue(
"oneValue",
createPage,
1
"oneValue", // Name of page
createPage, // Action
1, // Number of bus values depends on selection in Web configuration
true // Show display header on/off
);

View File

@ -37,17 +37,18 @@ public:
bool valid1 = true;
// Optical warning by limit violation
bool activViolation = false;
if(String(flashLED) == "Limit Violation"){
if(String(batType) == "Pb" && (value1 < 10.0 || value1 > 14.5)){
// Limits for Pb battery
if(activViolation == false && String(batType) == "Pb" && (value1 < 10.0 || value1 > 14.5)){
setPortPin(OBP_FLASH_LED, true);
activViolation = true;
}
else{
if(activViolation == true && String(batType) == "Pb" && !(value1 < 10.0 || value1 > 14.5)){
setPortPin(OBP_FLASH_LED, false);
activViolation = false;
}
}
else{
setPortPin(OBP_FLASH_LED, false);
}
// Logging voltage value
if (value1 == NULL) return;
@ -132,9 +133,9 @@ static Page *createPage(CommonData &common){
* and will will provide the names of the fixed values we need
*/
PageDescription registerPageVoltage(
"Voltage",
createPage,
0,
{},
true
"Voltage", // Name of page
createPage, // Action
0, // Number of bus values depends on selection in Web configuration
{}, // Names of bus values undepends on selection in Web configuration (refer GwBoatData.h)
true // Show display header on/off
);

View File

@ -59,3 +59,13 @@ class PageDescription{
this->header=header;
}
};
// Structure for formated boat values
typedef struct{
String svalue;
String unit;
} FormatedData;
// Formater for boat values
FormatedData formatValue(GwApi::BoatValue *value);