|
| 1 | +rust-sysfs-pwm |
| 2 | +============== |
| 3 | + |
| 4 | +[](https://travis-ci.org/posborne/rust-sysfs-pwm) |
| 5 | +[](https://crates.io/crates/sysfs-pwm) |
| 6 | +[](https://github.com/posborne/rust-sysfs-pwm/blob/master/README.md#license) |
| 7 | + |
| 8 | +- [API Documentation](http://posborne.github.io/rust-sysfs-pwm/) |
| 9 | + |
| 10 | +rust-sysfs-pwm is a rust library/crate providing access to the [Linux |
| 11 | +sysfs PWM interface](https://www.kernel.org/doc/Documentation/pwm.txt). |
| 12 | +It seeks to provide an API that is safe, convenient, and efficient. |
| 13 | + |
| 14 | +Install/Use |
| 15 | +----------- |
| 16 | + |
| 17 | +To use `sysfs-pwm`, first add this to your `Cargo.toml`: |
| 18 | + |
| 19 | +```toml |
| 20 | +[dependencies] |
| 21 | +# or latest version |
| 22 | +sysfs-pwm = "^0.1.0" |
| 23 | +``` |
| 24 | + |
| 25 | +Then, add this to your crate root: |
| 26 | + |
| 27 | +```rust |
| 28 | +extern crate sysfs_pwm; |
| 29 | +``` |
| 30 | + |
| 31 | +Example/API |
| 32 | +----------- |
| 33 | + |
| 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 | +... |
| 83 | + |
| 84 | +Cross Compiling |
| 85 | +--------------- |
| 86 | + |
| 87 | +Most likely, the machine you are running on is not your development |
| 88 | +machine (although it could be). In those cases, you will need to |
| 89 | +cross-compile. The [instructions here][rust-cross] provide great details on cross |
| 90 | +compiling for your platform. |
| 91 | + |
| 92 | +[rust-cross]: https://github.com/japaric/rust-cross |
| 93 | + |
| 94 | +Running the Example |
| 95 | +------------------- |
| 96 | + |
| 97 | +Cross-compiling can be done by specifying an appropriate target. You |
| 98 | +can then move that to your device by whatever means and run it. |
| 99 | + |
| 100 | +``` |
| 101 | +$ cargo build --target=arm-unknown-linux-gnueabihf --example breathe |
| 102 | +$ scp target/arm-unknown-linux-gnueabihf/debug/examples/breathe ... |
| 103 | +``` |
| 104 | + |
| 105 | +License |
| 106 | +------- |
| 107 | + |
| 108 | +``` |
| 109 | +Copyright (c) 2016, Paul Osborne <ospbau@gmail.com> |
| 110 | +
|
| 111 | +Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 112 | +http://www.apache.org/license/LICENSE-2.0> or the MIT license |
| 113 | +<LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 114 | +option. This file may not be copied, modified, or distributed |
| 115 | +except according to those terms. |
| 116 | +``` |
0 commit comments