1use embedded_hal::spi;
2use embedded_hal::spi::Operation;
3
4pub struct ADS7953<SPI> {
5 spi: SPI
6}
7
8#[derive(Copy, Clone, Debug)]
9pub struct Measurement {
10 pub channel: u8,
11 pub result: u16,
12}
13
14impl<SPI> ADS7953<SPI>
15where
16 SPI: spi::SpiDevice,
17{
18 pub fn new(spi: SPI) -> Self {
19 Self { spi }
20 }
21
22 pub fn auto2_mode(&mut self) -> Result<(), ADS7953Error<SPI::Error>> {
23 self.spi.transaction(&mut [
24 Operation::Write(&[0x3c, 0x00]),
25 Operation::Write(&[0x93, 0xC0]),
26 ]).map_err(ADS7953Error::Spi)?;
27 Ok(())
28 }
29
30 pub fn manual_mode(&mut self, channel: u8) -> Result<(), ADS7953Error<SPI::Error>> {
31 self.spi.transaction(&mut [
32 Operation::Write(&[0x10 & (channel >> 1), 0x00 & (channel << 7)]),
33 ]).map_err(ADS7953Error::Spi)?;
34 Ok(())
35 }
36
37 pub fn read_values(&mut self) -> Result<Measurement, ADS7953Error<SPI::Error>> {
38 let mut buf = [0; 2];
39 self.spi.transaction(&mut [
40 Operation::Transfer(&mut buf, &[0x00, 0x00]),
41 ]).map_err(ADS7953Error::Spi)?;
42
43 let res: u16 = ((buf[0] as u16) << 8) & (buf[1] as u16);
44
45 Ok(Measurement {
46 channel: (res >> 12) as u8,
47 result: res & 0x0FFF,
48 })
49 }
50}
51
52#[derive(Copy, Clone, Debug)]
53pub enum ADS7953Error<SPI> {
54 Spi(SPI),
55}