as5048a_async/diagnostics.rs
1//! Diagnostics registers for AS5048A
2
3/// Diagnostics flags from the `DIAG_AGC` register (0x3FFD)
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6pub struct Diagnostics {
7 raw: u16,
8}
9
10impl Diagnostics {
11 /// Create diagnostics from raw register value
12 #[must_use]
13 pub const fn new(raw: u16) -> Self {
14 Self { raw }
15 }
16
17 /// Get the raw register value
18 #[must_use]
19 pub const fn raw(&self) -> u16 {
20 self.raw
21 }
22
23 /// `COMP_HIGH`: Magnetic field too strong
24 ///
25 /// Set when the magnetic field is above the recommended range
26 /// The sensor may still work but accuracy could be affected
27 #[must_use]
28 pub const fn comp_high(&self) -> bool {
29 self.raw & 0x2000 != 0
30 }
31
32 /// `COMP_LOW`: Magnetic field too weak
33 ///
34 /// Set when the magnetic field is below the recommended range
35 /// The sensor may still work but accuracy could be affected
36 #[must_use]
37 pub const fn comp_low(&self) -> bool {
38 self.raw & 0x1000 != 0
39 }
40
41 /// COF: CORDIC overflow
42 ///
43 /// Set when an overflow occurred in the CORDIC calculation
44 /// When this bit is set, angle and magnitude data is invalid
45 #[must_use]
46 pub const fn cordic_overflow(&self) -> bool {
47 self.raw & 0x0800 != 0
48 }
49
50 /// OCF: Offset compensation finished
51 ///
52 /// This flag is set to 1 after power-up when the offset compensation
53 /// algorithm has finished. After power-up, the flag remains at 1
54 #[must_use]
55 pub const fn offset_comp_finished(&self) -> bool {
56 self.raw & 0x0400 != 0
57 }
58
59 /// Get the Automatic Gain Control (AGC) value
60 ///
61 /// Returns an 8-bit value where:
62 /// - 0 = high magnetic field (close to sensor)
63 /// - 255 = low magnetic field (far from sensor)
64 ///
65 /// Typical values are between 60-200. Values outside this range
66 /// may indicate the magnet is too close or too far
67 #[must_use]
68 pub const fn agc_value(&self) -> u8 {
69 (self.raw & 0x00FF) as u8
70 }
71
72 /// Check if the magnetic field strength is within acceptable range
73 ///
74 /// Returns `true` if neither `COMP_HIGH` nor `COMP_LOW` is set
75 #[must_use]
76 pub const fn magnetic_field_ok(&self) -> bool {
77 !self.comp_high() && !self.comp_low()
78 }
79
80 /// Check if data is valid
81 ///
82 /// Returns `true` if there's no CORDIC overflow and the magnetic
83 /// field is within acceptable range
84 #[must_use]
85 pub const fn is_valid(&self) -> bool {
86 !self.cordic_overflow() && self.magnetic_field_ok()
87 }
88}
89
90impl From<u16> for Diagnostics {
91 fn from(raw: u16) -> Self {
92 Self::new(raw)
93 }
94}