esp_idf_hal/rmt/encoder/
bytes_encoder.rs1use core::ptr;
2
3use esp_idf_sys::*;
4
5use crate::rmt::encoder::RawEncoder;
6use crate::rmt::{PinState, Pulse, PulseTicks, Symbol};
7
8#[derive(Debug, Clone)]
10pub struct BytesEncoderConfig {
11 pub bit0: Symbol,
13 pub bit1: Symbol,
15 pub msb_first: bool,
18 #[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#[derive(Debug)]
44pub struct BytesEncoder {
45 handle: rmt_encoder_handle_t,
46}
47
48impl BytesEncoder {
49 pub fn new() -> Result<Self, EspError> {
51 Self::with_config(&Default::default())
52 }
53
54 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 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}