Struct rppal::pwm::Pwm

source ·
pub struct Pwm { /* private fields */ }
Expand description

Provides access to the Raspberry Pi’s PWM peripheral.

Before using Pwm, make sure the selected PWM channel has been configured and activated. More information can be found here.

The embedded-hal PwmPin trait implementation for Pwm can be enabled by specifying the optional hal feature in the dependency declaration for the rppal crate.

The unproven embedded-hal Pwm trait implementation for Pwm can be enabled by specifying the optional hal-unproven feature in the dependency declaration for the rppal crate.

Implementations§

source§

impl Pwm

source

pub fn new(channel: Channel) -> Result<Pwm>

Constructs a new Pwm.

new doesn’t change the channel’s period, pulse width or polarity. The channel will remain disabled until enable is called.

source

pub fn with_period( channel: Channel, period: Duration, pulse_width: Duration, polarity: Polarity, enabled: bool ) -> Result<Pwm>

Constructs a new Pwm using the specified settings.

period indicates the time it takes for the PWM channel to complete one cycle.

pulse_width indicates the amount of time the PWM channel is active during a single period.

polarity configures the active logic level as either high (Normal) or low (Inverse).

enabled enables PWM on the selected channel. If enabled is set to false, the channel will remain disabled until enable is called.

This method will fail if period is shorter than pulse_width.

source

pub fn with_frequency( channel: Channel, frequency: f64, duty_cycle: f64, polarity: Polarity, enabled: bool ) -> Result<Pwm>

Constructs a new Pwm using the specified settings.

with_frequency is a convenience method that converts frequency to a period, and calculates the duty cycle as a percentage of the frequency.

frequency is specified in hertz (Hz).

duty_cycle is specified as a floating point value between 0.0 (0%) and 1.0 (100%).

polarity configures the active logic level as either high (Normal) or low (Inverse).

enabled enables PWM on the selected channel. If enabled is set to false, the channel will remain disabled until enable is called.

source

pub fn period(&self) -> Result<Duration>

Returns the period.

source

pub fn set_period(&self, period: Duration) -> Result<()>

Sets the period.

period indicates the time it takes for the PWM channel to complete one cycle.

This method will fail if period is shorter than the current pulse width.

source

pub fn pulse_width(&self) -> Result<Duration>

Returns the pulse width.

source

pub fn set_pulse_width(&self, pulse_width: Duration) -> Result<()>

Sets the pulse width.

pulse_width indicates the amount of time the PWM channel is active during a single period.

This method will fail if pulse_width is longer than the current period.

source

pub fn frequency(&self) -> Result<f64>

Returns the frequency.

frequency is a convenience method that calculates the frequency in hertz (Hz) based on the configured period.

source

pub fn set_frequency(&self, frequency: f64, duty_cycle: f64) -> Result<()>

Sets the frequency and duty cycle.

set_frequency is a convenience method that converts frequency to a period, and calculates the duty cycle as a percentage of the frequency.

frequency is specified in hertz (Hz).

duty_cycle is specified as a floating point value between 0.0 (0%) and 1.0 (100%).

source

pub fn duty_cycle(&self) -> Result<f64>

Returns the duty cycle.

duty_cycle is a convenience method that calculates the duty cycle as a floating point value between 0.0 (0%) and 1.0 (100%) based on the configured period and pulse width.

source

pub fn set_duty_cycle(&self, duty_cycle: f64) -> Result<()>

Sets the duty cycle.

set_duty_cycle is a convenience method that converts duty_cycle to a pulse width based on the configured period.

duty_cycle is specified as a floating point value between 0.0 (0%) and 1.0 (100%).

source

pub fn polarity(&self) -> Result<Polarity>

Returns the polarity.

source

pub fn set_polarity(&self, polarity: Polarity) -> Result<()>

Sets the polarity.

polarity configures the active logic level as either high (Normal) or low (Inverse).

source

pub fn is_enabled(&self) -> Result<bool>

Returns true if the PWM channel is enabled.

source

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

Enables the PWM channel.

source

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

Disables the PWM channel.

source

pub fn reset_on_drop(&self) -> bool

Returns the value of reset_on_drop.

source

pub fn set_reset_on_drop(&mut self, reset_on_drop: bool)

When enabled, disables the PWM channel when the Pwm instance goes out of scope. By default, this is set to true.

Note

Drop methods aren’t called when a process is abnormally terminated, for instance when a user presses Ctrl + C, and the SIGINT signal isn’t caught. You can catch those using crates such as simple_signal.

Trait Implementations§

source§

impl Debug for Pwm

source§

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

Formats the value using the given formatter. Read more
source§

impl Drop for Pwm

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.