1515//! PWM access under Linux using the PWM sysfs interface
1616
1717use std:: io:: prelude:: * ;
18- use std:: os:: unix:: prelude:: * ;
1918use std:: fs:: File ;
2019use std:: fs;
2120use std:: fs:: OpenOptions ;
@@ -45,25 +44,23 @@ pub type Result<T> = ::std::result::Result<T, error::Error>;
4544
4645/// Open the specified entry name as a writable file
4746fn 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) ) ) ;
47+ let f = OpenOptions :: new ( )
48+ . write ( true )
49+ . open ( format ! ( "/sys/class/pwm/pwmchip{}/pwm{}/{}" , chip. number, pin, name) ) ?;
5350 Ok ( f)
5451}
5552
5653/// Open the specified entry name as a readable file
5754fn pwm_file_ro ( chip : & PwmChip , pin : u32 , name : & str ) -> Result < File > {
58- let f = try! ( File :: open ( format ! ( "/sys/class/pwm/pwmchip{}/pwm{}/{}" , chip. number, pin, name) ) ) ;
55+ let f = File :: open ( format ! ( "/sys/class/pwm/pwmchip{}/pwm{}/{}" , chip. number, pin, name) ) ? ;
5956 Ok ( f)
6057}
6158
6259/// Get the u32 value from the given entry
6360fn pwm_file_parse < T : FromStr > ( chip : & PwmChip , pin : u32 , name : & str ) -> Result < T > {
6461 let mut s = String :: with_capacity ( 10 ) ;
65- let mut f = try! ( pwm_file_ro ( chip, pin, name) ) ;
66- try! ( f. read_to_string ( & mut s) ) ;
62+ let mut f = pwm_file_ro ( chip, pin, name) ? ;
63+ f. read_to_string ( & mut s) ? ;
6764 match s. trim ( ) . parse :: < T > ( ) {
6865 Ok ( r) => Ok ( r) ,
6966 Err ( _) => Err ( Error :: Unexpected ( format ! ( "Unexpeted value file contents: {:?}" , s) ) ) ,
@@ -73,15 +70,15 @@ fn pwm_file_parse<T: FromStr>(chip: &PwmChip, pin: u32, name: &str) -> Result<T>
7370
7471impl PwmChip {
7572 pub fn new ( number : u32 ) -> Result < PwmChip > {
76- try! ( fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}" , number) ) ) ;
73+ fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}" , number) ) ? ;
7774 Ok ( PwmChip { number : number } )
7875 }
7976
8077 pub fn count ( & self ) -> Result < u32 > {
8178 let npwm_path = format ! ( "/sys/class/pwm/pwmchip{}/npwm" , self . number) ;
82- let mut npwm_file = try! ( File :: open ( & npwm_path) ) ;
79+ let mut npwm_file = File :: open ( & npwm_path) ? ;
8380 let mut s = String :: new ( ) ;
84- try! ( npwm_file. read_to_string ( & mut s) ) ;
81+ npwm_file. read_to_string ( & mut s) ? ;
8582 match s. parse :: < u32 > ( ) {
8683 Ok ( n) => Ok ( n) ,
8784 Err ( _) => Err ( Error :: Unexpected ( format ! ( "Unexpected npwm contents: {:?}" , s) ) ) ,
@@ -94,7 +91,7 @@ impl PwmChip {
9491 self . number,
9592 number) ) {
9693 let path = format ! ( "/sys/class/pwm/pwmchip{}/export" , self . number) ;
97- let mut export_file = try! ( File :: create ( & path) ) ;
94+ let mut export_file = File :: create ( & path) ? ;
9895 let _ = export_file. write_all ( format ! ( "{}" , number) . as_bytes ( ) ) ;
9996 }
10097 Ok ( ( ) )
@@ -105,7 +102,7 @@ impl PwmChip {
105102 self . number,
106103 number) ) {
107104 let path = format ! ( "/sys/class/pwm/pwmchip{}/unexport" , self . number) ;
108- let mut export_file = try! ( File :: create ( & path) ) ;
105+ let mut export_file = File :: create ( & path) ? ;
109106 let _ = export_file. write_all ( format ! ( "{}" , number) . as_bytes ( ) ) ;
110107 }
111108 Ok ( ( ) )
@@ -117,19 +114,19 @@ impl Pwm {
117114 ///
118115 /// This function does not export the Pwm pin
119116 pub fn new ( chip : u32 , number : u32 ) -> Result < Pwm > {
120- let chip: PwmChip = try! ( PwmChip :: new ( chip) ) ;
117+ let chip: PwmChip = PwmChip :: new ( chip) ? ;
121118 Ok ( Pwm {
122- chip : chip,
123- number : number,
124- } )
119+ chip : chip,
120+ number : number,
121+ } )
125122 }
126123
127124 /// Run a closure with the GPIO exported
128125 #[ inline]
129126 pub fn with_exported < F > ( & self , closure : F ) -> Result < ( ) >
130127 where F : FnOnce ( ) -> Result < ( ) >
131128 {
132- try! ( self . export ( ) ) ;
129+ self . export ( ) ? ;
133130 match closure ( ) {
134131 Ok ( ( ) ) => self . unexport ( ) ,
135132 Err ( _) => self . unexport ( ) ,
@@ -148,13 +145,9 @@ impl Pwm {
148145
149146 /// Enable/Disable the PWM Signal
150147 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- "1"
154- } else {
155- "0"
156- } ;
157- try!( enable_file. write_all ( contents. as_bytes ( ) ) ) ;
148+ let mut enable_file = pwm_file_wo ( & self . chip , self . number , "enable" ) ?;
149+ let contents = if enable { "1" } else { "0" } ;
150+ enable_file. write_all ( contents. as_bytes ( ) ) ?;
158151 Ok ( ( ) )
159152 }
160153
@@ -168,8 +161,9 @@ impl Pwm {
168161 /// Value is in nanoseconds and must be less than the period.
169162 pub fn set_duty_cycle_ns ( & self , duty_cycle_ns : u32 ) -> Result < ( ) > {
170163 // we'll just let the kernel do the validation
171- let mut duty_cycle_file = try!( pwm_file_wo ( & self . chip , self . number , "duty_cycle" ) ) ;
172- try!( duty_cycle_file. write_all ( format ! ( "{}" , duty_cycle_ns) . as_bytes ( ) ) ) ;
164+ let mut duty_cycle_file = pwm_file_wo ( & self . chip , self . number , "duty_cycle" ) ?;
165+ duty_cycle_file
166+ . write_all ( format ! ( "{}" , duty_cycle_ns) . as_bytes ( ) ) ?;
173167 Ok ( ( ) )
174168 }
175169
@@ -180,8 +174,9 @@ impl Pwm {
180174
181175 /// The period of the PWM signal in Nanoseconds
182176 pub fn set_period_ns ( & self , period_ns : u32 ) -> Result < ( ) > {
183- let mut period_file = try!( pwm_file_wo ( & self . chip , self . number , "period" ) ) ;
184- try!( period_file. write_all ( format ! ( "{}" , period_ns) . as_bytes ( ) ) ) ;
177+ let mut period_file = pwm_file_wo ( & self . chip , self . number , "period" ) ?;
178+ period_file
179+ . write_all ( format ! ( "{}" , period_ns) . as_bytes ( ) ) ?;
185180 Ok ( ( ) )
186181 }
187182}
0 commit comments