# _embedded_hal_Pwm in k210_hal::prelude - Rust
Pulse Width Modulation
Keywords: rust, rustlang, rust-lang, _embedded_hal_Pwm

# Trait k210_hal::prelude::_embedded_hal_Pwm

source · [−]

    pub trait _embedded_hal_Pwm {
        type Error;
        type Channel;
        type Time;
        type Duty;
        fn try_disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
        fn try_enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>;
        fn try_get_period(&self) -> Result<Self::Time, Self::Error>;
        fn try_get_duty(
            &self,
            channel: Self::Channel
        ) -> Result<Self::Duty, Self::Error>;
        fn try_get_max_duty(&self) -> Result<Self::Duty, Self::Error>;
        fn try_set_duty(
            &mut self,
            channel: Self::Channel,
            duty: Self::Duty
        ) -> Result<(), Self::Error>;
        fn try_set_period<P>(&mut self, period: P) -> Result<(), Self::Error>
        where
            P: Into<Self::Time>;
    }

Expand description

Pulse Width Modulation

## Examples

Use this interface to control the power output of some actuator

    extern crate embedded_hal as hal;

    use hal::prelude::*;

    fn main() {
        let mut pwm: Pwm1 = {
            // ..
        };

        pwm.try_set_period(1.khz()).unwrap();

        let max_duty = pwm.try_get_max_duty().unwrap();

        // brightest LED
        pwm.try_set_duty(Channel::_1, max_duty).unwrap();

        // dimmer LED
        pwm.try_set_duty(Channel::_2, max_duty / 4).unwrap();
    }

## Associated Types

source

#### type Error

Enumeration of `Pwm` errors

source

#### type Channel

Enumeration of channels that can be used with this `Pwm` interface

If your `Pwm` interface has no channels you can use the type `()` here

source

#### type Time

A time unit that can be converted into a human time unit (e.g. seconds)

source

#### type Duty

Type for the `duty` methods

The implementer is free to choose a float / percentage representation (e.g. `0.0 .. 1.0`) or an integer representation (e.g. `0 .. 65535`)

## Required methods

source

#### fn try_disable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>

Disables a PWM `channel`

source

#### fn try_enable(&mut self, channel: Self::Channel) -> Result<(), Self::Error>

Enables a PWM `channel`

source

#### fn try_get_period(&self) -> Result<Self::Time, Self::Error>

Returns the current PWM period

source

#### fn try_get_duty(
&self,
channel: Self::Channel
) -> Result<Self::Duty, Self::Error>

Returns the current duty cycle

source

#### fn try_get_max_duty(&self) -> Result<Self::Duty, Self::Error>

Returns the maximum duty cycle value

source

#### fn try_set_duty(
&mut self,
channel: Self::Channel,
duty: Self::Duty
) -> Result<(), Self::Error>

Sets a new duty cycle

source

#### fn try_set_period<P>(&mut self, period: P) -> Result<(), Self::Error> where
P: Into<Self::Time>,

Sets a new PWM period

## Implementors