Skip to main content

esp_idf_hal/rmt/encoder/
bytes_encoder.rs

1use core::ptr;
2
3use esp_idf_sys::*;
4
5use crate::rmt::encoder::RawEncoder;
6use crate::rmt::{PinState, Pulse, PulseTicks, Symbol};
7
8/// The configuration for the [`BytesEncoder`].
9#[derive(Debug, Clone)]
10pub struct BytesEncoderConfig {
11    /// Specifies the symbol used to represent a `0` bit.
12    pub bit0: Symbol,
13    /// Specifies the symbol used to represent a `1` bit.
14    pub bit1: Symbol,
15    /// If `true`, bits are encoded most-significant bit first,
16    /// otherwise it will be least-significant bit (LSB) first.
17    pub msb_first: bool,
18    // This field is intentionally hidden to prevent non-exhaustive pattern matching.
19    // You should only construct this struct using the `..Default::default()` pattern.
20    // If you use this field directly, your code might break in future versions.
21    #[doc(hidden)]
22    #[allow(dead_code)]
23    pub __internal: (),
24}
25
26impl Default for BytesEncoderConfig {
27    fn default() -> Self {
28        let low_pulse = Pulse::new(PinState::Low, PulseTicks::new(1).unwrap());
29        let high_pulse = Pulse::new(PinState::High, PulseTicks::new(1).unwrap());
30
31        Self {
32            bit0: Symbol::new(low_pulse, low_pulse),
33            bit1: Symbol::new(high_pulse, high_pulse),
34            msb_first: false,
35            __internal: (),
36        }
37    }
38}
39
40/// An encoder that dynamically encodes a user space byte stream into RMT symbols.
41///
42/// It is usually used to encode dynamic data, e.g., the address and command fields in the IR protocol.
43#[derive(Debug)]
44pub struct BytesEncoder {
45    handle: rmt_encoder_handle_t,
46}
47
48impl BytesEncoder {
49    /// Constructs a new bytes encoder with default configuration.
50    pub fn new() -> Result<Self, EspError> {
51        Self::with_config(&Default::default())
52    }
53
54    /// Constructs a new bytes encoder with the provided configuration.
55    pub fn with_config(config: &BytesEncoderConfig) -> Result<Self, EspError> {
56        let sys_config = rmt_bytes_encoder_config_t {
57            bit0: config.bit0.0,
58            bit1: config.bit1.0,
59            flags: rmt_bytes_encoder_config_t__bindgen_ty_1 {
60                _bitfield_1: rmt_bytes_encoder_config_t__bindgen_ty_1::new_bitfield_1(
61                    config.msb_first as u32,
62                ),
63                ..Default::default()
64            },
65        };
66
67        let mut handle: rmt_encoder_handle_t = ptr::null_mut();
68        esp!(unsafe { rmt_new_bytes_encoder(&sys_config, &mut handle) })?;
69        Ok(Self { handle })
70    }
71}
72
73impl Drop for BytesEncoder {
74    fn drop(&mut self) {
75        // This is calling encoder->del(encoder);
76        unsafe { rmt_del_encoder(self.handle) };
77    }
78}
79
80impl RawEncoder for BytesEncoder {
81    type Item = u8;
82
83    fn handle(&mut self) -> &mut rmt_encoder_t {
84        unsafe { &mut *self.handle }
85    }
86}