refactor: Improve readability of printDShotInfo

Replaced the nested ternary operator in the `printDShotInfo` function with a `switch` statement.

This change improves code readability and maintainability, making it easier to add or modify DShot modes in the future.
This commit is contained in:
Wastl Kraus 2025-09-30 10:01:47 +02:00
parent 3537f4f2d8
commit 1a1bbcd7f3
2 changed files with 97 additions and 87 deletions

View File

@ -319,8 +319,8 @@ dshot_result_t DShotRMT::_initRXChannel()
_rx_channel_config.mem_block_symbols = RMT_BUFFER_SYMBOLS; _rx_channel_config.mem_block_symbols = RMT_BUFFER_SYMBOLS;
// Filter for pulses that are within a reasonable range for DShot telemetry // Filter for pulses that are within a reasonable range for DShot telemetry
_rmt_rx_config.signal_range_min_ns = DSHOT_PULSE_MIN; _rmt_rx_config.signal_range_min_ns = DSHOT_PULSE_MIN_NS;
_rmt_rx_config.signal_range_max_ns = DSHOT_PULSE_MAX; _rmt_rx_config.signal_range_max_ns = DSHOT_PULSE_MAX_NS;
if (rmt_new_rx_channel(&_rx_channel_config, &_rmt_rx_channel) != DSHOT_OK) if (rmt_new_rx_channel(&_rx_channel_config, &_rmt_rx_channel) != DSHOT_OK)
{ {
@ -542,10 +542,25 @@ bool IRAM_ATTR DShotRMT::_on_rx_done(rmt_channel_handle_t rmt_rx_channel, const
void DShotRMT::printDShotInfo(const DShotRMT &dshot_rmt, Stream &output) void DShotRMT::printDShotInfo(const DShotRMT &dshot_rmt, Stream &output)
{ {
output.println("\n === DShot Signal Info === "); output.println("\n === DShot Signal Info === ");
output.printf("Current Mode: DSHOT%d\n", dshot_rmt.getMode() == dshot_mode_t::DSHOT150 ? 150 : dshot_rmt.getMode() == dshot_mode_t::DSHOT300 ? 300
: dshot_rmt.getMode() == dshot_mode_t::DSHOT600 ? 600 uint16_t dshot_mode_val = 0;
: dshot_rmt.getMode() == dshot_mode_t::DSHOT1200 ? 1200 switch (dshot_rmt.getMode())
: 0); {
case dshot_mode_t::DSHOT150:
dshot_mode_val = 150;
break;
case dshot_mode_t::DSHOT300:
dshot_mode_val = 300;
break;
case dshot_mode_t::DSHOT600:
dshot_mode_val = 600;
break;
case dshot_mode_t::DSHOT1200:
dshot_mode_val = 1200;
break;
}
output.printf("Current Mode: DSHOT%d\n", dshot_mode_val);
output.printf("Bidirectional: %s\n", dshot_rmt.isBidirectional() ? "YES" : "NO"); output.printf("Bidirectional: %s\n", dshot_rmt.isBidirectional() ? "YES" : "NO");
output.printf("Current Packet: "); output.printf("Current Packet: ");

View File

@ -1,12 +1,12 @@
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
#include <driver/gpio.h> // Added for gpio_num_t #include <driver/gpio.h>
#include <driver/rmt_tx.h> #include <driver/rmt_tx.h>
#include <driver/rmt_rx.h> #include <driver/rmt_rx.h>
#include <atomic> // Added for std::atomic #include <atomic>
// Defines the available DShot communication speeds. // Defines the available DShot communication speeds with type safety.
enum class dshot_mode_t enum class dshot_mode_t
{ {
DSHOT_OFF, DSHOT_OFF,
@ -80,7 +80,7 @@ typedef struct dshot_result
uint16_t motor_rpm; // Motor RPM if telemetry is successful and magnet count is known. uint16_t motor_rpm; // Motor RPM if telemetry is successful and magnet count is known.
} dshot_result_t; } dshot_result_t;
// Enum class for standard DShot commands that can be sent to an ESC. // Standard DShot commands (regular enum for easier handling as per project conventions).
enum dshotCommands_e enum dshotCommands_e
{ {
DSHOT_CMD_MOTOR_STOP = 0, DSHOT_CMD_MOTOR_STOP = 0,
@ -89,110 +89,105 @@ enum dshotCommands_e
DSHOT_CMD_BEACON3, DSHOT_CMD_BEACON3,
DSHOT_CMD_BEACON4, DSHOT_CMD_BEACON4,
DSHOT_CMD_BEACON5, DSHOT_CMD_BEACON5,
DSHOT_CMD_ESC_INFO, // V2 includes settings DSHOT_CMD_ESC_INFO,
DSHOT_CMD_SPIN_DIRECTION_1, DSHOT_CMD_SPIN_DIRECTION_1,
DSHOT_CMD_SPIN_DIRECTION_2, DSHOT_CMD_SPIN_DIRECTION_2,
DSHOT_CMD_3D_MODE_OFF, DSHOT_CMD_3D_MODE_OFF,
DSHOT_CMD_3D_MODE_ON, DSHOT_CMD_3D_MODE_ON,
DSHOT_CMD_SETTINGS_REQUEST, // Currently not implemented DSHOT_CMD_SETTINGS_REQUEST,
DSHOT_CMD_SAVE_SETTINGS, DSHOT_CMD_SAVE_SETTINGS,
DSHOT_CMD_EXTENDED_TELEMETRY_ENABLE, DSHOT_CMD_EXTENDED_TELEMETRY_ENABLE,
DSHOT_CMD_EXTENDED_TELEMETRY_DISABLE, DSHOT_CMD_EXTENDED_TELEMETRY_DISABLE,
DSHOT_CMD_SPIN_DIRECTION_NORMAL = 20, DSHOT_CMD_SPIN_DIRECTION_NORMAL = 20,
DSHOT_CMD_SPIN_DIRECTION_REVERSED = 21, DSHOT_CMD_SPIN_DIRECTION_REVERSED = 21,
DSHOT_CMD_LED0_ON, // BLHeli32 only DSHOT_CMD_LED0_ON,
DSHOT_CMD_LED1_ON, // BLHeli32 only DSHOT_CMD_LED1_ON,
DSHOT_CMD_LED2_ON, // BLHeli32 only DSHOT_CMD_LED2_ON,
DSHOT_CMD_LED3_ON, // BLHeli32 only DSHOT_CMD_LED3_ON,
DSHOT_CMD_LED0_OFF, // BLHeli32 only DSHOT_CMD_LED0_OFF,
DSHOT_CMD_LED1_OFF, // BLHeli32 only DSHOT_CMD_LED1_OFF,
DSHOT_CMD_LED2_OFF, // BLHeli32 only DSHOT_CMD_LED2_OFF,
DSHOT_CMD_LED3_OFF, // BLHeli32 only DSHOT_CMD_LED3_OFF,
DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF = 30, // KISS audio Stream mode on/Off DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF = 30,
DSHOT_CMD_SILENT_MODE_ON_OFF = 31, // KISS silent Mode on/Off DSHOT_CMD_SILENT_MODE_ON_OFF = 31,
DSHOT_CMD_MAX = 47 DSHOT_CMD_MAX = 47
}; };
// Defines how DShot commands are sent. // --- Type-Safe Constants ---
enum class dshotCommandType_e
{
DSHOT_CMD_TYPE_INLINE = 0, // Commands sent inline with motor signal (motors must be enabled).
DSHOT_CMD_TYPE_BLOCKING // Commands sent in blocking method (motors must be disabled).
};
// DShot Protocol Constants // DShot Protocol Constants
const uint16_t DSHOT_THROTTLE_FAILSAFE = 0; static constexpr uint16_t DSHOT_THROTTLE_FAILSAFE = 0;
const uint16_t DSHOT_THROTTLE_MIN = 48; static constexpr uint16_t DSHOT_THROTTLE_MIN = 48;
const uint16_t DSHOT_THROTTLE_MAX = 2047; static constexpr uint16_t DSHOT_THROTTLE_MAX = 2047;
const uint16_t DSHOT_BITS_PER_FRAME = 16; static constexpr uint16_t DSHOT_BITS_PER_FRAME = 16;
const uint16_t DEFAULT_MOTOR_MAGNET_COUNT = 14; static constexpr uint16_t DEFAULT_MOTOR_MAGNET_COUNT = 14;
// Custom status codes // Custom status codes
const int DSHOT_OK = 0; static constexpr int DSHOT_OK = 0;
const int DSHOT_ERROR = 1; static constexpr int DSHOT_ERROR = 1;
// Configuration Constants (from DShotRMT.h private section) // Configuration Constants
const uint16_t DSHOT_NULL_PACKET = 0b0000000000000000; static constexpr uint16_t DSHOT_NULL_PACKET = 0b0000000000000000;
const uint16_t DSHOT_FULL_PACKET = 0b1111111111111111; static constexpr uint16_t DSHOT_FULL_PACKET = 0b1111111111111111;
const uint16_t DSHOT_CRC_MASK = 0b0000000000001111; static constexpr uint16_t DSHOT_CRC_MASK = 0b0000000000001111;
const rmt_clock_source_t DSHOT_CLOCK_SRC_DEFAULT = RMT_CLK_SRC_DEFAULT; static constexpr rmt_clock_source_t DSHOT_CLOCK_SRC_DEFAULT = RMT_CLK_SRC_DEFAULT;
const uint32_t DSHOT_RMT_RESOLUTION = 8 * 1000 * 1000; // 8 MHz resolution static constexpr uint32_t DSHOT_RMT_RESOLUTION = 8000000; // 8 MHz resolution
const uint16_t RMT_TICKS_PER_US = DSHOT_RMT_RESOLUTION / (1 * 1000 * 1000); // RMT Ticks per microsecond static constexpr uint16_t RMT_TICKS_PER_US = DSHOT_RMT_RESOLUTION / 1000000; // RMT Ticks per microsecond
const uint16_t DSHOT_RX_TIMEOUT_MS = 2; static constexpr uint16_t DSHOT_RX_TIMEOUT_MS = 2;
const uint16_t DSHOT_PADDING_US = 20; // Add to pause between frames for compatibility static constexpr uint16_t DSHOT_PADDING_US = 20; // Pause between frames
const uint16_t RMT_BUFFER_SYMBOLS = 64; static constexpr uint16_t RMT_BUFFER_SYMBOLS = 64;
const uint16_t RMT_QUEUE_DEPTH = 1; static constexpr uint16_t RMT_QUEUE_DEPTH = 1;
const uint16_t GCR_BITS_PER_FRAME = 21; // Number of GCR bits in a DShot answer frame static constexpr uint16_t GCR_BITS_PER_FRAME = 21; // GCR bits in a DShot answer frame
const uint16_t POLE_PAIRS_MIN = 1; static constexpr uint16_t POLE_PAIRS_MIN = 1;
const uint16_t MAGNETS_PER_POLE_PAIR = 2; static constexpr uint16_t MAGNETS_PER_POLE_PAIR = 2;
const uint16_t NO_DSHOT_TELEMETRY = 0; static constexpr uint16_t NO_DSHOT_TELEMETRY = 0;
const uint16_t DSHOT_PULSE_MIN = 800; // 0.8us minimum pulse static constexpr uint16_t DSHOT_PULSE_MIN_NS = 800; // 0.8us minimum pulse
const uint16_t DSHOT_PULSE_MAX = 8000; // 8.0us maximum pulse static constexpr uint16_t DSHOT_PULSE_MAX_NS = 8000; // 8.0us maximum pulse
const uint16_t DSHOT_TELEMETRY_INVALID = DSHOT_THROTTLE_MAX; static constexpr uint16_t DSHOT_TELEMETRY_INVALID = DSHOT_THROTTLE_MAX;
const uint16_t DSHOT_TELEMETRY_BIT_POSITION = 11; // Bit position of the telemetry request flag in the DShot frame static constexpr uint16_t DSHOT_TELEMETRY_BIT_POSITION = 11;
const uint16_t DSHOT_CRC_BIT_SHIFT = 4; // Number of bits to shift to extract data from data_and_crc static constexpr uint16_t DSHOT_CRC_BIT_SHIFT = 4;
// Command Constants (from DShotRMT.h private section) // Command Constants
const uint16_t DEFAULT_CMD_DELAY_US = 10; static constexpr uint16_t DEFAULT_CMD_DELAY_US = 10;
const uint16_t DEFAULT_CMD_REPEAT_COUNT = 1; static constexpr uint16_t DEFAULT_CMD_REPEAT_COUNT = 1;
const uint16_t SETTINGS_COMMAND_REPEATS = 10; // Settings commands need 10 repeats static constexpr uint16_t SETTINGS_COMMAND_REPEATS = 10;
const uint16_t SETTINGS_COMMAND_DELAY_US = 5; static constexpr uint16_t SETTINGS_COMMAND_DELAY_US = 5;
// Timing parameters for each DShot mode // Timing parameters for each DShot mode
// Format: {bit_length_us, t1h_length_us}
const dshot_timing_us_t DSHOT_TIMING_US[] = { const dshot_timing_us_t DSHOT_TIMING_US[] = {
{0.00, 0.00}, // DSHOT_OFF {0.00, 0.00}, // DSHOT_OFF
{6.67, 5.00}, // DSHOT150 {6.67, 5.00}, // DSHOT150
{3.33, 2.50}, // DSHOT300 {3.33, 2.50}, // DSHOT300
{1.67, 1.25}, // DSHOT600 {1.67, 1.25}, // DSHOT600
{0.83, 0.67}}; // DSHOT1200 {0.83, 0.67} // DSHOT1200
};
// Error Messages // Error Messages
static constexpr char *NONE = ""; static constexpr char NONE[] = "";
static constexpr char *UNKNOWN_ERROR = "Unknown Error!"; static constexpr char UNKNOWN_ERROR[] = "Unknown Error!";
static constexpr char *INIT_SUCCESS = "SignalGeneratorRMT initialized successfully"; static constexpr char INIT_SUCCESS[] = "SignalGeneratorRMT initialized successfully";
static constexpr char *INIT_FAILED = "SignalGeneratorRMT init failed!"; static constexpr char INIT_FAILED[] = "SignalGeneratorRMT init failed!";
static constexpr char *TX_INIT_SUCCESS = "TX RMT channel initialized successfully"; static constexpr char TX_INIT_SUCCESS[] = "TX RMT channel initialized successfully";
static constexpr char *TX_INIT_FAILED = "TX RMT channel init failed!"; static constexpr char TX_INIT_FAILED[] = "TX RMT channel init failed!";
static constexpr char *RX_INIT_SUCCESS = "RX RMT channel initialized successfully"; static constexpr char RX_INIT_SUCCESS[] = "RX RMT channel initialized successfully";
static constexpr char *RX_INIT_FAILED = "RX RMT channel init failed!"; static constexpr char RX_INIT_FAILED[] = "RX RMT channel init failed!";
static constexpr char *ENCODER_INIT_SUCCESS = "RMT encoder initialized successfully"; static constexpr char ENCODER_INIT_SUCCESS[] = "RMT encoder initialized successfully";
static constexpr char *ENCODER_INIT_FAILED = "RMT encoder init failed!"; static constexpr char ENCODER_INIT_FAILED[] = "RMT encoder init failed!";
static constexpr char *ENCODING_SUCCESS = "Packet encoded successfully"; static constexpr char ENCODING_SUCCESS[] = "Packet encoded successfully";
static constexpr char *TRANSMISSION_SUCCESS = "Transmission successfully"; static constexpr char TRANSMISSION_SUCCESS[] = "Transmission successfully";
static constexpr char *TRANSMISSION_FAILED = "Transmission failed!"; static constexpr char TRANSMISSION_FAILED[] = "Transmission failed!";
static constexpr char *RECEIVER_FAILED = "RMT receiver failed!"; static constexpr char RECEIVER_FAILED[] = "RMT receiver failed!";
static constexpr char *THROTTLE_NOT_IN_RANGE = "Throttle not in range! (48 - 2047)"; static constexpr char THROTTLE_NOT_IN_RANGE[] = "Throttle not in range! (48 - 2047)";
static constexpr char *PERCENT_NOT_IN_RANGE = "Percent not in range! (0.0 - 100.0)"; static constexpr char PERCENT_NOT_IN_RANGE[] = "Percent not in range! (0.0 - 100.0)";
static constexpr char *COMMAND_NOT_VALID = "Command not valid! (0 - 47)"; static constexpr char COMMAND_NOT_VALID[] = "Command not valid! (0 - 47)";
static constexpr char *BIDIR_NOT_ENABLED = "Bidirectional DShot not enabled!"; static constexpr char BIDIR_NOT_ENABLED[] = "Bidirectional DShot not enabled!";
static constexpr char *TELEMETRY_SUCCESS = "Valid Telemetric Frame received!"; static constexpr char TELEMETRY_SUCCESS[] = "Valid Telemetric Frame received!";
static constexpr char *TELEMETRY_FAILED = "No valid Telemetric Frame received!"; static constexpr char TELEMETRY_FAILED[] = "No valid Telemetric Frame received!";
static constexpr char *INVALID_MAGNET_COUNT = "Invalid motor magnet count!"; static constexpr char INVALID_MAGNET_COUNT[] = "Invalid motor magnet count!";
static constexpr char *TIMING_CORRECTION = "Timing correction!"; static constexpr char TIMING_CORRECTION[] = "Timing correction!";
static constexpr char *CALLBACK_REGISTERING_FAILED = "RMT RX Callback registering failed!"; static constexpr char CALLBACK_REGISTERING_FAILED[] = "RMT RX Callback registering failed!";
static constexpr char *INVALID_COMMAND = "Invalid command!"; static constexpr char INVALID_COMMAND[] = "Invalid command!";
static constexpr char *COMMAND_SUCCESS = "DShot command sent successfully"; static constexpr char COMMAND_SUCCESS[] = "DShot command sent successfully";
// Helper to get result code string // Helper to get result code string
inline const char* _get_result_code_str(dshot_msg_code_t code) inline const char* _get_result_code_str(dshot_msg_code_t code)