...simplify packet assambly

This commit is contained in:
Wastl Kraus 2025-07-25 22:00:26 +02:00
parent ebbeb99d4f
commit 3a80bdef75
2 changed files with 10 additions and 9 deletions

View File

@ -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

View File

@ -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 ---