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) Description: ------------ * Queries ELM327 for a specific type of vehicle telemetry data Inputs: ------- * uint8_t service - The diagnostic service ID. 01 is "Show current data" * uint16_t pi
| 684 | * double - The PID value if successfully received, else 0.0 |
| 685 | */ |
| 686 | double ELM327::processPID(const uint8_t& service, |
| 687 | const uint16_t& pid, |
| 688 | const uint8_t& num_responses, |
| 689 | const uint8_t& numExpectedBytes, |
| 690 | const double& scaleFactor, |
| 691 | const float& bias) |
| 692 | { |
| 693 | if (nb_query_state == SEND_COMMAND) |
| 694 | { |
| 695 | queryPID(service, pid, num_responses); |
| 696 | nb_query_state = WAITING_RESP; |
| 697 | } |
| 698 | else if (nb_query_state == WAITING_RESP) |
| 699 | { |
| 700 | get_response(); |
| 701 | if (nb_rx_state == ELM_SUCCESS) |
| 702 | { |
| 703 | nb_query_state = SEND_COMMAND; // Reset the query state machine for next command |
| 704 | findResponse(); |
| 705 | |
| 706 | /* This data manipulation seems duplicative of the responseByte_0, responseByte_1, etc vars and it is. |
| 707 | The duplcation is deliberate to provide a clear way for the calculator functions to access the relevant |
| 708 | data bytes from the response in the format they are commonly expressed in and without breaking backward |
| 709 | compatability with existing code that may be using the responseByte_n vars. |
| 710 | |
| 711 | In addition, we need to place the response values into static vars that can be accessed by the (static) |
| 712 | calculator functions. A future (breaking!) change could be made to eliminate this duplication. |
| 713 | */ |
| 714 | uint8_t responseBits = numExpectedBytes * 8; |
| 715 | uint8_t extractedBytes[8] = {0}; // Store extracted bytes |
| 716 | |
| 717 | // Extract bytes only if shift is non-negative |
| 718 | for (int i = 0; i < numExpectedBytes; i++) |
| 719 | { |
| 720 | int shiftAmount = responseBits - (8 * (i + 1)); // Compute shift amount |
| 721 | if (shiftAmount >= 0) { // Ensure valid shift |
| 722 | extractedBytes[i] = (response >> shiftAmount) & 0xFF; // Extract byte |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // Assign extracted values to response_A, response_B, ..., response_H safely |
| 727 | response_A = extractedBytes[0]; |
| 728 | response_B = extractedBytes[1]; |
| 729 | response_C = extractedBytes[2]; |
| 730 | response_D = extractedBytes[3]; |
| 731 | response_E = extractedBytes[4]; |
| 732 | response_F = extractedBytes[5]; |
| 733 | response_G = extractedBytes[6]; |
| 734 | response_H = extractedBytes[7]; |
| 735 | |
| 736 | double (*calculator)() = selectCalculator(pid); |
| 737 | |
| 738 | if (nullptr == calculator) { |
| 739 | //Use the default scaleFactor + Bias calculation |
| 740 | return conditionResponse(numExpectedBytes, scaleFactor, bias); |
| 741 | } |
| 742 | else { |
| 743 | return conditionResponse(calculator); |
nothing calls this directly
no outgoing calls
no test coverage detected