Skip to content

Commit 64084c3

Browse files
committed
Add functions for polarity set/get
1 parent 2fc4bad commit 64084c3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,28 @@ impl Pwm {
220220
period_file.write_all(format!("{}", period_ns).as_bytes())?;
221221
Ok(())
222222
}
223+
224+
/// Set the polarity of the PWM signal
225+
pub fn set_polarity(&self, polarity: Polarity) -> Result<()> {
226+
let mut polarity_file = pwm_file_wo(&self.chip, self.number, "polarity")?;
227+
match polarity {
228+
Polarity::Normal => polarity_file.write_all("normal".as_bytes())?,
229+
Polarity::Inverse => polarity_file.write_all("inversed".as_bytes())?,
230+
};
231+
Ok(())
232+
}
233+
234+
/// Get the polarity of the PWM signal
235+
pub fn get_polarity(&self) -> Result<Polarity> {
236+
let mut polarity_file = pwm_file_ro(&self.chip, self.number, "polarity")?;
237+
let mut s = String::new();
238+
polarity_file.read_to_string(&mut s)?;
239+
match s.trim() {
240+
"normal" => Ok(Polarity::Normal),
241+
"inversed" => Ok(Polarity::Inverse),
242+
_ => Err(Error::Unexpected(format!(
243+
"Unexpected polarity file contents: {:?}",
244+
s))),
245+
}
246+
}
223247
}

0 commit comments

Comments
 (0)