From b3784832dd004921b016e3de8fb4ffc4d255caf7 Mon Sep 17 00:00:00 2001 From: Jim Whitelaw Date: Mon, 1 Jan 2024 00:49:01 -0700 Subject: [PATCH] Fix for Value returned by supportedPIDS_xx_xx() methods is off by one bit. #211 Changes processPID() and conditionResponse() to return double instead of float to preserve all bits of _response_. --- src/ELMduino.cpp | 38 +++++++++++++++++++++++++++++++------- src/ELMduino.h | 4 ++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/ELMduino.cpp b/src/ELMduino.cpp index 0ae0f69..3e8e395 100644 --- a/src/ELMduino.cpp +++ b/src/ELMduino.cpp @@ -410,7 +410,7 @@ int8_t ELM327::nextIndex(char const *str, ------- * float - Converted numerical value */ -float ELM327::conditionResponse(const uint8_t &numExpectedBytes, const float &scaleFactor, const float &bias) +double ELM327::conditionResponse(const uint8_t &numExpectedBytes, const float &scaleFactor, const float &bias) { uint8_t numExpectedPayChars = numExpectedBytes * 2; uint8_t payCharDiff = numPayChars - numExpectedPayChars; @@ -438,7 +438,17 @@ float ELM327::conditionResponse(const uint8_t &numExpectedBytes, const float &sc return 0; } else if (numExpectedPayChars == numPayChars) - return (response * scaleFactor) + bias; + { + if (scaleFactor == 1 && bias == 0) // No scale/bias needed + { + return response; + } + else + { + return (response * scaleFactor) + bias; + } + } + // If there were more payload bytes returned than we expected, test the first and last bytes in the // returned payload and see which gives us a higher value. Sometimes ELM327's return leading zeros @@ -459,15 +469,29 @@ float ELM327::conditionResponse(const uint8_t &numExpectedBytes, const float &sc { if (debugMode) Serial.println("Lagging zeros found"); - - return ((float)(response >> (4 * payCharDiff)) * scaleFactor) + bias; + if (scaleFactor == 1 && bias == 0) // No scale/bias needed + { + return (response >> (4 * payCharDiff)); + } + else + { + return ((float)(response >> (4 * payCharDiff)) * scaleFactor) + bias; + } + } else { if (debugMode) Serial.println("Lagging zeros not found - assuming leading zeros"); - return (response * scaleFactor) + bias; + if (scaleFactor == 1 && bias == 0) // No scale/bias needed + { + return response; + } + else + { + return (response * scaleFactor) + bias; + } } } @@ -573,7 +597,7 @@ bool ELM327::queryPID(char queryStr[]) ------- * float - The PID value if successfully received, else 0.0 */ -float ELM327::processPID(const uint8_t &service, const uint16_t &pid, const uint8_t &num_responses, const uint8_t &numExpectedBytes, const float &scaleFactor, const float &bias) +double ELM327::processPID(const uint8_t &service, const uint16_t &pid, const uint8_t &num_responses, const uint8_t &numExpectedBytes, const float &scaleFactor, const float &bias) { if (nb_query_state == SEND_COMMAND) { @@ -588,7 +612,7 @@ float ELM327::processPID(const uint8_t &service, const uint16_t &pid, const uint nb_query_state = SEND_COMMAND; // Reset the query state machine for next command findResponse(); - + return conditionResponse(numExpectedBytes, scaleFactor, bias); } else if (nb_rx_state != ELM_GETTING_MSG) diff --git a/src/ELMduino.h b/src/ELMduino.h index dec93fc..b7f63ec 100644 --- a/src/ELMduino.h +++ b/src/ELMduino.h @@ -318,12 +318,12 @@ class ELM327 uint64_t findResponse(); bool queryPID(const uint8_t& service, const uint16_t& pid, const uint8_t& num_responses = 1); bool queryPID(char queryStr[]); - float processPID(const uint8_t& service, const uint16_t& pid, const uint8_t& num_responses, const uint8_t& numExpectedBytes, const float& scaleFactor = 1, const float& bias = 0); + double processPID(const uint8_t& service, const uint16_t& pid, const uint8_t& num_responses, const uint8_t& numExpectedBytes, const float& scaleFactor = 1, const float& bias = 0); void sendCommand(const char *cmd); int8_t sendCommand_Blocking(const char *cmd); int8_t get_response(); bool timeout(); - float conditionResponse(const uint8_t& numExpectedBytes, const float& scaleFactor = 1, const float& bias = 0); + double conditionResponse(const uint8_t& numExpectedBytes, const float& scaleFactor = 1, const float& bias = 0); float batteryVoltage(void); int8_t get_vin_blocking(char vin[]);