Skip to content

Commit

Permalink
rmt: Use gpio::Level for PulseCode::level{1|2}
Browse files Browse the repository at this point in the history
  • Loading branch information
wisp3rwind committed Jan 16, 2025
1 parent 9e90292 commit 6fef201
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- RMT: `TxChannelConfig` and `RxChannelConfig` now support the builder-lite pattern (#2978)
- RMT: Some fields of `TxChannelConfig` and `RxChannelConfig` are now `gpio::Level`-valued instead of `bool` (#2989)
- RMT: The `PulseCode` trait now uses `gpio::Level` to specify output levels instead of `bool` (#2989)

### Fixed

Expand Down
10 changes: 10 additions & 0 deletions esp-hal/MIGRATING-0.23.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ take the more descriptive `gpio::Level` type instead of a plain `bool`.
.unwrap();
```

### `PulseCode` now uses `gpio::Level` instead of `bool` to specify output levels

The more descriptive `gpio::Level` enum is now used to specify output levels of `PulseCode`:

```diff
+ use esp_hal::gpio::Level;
+
- let code = PulseCode::new(true, 200, false, 50);
+ let code = PulseCode::new(Level::High, 200, Level::Low, 50);
```
22 changes: 11 additions & 11 deletions esp-hal/src/rmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
//!
//! let delay = Delay::new();
//!
//! let mut data = [PulseCode::new(true, 200, false, 50); 20];
//! data[data.len() - 2] = PulseCode::new(true, 3000, false, 500);
//! let mut data = [PulseCode::new(Level::High, 200, Level::Low, 50); 20];
//! data[data.len() - 2] = PulseCode::new(Level::High, 3000, Level::Low, 500);
//! data[data.len() - 1] = PulseCode::empty();
//!
//! loop {
Expand Down Expand Up @@ -259,22 +259,22 @@ pub trait PulseCode: crate::private::Sealed {
fn reset(&mut self);

/// Logical output level in the first pulse code interval
fn level1(&self) -> bool;
fn level1(&self) -> Level;

/// Length of the first pulse code interval (in clock cycles)
fn length1(&self) -> u16;

/// Logical output level in the second pulse code interval
fn level2(&self) -> bool;
fn level2(&self) -> Level;

/// Length of the second pulse code interval (in clock cycles)
fn length2(&self) -> u16;
}

impl PulseCode for u32 {
fn new(level1: bool, length1: u16, level2: bool, length2: u16) -> Self {
let level1 = ((level1 as u32) << 15) | (length1 as u32 & 0b111_1111_1111_1111);
let level2 = ((level2 as u32) << 15) | (length2 as u32 & 0b111_1111_1111_1111);
fn new(level1: Level, length1: u16, level2: Level, length2: u16) -> Self {
let level1 = ((bool::from(level1) as u32) << 15) | (length1 as u32 & 0b111_1111_1111_1111);
let level2 = ((bool::from(level2) as u32) << 15) | (length2 as u32 & 0b111_1111_1111_1111);
level1 | (level2 << 16)
}

Expand All @@ -286,16 +286,16 @@ impl PulseCode for u32 {
*self = 0
}

fn level1(&self) -> bool {
self & (1 << 15) != 0
fn level1(&self) -> Level {
(self & (1 << 15) != 0).into()
}

fn length1(&self) -> u16 {
(self & 0b111_1111_1111_1111) as u16
}

fn level2(&self) -> bool {
self & (1 << 31) != 0
fn level2(&self) -> Level {
(self & (1 << 31) != 0).into()
}

fn length2(&self) -> u16 {
Expand Down
10 changes: 8 additions & 2 deletions examples/src/bin/embassy_rmt_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ async fn main(spawner: Spawner) {
}

let count = WIDTH / (total / entry.length1() as usize);
let c = if entry.level1() { '-' } else { '_' };
let c = match entry.level1() {
Level::High => '-',
Level::Low => '_',
};
for _ in 0..count + 1 {
print!("{}", c);
}
Expand All @@ -105,7 +108,10 @@ async fn main(spawner: Spawner) {
}

let count = WIDTH / (total / entry.length2() as usize);
let c = if entry.level2() { '-' } else { '_' };
let c = match entry.level2() {
Level::High => '-',
Level::Low => '_',
};
for _ in 0..count + 1 {
print!("{}", c);
}
Expand Down
5 changes: 3 additions & 2 deletions examples/src/bin/embassy_rmt_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_hal::{
gpio::Level,
rmt::{PulseCode, Rmt, TxChannelAsync, TxChannelConfig, TxChannelCreatorAsync},
time::RateExtU32,
timer::timg::TimerGroup,
Expand Down Expand Up @@ -47,9 +48,9 @@ async fn main(_spawner: Spawner) {
)
.unwrap();

let mut data = [PulseCode::new(true, 200, false, 50); 20];
let mut data = [PulseCode::new(Level::High, 200, Level::Low, 50); 20];

data[data.len() - 2] = PulseCode::new(true, 3000, false, 500);
data[data.len() - 2] = PulseCode::new(Level::High, 3000, Level::Low, 500);
data[data.len() - 1] = PulseCode::empty();

loop {
Expand Down

0 comments on commit 6fef201

Please sign in to comment.