Skip to content

Commit c0751dd

Browse files
committed
examples: get breate example compiling and update docs
Signed-off-by: Paul Osborne <osbpau@gmail.com>
1 parent f26bfa8 commit c0751dd

File tree

2 files changed

+66
-49
lines changed

2 files changed

+66
-49
lines changed

README.md

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,55 +31,19 @@ extern crate sysfs_pwm;
3131
Example/API
3232
-----------
3333

34-
Controlling a PWM (this example works on the Rasbperry Pi):
35-
36-
```rust
37-
extern crate sysfs_pwm;
38-
use sysfs_pwm::{Pwm};
39-
40-
const RPI_PWM_CHIP: u32 = 1;
41-
42-
fn pwm_increase_to_max(pwm: &Pwm,
43-
duration_ms: u32,
44-
update_period: u32) {
45-
let mut step: f32 = duration_ms / update_period;
46-
let duty_cycle: f32 = 0.0;
47-
while duty_cycle < 1.0 {
48-
pwm.set_duty_cycle(duty_cycle);
49-
duty_cycle += step;
50-
}
51-
pwm.set_duty_cycle(1.0);
52-
}
53-
54-
fn pwm_decrease_to_minimum(pwm: &Pwm,
55-
duration_ms: u32,
56-
update_period: u32) {
57-
let mut step: f32 = duration_ms / update_period;
58-
let mut duty_cycle = 1.0;
59-
while duty_cycle > 0.0 {
60-
pwm.set_duty_cycle(duty_cycle);
61-
duty_cycle -= step;
62-
}
63-
pwm.set_duty_cycle(0.0)
64-
}
65-
66-
/// Make an LED "breathe" by increasing and
67-
/// decreasing the brightness
68-
fn main() {
69-
let my_pwm = Pwm::new(1, 127); // number depends on chip, etc.
70-
my_pwm.with_exported(|| {
71-
loop {
72-
pwm_increase_to_max(pwm, 1000, 20);
73-
pwm_decrease_to_minimum(pwm, 1000, 20);
74-
}
75-
}).unwrap();
76-
}
77-
```
78-
79-
Features
80-
--------
81-
82-
...
34+
The main API consists of a Pwm struct with the following methods:
35+
* `Pwm::new` - Create a Pwm instance
36+
* `pwm.with_exported` - Execute a block with the Pwm exported
37+
* `pwm.set_active` - Enable/Disable the Pwm
38+
* `pwm.get_duty_cycle` - Get duty cycle as percentage of period
39+
* `pwm.set_duty_cycle` - Set duty cycle as percentage of period
40+
* `pwm.get_duty_cycle_ns` - Get duty cycle in nanoseconds
41+
* `pwm.set_duty_cycle_ns` - Set duty cyle in nanoseconds
42+
* `pwm.get_period_ns` - Get the Pwm period in nanoseconds
43+
* `pwm.set_period_ns` - Set the Pwm period in nanoseconds
44+
45+
Check out the [Breathing LED](examples/breathe.rs) example for a usage
46+
example.
8347

8448
Cross Compiling
8549
---------------

examples/breathe.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2016, Paul Osborne <osbpau@gmail.com>
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
//
9+
// Portions of this implementation are based on work by Nat Pryce:
10+
// https://github.com/npryce/rusty-pi/blob/master/src/pi/gpio.rs
11+
12+
extern crate sysfs_pwm;
13+
use sysfs_pwm::{Pwm, Result};
14+
15+
const RPI_PWM_CHIP: u32 = 1;
16+
const RPI_PWM_NUMBER: u32 = 127;
17+
18+
fn pwm_increase_to_max(pwm: &Pwm,
19+
duration_ms: u32,
20+
update_period: u32) -> Result<()> {
21+
let step: f32 = duration_ms as f32 / update_period as f32;
22+
let mut duty_cycle: f32 = 0.0;
23+
while duty_cycle < 1.0 {
24+
try!(pwm.set_duty_cycle(duty_cycle));
25+
duty_cycle += step;
26+
}
27+
pwm.set_duty_cycle(1.0)
28+
}
29+
30+
fn pwm_decrease_to_minimum(pwm: &Pwm,
31+
duration_ms: u32,
32+
update_period: u32) -> Result<()> {
33+
let step: f32 = duration_ms as f32 / update_period as f32;
34+
let mut duty_cycle = 1.0;
35+
while duty_cycle > 0.0 {
36+
try!(pwm.set_duty_cycle(duty_cycle));
37+
duty_cycle -= step;
38+
}
39+
pwm.set_duty_cycle(0.0)
40+
}
41+
42+
/// Make an LED "breathe" by increasing and
43+
/// decreasing the brightness
44+
fn main() {
45+
let pwm = Pwm::new(RPI_PWM_CHIP, RPI_PWM_NUMBER).unwrap(); // number depends on chip, etc.
46+
pwm.with_exported(|| {
47+
pwm.set_active(true).unwrap();
48+
loop {
49+
pwm_increase_to_max(&pwm, 1000, 20).unwrap();
50+
pwm_decrease_to_minimum(&pwm, 1000, 20).unwrap();
51+
}
52+
}).unwrap();
53+
}

0 commit comments

Comments
 (0)