1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! MSCURACT - Microstep current register (0x6B)
use super::{Address, ReadableRegister, Register};
/// Microstep current register.
///
/// Read-only register containing the actual microstep currents for both
/// coils A and B. Values are signed 9-bit values in range -255 to +255.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Mscuract(u32);
impl Mscuract {
/// Create with default value (0).
pub fn new() -> Self {
Self(0)
}
/// Get CUR_A - actual current for coil A.
///
/// Returns a signed 9-bit value (-255 to +255).
/// Represents the actual motor current for coil A.
pub fn cur_a(&self) -> i16 {
let raw = (self.0 & 0x1FF) as u16;
// Sign-extend from 9-bit to 16-bit
if raw & 0x100 != 0 {
(raw | 0xFE00) as i16
} else {
raw as i16
}
}
/// Get CUR_B - actual current for coil B.
///
/// Returns a signed 9-bit value (-255 to +255).
/// Represents the actual motor current for coil B.
pub fn cur_b(&self) -> i16 {
let raw = ((self.0 >> 16) & 0x1FF) as u16;
// Sign-extend from 9-bit to 16-bit
if raw & 0x100 != 0 {
(raw | 0xFE00) as i16
} else {
raw as i16
}
}
/// Get the raw register value.
pub fn raw(&self) -> u32 {
self.0
}
/// Create from raw value.
pub fn from_raw(value: u32) -> Self {
Self(value)
}
}
impl Default for Mscuract {
fn default() -> Self {
Self::new()
}
}
impl Register for Mscuract {
const ADDRESS: Address = Address::Mscuract;
}
impl ReadableRegister for Mscuract {}
impl From<u32> for Mscuract {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<Mscuract> for u32 {
fn from(reg: Mscuract) -> u32 {
reg.0
}
}