ws2812_esp32_rmt_driver/mock/
mod.rs

1//! Mock modules for local testing
2
3/// Mock module for `esp_idf_hal`
4pub mod esp_idf_hal {
5    pub use super::esp_idf_sys as sys;
6
7    /// Mock module for `esp_idf_hal::gpio`
8    pub mod gpio {
9        use super::peripheral::Peripheral;
10        use paste::paste;
11
12        /// Mock trait for `esp_idf_hal::gpio::OutputPin`.
13        pub trait OutputPin {}
14
15        macro_rules! define_pins_struct {
16            ($($num:expr),*) => {
17                paste! {
18                    /// Mock struct for `esp_idf_hal::gpio::Pins`.
19                    #[derive(Debug, Default)]
20                    pub struct Pins {
21                        $(
22                            pub [<gpio $num>]: [<Gpio $num>],
23                        )*
24                    }
25                }
26            }
27        }
28        define_pins_struct!(
29            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
30            24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
31            46, 47, 48
32        );
33
34        impl Pins {
35            pub(super) fn new() -> Self {
36                Default::default()
37            }
38        }
39
40        macro_rules! define_gpio_structs {
41            ($($num:expr),*) => {
42                paste! {
43                    $(
44                        #[doc = concat!("Mock struct for `esp_idf_hal::gpio::Gpio", stringify!($num) ,"`")]
45                        #[derive(Debug, Default)]
46                        pub struct [<Gpio $num>] {}
47
48                        //impl [<Gpio $num>] {
49                        //    pub(super) fn new() -> Self {
50                        //        Self {}
51                        //    }
52                        //}
53
54                        impl OutputPin for [<Gpio $num>] {}
55                        impl Peripheral for [<Gpio $num>] {
56                            type P=[<Gpio $num>];
57                        }
58                    )*
59                }
60            };
61        }
62        define_gpio_structs!(
63            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
64            24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
65            46, 47, 48
66        );
67    }
68
69    /// Mock module for `esp_idf_hal::peripheral`
70    pub mod peripheral {
71        /// Mock trait for `esp_idf_hal::peripheral::Peripheral`
72        pub trait Peripheral: Sized {
73            /// Peripheral singleton type
74            type P;
75        }
76    }
77
78    /// Mock module for `esp_idf_hal::peripherals`
79    pub mod peripherals {
80        use super::gpio;
81        use super::rmt;
82
83        /// Mock struct for `esp_idf_hal::peripherals::Peripherals`
84        pub struct Peripherals {
85            pub pins: gpio::Pins,
86            pub rmt: rmt::RMT,
87        }
88
89        impl Peripherals {
90            pub fn take() -> Result<Self, super::sys::EspError> {
91                Ok(Self::new())
92            }
93
94            // Create `Peripherals` instance.
95            //
96            // This function shall not used usually because
97            // the original `esp_idf_hal::peripherals::Peripherals::new()` is unsafe,
98            // and `take()` should be used instead.
99            pub fn new() -> Self {
100                Self {
101                    pins: gpio::Pins::new(),
102                    rmt: rmt::RMT::new(),
103                }
104            }
105        }
106    }
107
108    /// Mock module for `esp_idf_hal::rmt`
109    pub mod rmt {
110        use super::gpio::OutputPin;
111        use super::peripheral::Peripheral;
112        use super::sys::EspError;
113        use super::units::Hertz;
114        use config::TransmitConfig;
115        use core::marker::PhantomData;
116        use paste::paste;
117
118        macro_rules! define_channel_structs {
119            ($($num:expr),*) => {
120                paste! {
121                    $(
122                        #[doc = concat!("Mock struct for `esp_idf_hal::rmt::CHANNEL", stringify!($num) ,"`")]
123                        #[derive(Debug, Default)]
124                        pub struct [<CHANNEL $num>] {}
125
126                        impl [<CHANNEL $num>] {
127                            pub fn new() -> Self {
128                                Self {}
129                            }
130                        }
131
132                        impl Peripheral for [<CHANNEL $num>] {
133                            type P=[<CHANNEL $num>];
134                        }
135
136                        impl RmtChannel for [<CHANNEL $num>] {}
137                    )*
138                }
139            };
140        }
141        define_channel_structs!(0, 1, 2, 3, 4, 5, 6, 7);
142
143        /// mock struct for `esp_idf_hal::rmt::RMT`
144        #[derive(Debug, Default)]
145        pub struct RMT {
146            pub channel0: CHANNEL0,
147            pub channel1: CHANNEL1,
148            pub channel2: CHANNEL2,
149            pub channel3: CHANNEL3,
150            pub channel4: CHANNEL4,
151            pub channel5: CHANNEL5,
152            pub channel6: CHANNEL6,
153            pub channel7: CHANNEL7,
154        }
155
156        impl RMT {
157            pub fn new() -> Self {
158                Default::default()
159            }
160        }
161
162        /// Mock trait fo `esp_idf_hal::rmt::RmtChannel`
163        pub trait RmtChannel {}
164
165        //pub type RmtTransmitConfig = config::TransmitConfig;
166
167        /// Mock module for `esp_idf_hal::rmt::TxRmtDriver`
168        pub struct TxRmtDriver<'d> {
169            _p: PhantomData<&'d mut ()>,
170        }
171
172        impl<'d> TxRmtDriver<'d> {
173            /// Initialize the mock of `TxRmtDriver`.
174            /// No argument is used in this mock.
175            pub fn new<C: RmtChannel>(
176                _channel: impl Peripheral<P = C> + 'd,
177                _pin: impl Peripheral<P = impl OutputPin> + 'd,
178                _config: &TransmitConfig,
179            ) -> Result<Self, EspError> {
180                Ok(Self { _p: PhantomData })
181            }
182
183            pub fn counter_clock(&self) -> Result<Hertz, EspError> {
184                let ticks_hz: u32 = 80000000; // 80MHz
185                Ok(Hertz(ticks_hz))
186            }
187        }
188
189        /// Mock module for `esp_idf_hal::rmt::config`
190        pub mod config {
191            /// Mock struct for `esp_idf_hal::rmt::config::TransmitConfig`
192            #[derive(Debug, Clone)]
193            pub struct TransmitConfig {
194                pub clock_divider: u8,
195                pub mem_block_num: u8,
196                // Other parameters are omitted
197            }
198
199            impl TransmitConfig {
200                pub fn new() -> Self {
201                    Self {
202                        mem_block_num: 1,
203                        clock_divider: 80,
204                    }
205                }
206                #[must_use]
207                pub fn clock_divider(mut self, divider: u8) -> Self {
208                    self.clock_divider = divider;
209                    self
210                }
211                #[must_use]
212                pub fn mem_block_num(mut self, mem_block_num: u8) -> Self {
213                    self.mem_block_num = mem_block_num;
214                    self
215                }
216            }
217        }
218    }
219
220    /// Mock module for `esp_idf_hal::units`
221    pub mod units {
222        pub type ValueType = u32;
223        pub type LargeValueType = u64;
224
225        /// Mock struct for `esp_idf_hal::units::Hertz`
226        #[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Default)]
227        pub struct Hertz(pub ValueType);
228    }
229}
230
231/// Mock module for `esp_idf_sys`
232pub mod esp_idf_sys {
233    use core::fmt;
234
235    /// Mock struct for `esp_idf_sys::EspError`
236    #[repr(transparent)]
237    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
238    pub struct EspError();
239
240    #[cfg(feature = "std")]
241    impl std::error::Error for EspError {}
242
243    impl fmt::Display for EspError {
244        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
245            fmt::Display::fmt("EspError", f)
246        }
247    }
248}