mpu6050_dmp/error.rs
1//! MPU6050 Error Types
2//!
3//! This module defines the error types that can occur when:
4//! - Initializing the sensor
5//! - Communicating over I2C
6//! - Validating sensor responses
7
8use core::fmt::{Debug, Formatter};
9use embedded_hal::i2c::I2c;
10
11/// Error that occurs during sensor initialization.
12///
13/// Initialization can fail due to:
14/// - I2C communication errors
15/// - Wrong device connected (incorrect WHO_AM_I response)
16/// - Power-on reset failure
17/// - Configuration errors
18///
19/// Contains both the error and the I2C interface for error recovery.
20#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
21pub struct InitError<I>
22where
23 I: I2c,
24{
25 pub i2c: I,
26 pub error: Error<I>,
27}
28
29impl<I> Debug for InitError<I>
30where
31 I: I2c,
32{
33 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
34 self.error.fmt(f)
35 }
36}
37
38/// Errors that can occur during normal sensor operation.
39///
40/// These errors typically happen when:
41/// - Reading sensor data
42/// - Writing configuration values
43/// - Accessing the FIFO buffer
44/// - Loading DMP firmware
45#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
46pub enum Error<I>
47where
48 I: I2c,
49{
50 /// Failed to write data to sensor
51 /// Common causes:
52 /// - I2C bus busy
53 /// - Device not responding
54 /// - Incorrect address
55 WriteError(I::Error),
56
57 /// Failed to read data from sensor
58 /// Common causes:
59 /// - I2C bus busy
60 /// - Device not responding
61 /// - Invalid register address
62 WriteReadError(I::Error),
63
64 /// Connected device is not an MPU6050
65 /// Common causes:
66 /// - Wrong device connected
67 /// - Incorrect I2C address
68 /// - Device malfunction
69 WrongDevice,
70}
71
72impl<I> Debug for Error<I>
73where
74 I: I2c,
75{
76 fn fmt(&self, f: &mut Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
77 match self {
78 Self::WriteReadError(e) => f.debug_tuple("WriteReadError").field(e).finish(),
79 Self::WriteError(e) => f.debug_tuple("WriteError").field(e).finish(),
80 Self::WrongDevice => f.write_str("WrongDevice"),
81 }
82 }
83}