DShotRMT/DShotCommandManager.h

125 lines
3.8 KiB
C
Raw Normal View History

/*
* DShotCommandManager.h
* Advanced DShot command management for DShotRMT library
* Author: Wastl Kraus
* Date: 2025-09-04
* License: MIT
*/
#pragma once
#include <Arduino.h>
#include <DShotRMT.h>
#include <dshot_commands.h>
// Command sequence item
typedef struct
{
dshot_commands_t command;
uint16_t repeat_count;
uint32_t delay_ms;
} dshot_command_sequence_item_t;
// Advanced DShot command manager class
class DShotCommandManager
{
public:
// Constructor
explicit DShotCommandManager(DShotRMT &dshot_instance);
// Initialize command manager
2025-09-05 11:16:19 +01:00
dshot_result_t begin();
void handleMenuInput(const String &input, Stream &output = Serial);
// Send a single DShot command
2025-09-05 11:16:19 +01:00
dshot_result_t sendCommand(dshot_commands_t command, uint16_t repeat_count = 1);
// Send command with specified delay between repetitions
2025-09-05 11:16:19 +01:00
dshot_result_t sendCommandWithDelay(dshot_commands_t command, uint16_t repeat_count, uint32_t delay_ms);
// --- MOTOR CONTROL COMMANDS ---
// Stop motor (send MOTOR_STOP command)
2025-09-05 11:16:19 +01:00
dshot_result_t stopMotor();
// Enable/disable 3D mode
2025-09-05 11:16:19 +01:00
dshot_result_t set3DMode(bool enable);
// Set motor spin direction
2025-09-05 11:16:19 +01:00
dshot_result_t setSpinDirection(bool reversed);
// Save current settings to ESC
2025-09-05 11:16:19 +01:00
dshot_result_t saveSettings();
// --- TELEMETRY COMMANDS ---
// Enable/disable extended telemetry
2025-09-05 11:16:19 +01:00
dshot_result_t setExtendedTelemetry(bool enable);
// Request ESC information
2025-09-05 11:16:19 +01:00
dshot_result_t requestESCInfo();
// --- LED CONTROL COMMANDS (BLHeli32 only) ---
// Control ESC LEDs (BLHeli32 only)
2025-09-05 11:16:19 +01:00
dshot_result_t setLED(uint8_t led_number, bool state);
// --- BEACON COMMANDS ---
// Activate beacon (motor beeping)
2025-09-05 11:16:19 +01:00
dshot_result_t activateBeacon(uint8_t beacon_number);
// --- KISS ESC SPECIFIC COMMANDS ---
// Enable/disable audio stream mode (KISS ESCs)
2025-09-05 11:16:19 +01:00
dshot_result_t setAudioStreamMode(bool enable);
// Enable/disable silent mode (KISS ESCs)
2025-09-05 11:16:19 +01:00
dshot_result_t setSilentMode(bool enable);
// --- SEQUENCE COMMANDS ---
// Execute a sequence of DShot commands
2025-09-05 11:16:19 +01:00
dshot_result_t executeSequence(const dshot_command_sequence_item_t *sequence, size_t sequence_length);
// Execute ESC initialization sequence
2025-09-05 11:16:19 +01:00
dshot_result_t executeInitSequence();
// Execute ESC calibration sequence
2025-09-05 11:16:19 +01:00
dshot_result_t executeCalibrationSequence();
// --- UTILITY METHODS ---
// Get command name as string
static const char *getCommandName(dshot_commands_t command);
// Check if command is valid
static bool isValidCommand(dshot_commands_t command);
// --- GETTERS ---
// Get total number of commands sent
uint32_t getTotalCommandCount() const { return _total_commands_sent; }
// Get number of failed commands
uint32_t getFailedCommandCount() const { return _failed_commands; }
2025-09-05 11:16:19 +01:00
// Get reference to underlying DShotRMT instance
DShotRMT &getDShotRMT() { return _dshot; }
const DShotRMT &getDShotRMT() const { return _dshot; }
private:
// --- PRIVATE MEMBERS ---
DShotRMT &_dshot; // Reference to DShotRMT instance
uint32_t _total_commands_sent; // Total commands sent counter
uint32_t _failed_commands; // Failed commands counter
uint64_t _last_command_timestamp; // Timestamp of last command
// --- PRIVATE METHODS ---
// Execute single command with timing
2025-09-05 11:16:19 +01:00
dshot_result_t _executeCommand(dshot_commands_t command);
// Wait for specified delay
2025-09-05 11:16:19 +01:00
void _delay_ms(uint32_t delay_ms);
// --- CONSTANTS ---
static constexpr uint32_t DEFAULT_COMMAND_DELAY_MS = 10;
static constexpr uint16_t DEFAULT_REPEAT_COUNT = 1;
static constexpr uint16_t SETTINGS_COMMAND_REPEATS = 10; // Settings commands need 10 repeats
static constexpr uint32_t SETTINGS_COMMAND_DELAY_MS = 5;
};