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, and the datasheet rules out loading the same count into both, so 0 % and 100 % are only ever those flags.
§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.on(), half.off()), (0, 2048));
// What a channel's four registers hold reads back as the same setting.
assert_eq!(Pwm::from_bytes(&half.bytes()), half);
// Fully off is its own encoding, and a zero duty is that encoding.
assert_eq!(Pwm::duty(0), Pwm::full_off());Implementations§
Source§impl Pwm
impl Pwm
Sourcepub fn from_counts(on: u16, off: u16) -> Pwm
pub fn from_counts(on: u16, off: u16) -> Pwm
Sourcepub fn duty(off: u16) -> Pwm
pub fn duty(off: u16) -> Pwm
Builds a setting with no phase delay: on at count 0, off at off.
The datasheet says the on and off counts should never hold the same value, which a count of 0 would load, so 0 is the full-off setting, and a count of 4096 or more the full-on one.
§Arguments
off- the count at which the output goes low, which sets the duty cycle.
§Returns
The PWM setting.
Sourcepub fn servo(pulse_micros: u32, update_rate_hz: u32) -> Pwm
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.
Sourcepub fn full_off() -> Pwm
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.
Sourcepub fn bytes(self) -> [u8; 4]
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].
Sourcepub fn from_bytes(bytes: &[u8; 4]) -> Pwm
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.