Skip to main content

esp_idf_hal/rmt/encoder/
copy_encoder.rs

1use core::ptr;
2
3use esp_idf_sys::*;
4
5use super::RawEncoder;
6use crate::rmt::Symbol;
7
8/// A copy encoder copies the provided symbols from the user space into the driver layer.
9///
10/// It is usually used to encode non-`mut` data, i.e. data that does not change at runtime
11/// after initialization, such as the leading code in the IR protocol.
12///
13/// For custom [`Encoder`](crate::rmt::encoder::Encoder) this type is essential to
14/// copy the generated symbols into the RMT driver.
15#[derive(Debug)]
16pub struct CopyEncoder {
17    handle: rmt_encoder_handle_t,
18}
19
20impl CopyEncoder {
21    /// Constructs a new copy encoder with default configuration.
22    pub fn new() -> Result<Self, EspError> {
23        Self::with_config(&rmt_copy_encoder_config_t {})
24    }
25
26    /// Constructs a new copy encoder with the provided configuration.
27    pub fn with_config(config: &rmt_copy_encoder_config_t) -> Result<Self, EspError> {
28        let mut handle: rmt_encoder_handle_t = ptr::null_mut();
29        esp!(unsafe { rmt_new_copy_encoder(config, &mut handle) })?;
30        Ok(Self { handle })
31    }
32}
33
34impl Drop for CopyEncoder {
35    fn drop(&mut self) {
36        // This is calling encoder->del(encoder);
37        unsafe { rmt_del_encoder(self.handle) };
38    }
39}
40
41impl RawEncoder for CopyEncoder {
42    type Item = Symbol;
43
44    fn handle(&mut self) -> &mut rmt_encoder_t {
45        unsafe { &mut *self.handle }
46    }
47}