Skip to content

Commit 03081de

Browse files
bors[bot]posborne
andcommitted
Merge #8
8: Various Housekeeping and Post Transfer Updates r=nastevens a=posborne Co-authored-by: Paul Osborne <osbpau@gmail.com>
2 parents 182d51b + 98c972f commit 03081de

File tree

6 files changed

+59
-42
lines changed

6 files changed

+59
-42
lines changed

.travis.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
language: rust
22
rust:
3-
- 1.2.0
4-
- 1.3.0
5-
- 1.4.0
6-
- 1.5.0
7-
- 1.6.0
3+
- 1.28.0
4+
- stable
85
- beta
96
- nightly
107

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[package]
22
name = "sysfs-pwm"
3-
version = "0.1.1"
4-
authors = ["Paul Osborne <osbpau@gmail.com>"]
3+
version = "0.2.0"
4+
authors = ["Rust Embedded WG Linux Team <embedded-linux@teams.rust-embedded.org>",
5+
"Paul Osborne <osbpau@gmail.com>"]
56
license = "MIT/Apache-2.0"
67
homepage = "https://github.com/posborne/rust-sysfs-pwm"
78
documentation = "https://posborne.github.io/rust-sysfs-pwm"

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# rust-sysfs-pwm
22

3-
[![Build Status](https://travis-ci.org/rust-embedded/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/rust-embedded/rust-sysfs-pwm)
44
[![Version](https://img.shields.io/crates/v/sysfs-pwm.svg)](https://crates.io/crates/sysfs-pwm)
5-
[![License](https://img.shields.io/crates/l/sysfs-pwm.svg)](https://github.com/posborne/rust-sysfs-pwm/blob/master/README.md#license)
5+
[![License](https://img.shields.io/crates/l/sysfs-pwm.svg)](README.md#license)
66

7-
- [API Documentation](http://posborne.github.io/rust-sysfs-pwm/)
7+
- [API Documentation](https://docs.rs/sysfs-pwm)
88

99
rust-sysfs-pwm is a rust library/crate providing access to the [Linux
1010
sysfs PWM interface](https://www.kernel.org/doc/Documentation/pwm.txt).
@@ -26,6 +26,11 @@ Then, add this to your crate root:
2626
extern crate sysfs_pwm;
2727
```
2828

29+
## MSRV (Minimum Supported Rust Version)
30+
31+
This crate is guaranteed to compile on stable Rust 1.28.0 and up. It *might*
32+
compile with older versions but that may change in any new patch release.
33+
2934
## Example/API
3035

3136
The main API consists of a Pwm struct with the following methods:

examples/breathe.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ fn pwm_decrease_to_minimum(pwm: &Pwm, duration_ms: u32, update_period_ms: u32) -
4343
fn main() {
4444
let pwm = Pwm::new(BB_PWM_CHIP, BB_PWM_NUMBER).unwrap(); // number depends on chip, etc.
4545
pwm.with_exported(|| {
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();
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();
5454
}

src/error.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ impl fmt::Display for Error {
4646
}
4747
}
4848

49-
5049
impl convert::From<io::Error> for Error {
5150
fn from(e: io::Error) -> Error {
5251
Error::Io(e)

src/lib.rs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
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;
1914
use std::fs;
15+
use std::fs::File;
2016
use std::fs::OpenOptions;
17+
use std::io::prelude::*;
2118
use std::str::FromStr;
2219

2320
mod 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
4643
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+
))?;
5048
Ok(f)
5149
}
5250

5351
/// Open the specified entry name as a readable file
5452
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+
))?;
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-
7174
impl 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

Comments
 (0)