@@ -18,9 +18,8 @@ use std::io::prelude::*;
18
18
use std:: os:: unix:: prelude:: * ;
19
19
use std:: fs:: File ;
20
20
use std:: fs;
21
+ use std:: fs:: OpenOptions ;
21
22
use std:: str:: FromStr ;
22
- use std:: cmp:: { min, max} ;
23
- use std:: path:: Path ;
24
23
25
24
mod error;
26
25
pub use error:: Error ;
@@ -45,11 +44,12 @@ pub enum Polarity {
45
44
pub type Result < T > = :: std:: result:: Result < T , error:: Error > ;
46
45
47
46
/// 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) ) ) ;
53
53
Ok ( f)
54
54
}
55
55
@@ -64,7 +64,7 @@ fn pwm_file_parse<T: FromStr>(chip: &PwmChip, pin: u32, name: &str) -> Result<T>
64
64
let mut s = String :: with_capacity ( 10 ) ;
65
65
let mut f = try!( pwm_file_ro ( chip, pin, name) ) ;
66
66
try!( f. read_to_string ( & mut s) ) ;
67
- match s. parse :: < T > ( ) {
67
+ match s. trim ( ) . parse :: < T > ( ) {
68
68
Ok ( r) => Ok ( r) ,
69
69
Err ( _) => Err ( Error :: Unexpected ( format ! ( "Unexpeted value file contents: {:?}" , s) ) ) ,
70
70
}
@@ -147,39 +147,19 @@ impl Pwm {
147
147
}
148
148
149
149
/// 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 {
153
153
"1"
154
154
} else {
155
155
"0"
156
156
} ;
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 ( ) ) ) ;
178
158
Ok ( ( ) )
179
159
}
180
160
181
161
/// 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 > {
183
163
pwm_file_parse :: < u32 > ( & self . chip , self . number , "duty_cycle" )
184
164
}
185
165
@@ -188,7 +168,7 @@ impl Pwm {
188
168
/// Value is in nanoseconds and must be less than the period.
189
169
pub fn set_duty_cycle_ns ( & self , duty_cycle_ns : u32 ) -> Result < ( ) > {
190
170
// 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" ) ) ;
192
172
try!( duty_cycle_file. write_all ( format ! ( "{}" , duty_cycle_ns) . as_bytes ( ) ) ) ;
193
173
Ok ( ( ) )
194
174
}
@@ -200,7 +180,7 @@ impl Pwm {
200
180
201
181
/// The period of the PWM signal in Nanoseconds
202
182
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" ) ) ;
204
184
try!( period_file. write_all ( format ! ( "{}" , period_ns) . as_bytes ( ) ) ) ;
205
185
Ok ( ( ) )
206
186
}
0 commit comments