Skip to content

Commit 1cd372d

Browse files
committed
added the ability to read the capture file descriptor
1 parent 22286a4 commit 1cd372d

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/lib.rs

Lines changed: 29 additions & 2 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))?;
@@ -186,6 +202,16 @@ impl Pwm {
186202
pwm_file_parse::<u32>(&self.chip, self.number, "duty_cycle")
187203
}
188204

205+
/// Get the capture
206+
pub fn get_capture(&self) -> Result<(u32, u32)> {
207+
let t = pwm_capture_parse::<u32>(&self.chip, self.number, "capture")?;
208+
if t.len() == 2 {
209+
Ok((t[0], t[1]))
210+
} else {
211+
Err(error::Error::Unexpected(format!("Failed exporting")))
212+
}
213+
}
214+
189215
/// The active time of the PWM signal
190216
///
191217
/// Value is in nanoseconds and must be less than the period.
@@ -240,8 +266,9 @@ impl Pwm {
240266
"normal" => Ok(Polarity::Normal),
241267
"inversed" => Ok(Polarity::Inverse),
242268
_ => Err(Error::Unexpected(format!(
243-
"Unexpected polarity file contents: {:?}",
244-
s))),
269+
"Unexpected polarity file contents: {:?}",
270+
s
271+
))),
245272
}
246273
}
247274
}

0 commit comments

Comments
 (0)