@@ -18,9 +18,8 @@ use std::io::prelude::*;
1818use std:: os:: unix:: prelude:: * ;
1919use std:: fs:: File ;
2020use std:: fs;
21+ use std:: fs:: OpenOptions ;
2122use std:: str:: FromStr ;
22- use std:: cmp:: { min, max} ;
23- use std:: path:: Path ;
2423
2524mod error;
2625pub use error:: Error ;
@@ -45,11 +44,12 @@ pub enum Polarity {
4544pub type Result < T > = :: std:: result:: Result < T , error:: Error > ;
4645
4746/// Open the specified entry name as a writable file
48- fn pwm_file_rw ( chip : & PwmChip , pin : u32 , name : & str ) -> Result < File > {
49- let f = try!( File :: create ( format ! ( "/sys/class/pwm/pwmchip{}/pwm{}/{}" ,
50- chip. number,
51- pin,
52- name) ) ) ;
47+ fn pwm_file_wo ( chip : & PwmChip , pin : u32 , name : & str ) -> Result < File > {
48+ let f = try!( OpenOptions :: new ( ) . write ( true )
49+ . open ( format ! ( "/sys/class/pwm/pwmchip{}/pwm{}/{}" ,
50+ chip. number,
51+ pin,
52+ name) ) ) ;
5353 Ok ( f)
5454}
5555
@@ -64,7 +64,7 @@ fn pwm_file_parse<T: FromStr>(chip: &PwmChip, pin: u32, name: &str) -> Result<T>
6464 let mut s = String :: with_capacity ( 10 ) ;
6565 let mut f = try!( pwm_file_ro ( chip, pin, name) ) ;
6666 try!( f. read_to_string ( & mut s) ) ;
67- match s. parse :: < T > ( ) {
67+ match s. trim ( ) . parse :: < T > ( ) {
6868 Ok ( r) => Ok ( r) ,
6969 Err ( _) => Err ( Error :: Unexpected ( format ! ( "Unexpeted value file contents: {:?}" , s) ) ) ,
7070 }
@@ -147,39 +147,19 @@ impl Pwm {
147147 }
148148
149149 /// Enable/Disable the PWM Signal
150- pub fn set_active ( & self , active : bool ) -> Result < ( ) > {
151- let mut active_file = try!( pwm_file_rw ( & self . chip , self . number , "active " ) ) ;
152- let contents = if active {
150+ pub fn enable ( & self , enable : bool ) -> Result < ( ) > {
151+ let mut enable_file = try!( pwm_file_wo ( & self . chip , self . number , "enable " ) ) ;
152+ let contents = if enable {
153153 "1"
154154 } else {
155155 "0"
156156 } ;
157- try!( active_file. write_all ( contents. as_bytes ( ) ) ) ;
158- Ok ( ( ) )
159- }
160-
161- /// Get the currently configured duty cycle as a percentage
162- pub fn get_duty_cycle ( & self ) -> Result < f32 > {
163- let raw_duty_cycle = try!( pwm_file_parse :: < u32 > ( & self . chip , self . number , "duty" ) ) ;
164- Ok ( ( raw_duty_cycle as f32 ) / 1000.0 )
165- }
166-
167- /// Set the duty cycle as a percentage of time active
168- ///
169- /// This value is expected to be a floating point value
170- /// between 0.0 and 1.0. It maps to a value with resolution 0 -
171- /// 1000. Values < 0 or > 1.0 are capped at the minimum or
172- /// maximum respectively.
173- pub fn set_duty_cycle ( & self , percent : f32 ) -> Result < ( ) > {
174- let raw_percent_adj: u32 = ( percent * 1000.0 ) . floor ( ) as u32 ;
175- let percent_adj: u32 = max ( 0 , min ( raw_percent_adj, 1000 ) ) ;
176- let mut dc_file = try!( pwm_file_rw ( & self . chip , self . number , "duty" ) ) ;
177- try!( dc_file. write_all ( format ! ( "{}" , percent_adj) . as_bytes ( ) ) ) ;
157+ try!( enable_file. write_all ( contents. as_bytes ( ) ) ) ;
178158 Ok ( ( ) )
179159 }
180160
181161 /// Get the currently configured duty_cycle in nanoseconds
182- pub fn get_duty_cycle_ns ( & self , duty_cycle_ns : u32 ) -> Result < u32 > {
162+ pub fn get_duty_cycle_ns ( & self ) -> Result < u32 > {
183163 pwm_file_parse :: < u32 > ( & self . chip , self . number , "duty_cycle" )
184164 }
185165
@@ -188,7 +168,7 @@ impl Pwm {
188168 /// Value is in nanoseconds and must be less than the period.
189169 pub fn set_duty_cycle_ns ( & self , duty_cycle_ns : u32 ) -> Result < ( ) > {
190170 // we'll just let the kernel do the validation
191- let mut duty_cycle_file = try!( pwm_file_rw ( & self . chip , self . number , "duty_cycle" ) ) ;
171+ let mut duty_cycle_file = try!( pwm_file_wo ( & self . chip , self . number , "duty_cycle" ) ) ;
192172 try!( duty_cycle_file. write_all ( format ! ( "{}" , duty_cycle_ns) . as_bytes ( ) ) ) ;
193173 Ok ( ( ) )
194174 }
@@ -200,7 +180,7 @@ impl Pwm {
200180
201181 /// The period of the PWM signal in Nanoseconds
202182 pub fn set_period_ns ( & self , period_ns : u32 ) -> Result < ( ) > {
203- let mut period_file = try!( pwm_file_rw ( & self . chip , self . number , "period" ) ) ;
183+ let mut period_file = try!( pwm_file_wo ( & self . chip , self . number , "period" ) ) ;
204184 try!( period_file. write_all ( format ! ( "{}" , period_ns) . as_bytes ( ) ) ) ;
205185 Ok ( ( ) )
206186 }
0 commit comments