Skip to content

Commit

Permalink
RMT: Implement builder-lite for {Rx|Tx}ChannelConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
wisp3rwind committed Jan 16, 2025
1 parent 4b66e3d commit c95a24e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 29 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
- RMT: `TxChannelConfig` and `RxChannelConfig` now support the builder-lite pattern (#2978)

### Fixed

Expand Down
25 changes: 25 additions & 0 deletions esp-hal/MIGRATING-0.23.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Migration Guide from v0.23.x to v?.??.?

## RMT changes

The `TxChannelConfig` and `RxChannelConfig` structs now support the builder-lite pattern.
Thus, explicit initialization of all fields can be replaced by only the necessary setter methods:

```diff
let mut channel = rmt
.channel0
.configure(
peripherals.GPIO1,
- TxChannelConfig {
- clk_divider: 1,
- idle_output_level: false,
- idle_output: false,
- carrier_modulation: false,
- carrier_high: 1,
- carrier_low: 1,
- carrier_level: false,
- },
+ TxChannelConfig::default().with_clk_divider(1)
)
.unwrap();
```
34 changes: 14 additions & 20 deletions esp-hal/src/rmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@
//! .channel0
//! .configure(
//! peripherals.GPIO1,
//! TxChannelConfig {
//! clk_divider: 1,
//! idle_output_level: false,
//! idle_output: false,
//! carrier_modulation: false,
//! carrier_high: 1,
//! carrier_low: 1,
//! carrier_level: false,
//! },
//! TxChannelConfig::default()
//! .with_clk_divider(1)
//! .with_idle_output_level(false)
//! .with_idle_output(false)
//! .with_carrier_modulation(false)
//! .with_carrier_high(1)
//! .with_carrier_low(1)
//! .with_carrier_level(false),
//! )
//! .unwrap();
//! # }
Expand All @@ -87,10 +86,7 @@
#![cfg_attr(not(esp32h2), doc = "let freq = 80.MHz();")]
//! let rmt = Rmt::new(peripherals.RMT, freq).unwrap();
//!
//! let tx_config = TxChannelConfig {
//! clk_divider: 255,
//! ..TxChannelConfig::default()
//! };
//! let tx_config = TxChannelConfig::default().with_clk_divider(255);
//!
//! let mut channel = rmt
//! .channel0
Expand Down Expand Up @@ -127,11 +123,9 @@
#![cfg_attr(not(esp32h2), doc = "let freq = 80.MHz();")]
//! let rmt = Rmt::new(peripherals.RMT, freq).unwrap();
//!
//! let rx_config = RxChannelConfig {
//! clk_divider: 1,
//! idle_threshold: 10000,
//! ..RxChannelConfig::default()
//! };
//! let rx_config = RxChannelConfig::default()
//! .with_clk_divider(1)
//! .with_idle_threshold(10000);
#![cfg_attr(
any(esp32, esp32s2),
doc = "let mut channel = rmt.channel0.configure(peripherals.GPIO4, rx_config).unwrap();"
Expand Down Expand Up @@ -310,7 +304,7 @@ impl PulseCode for u32 {
}

/// Channel configuration for TX channels
#[derive(Debug, Copy, Clone, Default)]
#[derive(Debug, Copy, Clone, Default, procmacros::BuilderLite)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TxChannelConfig {
/// Channel's clock divider
Expand All @@ -330,7 +324,7 @@ pub struct TxChannelConfig {
}

/// Channel configuration for RX channels
#[derive(Debug, Copy, Clone, Default)]
#[derive(Debug, Copy, Clone, Default, procmacros::BuilderLite)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct RxChannelConfig {
/// Channel's clock divider
Expand Down
8 changes: 3 additions & 5 deletions examples/src/bin/embassy_rmt_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ async fn main(spawner: Spawner) {
};

let rmt = Rmt::new(peripherals.RMT, freq).unwrap().into_async();
let rx_config = RxChannelConfig {
clk_divider: 255,
idle_threshold: 10000,
..RxChannelConfig::default()
};
let rx_config = RxChannelConfig::default()
.with_clk_divider(255)
.with_idle_threshold(10000);

cfg_if::cfg_if! {
if #[cfg(any(feature = "esp32", feature = "esp32s2"))] {
Expand Down
5 changes: 1 addition & 4 deletions examples/src/bin/embassy_rmt_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ async fn main(_spawner: Spawner) {
.channel0
.configure(
peripherals.GPIO4,
TxChannelConfig {
clk_divider: 255,
..TxChannelConfig::default()
},
TxChannelConfig::default().with_clk_divider(255),
)
.unwrap();

Expand Down

0 comments on commit c95a24e

Please sign in to comment.