Skip to content

Commit

Permalink
rmt: Allow working with raw rmt_item32_t for efficiency (#348)
Browse files Browse the repository at this point in the history
* rmt: Allow using a &[rmt_item32_t] as a trivial signal

* rmt: Make a rmt_item32_t from a pair of Pulses
  • Loading branch information
navaati authored Dec 21, 2023
1 parent 0ae0e48 commit e87abce
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/rmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ impl Pulse {
let ticks = PulseTicks::new_with_duration(ticks_hz, duration)?;
Ok(Self::new(pin_state, ticks))
}

pub fn into_rmt_item(level0: Self, level1: Self) -> rmt_item32_t {
let mut inner_item = rmt_item32_t__bindgen_ty_1__bindgen_ty_1::default();

inner_item.set_level0(level0.pin_state as u32);
inner_item.set_duration0(level0.ticks.ticks() as u32);
inner_item.set_level1(level1.pin_state as u32);
inner_item.set_duration1(level1.ticks.ticks() as u32);

rmt_item32_t {
__bindgen_anon_1: rmt_item32_t__bindgen_ty_1 {
__bindgen_anon_1: inner_item,
},
}
}
}

impl Default for Pulse {
Expand Down Expand Up @@ -597,14 +612,14 @@ impl<'d> TxRmtDriver<'d> {
}

/// Start sending the given signal while blocking.
pub fn start_blocking<S>(&mut self, signal: &S) -> Result<(), EspError>
pub fn start_blocking<S: ?Sized>(&mut self, signal: &S) -> Result<(), EspError>
where
S: Signal,
{
self.write_items(signal, true)
}

fn write_items<S>(&mut self, signal: &S, block: bool) -> Result<(), EspError>
fn write_items<S: ?Sized>(&mut self, signal: &S, block: bool) -> Result<(), EspError>
where
S: Signal,
{
Expand Down Expand Up @@ -777,6 +792,12 @@ pub trait Signal {
fn as_slice(&self) -> &[rmt_item32_t];
}

impl Signal for [rmt_item32_t] {
fn as_slice(&self) -> &[rmt_item32_t] {
self
}
}

/// Stack based signal storage for an RMT signal.
///
/// Use this if you know the length of the pulses ahead of time and prefer to use the stack.
Expand Down

0 comments on commit e87abce

Please sign in to comment.