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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//! This module contains the low level functions for reading values from the AFE4404.
use crate::{device::AFE4404, errors::AfeError, modes::LedMode};
impl<I2C, MODE> AFE4404<I2C, MODE>
where
I2C: I2c<SevenBitAddress>,
MODE: LedMode,
{
/// Reads the LED1 sampled value.
///
/// # Notes
///
/// Call this function after an `ADC_RDY` pulse, data will remain valid until next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_led1(&mut self) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r2ch_prev = self.registers.r2Ch.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r2ch_prev.led1val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
/// Reads the LED2 sampled value.
///
/// # Notes
///
/// Call this function after an `ADC_RDY` pulse, data will remain valid until next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_led2(&mut self) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r2ah_prev = self.registers.r2Ah.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r2ah_prev.led2val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
}
impl<I2C> AFE4404<I2C, ThreeLedsMode>
where
I2C: I2c<SevenBitAddress>,
{
/// Reads the LED3 sampled value.
///
/// # Notes
///
/// Call this function after an `ADC_RDY` pulse, data will remain valid until next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_led3(&mut self) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r2bh_prev = self.registers.r2Bh.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r2bh_prev.aled2val_or_led3val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
/// Reads the Ambient sampled value.
///
/// # Notes
///
/// Call this function after an `ADC_RDY` pulse, data will remain valid until next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_ambient(&mut self) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r2dh_prev = self.registers.r2Dh.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r2dh_prev.aled1val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
/// Reads the LED1 minus Ambient sampled value.
///
/// # Notes
///
/// Call this function after an `ADC_RDY` pulse, data will remain valid until next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_led1_minus_ambient(&mut self) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r2fh_prev = self.registers.r2Fh.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r2fh_prev.led1_minus_aled1val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
/// Reads the LED1 minus Ambient value averaged over a number of samples set by the `decimation_factor`.
///
/// # Notes
///
/// When the decimation factor is greater than one, call this function after an `ADC_RDY` pulse, data will remain valid untill next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_averaged_led1_minus_ambient(
&mut self,
) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r40h_prev = self.registers.r40h.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r40h_prev.avg_led1_minus_aled1val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
}
impl<I2C> AFE4404<I2C, TwoLedsMode>
where
I2C: I2c<SevenBitAddress>,
{
/// Reads the Ambient1 sampled value.
///
/// # Notes
///
/// Call this function after an `ADC_RDY` pulse, data will remain valid until next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_ambient1(&mut self) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r2dh_prev = self.registers.r2Dh.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r2dh_prev.aled1val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
/// Reads the Ambient2 sampled value.
///
/// # Notes
///
/// Call this function after an `ADC_RDY` pulse, data will remain valid until next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_ambient2(&mut self) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r2bh_prev = self.registers.r2Bh.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r2bh_prev.aled2val_or_led3val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
/// Reads the LED1 minus Ambient1 value.
///
/// # Notes
///
/// Call this function after an `ADC_RDY` pulse, data will remain valid until next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_led1_minus_ambient1(&mut self) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r2fh_prev = self.registers.r2Fh.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r2fh_prev.led1_minus_aled1val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
/// Reads the LED2 minus Ambient2 value.
///
/// # Notes
///
/// Call this function after an `ADC_RDY` pulse, data will remain valid until next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_led2_minus_ambient2(&mut self) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r2eh_prev = self.registers.r2Eh.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r2eh_prev.led2_minus_aled2val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
/// Reads the LED1 minus Ambient1 value averaged over a number of samples set by the `decimation_factor`.
///
/// # Notes
///
/// When the decimation factor is greater than one, call this function after an `ADC_RDY` pulse, data will remain valid untill next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_averaged_led1_minus_ambient1(
&mut self,
) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r40h_prev = self.registers.r40h.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r40h_prev.avg_led1_minus_aled1val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
/// Reads the LED2 minus Ambient2 value averaged over a number of samples set by the `decimation_factor`.
///
/// # Notes
///
/// When the decimation factor is greater than one, call this function after an `ADC_RDY` pulse, data will remain valid untill next `ADC_RDY` pulse.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// This function returns an error if the ADC reading falls outside the allowed range.
pub fn read_averaged_led2_minus_ambient2(
&mut self,
) -> Result<ElectricPotential, AfeError<I2C::Error>> {
let r41h_prev = self.registers.r41h.read()?;
let quantisation: ElectricPotential = ElectricPotential::new::<volt>(1.2) / 2_097_151.0;
// We are converting a 22 bit reading (stored in a 32 bit register) to a 32 bit float.
// Since the 32 bit float has a 23 bits, we allow a precision loss.
// We also allow wraps since we take the sign into account.
let sign_extension_bits = ((r41h_prev.avg_led2_minus_aled2val() & 0x00FF_FFFF) >> 21) as u8;
let signed_value = match sign_extension_bits {
0b000 => register_value as i32, // The value is positive.
0b111 => (register_value | 0xFF00_0000) as i32, // Extend the sign of the negative value.
_ => return Err(AfeError::AdcReadingOutsideAllowedRange),
};
Ok(signed_value as f32 * quantisation);
}
}