Skip to main content

Pwm

Struct Pwm 

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

A channel’s PWM setting: when in the period it turns on and when it turns off.

The PCA9685 counts from 0 to 4095 each period and lets a channel turn on at one count and off at another, so duty and phase are both programmable. The special full-on and full-off states are encoded in a dedicated bit rather than as counts.

§Examples

use pamoja_actuators::pca9685::Pwm;

// Half brightness with no phase delay: on at count 0, off at the midpoint.
let half = Pwm::duty(2048);
assert_eq!(half.bytes(), [0x00, 0x00, 0x00, 0x08]);

// Fully off is its own encoding, not a zero duty.
assert_eq!(Pwm::full_off().bytes(), [0x00, 0x00, 0x00, 0x10]);

Implementations§

Source§

impl Pwm

Source

pub fn from_counts(on: u16, off: u16) -> Pwm

Builds a setting from explicit on and off counts.

§Arguments
  • on - the count at which the output goes high, 0..=4095.
  • off - the count at which it goes low, 0..=4095.
§Returns

The PWM setting; counts are masked to 12 bits.

Source

pub fn duty(off: u16) -> Pwm

Builds a setting with no phase delay: on at count 0, off at off.

§Arguments
  • off - the count at which the output goes low, which sets the duty cycle.
§Returns

The PWM setting.

Source

pub fn servo(pulse_micros: u32, update_rate_hz: u32) -> Pwm

Builds the setting that drives a hobby servo to a given pulse width.

A servo reads the high-pulse width each period; the count is that width as a fraction of the period, pulse * 4096 / period. Typical travel is about 1000 to 2000 microseconds at a 50 Hz update rate.

§Arguments
  • pulse_micros - the high-pulse width in microseconds.
  • update_rate_hz - the PWM frequency the controller is set to.
§Returns

The PWM setting for that pulse width.

Source

pub fn full_on() -> Pwm

The setting that holds the output continuously high.

§Returns

The full-on setting.

Source

pub fn full_off() -> Pwm

The setting that holds the output continuously low.

§Returns

The full-off setting, the power-on state of every channel.

Source

pub fn bytes(self) -> [u8; 4]

Returns the four register bytes for this setting.

The order is on-low, on-high, off-low, off-high, matching the channel’s four consecutive registers; the full-on and full-off flags ride in bit 4 of the high bytes.

§Returns

[on_low, on_high, off_low, off_high].

Source

pub fn from_bytes(bytes: &[u8; 4]) -> Pwm

Reads a setting back from the four register bytes a channel holds.

The inverse of bytes, so a caller can read a channel back off the bus and see what it is set to rather than decoding the registers by hand.

§Arguments
  • bytes - the four channel registers, [on_low, on_high, off_low, off_high].
§Returns

The PWM setting those registers hold, full-on and full-off flags included.

Source

pub fn on(self) -> u16

Returns the count at which the output goes high.

§Returns

The on count, including the full-on flag in bit 12 when the output is held high.

Source

pub fn off(self) -> u16

Returns the count at which the output goes low.

§Returns

The off count, including the full-off flag in bit 12 when the output is held low.

Trait Implementations§

Source§

impl Clone for Pwm

Source§

fn clone(&self) -> Pwm

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Pwm

Source§

impl Debug for Pwm

Source§

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

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

impl Eq for Pwm

Source§

impl PartialEq for Pwm

Source§

fn eq(&self, other: &Pwm) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Pwm

Auto Trait Implementations§

§

impl Freeze for Pwm

§

impl RefUnwindSafe for Pwm

§

impl Send for Pwm

§

impl Sync for Pwm

§

impl Unpin for Pwm

§

impl UnsafeUnpin 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.