Skip to content

Commit

Permalink
Merge pull request #213 from jimwhitelaw/master
Browse files Browse the repository at this point in the history
Fix for #211 Value returned by supportedPIDS_xx_xx() methods is off by one bit.
  • Loading branch information
PowerBroker2 authored Jan 2, 2024
2 parents feaa1c0 + b378483 commit c2df64d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
38 changes: 31 additions & 7 deletions src/ELMduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/ELMduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);
Expand Down

0 comments on commit c2df64d

Please sign in to comment.