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
use errors::*;
use util::*;
#[derive(Debug)]
pub struct ADC {
adc_num: u8,
scaling_factor: f32,
}
impl ADC {
pub fn new(m_adc_num: u8, m_scaling_factor: f32) -> ADC {
ADC {
adc_num: m_adc_num,
scaling_factor: m_scaling_factor,
}
}
pub fn read(&self) -> Result<u32> {
let path = format!("/sys/bus/iio/devices/iio:device0/in_voltage{}_raw",
&self.adc_num);
Ok(read_file(&path)
.chain_err(|| format!("Failed to read from ADC #{}", &self.adc_num))?
.trim()
.to_string()
.parse::<u32>()
.chain_err(|| format!("Failed to parse ADC #{} value", &self.adc_num))?)
}
pub fn scaled_read(&self) -> Result<f32> {
let path = format!("/sys/bus/iio/devices/iio:device0/in_voltage{}_raw",
&self.adc_num);
let raw_value = read_file(&path)
.chain_err(|| format!("Failed to read from ADC #{}", &self.adc_num))?
.trim()
.to_string()
.parse::<u32>()
.chain_err(|| format!("Failed to parse ADC #{} value", &self.adc_num))?;
Ok(raw_value as f32 * &self.scaling_factor)
}
}