diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index cb2206f60..35d213292 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - You can now use `Input`, `Output`, `OutputOpenDrain` and `Flex` pins as EXTI and RTCIO wakeup sources (#2095) - Added `Rtc::set_current_time` to allow setting RTC time, and `Rtc::current_time` to getting RTC time while taking into account boot time (#1883) - Added APIs to allow connecting signals through the GPIO matrix. (#2128) +- Implement `TryFrom` for `ledc::timer::config::Duty` (#1984) ### Changed diff --git a/esp-hal/src/ledc/timer.rs b/esp-hal/src/ledc/timer.rs index ef3d7b7b6..6b6c0f8c1 100644 --- a/esp-hal/src/ledc/timer.rs +++ b/esp-hal/src/ledc/timer.rs @@ -117,6 +117,42 @@ pub mod config { Duty20Bit, } + impl TryFrom for Duty { + type Error = (); + + fn try_from(value: u32) -> Result { + Ok(match value { + 1 => Self::Duty1Bit, + 2 => Self::Duty2Bit, + 3 => Self::Duty3Bit, + 4 => Self::Duty4Bit, + 5 => Self::Duty5Bit, + 6 => Self::Duty6Bit, + 7 => Self::Duty7Bit, + 8 => Self::Duty8Bit, + 9 => Self::Duty9Bit, + 10 => Self::Duty10Bit, + 11 => Self::Duty11Bit, + 12 => Self::Duty12Bit, + 13 => Self::Duty13Bit, + 14 => Self::Duty14Bit, + #[cfg(esp32)] + 15 => Self::Duty15Bit, + #[cfg(esp32)] + 16 => Self::Duty16Bit, + #[cfg(esp32)] + 17 => Self::Duty17Bit, + #[cfg(esp32)] + 18 => Self::Duty18Bit, + #[cfg(esp32)] + 19 => Self::Duty19Bit, + #[cfg(esp32)] + 20 => Self::Duty20Bit, + _ => Err(())?, + }) + } + } + /// Timer configuration #[derive(Copy, Clone)] pub struct Config {