Skip to main content

motor_driver_hal/
error.rs

1/// Error types that can occur during motor driver operations.
2/// 
3/// This enum represents all possible error conditions that can arise when
4/// using motor driver implementations. Errors are categorized by their source
5/// and can help diagnose hardware or configuration issues.
6/// 
7/// # Example
8/// 
9/// ```rust
10/// use motor_driver_hal::MotorDriverError;
11/// 
12/// match motor.set_speed(1500) {
13///     Ok(()) => println!("Speed set successfully"),
14///     Err(MotorDriverError::InvalidSpeed) => println!("Speed value too high"),
15///     Err(MotorDriverError::NotInitialized) => println!("Driver not initialized"),
16///     Err(e) => println!("Other error: {}", e),
17/// }
18/// ```
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub enum MotorDriverError {
21    /// GPIO pin control operation failed.
22    /// 
23    /// This error occurs when setting GPIO pin states (high/low) fails,
24    /// typically due to hardware issues or incorrect pin configuration.
25    GpioError,
26    
27    /// PWM channel operation failed.
28    /// 
29    /// This error occurs when PWM duty cycle setting fails, which can
30    /// happen due to invalid duty cycle values or PWM hardware issues.
31    PwmError,
32    
33    /// Invalid speed value provided.
34    /// 
35    /// This error occurs when the speed value exceeds the configured
36    /// maximum duty cycle or is otherwise invalid for the motor configuration.
37    InvalidSpeed,
38    
39    /// Invalid configuration detected.
40    /// 
41    /// This error occurs when the motor driver configuration is incomplete
42    /// or contains conflicting settings that prevent proper operation.
43    InvalidConfiguration,
44    
45    /// Motor driver has not been initialized.
46    /// 
47    /// This error occurs when attempting to perform operations before
48    /// calling the `initialize()` method.
49    NotInitialized,
50    
51    /// Hardware fault detected.
52    /// 
53    /// This error indicates a general hardware fault that prevents normal
54    /// operation, such as encoder reading failures or unsupported operations.
55    HardwareFault,
56    
57    /// Motor current consumption exceeds safe limits.
58    /// 
59    /// This error occurs when the motor draws more current than the
60    /// configured safe operating limits.
61    OverCurrent,
62    
63    /// Motor driver temperature exceeds safe operating limits.
64    /// 
65    /// This error occurs when the motor driver's temperature sensor
66    /// detects overheating conditions.
67    OverTemperature,
68    
69    /// Supply voltage is below minimum operating requirements.
70    /// 
71    /// This error occurs when the motor driver's supply voltage
72    /// drops below the minimum required for safe operation.
73    UnderVoltage,
74    
75    /// Supply voltage exceeds maximum operating limits.
76    /// 
77    /// This error occurs when the motor driver's supply voltage
78    /// exceeds the maximum safe operating voltage.
79    OverVoltage,
80    
81    /// Communication with motor driver hardware failed.
82    /// 
83    /// This error occurs when communication protocols (I2C, SPI, UART)
84    /// fail to communicate with smart motor drivers.
85    CommunicationError,
86}
87
88impl core::fmt::Display for MotorDriverError {
89    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
90        match self {
91            MotorDriverError::GpioError => write!(f, "GPIO control error"),
92            MotorDriverError::PwmError => write!(f, "PWM control error"),
93            MotorDriverError::InvalidSpeed => write!(f, "Invalid speed value"),
94            MotorDriverError::InvalidConfiguration => write!(f, "Invalid configuration"),
95            MotorDriverError::NotInitialized => write!(f, "Driver not initialized"),
96            MotorDriverError::HardwareFault => write!(f, "Hardware fault detected"),
97            MotorDriverError::OverCurrent => write!(f, "Over current condition"),
98            MotorDriverError::OverTemperature => write!(f, "Over temperature condition"),
99            MotorDriverError::UnderVoltage => write!(f, "Under voltage condition"),
100            MotorDriverError::OverVoltage => write!(f, "Over voltage condition"),
101            MotorDriverError::CommunicationError => write!(f, "Communication error"),
102        }
103    }
104}
105
106#[cfg(feature = "std")]
107impl std::error::Error for MotorDriverError {}