From a787a13441c32b6fa7eebbf99c63aeb6cb5a5568 Mon Sep 17 00:00:00 2001 From: Anthony Grondin <104731965+AnthonyGrondin@users.noreply.github.com> Date: Tue, 17 Sep 2024 09:44:12 -0400 Subject: [PATCH] Implement `TryFrom` for `ledc::timer::config::Duty` (#1984) * feat(buzzer): Add `esp-hal-buzzer` to drive a piezo-electric buzzer. Provides a driver for a piezo-electric buzzer by abstracting LEDC and offering a user-friendly API for a buzzer * Move songs into example and refactor songs module. * Move `esp-hal-buzzer` to https://github.com/esp-rs/esp-hal-community/ * Update `CHANGELOG.md` --------- Co-authored-by: Jesse Braham --- esp-hal/CHANGELOG.md | 1 + esp-hal/src/ledc/timer.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) 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 {