9
9
// Portions of this implementation are based on work by Nat Pryce:
10
10
// https://github.com/npryce/rusty-pi/blob/master/src/pi/gpio.rs
11
11
12
- #![ crate_type = "lib" ]
13
- #![ crate_name = "sysfs_pwm" ]
14
-
15
12
//! PWM access under Linux using the PWM sysfs interface
16
13
17
- use std:: io:: prelude:: * ;
18
- use std:: fs:: File ;
19
14
use std:: fs;
15
+ use std:: fs:: File ;
20
16
use std:: fs:: OpenOptions ;
17
+ use std:: io:: prelude:: * ;
21
18
use std:: str:: FromStr ;
22
19
23
20
mod error;
@@ -44,15 +41,19 @@ pub type Result<T> = ::std::result::Result<T, error::Error>;
44
41
45
42
/// Open the specified entry name as a writable file
46
43
fn 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
+ ) ) ?;
50
48
Ok ( f)
51
49
}
52
50
53
51
/// Open the specified entry name as a readable file
54
52
fn 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
+ ) ) ?;
56
57
Ok ( f)
57
58
}
58
59
@@ -63,11 +64,13 @@ fn pwm_file_parse<T: FromStr>(chip: &PwmChip, pin: u32, name: &str) -> Result<T>
63
64
f. read_to_string ( & mut s) ?;
64
65
match s. trim ( ) . parse :: < T > ( ) {
65
66
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
+ ) ) ) ,
67
71
}
68
72
}
69
73
70
-
71
74
impl PwmChip {
72
75
pub fn new ( number : u32 ) -> Result < PwmChip > {
73
76
fs:: metadata ( & format ! ( "/sys/class/pwm/pwmchip{}" , number) ) ?;
@@ -81,13 +84,21 @@ impl PwmChip {
81
84
npwm_file. read_to_string ( & mut s) ?;
82
85
match s. parse :: < u32 > ( ) {
83
86
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
+ ) ) ) ,
85
91
}
86
92
}
87
93
88
94
pub fn export ( & self , number : u32 ) -> Result < ( ) > {
89
95
// 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
+ {
91
102
let path = format ! ( "/sys/class/pwm/pwmchip{}/export" , self . number) ;
92
103
let mut export_file = File :: create ( & path) ?;
93
104
let _ = export_file. write_all ( format ! ( "{}" , number) . as_bytes ( ) ) ;
@@ -96,7 +107,12 @@ impl PwmChip {
96
107
}
97
108
98
109
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
+ {
100
116
let path = format ! ( "/sys/class/pwm/pwmchip{}/unexport" , self . number) ;
101
117
let mut export_file = File :: create ( & path) ?;
102
118
let _ = export_file. write_all ( format ! ( "{}" , number) . as_bytes ( ) ) ;
@@ -112,15 +128,16 @@ impl Pwm {
112
128
pub fn new ( chip : u32 , number : u32 ) -> Result < Pwm > {
113
129
let chip: PwmChip = PwmChip :: new ( chip) ?;
114
130
Ok ( Pwm {
115
- chip : chip,
116
- number : number,
117
- } )
131
+ chip : chip,
132
+ number : number,
133
+ } )
118
134
}
119
135
120
136
/// Run a closure with the GPIO exported
121
137
#[ inline]
122
138
pub fn with_exported < F > ( & self , closure : F ) -> Result < ( ) >
123
- where F : FnOnce ( ) -> Result < ( ) >
139
+ where
140
+ F : FnOnce ( ) -> Result < ( ) > ,
124
141
{
125
142
self . export ( ) ?;
126
143
match closure ( ) {
@@ -152,7 +169,7 @@ impl Pwm {
152
169
match enable_state {
153
170
1 => true ,
154
171
0 => false ,
155
- _ => panic ! ( "enable != 1|0 should be unreachable" )
172
+ _ => panic ! ( "enable != 1|0 should be unreachable" ) ,
156
173
}
157
174
} )
158
175
}
@@ -168,8 +185,7 @@ impl Pwm {
168
185
pub fn set_duty_cycle_ns ( & self , duty_cycle_ns : u32 ) -> Result < ( ) > {
169
186
// we'll just let the kernel do the validation
170
187
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 ( ) ) ?;
173
189
Ok ( ( ) )
174
190
}
175
191
@@ -181,8 +197,7 @@ impl Pwm {
181
197
/// The period of the PWM signal in Nanoseconds
182
198
pub fn set_period_ns ( & self , period_ns : u32 ) -> Result < ( ) > {
183
199
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 ( ) ) ?;
186
201
Ok ( ( ) )
187
202
}
188
203
}
0 commit comments