adafruit_seesaw/modules/
adc.rs

1use super::{HardwareId, Modules, Reg};
2use crate::{devices::SeesawDevice, Driver, DriverExt, SeesawError};
3
4/// RO - 8 bits
5#[allow(dead_code)]
6const STATUS: &Reg = &[Modules::Adc.into_u8(), 0x00];
7
8/// WO - 8 bits
9/// Writing a 1 to any bit in this register enables the corresponding interrupt.
10/// Writing zeros to this register has no effect.
11#[allow(dead_code)]
12const INTENSET: &Reg = &[Modules::Adc.into_u8(), 0x02];
13
14/// NOT SUPPORTED BY SEESAW PLATFORM
15///
16/// WO - 8 bits
17/// Writing a 1 to any bit in this register enables the corresponding interrupt.
18/// Writing zeros to this register has no effect.
19#[allow(dead_code)]
20const INTENCLR: &Reg = &[Modules::Adc.into_u8(), 0x03];
21
22/// NOT SUPPORTED BY SEESAW PLATFORM
23///
24/// WO
25/// Writing 1 to this register sets window control.
26#[allow(dead_code)]
27const WINMODE: &Reg = &[Modules::Adc.into_u8(), 0x04];
28
29/// NOT SUPPORTED BY SEESAW PLATFORM
30///
31/// WO - 32 bits
32/// This register sets the threshold values for window mode.
33/// B31 - B16: High threshold
34/// B15 - B0: Low threshold
35#[allow(dead_code)]
36const WINTHRESH: &Reg = &[Modules::Adc.into_u8(), 0x05];
37
38/// RO - 16bits
39/// ADC value for channel 0
40const CHANNEL_0: &Reg = &[Modules::Adc.into_u8(), 0x07];
41
42/// The ADC provides the ability to measure analog voltages at 10-bit
43/// resolution. The SAMD09 seesaw has 4 ADC inputs, the Attiny8x7 has 11 ADC
44/// inputs.
45///
46/// The module base register address for the ADC is 0x09
47///
48/// Conversions can be read by reading the corresponding CHANNEL register.
49///
50/// When reading ADC data, there should be at least a 500 uS delay between
51/// writing the register number you would like to read from and attempting to
52/// read the data.
53///
54/// Allow a delay of at least 1ms in between sequential ADC reads on different
55/// channels.
56pub trait AdcModule<D: Driver>: SeesawDevice<Driver = D> {
57    /// Read the analog value on an ADC-enabled pin.
58    ///
59    /// On the SAMD09 breakout, the pin corresponds to the number on the
60    /// silkscreen. On the default seesaw firmware on the SAMD09 breakout, pins
61    /// 2, 3, and 4 are ADC-enabled.
62    fn analog_read(&mut self, pin: u8) -> Result<u16, SeesawError<D::Error>> {
63        let pin_offset = match Self::HARDWARE_ID {
64            HardwareId::SAMD09 => match pin {
65                2 => 0,
66                3 => 1,
67                4 => 2,
68                5 => 3,
69                _ => 0,
70            },
71            _ => pin,
72        };
73
74        let addr = self.addr();
75        self.driver()
76            .read_u16(addr, &[CHANNEL_0[0], CHANNEL_0[1] + pin_offset])
77            .map_err(SeesawError::I2c)
78    }
79}