From b556016bbafd5300d08e2d3a155b2b0b3b1ca29e Mon Sep 17 00:00:00 2001 From: Sam Nobs <3915450+samsta@users.noreply.github.com> Date: Fri, 18 Sep 2020 15:29:28 +1200 Subject: [PATCH] Use integer formatting where possible when interpreting signal. Numbers are formatted as floating point numbers, which looks a bit crap for things like e.g. serial numbers (we don't need mantissa and exponent). This change checks the scaling factor, and if it is integer representable, the resulting number is formatted as an integer number as that's most likely what's intended, particularly if the factor is 1. --- dbc/dbc_classes.cpp | 11 +++++++---- dbc/dbc_classes.h | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/dbc/dbc_classes.cpp b/dbc/dbc_classes.cpp index f692f7e..e9a1e3e 100644 --- a/dbc/dbc_classes.cpp +++ b/dbc/dbc_classes.cpp @@ -40,6 +40,7 @@ bool DBC_SIGNAL::processAsText(const CANFrame &frame, QString &outString, bool o { int64_t result = 0; bool isSigned = false; + bool isInteger = false; double endResult; if (valType == STRING) @@ -70,7 +71,9 @@ bool DBC_SIGNAL::processAsText(const CANFrame &frame, QString &outString, bool o { result = Utility::processIntegerSignal(frame.data, startBit, signalSize, intelByteOrder, isSigned); endResult = ((double)result * factor) + bias; - result = (int64_t)endResult; + result = (int64_t)endResult; + // if factor is an integer, we don't need the possibly human-unreadable float representation + isInteger = (factor == ceilf(factor)); } else if (valType == SP_FLOAT) { @@ -95,12 +98,12 @@ bool DBC_SIGNAL::processAsText(const CANFrame &frame, QString &outString, bool o endResult = (*((double *)(&result)) * factor) + bias; } - outString = makePrettyOutput(endResult, result, outputName); + outString = makePrettyOutput(endResult, result, outputName, isInteger); cachedValue = endResult; return true; } -QString DBC_SIGNAL::makePrettyOutput(double floatVal, int64_t intVal, bool outputName) +QString DBC_SIGNAL::makePrettyOutput(double floatVal, int64_t intVal, bool outputName, bool isInteger) { QString outputString; @@ -122,7 +125,7 @@ QString DBC_SIGNAL::makePrettyOutput(double floatVal, int64_t intVal, bool outpu } else //otherwise display the actual number and unit (if it exists) { - outputString += QString::number(floatVal) + unitName; + outputString += (isInteger ? QString::number(intVal) : QString::number(floatVal)) + unitName; } return outputString; } diff --git a/dbc/dbc_classes.h b/dbc/dbc_classes.h index 5d32b67..6b56e02 100644 --- a/dbc/dbc_classes.h +++ b/dbc/dbc_classes.h @@ -104,7 +104,7 @@ public: //TODO: this is sloppy. It shouldn't all be public! bool processAsText(const CANFrame &frame, QString &outString, bool outputName = true); bool processAsInt(const CANFrame &frame, int32_t &outValue); bool processAsDouble(const CANFrame &frame, double &outValue); - QString makePrettyOutput(double floatVal, int64_t intVal, bool outputName = true); + QString makePrettyOutput(double floatVal, int64_t intVal, bool outputName = true, bool isInteger = false); DBC_ATTRIBUTE_VALUE *findAttrValByName(QString name); DBC_ATTRIBUTE_VALUE *findAttrValByIdx(int idx); };