Skip to content

Commit 774a96d

Browse files
bors[bot]lucavezoc
andauthored
Merge #18
18: added the ability to read the capture file descriptor r=eldruin a=lucaVuitton You can now read the capture so you can calculate live fan RPMs Co-authored-by: lucaVuitton <[email protected]>
2 parents df9f5bd + 1cd372d commit 774a96d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ fn pwm_file_parse<T: FromStr>(chip: &PwmChip, pin: u32, name: &str) -> Result<T>
7171
}
7272
}
7373

74+
/// Get the two u32 from capture file descriptor
75+
fn pwm_capture_parse<T: FromStr>(chip: &PwmChip, pin: u32, name: &str) -> Result<Vec<T>> {
76+
let mut s = String::with_capacity(10);
77+
let mut f = pwm_file_ro(chip, pin, name)?;
78+
f.read_to_string(&mut s)?;
79+
s = s.trim().to_string();
80+
let capture = s.split_whitespace().collect::<Vec<_>>();
81+
let mut vec: Vec<T> = vec![];
82+
for s in capture.iter() {
83+
if let Ok(j) = s.parse::<T>() {
84+
vec.push(j);
85+
}
86+
}
87+
Ok(vec)
88+
}
89+
7490
impl PwmChip {
7591
pub fn new(number: u32) -> Result<PwmChip> {
7692
fs::metadata(&format!("/sys/class/pwm/pwmchip{}", number))?;
@@ -183,6 +199,16 @@ impl Pwm {
183199
pwm_file_parse::<u32>(&self.chip, self.number, "duty_cycle")
184200
}
185201

202+
/// Get the capture
203+
pub fn get_capture(&self) -> Result<(u32, u32)> {
204+
let t = pwm_capture_parse::<u32>(&self.chip, self.number, "capture")?;
205+
if t.len() == 2 {
206+
Ok((t[0], t[1]))
207+
} else {
208+
Err(error::Error::Unexpected(format!("Failed exporting")))
209+
}
210+
}
211+
186212
/// The active time of the PWM signal
187213
///
188214
/// Value is in nanoseconds and must be less than the period.

0 commit comments

Comments
 (0)