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
use crate::{Error, SevenSegInterface};
use embedded_hal::{blocking::spi::Write, digital::v2::OutputPin};
#[non_exhaustive]
pub enum SpimError<SPIM, GPIO> {
Spim(SPIM),
Gpio(GPIO),
}
pub struct SevSegSpim<SPIM, CS> {
spim: SPIM,
csn: CS,
}
impl<SPIM, CS> SevSegSpim<SPIM, CS>
where
SPIM: Write<u8>,
CS: OutputPin,
{
pub fn new(spim: SPIM, csn: CS) -> Self {
Self { spim, csn }
}
pub fn release(self) -> (SPIM, CS) {
(self.spim, self.csn)
}
}
impl<SPIM, CS> SevenSegInterface for SevSegSpim<SPIM, CS>
where
SPIM: Write<u8>,
CS: OutputPin,
{
type InterfaceError = SpimError<SPIM::Error, CS::Error>;
fn send(&mut self, data: &[u8]) -> Result<(), Error<Self::InterfaceError>> {
self.csn
.set_low()
.map_err(|e| Error::Interface(SpimError::Gpio(e)))?;
let ret = self
.spim
.write(&data)
.map_err(|e| Error::Interface(SpimError::Spim(e)))
.map(drop);
self.csn
.set_high()
.map_err(|e| Error::Interface(SpimError::Gpio(e)))?;
ret
}
}