Struct Pwm

Source
pub struct Pwm { /* private fields */ }

Implementations§

Source§

impl Pwm

Source

pub fn new(chip: u32, number: u32) -> Result<Pwm>

Create a new Pwm wiht the provided chip/number

This function does not export the Pwm pin

Examples found in repository?
examples/breathe.rs (line 48)
47fn main() {
48    let pwm = Pwm::new(BB_PWM_CHIP, BB_PWM_NUMBER).unwrap(); // number depends on chip, etc.
49    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();
57}
Source

pub fn with_exported<F>(&self, closure: F) -> Result<()>
where F: FnOnce() -> Result<()>,

Run a closure with the GPIO exported

Examples found in repository?
examples/breathe.rs (lines 49-56)
47fn main() {
48    let pwm = Pwm::new(BB_PWM_CHIP, BB_PWM_NUMBER).unwrap(); // number depends on chip, etc.
49    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();
57}
Source

pub fn export(&self) -> Result<()>

Export the Pwm for use

Source

pub fn unexport(&self) -> Result<()>

Unexport the PWM

Source

pub fn enable(&self, enable: bool) -> Result<()>

Enable/Disable the PWM Signal

Examples found in repository?
examples/breathe.rs (line 50)
47fn main() {
48    let pwm = Pwm::new(BB_PWM_CHIP, BB_PWM_NUMBER).unwrap(); // number depends on chip, etc.
49    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();
57}
Source

pub fn get_duty_cycle_ns(&self) -> Result<u32>

Get the currently configured duty_cycle in nanoseconds

Source

pub fn set_duty_cycle_ns(&self, duty_cycle_ns: u32) -> Result<()>

The active time of the PWM signal

Value is in nanoseconds and must be less than the period.

Examples found in repository?
examples/breathe.rs (line 26)
19fn pwm_increase_to_max(pwm: &Pwm,
20                       duration_ms: u32,
21                       update_period_ms: u32) -> Result<()> {
22    let step: f32 = duration_ms as f32 / update_period_ms as f32;
23    let mut duty_cycle = 0.0;
24    let period_ns: u32 = try!(pwm.get_period_ns());
25    while duty_cycle < 1.0 {
26        try!(pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32));
27        duty_cycle += step;
28    }
29    pwm.set_duty_cycle_ns(period_ns)
30}
31
32fn pwm_decrease_to_minimum(pwm: &Pwm,
33                           duration_ms: u32,
34                           update_period_ms: u32) -> Result<()> {
35    let step: f32 = duration_ms as f32 / update_period_ms as f32;
36    let mut duty_cycle = 1.0;
37    let period_ns: u32 = try!(pwm.get_period_ns());
38    while duty_cycle > 0.0 {
39        try!(pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32));
40        duty_cycle -= step;
41    }
42    pwm.set_duty_cycle_ns(0)
43}
Source

pub fn get_period_ns(&self) -> Result<u32>

Get the currently configured period in nanoseconds

Examples found in repository?
examples/breathe.rs (line 24)
19fn pwm_increase_to_max(pwm: &Pwm,
20                       duration_ms: u32,
21                       update_period_ms: u32) -> Result<()> {
22    let step: f32 = duration_ms as f32 / update_period_ms as f32;
23    let mut duty_cycle = 0.0;
24    let period_ns: u32 = try!(pwm.get_period_ns());
25    while duty_cycle < 1.0 {
26        try!(pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32));
27        duty_cycle += step;
28    }
29    pwm.set_duty_cycle_ns(period_ns)
30}
31
32fn pwm_decrease_to_minimum(pwm: &Pwm,
33                           duration_ms: u32,
34                           update_period_ms: u32) -> Result<()> {
35    let step: f32 = duration_ms as f32 / update_period_ms as f32;
36    let mut duty_cycle = 1.0;
37    let period_ns: u32 = try!(pwm.get_period_ns());
38    while duty_cycle > 0.0 {
39        try!(pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32));
40        duty_cycle -= step;
41    }
42    pwm.set_duty_cycle_ns(0)
43}
Source

pub fn set_period_ns(&self, period_ns: u32) -> Result<()>

The period of the PWM signal in Nanoseconds

Examples found in repository?
examples/breathe.rs (line 51)
47fn main() {
48    let pwm = Pwm::new(BB_PWM_CHIP, BB_PWM_NUMBER).unwrap(); // number depends on chip, etc.
49    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();
57}

Trait Implementations§

Source§

impl Debug for Pwm

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Pwm

§

impl RefUnwindSafe for Pwm

§

impl Send for Pwm

§

impl Sync for Pwm

§

impl Unpin for Pwm

§

impl UnwindSafe for Pwm

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.