|
1 |
| -#[test] |
2 |
| -fn it_works() { |
| 1 | +// Copyright 2016, Paul Osborne <osbpau@gmail.com> |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// http://www.apache.org/license/LICENSE-2.0> or the MIT license |
| 5 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | +// option. This file may not be copied, modified, or distributed |
| 7 | +// except according to those terms. |
| 8 | +// |
| 9 | +// Portions of this implementation are based on work by Nat Pryce: |
| 10 | +// https://github.com/npryce/rusty-pi/blob/master/src/pi/gpio.rs |
| 11 | + |
| 12 | +#![crate_type = "lib"] |
| 13 | +#![crate_name = "sysfs_pwm"] |
| 14 | + |
| 15 | +//! PWM access under Linux using the PWM sysfs interface |
| 16 | +
|
| 17 | +use std::io::prelude::*; |
| 18 | +use std::os::unix::prelude::*; |
| 19 | +use std::fs::File; |
| 20 | +use std::fs; |
| 21 | + |
| 22 | +mod error; |
| 23 | +pub use error::Error; |
| 24 | + |
| 25 | +#[derive(Debug)] |
| 26 | +pub struct PwmChip { |
| 27 | + chip: u32 |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug)] |
| 31 | +pub struct Pwm { |
| 32 | + chip: PwmChip, |
| 33 | + number: u32, |
| 34 | +} |
| 35 | + |
| 36 | +pub type Result<T> = ::std::result::Result<T, error::Error>; |
| 37 | + |
| 38 | +impl PwmChip { |
| 39 | + pub fn new(chip: u32) -> Result<PwmChip> { |
| 40 | + try!(fs::metadata(&format!("/sys/class/pwm/pwmchip{}", chip))); |
| 41 | + Ok(PwmChip { chip: chip }) |
| 42 | + } |
| 43 | + |
| 44 | + pub fn count(&self) -> Result<u32> { |
| 45 | + let npwm_path = format!("/sys/class/pwm/pwmchip{}/npwm", self.chip); |
| 46 | + let mut npwm_file = try!(File::open(&npwm_path)); |
| 47 | + let mut s = String::new(); |
| 48 | + try!(npwm_file.read_to_string(&mut s)); |
| 49 | + match s.parse::<u32>() { |
| 50 | + Ok(n) => Ok(n), |
| 51 | + Err(_) => Err(Error::Unexpected( |
| 52 | + format!("Unexpected npwm contents: {:?}", s))), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn export(&self, number: u32) -> Result<()> { |
| 57 | + // only export if not already exported |
| 58 | + if let Err(_) = fs::metadata(&format!("/sys/class/pwm/pwmchip{}/pwm{}", self.chip, number)) { |
| 59 | + let path = format!("/sys/class/pwm/pwmchip{}/export", self.chip); |
| 60 | + let mut export_file = try!(File::create(&path)); |
| 61 | + let _ = export_file.write_all(format!("{}", number).as_bytes()); |
| 62 | + } |
| 63 | + Ok(()) |
| 64 | + } |
| 65 | + |
| 66 | + pub fn unexport(&self, number: u32) -> Result<()> { |
| 67 | + if let Ok(_) = fs::metadata(&format!("/sys/class/pwm/pwmchip{}/pwm{}", self.chip, number)) { |
| 68 | + let path = format!("/sys/class/pwm/pwmchip{}/unexport", self.chip); |
| 69 | + let mut export_file = try!(File::create(&path)); |
| 70 | + let _ = export_file.write_all(format!("{}", number).as_bytes()); |
| 71 | + } |
| 72 | + Ok(()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl Pwm { |
| 77 | + |
| 78 | + /// Create a new Pwm wiht the provided chip/number |
| 79 | + /// |
| 80 | + /// This function does not export the Pwm pin |
| 81 | + pub fn new(chip: u32, number: u32) -> Result<Pwm> { |
| 82 | + let chip: PwmChip = try!(PwmChip::new(chip)); |
| 83 | + Ok(Pwm { chip: chip, number: number }) |
| 84 | + } |
| 85 | + |
| 86 | + /// Run a closure with the GPIO exported |
| 87 | + #[inline] |
| 88 | + pub fn with_exported<F>(&self, closure: F) -> Result<()> where |
| 89 | + F: FnOnce() -> Result<()> |
| 90 | + { |
| 91 | + try!(self.export()); |
| 92 | + match closure() { |
| 93 | + Ok(()) => self.unexport(), |
| 94 | + Err(_) => self.unexport(), |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// Export the Pwm for use |
| 99 | + pub fn export(&self) -> Result<()> { |
| 100 | + self.chip.export(self.number) |
| 101 | + } |
| 102 | + |
| 103 | + /// Unexport the PWM |
| 104 | + pub fn unexport(&self) -> Result<()> { |
| 105 | + self.chip.unexport(self.number) |
| 106 | + } |
| 107 | + |
3 | 108 | }
|
0 commit comments