@@ -71,6 +71,22 @@ fn pwm_file_parse<T: FromStr>(chip: &PwmChip, pin: u32, name: &str) -> Result<T>
71
71
}
72
72
}
73
73
74
+ /// Get the two u32 from capture file descriptor
75
+ fn pwm_capture_parse < T : FromStr > ( chip : & PwmChip , pin : u32 , name : & str ) -> Result < Vec < T > > {
76
+ let mut s = String :: with_capacity ( 10 ) ;
77
+ let mut f = pwm_file_ro ( chip, pin, name) ?;
78
+ f. read_to_string ( & mut s) ?;
79
+ s = s. trim ( ) . to_string ( ) ;
80
+ let capture = s. split_whitespace ( ) . collect :: < Vec < _ > > ( ) ;
81
+ let mut vec: Vec < T > = vec ! [ ] ;
82
+ for s in capture. iter ( ) {
83
+ if let Ok ( j) = s. parse :: < T > ( ) {
84
+ vec. push ( j) ;
85
+ }
86
+ }
87
+ Ok ( vec)
88
+ }
89
+
74
90
impl PwmChip {
75
91
pub fn new ( number : u32 ) -> Result < PwmChip > {
76
92
fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}" , number) ) ?;
@@ -186,6 +202,16 @@ impl Pwm {
186
202
pwm_file_parse :: < u32 > ( & self . chip , self . number , "duty_cycle" )
187
203
}
188
204
205
+ /// Get the capture
206
+ pub fn get_capture ( & self ) -> Result < ( u32 , u32 ) > {
207
+ let t = pwm_capture_parse :: < u32 > ( & self . chip , self . number , "capture" ) ?;
208
+ if t. len ( ) == 2 {
209
+ Ok ( ( t[ 0 ] , t[ 1 ] ) )
210
+ } else {
211
+ Err ( error:: Error :: Unexpected ( format ! ( "Failed exporting" ) ) )
212
+ }
213
+ }
214
+
189
215
/// The active time of the PWM signal
190
216
///
191
217
/// Value is in nanoseconds and must be less than the period.
@@ -240,8 +266,9 @@ impl Pwm {
240
266
"normal" => Ok ( Polarity :: Normal ) ,
241
267
"inversed" => Ok ( Polarity :: Inverse ) ,
242
268
_ => Err ( Error :: Unexpected ( format ! (
243
- "Unexpected polarity file contents: {:?}" ,
244
- s) ) ) ,
269
+ "Unexpected polarity file contents: {:?}" ,
270
+ s
271
+ ) ) ) ,
245
272
}
246
273
}
247
274
}
0 commit comments