1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Mock APIs for `embedded-hal` traits

/// Mock APIs for `embedded_hal::delay` traits
#[cfg(feature = "mock-embedded-hal-1")]
pub mod delay {
    use crate::unimock;

    #[unimock(prefix=crate, api=DelayNsMock, mirror=embedded_hal_1::delay::DelayNs)]
    pub trait DelayNs {
        fn delay_ns(&mut self, ns: u32);

        fn delay_us(&mut self, us: u32) {}
        fn delay_ms(&mut self, ms: u32) {}
    }
}

/// Mock APIs for `embedded_hal::digital` traits
#[cfg(feature = "mock-embedded-hal-1")]
pub mod digital {
    use crate::{private::DefaultImplDelegator, unimock, Unimock};
    use embedded_hal_1::digital::{ErrorKind, ErrorType, PinState};

    #[unimock(prefix=crate, api=ErrorMock, mirror=embedded_hal_1::digital::Error)]
    pub trait Error {
        fn kind(&self) -> ErrorKind;
    }

    #[unimock(prefix=crate, api=InputPinMock, mirror=embedded_hal_1::digital::InputPin)]
    pub trait InputPin: ErrorType {
        fn is_high(&mut self) -> Result<bool, <Self as ErrorType>::Error>;
        fn is_low(&mut self) -> Result<bool, <Self as ErrorType>::Error>;
    }

    #[unimock(prefix=crate, api=OutputPinMock, mirror=embedded_hal_1::digital::OutputPin)]
    pub trait OutputPin: ErrorType {
        // Required methods
        fn set_low(&mut self) -> Result<(), <Self as ErrorType>::Error>;
        fn set_high(&mut self) -> Result<(), <Self as ErrorType>::Error>;

        // Provided method
        fn set_state(&mut self, state: PinState) -> Result<(), <Self as ErrorType>::Error> {}
    }

    #[unimock(prefix=crate, api=StatefulOutputPinMock, mirror=embedded_hal_1::digital::StatefulOutputPin)]
    pub trait StatefulOutputPin: OutputPin {
        // Required methods
        fn is_set_high(&mut self) -> Result<bool, <Self as ErrorType>::Error>;
        fn is_set_low(&mut self) -> Result<bool, <Self as ErrorType>::Error>;

        // Provided method
        fn toggle(&mut self) -> Result<(), <Self as ErrorType>::Error> {}
    }

    impl ErrorType for Unimock {
        type Error = Unimock;
    }
    impl ErrorType for DefaultImplDelegator {
        type Error = Unimock;
    }
}

/// Mock APIs for `embedded_hal::i2c` traits
#[cfg(feature = "mock-embedded-hal-1")]
pub mod i2c {
    use crate::{private::DefaultImplDelegator, unimock, Unimock};
    use embedded_hal_1::i2c::{AddressMode, ErrorKind, ErrorType, Operation};

    #[unimock(prefix=crate, api=ErrorMock, mirror=embedded_hal_1::i2c::Error)]
    pub trait Error {
        fn kind(&self) -> ErrorKind;
    }

    #[unimock(prefix=crate, api=I2cMock, mirror=embedded_hal_1::i2c::I2c)]
    pub trait I2c<A: AddressMode>: ErrorType {
        // Required methods
        fn transaction(
            &mut self,
            address: A,
            operations: &mut [Operation<'_>],
        ) -> Result<(), <Self as ErrorType>::Error>;

        // Provided methods
        fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), <Self as ErrorType>::Error> {}

        fn write(&mut self, address: A, write: &[u8]) -> Result<(), <Self as ErrorType>::Error> {}

        fn write_read(
            &mut self,
            address: A,
            write: &[u8],
            read: &mut [u8],
        ) -> Result<(), <Self as ErrorType>::Error> {
        }
    }

    impl ErrorType for Unimock {
        type Error = Unimock;
    }
    impl ErrorType for DefaultImplDelegator {
        type Error = Unimock;
    }
}

