99// Portions of this implementation are based on work by Nat Pryce:
1010// https://github.com/npryce/rusty-pi/blob/master/src/pi/gpio.rs
1111
12- #![ crate_type = "lib" ]
13- #![ crate_name = "sysfs_pwm" ]
14-
1512//! PWM access under Linux using the PWM sysfs interface
1613
17- use std:: io:: prelude:: * ;
18- use std:: fs:: File ;
1914use std:: fs;
15+ use std:: fs:: File ;
2016use std:: fs:: OpenOptions ;
17+ use std:: io:: prelude:: * ;
2118use std:: str:: FromStr ;
2219
2320mod error;
@@ -44,15 +41,19 @@ pub type Result<T> = ::std::result::Result<T, error::Error>;
4441
4542/// Open the specified entry name as a writable file
4643fn pwm_file_wo ( chip : & PwmChip , pin : u32 , name : & str ) -> Result < File > {
47- let f = OpenOptions :: new ( )
48- . write ( true )
49- . open ( format ! ( "/sys/class/pwm/pwmchip{}/pwm{}/{}" , chip. number, pin, name) ) ?;
44+ let f = OpenOptions :: new ( ) . write ( true ) . open ( format ! (
45+ "/sys/class/pwm/pwmchip{}/pwm{}/{}" ,
46+ chip. number, pin, name
47+ ) ) ?;
5048 Ok ( f)
5149}
5250
5351/// Open the specified entry name as a readable file
5452fn pwm_file_ro ( chip : & PwmChip , pin : u32 , name : & str ) -> Result < File > {
55- let f = File :: open ( format ! ( "/sys/class/pwm/pwmchip{}/pwm{}/{}" , chip. number, pin, name) ) ?;
53+ let f = File :: open ( format ! (
54+ "/sys/class/pwm/pwmchip{}/pwm{}/{}" ,
55+ chip. number, pin, name
56+ ) ) ?;
5657 Ok ( f)
5758}
5859
@@ -63,11 +64,13 @@ fn pwm_file_parse<T: FromStr>(chip: &PwmChip, pin: u32, name: &str) -> Result<T>
6364 f. read_to_string ( & mut s) ?;
6465 match s. trim ( ) . parse :: < T > ( ) {
6566 Ok ( r) => Ok ( r) ,
66- Err ( _) => Err ( Error :: Unexpected ( format ! ( "Unexpeted value file contents: {:?}" , s) ) ) ,
67+ Err ( _) => Err ( Error :: Unexpected ( format ! (
68+ "Unexpeted value file contents: {:?}" ,
69+ s
70+ ) ) ) ,
6771 }
6872}
6973
70-
7174impl PwmChip {
7275 pub fn new ( number : u32 ) -> Result < PwmChip > {
7376 fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}" , number) ) ?;
@@ -81,13 +84,21 @@ impl PwmChip {
8184 npwm_file. read_to_string ( & mut s) ?;
8285 match s. parse :: < u32 > ( ) {
8386 Ok ( n) => Ok ( n) ,
84- Err ( _) => Err ( Error :: Unexpected ( format ! ( "Unexpected npwm contents: {:?}" , s) ) ) ,
87+ Err ( _) => Err ( Error :: Unexpected ( format ! (
88+ "Unexpected npwm contents: {:?}" ,
89+ s
90+ ) ) ) ,
8591 }
8692 }
8793
8894 pub fn export ( & self , number : u32 ) -> Result < ( ) > {
8995 // only export if not already exported
90- if fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}/pwm{}" , self . number, number) ) . is_err ( ) {
96+ if fs:: metadata ( & format ! (
97+ "/sys/class/pwm/pwmchip{}/pwm{}" ,
98+ self . number, number
99+ ) )
100+ . is_err ( )
101+ {
91102 let path = format ! ( "/sys/class/pwm/pwmchip{}/export" , self . number) ;
92103 let mut export_file = File :: create ( & path) ?;
93104 let _ = export_file. write_all ( format ! ( "{}" , number) . as_bytes ( ) ) ;
@@ -96,7 +107,12 @@ impl PwmChip {
96107 }
97108
98109 pub fn unexport ( & self , number : u32 ) -> Result < ( ) > {
99- if fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}/pwm{}" , self . number, number) ) . is_ok ( ) {
110+ if fs:: metadata ( & format ! (
111+ "/sys/class/pwm/pwmchip{}/pwm{}" ,
112+ self . number, number
113+ ) )
114+ . is_ok ( )
115+ {
100116 let path = format ! ( "/sys/class/pwm/pwmchip{}/unexport" , self . number) ;
101117 let mut export_file = File :: create ( & path) ?;
102118 let _ = export_file. write_all ( format ! ( "{}" , number) . as_bytes ( ) ) ;
@@ -112,15 +128,16 @@ impl Pwm {
112128 pub fn new ( chip : u32 , number : u32 ) -> Result < Pwm > {
113129 let chip: PwmChip = PwmChip :: new ( chip) ?;
114130 Ok ( Pwm {
115- chip : chip,
116- number : number,
117- } )
131+ chip : chip,
132+ number : number,
133+ } )
118134 }
119135
120136 /// Run a closure with the GPIO exported
121137 #[ inline]
122138 pub fn with_exported < F > ( & self , closure : F ) -> Result < ( ) >
123- where F : FnOnce ( ) -> Result < ( ) >
139+ where
140+ F : FnOnce ( ) -> Result < ( ) > ,
124141 {
125142 self . export ( ) ?;
126143 match closure ( ) {
@@ -152,7 +169,7 @@ impl Pwm {
152169 match enable_state {
153170 1 => true ,
154171 0 => false ,
155- _ => panic ! ( "enable != 1|0 should be unreachable" )
172+ _ => panic ! ( "enable != 1|0 should be unreachable" ) ,
156173 }
157174 } )
158175 }
@@ -168,8 +185,7 @@ impl Pwm {
168185 pub fn set_duty_cycle_ns ( & self , duty_cycle_ns : u32 ) -> Result < ( ) > {
169186 // we'll just let the kernel do the validation
170187 let mut duty_cycle_file = pwm_file_wo ( & self . chip , self . number , "duty_cycle" ) ?;
171- duty_cycle_file
172- . write_all ( format ! ( "{}" , duty_cycle_ns) . as_bytes ( ) ) ?;
188+ duty_cycle_file. write_all ( format ! ( "{}" , duty_cycle_ns) . as_bytes ( ) ) ?;
173189 Ok ( ( ) )
174190 }
175191
@@ -181,8 +197,7 @@ impl Pwm {
181197 /// The period of the PWM signal in Nanoseconds
182198 pub fn set_period_ns ( & self , period_ns : u32 ) -> Result < ( ) > {
183199 let mut period_file = pwm_file_wo ( & self . chip , self . number , "period" ) ?;
184- period_file
185- . write_all ( format ! ( "{}" , period_ns) . as_bytes ( ) ) ?;
200+ period_file. write_all ( format ! ( "{}" , period_ns) . as_bytes ( ) ) ?;
186201 Ok ( ( ) )
187202 }
188203}
0 commit comments