Skip to main content

mc_sst25/
example.rs

1//! # Mocks for doc examples
2use core::convert::Infallible;
3use embedded_hal::digital::{ErrorType, OutputPin};
4use embedded_hal::spi::{Operation, SpiDevice};
5
6/// Mocked GPIO output pin
7#[derive(Default, Debug)]
8pub struct MockPin {}
9
10impl ErrorType for MockPin {
11    type Error = Infallible;
12}
13
14impl OutputPin for MockPin {
15    fn set_low(&mut self) -> Result<(), Self::Error> {
16        Ok(())
17    }
18
19    fn set_high(&mut self) -> Result<(), Self::Error> {
20        Ok(())
21    }
22}
23
24/// Mocked SPI Bus
25#[derive(Default, Debug)]
26pub struct MockBus {
27    /// Was previous transfer a read command?
28    read_command: bool,
29}
30
31impl embedded_hal::spi::ErrorType for MockBus {
32    type Error = Infallible;
33}
34
35impl SpiDevice<u8> for MockBus {
36    fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> {
37        for operation in operations {
38            match operation {
39                Operation::Read(buffer) => {
40                    if self.read_command {
41                        self.read_command = false;
42
43                        if buffer.len() == 5 {
44                            buffer.copy_from_slice(&[0x66, 0x1, 0x2, 0x3, 0x4])
45                        } else {
46                            buffer.copy_from_slice(&[0xa, 0xb, 0xc, 0xd])
47                        };
48                    }
49                }
50                Operation::Write(words) => {
51                    if words[0] == 0b0000_0011 {
52                        self.read_command = true;
53                    }
54                }
55                Operation::Transfer(_, _) => unimplemented!(),
56                Operation::TransferInPlace(_) => unimplemented!(),
57                Operation::DelayNs(_) => unimplemented!(),
58            }
59        }
60
61        Ok(())
62    }
63}