Skip to content

Commit 3874def

Browse files
authored
Merge pull request #3 from ekmecic/master
Minor fixes
2 parents d691889 + 44ca04c commit 3874def

File tree

3 files changed

+43
-56
lines changed

3 files changed

+43
-56
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# rust-sysfs-pwm
22

3-
[![Build Status](https://travis-ci.org/posborne/rust-sysfs-pwm.svg?branch=master)](https://travis-ci.org/posborne/rust-sysfs-pwm)
3+
[![Build Status](https://travis-ci.org/rust-embedded/rust-sysfs-pwm.svg?branch=master)](https://travis-ci.org/posborne/rust-sysfs-pwm)
44
[![Version](https://img.shields.io/crates/v/sysfs-pwm.svg)](https://crates.io/crates/sysfs-pwm)
55
[![License](https://img.shields.io/crates/l/sysfs-pwm.svg)](https://github.com/posborne/rust-sysfs-pwm/blob/master/README.md#license)
66

examples/breathe.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,23 @@ use sysfs_pwm::{Pwm, Result};
1616
const BB_PWM_CHIP: u32 = 0;
1717
const BB_PWM_NUMBER: u32 = 0;
1818

19-
fn pwm_increase_to_max(pwm: &Pwm,
20-
duration_ms: u32,
21-
update_period_ms: u32) -> Result<()> {
19+
fn pwm_increase_to_max(pwm: &Pwm, duration_ms: u32, update_period_ms: u32) -> Result<()> {
2220
let step: f32 = duration_ms as f32 / update_period_ms as f32;
2321
let mut duty_cycle = 0.0;
24-
let period_ns: u32 = try!(pwm.get_period_ns());
22+
let period_ns: u32 = pwm.get_period_ns()?;
2523
while duty_cycle < 1.0 {
26-
try!(pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32));
24+
pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32)?;
2725
duty_cycle += step;
2826
}
2927
pwm.set_duty_cycle_ns(period_ns)
3028
}
3129

32-
fn pwm_decrease_to_minimum(pwm: &Pwm,
33-
duration_ms: u32,
34-
update_period_ms: u32) -> Result<()> {
30+
fn pwm_decrease_to_minimum(pwm: &Pwm, duration_ms: u32, update_period_ms: u32) -> Result<()> {
3531
let step: f32 = duration_ms as f32 / update_period_ms as f32;
3632
let mut duty_cycle = 1.0;
37-
let period_ns: u32 = try!(pwm.get_period_ns());
33+
let period_ns: u32 = pwm.get_period_ns()?;
3834
while duty_cycle > 0.0 {
39-
try!(pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32));
35+
pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32)?;
4036
duty_cycle -= step;
4137
}
4238
pwm.set_duty_cycle_ns(0)
@@ -47,11 +43,12 @@ fn pwm_decrease_to_minimum(pwm: &Pwm,
4743
fn main() {
4844
let pwm = Pwm::new(BB_PWM_CHIP, BB_PWM_NUMBER).unwrap(); // number depends on chip, etc.
4945
pwm.with_exported(|| {
50-
pwm.enable(true).unwrap();
51-
pwm.set_period_ns(20_000).unwrap();
52-
loop {
53-
pwm_increase_to_max(&pwm, 1000, 20).unwrap();
54-
pwm_decrease_to_minimum(&pwm, 1000, 20).unwrap();
55-
}
56-
}).unwrap();
46+
pwm.enable(true).unwrap();
47+
pwm.set_period_ns(20_000).unwrap();
48+
loop {
49+
pwm_increase_to_max(&pwm, 1000, 20).unwrap();
50+
pwm_decrease_to_minimum(&pwm, 1000, 20).unwrap();
51+
}
52+
})
53+
.unwrap();
5754
}

src/lib.rs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//! PWM access under Linux using the PWM sysfs interface
1616
1717
use std::io::prelude::*;
18-
use std::os::unix::prelude::*;
1918
use std::fs::File;
2019
use std::fs;
2120
use 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
4746
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))?;
5350
Ok(f)
5451
}
5552

5653
/// Open the specified entry name as a readable file
5754
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))?;
5956
Ok(f)
6057
}
6158

6259
/// Get the u32 value from the given entry
6360
fn 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

7471
impl 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))),
@@ -90,22 +87,18 @@ impl PwmChip {
9087

9188
pub fn export(&self, number: u32) -> Result<()> {
9289
// 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() {
9691
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)?;
9893
let _ = export_file.write_all(format!("{}", number).as_bytes());
9994
}
10095
Ok(())
10196
}
10297

10398
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() {
107100
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)?;
109102
let _ = export_file.write_all(format!("{}", number).as_bytes());
110103
}
111104
Ok(())
@@ -117,22 +110,21 @@ impl Pwm {
117110
///
118111
/// This function does not export the Pwm pin
119112
pub fn new(chip: u32, number: u32) -> Result<Pwm> {
120-
let chip: PwmChip = try!(PwmChip::new(chip));
113+
let chip: PwmChip = PwmChip::new(chip)?;
121114
Ok(Pwm {
122-
chip: chip,
123-
number: number,
124-
})
115+
chip: chip,
116+
number: number,
117+
})
125118
}
126119

127120
/// Run a closure with the GPIO exported
128121
#[inline]
129122
pub fn with_exported<F>(&self, closure: F) -> Result<()>
130123
where F: FnOnce() -> Result<()>
131124
{
132-
try!(self.export());
125+
self.export()?;
133126
match closure() {
134-
Ok(()) => self.unexport(),
135-
Err(_) => self.unexport(),
127+
Ok(()) | Err(_) => self.unexport(),
136128
}
137129
}
138130

@@ -148,13 +140,9 @@ impl Pwm {
148140

149141
/// Enable/Disable the PWM Signal
150142
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())?;
158146
Ok(())
159147
}
160148

@@ -168,8 +156,9 @@ impl Pwm {
168156
/// Value is in nanoseconds and must be less than the period.
169157
pub fn set_duty_cycle_ns(&self, duty_cycle_ns: u32) -> Result<()> {
170158
// 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())?;
173162
Ok(())
174163
}
175164

@@ -180,8 +169,9 @@ impl Pwm {
180169

181170
/// The period of the PWM signal in Nanoseconds
182171
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())?;
185175
Ok(())
186176
}
187177
}

0 commit comments

Comments
 (0)