tmag5170/
sensor_config.rs

1/// Enable Angle calculation using two axis data
2pub enum AngleEn {
3    /// No angle calculation (default)
4    NoAngle = 0x00,
5
6    /// X-Y-angle calculation enabled
7    Xy = 0x01,
8
9    /// Y-Z-angle calculation enabled
10    Yz = 0x02,
11
12    /// Z-X-angle calculation enabled
13    Zx = 0x03,
14}
15
16/// Selects the time spent in low power mode between conversions
17/// when OPERATING_MODE =010b (OperatingMode::Active)
18pub enum SleepTime {
19    /// 1ms
20    Ms1 = 0x00,
21
22    /// 5ms
23    Ms5 = 0x01,
24
25    /// 10ms
26    Ms10 = 0x02,
27
28    /// 15ms
29    Ms15 = 0x03,
30
31    /// 20ms
32    Ms20 = 0x04,
33
34    /// 30ms
35    Ms30 = 0x05,
36
37    /// 50ms
38    Ms50 = 0x06,
39
40    /// 100ms
41    Ms100 = 0x07,
42
43    /// 500ms
44    Ms500 = 0x08,
45
46    /// 1000ms
47    Ms1000 = 0x09,
48}
49
50/// Enables data acquisition of the magnetic axis channel(s)
51#[allow(non_camel_case_types)]
52pub enum MagChEn {
53    /// All magnetic channels of OFF
54    Off = 0x00,
55
56    /// X channel enabled
57    X = 0x01,
58
59    /// Y channel enabled
60    Y = 0x02,
61
62    /// XY channel enabled
63    Xy = 0x03,
64
65    /// Z channel enabled
66    Z = 0x04,
67
68    /// ZX channel enabled
69    Zx = 0x05,
70
71    /// YZ channel enabled
72    Yz = 0x06,
73
74    /// XYZ channel enabled
75    Xyz = 0x07,
76
77    /// XYX channel enabled
78    Xyx = 0x08,
79
80    /// YXY channel enabled
81    Yxy = 0x09,
82
83    /// YZY channel enabled
84    Yzy = 0x0a,
85
86    /// ZYZ channel enabled
87    Zyz = 0x0b,
88
89    /// ZXZ channel enabled
90    Zxz = 0x0c,
91
92    /// XZX channel enabled
93    Xzx = 0x0d,
94
95    /// XYZYX channel enabled
96    Xyzyx = 0x0e,
97
98    /// XYZZYX channel enabled
99    Xyzzyx = 0x0f,
100}
101
102/// Enables different magnetic ranges
103#[allow(non_camel_case_types)]
104pub enum Range {
105    /// ±50mT (TMAG5170A1) / ±200mT(TMAG5170A2)
106    A1_50mT_A2_200mT = 0x00,
107
108    /// ±25mT (TMAG5170A1) / ±133mT(TMAG5170A2)
109    A1_25mT_A2_133mT = 0x01,
110
111    /// ±100mT (TMAG5170A1) / ±300mT(TMAG5170A2)
112    A1_100mT_A2_300mT = 0x02,
113}
114
115/// Configure Device Operation Modes - SENSOR_CONFIG
116pub struct SensorConfig {
117    config: u16,
118}
119
120impl SensorConfig {
121    /// Creates default config
122    pub fn new() -> Self {
123        let config = 0x00;
124        SensorConfig { config }
125    }
126
127    /// Creates config from u16 value
128    pub fn form_u16(config: u16) -> Self {
129        SensorConfig { config }
130    }
131
132    /// Convert config to u16 value
133    pub fn to_u16(&self) -> u16 {
134        self.config
135    }
136
137    /// Set AngleEn field
138    pub fn set_angle_en(mut self, angle_en: AngleEn) -> Self {
139        self.config = self.config & !(0b11 << 14) | ((angle_en as u16) << 14);
140        self
141    }
142
143    /// Set SleepTime field
144    pub fn set_sleep_time(mut self, sleep_time: SleepTime) -> Self {
145        self.config = self.config & !(0b1111 << 10) | ((sleep_time as u16) << 10);
146        self
147    }
148
149    /// Set AngleEn field
150    pub fn set_mag_ch_en(mut self, mag_ch_en: MagChEn) -> Self {
151        self.config = self.config & !(0b1111 << 6) | ((mag_ch_en as u16) << 6);
152        self
153    }
154
155    /// Set Z Range field
156    pub fn set_z_range(mut self, z_range: Range) -> Self {
157        self.config = self.config & !(0b11 << 4) | ((z_range as u16) << 4);
158        self
159    }
160
161    /// Set Y Range field
162    pub fn set_y_range(mut self, y_range: Range) -> Self {
163        self.config = self.config & !(0b11 << 2) | ((y_range as u16) << 2);
164        self
165    }
166
167    /// Set X Range field
168    pub fn set_x_range(mut self, x_range: Range) -> Self {
169        self.config = self.config & !(0b11) | (x_range as u16);
170        self
171    }
172}
173
174impl Default for SensorConfig {
175    fn default() -> Self {
176        SensorConfig::new()
177    }
178}