mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2026-03-28 18:06:37 +01:00
Integrate many changes from master
This commit is contained in:
@@ -67,14 +67,23 @@ fmtTime Formatter::getTimeFormat(String sformat) {
|
||||
return fmtTime::MMHH; // default
|
||||
}
|
||||
|
||||
FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &commondata){
|
||||
FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &commondata, bool ignoreSimuDataSetting) {
|
||||
GwLog *logger = commondata.logger;
|
||||
FormattedData result;
|
||||
static int dayoffset = 0;
|
||||
double rawvalue = 0;
|
||||
|
||||
bool simulation;
|
||||
if (ignoreSimuDataSetting) {
|
||||
simulation = false; // ignore user setting for simulation data; we want to format the boat value passed to this function
|
||||
} else {
|
||||
simulation = usesimudata; // use setting from configuration
|
||||
}
|
||||
|
||||
result.cvalue = value->value;
|
||||
|
||||
// If boat value not valid
|
||||
if (! value->valid && !usesimudata){
|
||||
if (! value->valid && !simulation){
|
||||
result.svalue = placeholder;
|
||||
return result;
|
||||
}
|
||||
@@ -98,7 +107,7 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
tmElements_t parts;
|
||||
time_t tv=tNMEA0183Msg::daysToTime_t(value->value + dayoffset);
|
||||
tNMEA0183Msg::breakTime(tv,parts);
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
if (String(dateFormat) == "DE") {
|
||||
snprintf(buffer,bsize, "%02d.%02d.%04d", parts.tm_mday, parts.tm_mon+1, parts.tm_year+1900);
|
||||
}
|
||||
@@ -132,11 +141,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
if (timeInSeconds > 86400) {timeInSeconds = timeInSeconds - 86400;}
|
||||
if (timeInSeconds < 0) {timeInSeconds = timeInSeconds + 86400;}
|
||||
// LOG_DEBUG(GwLog::DEBUG,"... formatTime value: %f tz: %f corrected timeInSeconds: %f ", value->value, timeZone, timeInSeconds);
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
val = modf(timeInSeconds/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.cvalue = timeInSeconds;
|
||||
}
|
||||
else{
|
||||
static long sec;
|
||||
@@ -146,13 +156,14 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
}
|
||||
sec = sec % 60;
|
||||
snprintf(buffer, bsize, "11:36:%02i", int(sec));
|
||||
result.cvalue = sec;
|
||||
lasttime = millis();
|
||||
}
|
||||
result.unit = ((timeZone == 0) ? "UTC" : "LOT");
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatFixed0"){
|
||||
if(usesimudata == false) {
|
||||
if(simulation == false) {
|
||||
snprintf(buffer, bsize, "%3.0f", value->value);
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -161,16 +172,17 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, "%3.0f", rawvalue);
|
||||
}
|
||||
result.unit = "";
|
||||
result.cvalue = rawvalue;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatCourse" || value->getFormat() == "formatWind"){
|
||||
double course = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
course = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
else {
|
||||
course = 2.53 + float(random(0, 10) / 100.0);
|
||||
course = M_PI_2 + float(random(-17, 17) / 100.0); // create random course/wind values with 90° +/- 10°
|
||||
rawvalue = course;
|
||||
}
|
||||
course = course * 57.2958; // Unit conversion form rad to deg
|
||||
@@ -178,16 +190,17 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
// Format 3 numbers with prefix zero
|
||||
snprintf(buffer,bsize,"%03.0f",course);
|
||||
result.unit = "Deg";
|
||||
result.cvalue = course;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatKnots" && (value->getName() == "SOG" || value->getName() == "STW")){
|
||||
double speed = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
speed = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
else{
|
||||
rawvalue = 4.0 + float(random(0, 40));
|
||||
rawvalue = 4.0 + float(random(-30, 40) / 10.0); // create random speed values from [1..8] m/s
|
||||
speed = rawvalue;
|
||||
}
|
||||
if (String(speedFormat) == "km/h"){
|
||||
@@ -211,16 +224,17 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
else {
|
||||
snprintf(buffer, bsize, fmt_dec_100, speed);
|
||||
}
|
||||
result.cvalue = speed;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatKnots" && (value->getName() == "AWS" || value->getName() == "TWS" || value->getName() == "MaxAws" || value->getName() == "MaxTws")){
|
||||
double speed = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
speed = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
else {
|
||||
rawvalue = 4.0 + float(random(0, 40));
|
||||
rawvalue = 4.0 + float(random(0, 40) / 10.0); // create random wind speed values from [4..8] m/s
|
||||
speed = rawvalue;
|
||||
}
|
||||
if (String(windspeedFormat) == "km/h"){
|
||||
@@ -291,11 +305,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, speed);
|
||||
}
|
||||
}
|
||||
result.cvalue = speed;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatRot"){
|
||||
double rotation = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
rotation = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -317,11 +332,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
else {
|
||||
snprintf(buffer, bsize, "%3.0f", rotation);
|
||||
}
|
||||
result.cvalue = rotation;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatDop"){
|
||||
double dop = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
dop = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -342,10 +358,11 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
else {
|
||||
snprintf(buffer, bsize, fmt_dec_100, dop);
|
||||
}
|
||||
result.cvalue = dop;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatLatitude"){
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
double lat = value->value;
|
||||
rawvalue = value->value;
|
||||
String latitude = "";
|
||||
@@ -361,10 +378,11 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
rawvalue = 35.0 + float(random(0, 10)) / 10000.0;
|
||||
snprintf(buffer, bsize, " 51\" %2.4f' N", rawvalue);
|
||||
}
|
||||
result.cvalue = rawvalue;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatLongitude"){
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
double lon = value->value;
|
||||
rawvalue = value->value;
|
||||
String longitude = "";
|
||||
@@ -380,16 +398,17 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
rawvalue = 6.0 + float(random(0, 10)) / 100000.0;
|
||||
snprintf(buffer, bsize, " 15\" %2.4f'", rawvalue);
|
||||
}
|
||||
result.cvalue = rawvalue;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatDepth"){
|
||||
double depth = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
depth = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
else {
|
||||
rawvalue = 18.0 + float(random(0, 100)) / 10.0;
|
||||
rawvalue = 18.0 + float(random(0, 100)) / 10.0; // create random depth values from [18..28] metres
|
||||
depth = rawvalue;
|
||||
}
|
||||
if(String(lengthFormat) == "ft"){
|
||||
@@ -408,11 +427,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
else {
|
||||
snprintf(buffer, bsize, fmt_dec_100, depth);
|
||||
}
|
||||
result.cvalue = depth;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXte"){
|
||||
double xte = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
xte = value->value;
|
||||
rawvalue = value->value;
|
||||
} else {
|
||||
@@ -436,11 +456,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
else {
|
||||
snprintf(buffer, bsize, "%3.0f", xte);
|
||||
}
|
||||
result.cvalue = xte;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "kelvinToC"){
|
||||
double temp = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
temp = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -468,11 +489,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
else {
|
||||
snprintf(buffer, bsize, fmt_dec_100, temp);
|
||||
}
|
||||
result.cvalue = temp;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "mtr2nm"){
|
||||
double distance = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
distance = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -500,6 +522,7 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
else {
|
||||
snprintf(buffer, bsize, fmt_dec_100, distance);
|
||||
}
|
||||
result.cvalue = distance;
|
||||
}
|
||||
//########################################################
|
||||
// Special XDR formats
|
||||
@@ -507,7 +530,7 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:P:P"){
|
||||
double pressure = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
pressure = value->value;
|
||||
rawvalue = value->value;
|
||||
pressure = pressure / 100.0; // Unit conversion form Pa to hPa
|
||||
@@ -518,11 +541,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
}
|
||||
snprintf(buffer, bsize, "%4.0f", pressure);
|
||||
result.unit = "hPa";
|
||||
result.cvalue = pressure;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:P:B"){
|
||||
double pressure = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
pressure = value->value;
|
||||
rawvalue = value->value;
|
||||
pressure = pressure / 100.0; // Unit conversion form Pa to mBar
|
||||
@@ -533,11 +557,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
}
|
||||
snprintf(buffer, bsize, "%4.0f", pressure);
|
||||
result.unit = "mBar";
|
||||
result.cvalue = pressure;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:U:V"){
|
||||
double voltage = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
voltage = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -552,11 +577,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_10, voltage);
|
||||
}
|
||||
result.unit = "V";
|
||||
result.cvalue = voltage;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:I:A"){
|
||||
double current = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
current = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -574,11 +600,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, current);
|
||||
}
|
||||
result.unit = "A";
|
||||
result.cvalue = current;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:C:K"){
|
||||
double temperature = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
temperature = value->value - 273.15; // Convert K to C
|
||||
rawvalue = value->value - 273.15;
|
||||
}
|
||||
@@ -596,11 +623,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, temperature);
|
||||
}
|
||||
result.unit = "Deg C";
|
||||
result.cvalue = temperature;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:C:C"){
|
||||
double temperature = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
temperature = value->value; // Value in C
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -618,11 +646,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, temperature);
|
||||
}
|
||||
result.unit = "Deg C";
|
||||
result.cvalue = temperature;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:H:P"){
|
||||
double humidity = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
humidity = value->value; // Value in %
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -640,11 +669,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, humidity);
|
||||
}
|
||||
result.unit = "%";
|
||||
result.cvalue = humidity;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:V:P"){
|
||||
double volume = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
volume = value->value; // Value in %
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -662,11 +692,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, volume);
|
||||
}
|
||||
result.unit = "%";
|
||||
result.cvalue = volume;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:V:M"){
|
||||
double volume = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
volume = value->value; // Value in l
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -684,11 +715,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, volume);
|
||||
}
|
||||
result.unit = "l";
|
||||
result.cvalue = volume;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:R:I"){
|
||||
double flow = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
flow = value->value; // Value in l/min
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -706,11 +738,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, flow);
|
||||
}
|
||||
result.unit = "l/min";
|
||||
result.cvalue = flow;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:G:"){
|
||||
double generic = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
generic = value->value;
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -728,11 +761,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, generic);
|
||||
}
|
||||
result.unit = "";
|
||||
result.cvalue = generic;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:A:P"){
|
||||
double dplace = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
dplace = value->value; // Value in %
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -750,11 +784,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, dplace);
|
||||
}
|
||||
result.unit = "%";
|
||||
result.cvalue = dplace;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:A:D"){
|
||||
else if ((value->getFormat() == "formatXdr:A:D") || ((value->getFormat() == "formatXdr:A:rd"))){
|
||||
double angle = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
angle = value->value;
|
||||
angle = angle * 57.2958; // Unit conversion form rad to deg
|
||||
rawvalue = value->value;
|
||||
@@ -770,11 +805,12 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer,bsize,"%3.0f",angle);
|
||||
}
|
||||
result.unit = "Deg";
|
||||
result.cvalue = angle;
|
||||
}
|
||||
//########################################################
|
||||
else if (value->getFormat() == "formatXdr:T:R"){
|
||||
double rpm = 0;
|
||||
if (usesimudata == false) {
|
||||
if (simulation == false) {
|
||||
rpm = value->value; // Value in rpm
|
||||
rawvalue = value->value;
|
||||
}
|
||||
@@ -792,6 +828,7 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, rpm);
|
||||
}
|
||||
result.unit = "rpm";
|
||||
result.cvalue = rpm;
|
||||
}
|
||||
//########################################################
|
||||
// Default format
|
||||
@@ -807,6 +844,7 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
snprintf(buffer, bsize, fmt_dec_100, value->value);
|
||||
}
|
||||
result.unit = "";
|
||||
result.cvalue = value->value;
|
||||
}
|
||||
buffer[bsize] = 0;
|
||||
result.value = rawvalue; // Return value is only necessary in case of simulation of graphic pointer
|
||||
@@ -814,6 +852,37 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
|
||||
return result;
|
||||
}
|
||||
|
||||
// Convert and format boat value from SI to user defined format (definition for compatibility purposes)
|
||||
FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &commondata) {
|
||||
return formatValue(value, commondata, false); // call <formatValue> with standard handling of user setting for simulation data
|
||||
}
|
||||
|
||||
// Helper method for conversion of any data value from SI to user defined format
|
||||
double Formatter::convertValue(const double &value, const String &name, const String &format, CommonData &commondata)
|
||||
{
|
||||
std::unique_ptr<GwApi::BoatValue> tmpBValue; // Temp variable to get converted data value from <OBP60Formatter::formatValue>
|
||||
double result; // data value converted to user defined target data format
|
||||
constexpr bool NO_SIMUDATA = true; // switch off simulation feature of <formatValue> function
|
||||
|
||||
// prepare temporary BoatValue structure for use in <formatValue>
|
||||
tmpBValue = std::unique_ptr<GwApi::BoatValue>(new GwApi::BoatValue(name)); // we don't need boat value name for pure value conversion
|
||||
tmpBValue->setFormat(format);
|
||||
tmpBValue->valid = true;
|
||||
tmpBValue->value = value;
|
||||
|
||||
result = formatValue(tmpBValue.get(), commondata, NO_SIMUDATA).cvalue; // get value (converted); ignore any simulation data setting
|
||||
return result;
|
||||
}
|
||||
|
||||
// Helper method for conversion of any data value from SI to user defined format
|
||||
double Formatter::convertValue(const double &value, const String &format, CommonData &commondata)
|
||||
{
|
||||
double result; // data value converted to user defined target data format
|
||||
|
||||
result = convertValue(value, "dummy", format, commondata);
|
||||
return result;
|
||||
}
|
||||
|
||||
String formatDate(fmtDate fmttype, uint16_t year, uint8_t month, uint8_t day) {
|
||||
char buffer[12];
|
||||
if (fmttype == fmtDate::GB) {
|
||||
|
||||
Reference in New Issue
Block a user