MCPcopy Create free account
hub / github.com/PowerBroker2/ELMduino / conditionResponse

Method conditionResponse

src/ELMduino.cpp:490–562  ·  view source on GitHub ↗

double ELM327::conditionResponse(const uint8_t &numExpectedBytes, const float &scaleFactor, const float &bias) Description: ------------ * Converts the ELM327's response into its correct, numerical value. Returns 0 if numExpectedBytes > numPayChars Inputs: ------- * uint64_t response - ELM327's response * uint8_t numExpectedBytes - Number of valid bytes from the respons

Source from the content-addressed store, hash-verified

488 * double - Converted numerical value
489*/
490double ELM327::conditionResponse(const uint8_t& numExpectedBytes,
491 const double& scaleFactor,
492 const double& bias)
493{
494 uint8_t numExpectedPayChars = numExpectedBytes * 2;
495 uint8_t payCharDiff = numPayChars - numExpectedPayChars;
496
497 if (numExpectedBytes > 8)
498 {
499 if (debugMode)
500 Serial.println(F("WARNING: Number of expected response bytes is greater than 8 - returning 0"));
501
502 return 0;
503 }
504
505 if (numPayChars < numExpectedPayChars)
506 {
507 if (debugMode)
508 Serial.println(F("WARNING: Number of payload chars is less than the number of expected response chars returned by ELM327 - returning 0"));
509
510 return 0;
511 }
512 else if (numPayChars & 0x1)
513 {
514 if (debugMode)
515 Serial.println(F("WARNING: Number of payload chars returned by ELM327 is an odd value - returning 0"));
516
517 return 0;
518 }
519 else if (numExpectedPayChars == numPayChars)
520 {
521 if (scaleFactor == 1 && bias == 0) // No scale/bias needed
522 return response;
523 else
524 return (response * scaleFactor) + bias;
525 }
526
527 // If there were more payload bytes returned than we expected, test the first and last bytes in the
528 // returned payload and see which gives us a higher value. Sometimes ELM327's return leading zeros
529 // and others return trailing zeros. The following approach gives us the best chance at determining
530 // where the real data is. Note that if the payload returns BOTH leading and trailing zeros, this
531 // will not give accurate results!
532
533 if (debugMode)
534 Serial.println(F("Looking for lagging zeros"));
535
536 uint16_t numExpectedBits = numExpectedBytes * 8;
537 uint64_t laggingZerosMask = 0;
538
539 for (uint16_t i = 0; i < numExpectedBits; i++)
540 laggingZerosMask |= (1 << i);
541
542 if (!(laggingZerosMask & response)) // Detect all lagging zeros in `response`
543 {
544 if (debugMode)
545 Serial.println(F("Lagging zeros found"));
546
547 if (scaleFactor == 1 && bias == 0) // No scale/bias needed

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected