15
15
//! PWM access under Linux using the PWM sysfs interface
16
16
17
17
use std:: io:: prelude:: * ;
18
- use std:: os:: unix:: prelude:: * ;
19
18
use std:: fs:: File ;
20
19
use std:: fs;
21
20
use std:: fs:: OpenOptions ;
@@ -45,25 +44,23 @@ pub type Result<T> = ::std::result::Result<T, error::Error>;
45
44
46
45
/// Open the specified entry name as a writable file
47
46
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) ) ) ;
47
+ let f = OpenOptions :: new ( )
48
+ . write ( true )
49
+ . open ( format ! ( "/sys/class/pwm/pwmchip{}/pwm{}/{}" , chip. number, pin, name) ) ?;
53
50
Ok ( f)
54
51
}
55
52
56
53
/// Open the specified entry name as a readable file
57
54
fn 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) ) ? ;
59
56
Ok ( f)
60
57
}
61
58
62
59
/// Get the u32 value from the given entry
63
60
fn pwm_file_parse < T : FromStr > ( chip : & PwmChip , pin : u32 , name : & str ) -> Result < T > {
64
61
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) ? ;
67
64
match s. trim ( ) . parse :: < T > ( ) {
68
65
Ok ( r) => Ok ( r) ,
69
66
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>
73
70
74
71
impl PwmChip {
75
72
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) ) ? ;
77
74
Ok ( PwmChip { number : number } )
78
75
}
79
76
80
77
pub fn count ( & self ) -> Result < u32 > {
81
78
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) ? ;
83
80
let mut s = String :: new ( ) ;
84
- try! ( npwm_file. read_to_string ( & mut s) ) ;
81
+ npwm_file. read_to_string ( & mut s) ? ;
85
82
match s. parse :: < u32 > ( ) {
86
83
Ok ( n) => Ok ( n) ,
87
84
Err ( _) => Err ( Error :: Unexpected ( format ! ( "Unexpected npwm contents: {:?}" , s) ) ) ,
@@ -90,22 +87,18 @@ impl PwmChip {
90
87
91
88
pub fn export ( & self , number : u32 ) -> Result < ( ) > {
92
89
// only export if not already exported
93
- if let Err ( _) = fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}/pwm{}" ,
94
- self . number,
95
- number) ) {
90
+ if fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}/pwm{}" , self . number, number) ) . is_err ( ) {
96
91
let path = format ! ( "/sys/class/pwm/pwmchip{}/export" , self . number) ;
97
- let mut export_file = try! ( File :: create ( & path) ) ;
92
+ let mut export_file = File :: create ( & path) ? ;
98
93
let _ = export_file. write_all ( format ! ( "{}" , number) . as_bytes ( ) ) ;
99
94
}
100
95
Ok ( ( ) )
101
96
}
102
97
103
98
pub fn unexport ( & self , number : u32 ) -> Result < ( ) > {
104
- if let Ok ( _) = fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}/pwm{}" ,
105
- self . number,
106
- number) ) {
99
+ if fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}/pwm{}" , self . number, number) ) . is_ok ( ) {
107
100
let path = format ! ( "/sys/class/pwm/pwmchip{}/unexport" , self . number) ;
108
- let mut export_file = try! ( File :: create ( & path) ) ;
101
+ let mut export_file = File :: create ( & path) ? ;
109
102
let _ = export_file. write_all ( format ! ( "{}" , number) . as_bytes ( ) ) ;
110
103
}
111
104
Ok ( ( ) )
@@ -117,22 +110,21 @@ impl Pwm {
117
110
///
118
111
/// This function does not export the Pwm pin
119
112
pub fn new ( chip : u32 , number : u32 ) -> Result < Pwm > {
120
- let chip: PwmChip = try! ( PwmChip :: new ( chip) ) ;
113
+ let chip: PwmChip = PwmChip :: new ( chip) ? ;
121
114
Ok ( Pwm {
122
- chip : chip,
123
- number : number,
124
- } )
115
+ chip : chip,
116
+ number : number,
117
+ } )
125
118
}
126
119
127
120
/// Run a closure with the GPIO exported
128
121
#[ inline]
129
122
pub fn with_exported < F > ( & self , closure : F ) -> Result < ( ) >
130
123
where F : FnOnce ( ) -> Result < ( ) >
131
124
{
132
- try! ( self . export ( ) ) ;
125
+ self . export ( ) ? ;
133
126
match closure ( ) {
134
- Ok ( ( ) ) => self . unexport ( ) ,
135
- Err ( _) => self . unexport ( ) ,
127
+ Ok ( ( ) ) | Err ( _) => self . unexport ( ) ,
136
128
}
137
129
}
138
130
@@ -148,13 +140,9 @@ impl Pwm {
148
140
149
141
/// Enable/Disable the PWM Signal
150
142
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 ( ) ) ) ;
143
+ let mut enable_file = pwm_file_wo ( & self . chip , self . number , "enable" ) ?;
144
+ let contents = if enable { "1" } else { "0" } ;
145
+ enable_file. write_all ( contents. as_bytes ( ) ) ?;
158
146
Ok ( ( ) )
159
147
}
160
148
@@ -168,8 +156,9 @@ impl Pwm {
168
156
/// Value is in nanoseconds and must be less than the period.
169
157
pub fn set_duty_cycle_ns ( & self , duty_cycle_ns : u32 ) -> Result < ( ) > {
170
158
// 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 ( ) ) ) ;
159
+ let mut duty_cycle_file = pwm_file_wo ( & self . chip , self . number , "duty_cycle" ) ?;
160
+ duty_cycle_file
161
+ . write_all ( format ! ( "{}" , duty_cycle_ns) . as_bytes ( ) ) ?;
173
162
Ok ( ( ) )
174
163
}
175
164
@@ -180,8 +169,9 @@ impl Pwm {
180
169
181
170
/// The period of the PWM signal in Nanoseconds
182
171
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 ( ) ) ) ;
172
+ let mut period_file = pwm_file_wo ( & self . chip , self . number , "period" ) ?;
173
+ period_file
174
+ . write_all ( format ! ( "{}" , period_ns) . as_bytes ( ) ) ?;
185
175
Ok ( ( ) )
186
176
}
187
177
}
0 commit comments