/// Mock APIs for `embedded_hal::pwm` traits
#[cfg(feature = "mock-embedded-hal-1")]
pub mod pwm {
    use crate::{private::DefaultImplDelegator, unimock, Unimock};
    use embedded_hal_1::pwm::{ErrorKind, ErrorType};

    #[unimock(prefix=crate, api=ErrorMock, mirror=embedded_hal_1::pwm::Error)]
    pub trait Error {
        fn kind(&self) -> ErrorKind;
    }

    #[unimock(prefix=crate, api=SetDutyCycleMock, mirror=embedded_hal_1::pwm::SetDutyCycle)]
    pub trait SetDutyCycle: ErrorType {
        // Required methods
        fn max_duty_cycle(&self) -> u16;
        fn set_duty_cycle(&mut self, duty: u16) -> Result<(), <Self as ErrorType>::Error>;

        // Provided methods
        fn set_duty_cycle_fully_off(&mut self) -> Result<(), <Self as ErrorType>::Error> {}

        fn set_duty_cycle_fully_on(&mut self) -> Result<(), <Self as ErrorType>::Error> {}

        fn set_duty_cycle_fraction(
            &mut self,
            num: u16,
            denom: u16,
        ) -> Result<(), <Self as ErrorType>::Error> {
        }

        fn set_duty_cycle_percent(
            &mut self,
            percent: u8,
        ) -> Result<(), <Self as ErrorType>::Error> {
        }
    }

    impl ErrorType for Unimock {
        type Error = Unimock;
    }
    impl ErrorType for DefaultImplDelegator {
        type Error = Unimock;
    }
}

/// Mock APIs for `embedded_hal::spi` traits
#[cfg(feature = "mock-embedded-hal-1")]
pub mod spi {
    use crate::{private::DefaultImplDelegator, unimock, Unimock};
    use embedded_hal_1::spi::{ErrorKind, ErrorType, Operation};

    #[unimock(prefix=crate, api=ErrorMock, mirror=embedded_hal_1::spi::Error)]
    pub trait Error {
        fn kind(&self) -> ErrorKind;
    }

    #[unimock(prefix=crate, api=SpiBusMock, mirror=embedded_hal_1::spi::SpiBus)]
    pub trait SpiBus<Word: Copy + 'static> {
        // Required methods
        fn read(&mut self, words: &mut [Word]) -> Result<(), <Self as ErrorType>::Error>;

        fn write(&mut self, words: &[Word]) -> Result<(), <Self as ErrorType>::Error>;

        fn transfer(
            &mut self,
            read: &mut [Word],
            write: &[Word],
        ) -> Result<(), <Self as ErrorType>::Error>;

        fn transfer_in_place(
            &mut self,
            words: &mut [Word],
        ) -> Result<(), <Self as ErrorType>::Error>;
        fn flush(&mut self) -> Result<(), <Self as ErrorType>::Error>;
    }

    #[unimock(prefix=crate, api=SpiDeviceMock, mirror=embedded_hal_1::spi::SpiDevice)]
    pub trait SpiDevice<Word: Copy + 'static> {
        // Required method
        fn transaction(
            &mut self,
            operations: &mut [Operation<'_, Word>],
        ) -> Result<(), <Self as ErrorType>::Error>;

        // Provided methods
        fn read(&mut self, buf: &mut [Word]) -> Result<(), <Self as ErrorType>::Error> {}

        fn write(&mut self, buf: &[Word]) -> Result<(), <Self as ErrorType>::Error> {}

        fn transfer(
            &mut self,
            read: &mut [Word],
            write: &[Word],
        ) -> Result<(), <Self as ErrorType>::Error> {
        }

        fn transfer_in_place(
            &mut self,
            buf: &mut [Word],
        ) -> Result<(), <Self as ErrorType>::Error> {
        }
    }

    impl ErrorType for Unimock {
        type Error = Unimock;
    }
    impl ErrorType for DefaultImplDelegator {
        type Error = Unimock;
    }
}