From 3a80bdef750de4f85d9c5832bb8876f06f8029ff Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Fri, 25 Jul 2025 22:00:26 +0200 Subject: [PATCH] ...simplify packet assambly --- DShotRMT.cpp | 10 +++++----- DShotRMT.h | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/DShotRMT.cpp b/DShotRMT.cpp index d2deeff..673f28c 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -184,13 +184,13 @@ uint32_t DShotRMT::getMotorRPM(uint8_t magnet_count) // Calculates CRC for DShot packet void DShotRMT::calculateCRC(dshot_packet_t *dshot_packet) { - uint16_t packet = (dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request); + _parsed_dshot_tx_packet = (dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request); // Reset CRC container dshot_packet->checksum = DSHOT_NULL_PACKET; // CRC calculation for DShot (4 bits) - dshot_packet->checksum = ((packet ^ (packet >> 4) ^ (packet >> 8)) & 0b0000000000001111); + dshot_packet->checksum = ((_parsed_dshot_tx_packet ^ (_parsed_dshot_tx_packet >> 4) ^ (_parsed_dshot_tx_packet >> 8)) & 0b0000000000001111); // CRC is inverted for bidirectional DShot if (dshot_packet->telemetric_request) @@ -198,10 +198,10 @@ void DShotRMT::calculateCRC(dshot_packet_t *dshot_packet) } // Assembles DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC) -uint16_t DShotRMT::parseDShotPacket(const dshot_packet_t *dshot_packet) const +uint16_t DShotRMT::parseDShotPacket(const dshot_packet_t *dshot_packet) { - uint16_t parsed_packet = ((dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request)) & 0b0000111111111111; - return ((parsed_packet << 4) | (dshot_packet->checksum)) & 0b1111111111111111; + _parsed_dshot_tx_packet = ((dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request)) & 0b0000111111111111; + return ((_parsed_dshot_tx_packet << 4) | (dshot_packet->checksum)) & 0b1111111111111111; } // Converts a 16-bit packet into a valid DShot frame for RMT diff --git a/DShotRMT.h b/DShotRMT.h index 049aab6..c763e02 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -71,13 +71,14 @@ public: gpio_num_t getGPIO() const { return _gpio; } dshot_mode_t getDShotMode() const { return _mode; } uint16_t getFrameLenght() const { return _frameLength; } + uint16_t getDShotPacket() const { return _parsed_dshot_tx_packet; } -protected: +private: // Calculates the checksum for a DShot packet void calculateCRC(dshot_packet_t *dshot_packet); // Parses the DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC) - uint16_t parseDShotPacket(const dshot_packet_t *dshot_packet) const; + uint16_t parseDShotPacket(const dshot_packet_t *dshot_packet); // Converts a 16-bit DShot packet into RMT symbols void encodeDShotTX(dshot_packet_t *dshot_packet, rmt_symbol_word_t *symbols, size_t &count); @@ -85,8 +86,7 @@ protected: // Decodes the ESC answer uint16_t decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t count); -private: - // --- Configuration Parameters --- +// --- Configuration Parameters --- gpio_num_t _gpio = GPIO_NUM_NC; dshot_mode_t _mode = DSHOT_OFF; bool _isBidirectional = false; @@ -94,6 +94,7 @@ private: // --- DShot Packets Container --- uint16_t _rx_packet = DSHOT_NULL_PACKET; + uint16_t _parsed_dshot_tx_packet = DSHOT_NULL_PACKET; dshot_packet_t _dshot_packet = {}; // --- RMT Channel Handles ---