Process accelerometer data received over CAN
| 565 | |
| 566 | // Process accelerometer data received over CAN |
| 567 | void Accelerometers::ProcessReceivedData(CanAddress src, const CanMessageAccelerometerData& msg, size_t msgLen) noexcept |
| 568 | { |
| 569 | # ifdef DUET3_ATE |
| 570 | if (Duet3Ate::ProcessAccelerometerData(src, msg, msgLen)) |
| 571 | { |
| 572 | return; // ATE has processed the data |
| 573 | } |
| 574 | # endif |
| 575 | |
| 576 | FileStore * const f = accelerometerFile; |
| 577 | if (f != nullptr) |
| 578 | { |
| 579 | if (msgLen < msg.GetActualDataLength()) |
| 580 | { |
| 581 | f->Write("Received bad data\n"); |
| 582 | f->Truncate(); // truncate the file in case we didn't write all the preallocated space |
| 583 | f->Close(); |
| 584 | accelerometerFile = nullptr; |
| 585 | reprap.GetExpansion().AddAccelerometerRun(src, 0); |
| 586 | } |
| 587 | else if (msg.axes != expectedRemoteAxes || msg.firstSampleNumber != expectedRemoteSampleNumber || src != expectedRemoteBoardAddress) |
| 588 | { |
| 589 | f->Write("Received mismatched data\n"); |
| 590 | f->Truncate(); // truncate the file in case we didn't write all the preallocated space |
| 591 | f->Close(); |
| 592 | accelerometerFile = nullptr; |
| 593 | reprap.GetExpansion().AddAccelerometerRun(src, 0); |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | unsigned int numSamples = msg.numSamples; |
| 598 | const unsigned int numAxes = (expectedRemoteAxes & 1u) + ((expectedRemoteAxes >> 1) & 1u) + ((expectedRemoteAxes >> 2) & 1u); |
| 599 | size_t dataIndex = 0; |
| 600 | uint16_t currentBits = 0; |
| 601 | unsigned int bitsLeft = 0; |
| 602 | const unsigned int receivedResolution = msg.bitsPerSampleMinusOne + 1; |
| 603 | const uint16_t mask = (1u << receivedResolution) - 1; |
| 604 | const int decimalPlaces = GetDecimalPlaces(receivedResolution); |
| 605 | if (msg.overflowed) |
| 606 | { |
| 607 | ++numRemoteOverflows; |
| 608 | } |
| 609 | |
| 610 | while (numSamples != 0) |
| 611 | { |
| 612 | String<StringLength50> temp; |
| 613 | temp.printf("%u", expectedRemoteSampleNumber); |
| 614 | ++expectedRemoteSampleNumber; |
| 615 | |
| 616 | for (unsigned int axis = 0; axis < numAxes; ++axis) |
| 617 | { |
| 618 | // Extract one value from the message. A value spans at most two words in the buffer. |
| 619 | uint16_t val = currentBits; |
| 620 | if (bitsLeft >= receivedResolution) |
| 621 | { |
| 622 | bitsLeft -= receivedResolution; |
| 623 | currentBits >>= receivedResolution; |
| 624 | } |
nothing calls this directly
no test coverage detected