diff --git a/DShotRMT.cpp b/DShotRMT.cpp index 5abab84..aea8923 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -13,7 +13,7 @@ DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional) void DShotRMT::begin() { - rmt_tx_channel_config_t tx_config = { + rmt_tx_channel_config_t rmt_tx_channel_config = { .gpio_num = _gpio, .clk_src = RMT_CLK_SRC_DEFAULT, .resolution_hz = DEFAULT_RES_HZ, @@ -23,8 +23,8 @@ void DShotRMT::begin() .invert_out = _isBidirectional, .with_dma = false}}; - rmt_new_tx_channel(&tx_config, &_channel); - rmt_enable(_channel); + rmt_new_tx_channel(&rmt_tx_channel_config, &_rmt_channel); + rmt_enable(_rmt_channel); // Create encoder only once if (!_encoder) @@ -33,19 +33,22 @@ void DShotRMT::begin() rmt_new_copy_encoder(&enc_cfg, &_encoder); } - _tx_config.loop_count = -1; // Infinite loop - _tx_config.flags.eot_level = 0; + _transmit_config.loop_count = -1; // Infinite loop + _transmit_config.flags.eot_level = 0; } void DShotRMT::setThrottle(uint16_t throttle, bool telemetry) { + // Send only new Throttle values + static uint16_t _lastThrottle = 0; + // Clamp to 11 bits throttle &= 0x07FF; - if (throttle == _lastThrottle && telemetry == _lastTelemetry) + + if (throttle == _lastThrottle) return; _lastThrottle = throttle; - _lastTelemetry = telemetry; // Build 16-bit DShot packet uint16_t packet = (throttle << 1) | (telemetry ? 1 : 0); @@ -59,10 +62,10 @@ void DShotRMT::setThrottle(uint16_t throttle, bool telemetry) buildFrameSymbols(packet, symbols, count); // Transmit - rmt_disable(_channel); // Ensure safe restart - rmt_enable(_channel); + rmt_disable(_rmt_channel); // Ensure safe restart + rmt_enable(_rmt_channel); - rmt_transmit(_channel, _encoder, symbols, count * sizeof(rmt_symbol_word_t), &_tx_config); + rmt_transmit(_rmt_channel, _encoder, symbols, count * sizeof(rmt_symbol_word_t), &_transmit_config); } void DShotRMT::buildFrameSymbols(uint16_t dshot_packet, rmt_symbol_word_t *symbols, size_t &count) diff --git a/DShotRMT.h b/DShotRMT.h index 494d054..a13cd7a 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -42,12 +42,9 @@ private: dshot_mode_t _mode; bool _isBidirectional; - rmt_channel_handle_t _channel = nullptr; + rmt_channel_handle_t _rmt_channel = nullptr; rmt_encoder_handle_t _encoder = nullptr; - rmt_transmit_config_t _tx_config = {}; - - uint16_t _lastThrottle = 0; - bool _lastTelemetry = false; + rmt_transmit_config_t _transmit_config = {}; void buildFrameSymbols(uint16_t frame, rmt_symbol_word_t *symbols, size_t &count); }; diff --git a/examples/dshot300/dshot300.ino b/examples/dshot300/dshot300.ino index 14cab61..07e916f 100644 --- a/examples/dshot300/dshot300.ino +++ b/examples/dshot300/dshot300.ino @@ -10,12 +10,12 @@ #include // USB serial port settings -#define USB_Serial Serial -const uint32_t USB_SERIAL_BAUD = 115200; +#define USB_Serial Serial0 +constexpr auto USB_SERIAL_BAUD = 115200; // Motor configuration -const gpio_num_t MOTOR01_PIN = GPIO_NUM_17; -const dshot_mode_t DSHOT_MODE = DSHOT300; +constexpr auto MOTOR01_PIN = GPIO_NUM_17; +constexpr auto DSHOT_MODE = DSHOT300; // Create DShotRMT instance DShotRMT motor01(MOTOR01_PIN, DSHOT_MODE); @@ -48,6 +48,7 @@ void loop() // Reads throttle value from serial input int readSerialThrottle() { + // static int last_throttle = DSHOT_THROTTLE_MIN; if (USB_Serial.available() > 0) @@ -64,5 +65,6 @@ int readSerialThrottle() USB_Serial.println("Enter a throttle value (48–2047):"); } + return last_throttle; } \ No newline at end of file