max44009/
types.rs

1//! Public types
2
3/// All possible errors in this crate
4#[derive(Debug)]
5pub enum Error<E> {
6    /// I²C bus error.
7    I2C(E),
8    /// A manual-configuration-mode-only was attempted while in automatic
9    /// configuration mode.
10    OperationNotAvailable,
11}
12
13/// Measurement mode
14#[derive(Debug, Clone, Copy, PartialEq)]
15pub enum MeasurementMode {
16    /// Once every 800ms mode (default).
17    ///
18    /// Measures lux intensity every 800ms regardless of the integration time.
19    /// Sensor operates on lowest possible supply current.
20    OnceEvery800ms,
21    /// Continuous mode.
22    ///
23    /// Continuously measures lux intensity. As soon as a reading finishes,
24    /// the next one begins. The actual cadence depends on the integration
25    /// time selected.
26    Continuous,
27}
28
29/// Configuration mode
30#[derive(Debug, Clone, Copy, PartialEq)]
31pub enum ConfigurationMode {
32    /// Automatic mode (default).
33    ///
34    /// On-chip algorithm selects the integration time (100ms - 800ms) and
35    /// the current division ratio
36    Automatic,
37    /// Manual mode.
38    ///
39    /// The user can select the integration time and the current division
40    /// ratio manually.
41    Manual,
42}
43
44/// Integration time
45#[derive(Debug, Clone, Copy, PartialEq)]
46pub enum IntegrationTime {
47    /// 6.25ms. (Only in manual mode)
48    _6_25ms,
49    /// 12.5ms. (Only in manual mode)
50    _12_5ms,
51    /// 25ms. (Only in manual mode)
52    _25ms,
53    /// 50ms. (Only in manual mode)
54    _50ms,
55    /// 100ms. (Preferred mode for high-brightness applications)
56    _100ms,
57    /// 200ms
58    _200ms,
59    /// 400ms
60    _400ms,
61    /// 800ms. (Preferred mode for boosting low-light sensitivity)
62    _800ms,
63}
64
65/// Current division ratio
66#[derive(Debug, Clone, Copy, PartialEq)]
67pub enum CurrentDivisionRatio {
68    /// No current division (default).
69    ///
70    /// All the photodiode current goes to the ADC.
71    One,
72    /// 1/8 current division ratio.
73    ///
74    /// Only 1/8 of the photodiode current goes to the ADC. This mode is used in
75    /// high-brightness situations.
76    OneEighth,
77}
78
79/// Possible slave addresses
80#[derive(Debug, Clone, Copy, PartialEq)]
81pub enum SlaveAddr {
82    /// Default slave address
83    Default,
84    /// Alternative slave address providing bit value for A0
85    Alternative(bool),
86}
87
88impl Default for SlaveAddr {
89    /// Default slave address
90    fn default() -> Self {
91        SlaveAddr::Default
92    }
93}
94
95impl SlaveAddr {
96    pub(crate) fn addr(self, default: u8) -> u8 {
97        match self {
98            SlaveAddr::Default => default,
99            SlaveAddr::Alternative(a0) => default | a0 as u8,
100        }
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use crate::DEVICE_BASE_ADDRESS;
108
109    #[test]
110    fn can_get_default_address() {
111        let addr = SlaveAddr::default();
112        assert_eq!(DEVICE_BASE_ADDRESS, addr.addr(DEVICE_BASE_ADDRESS));
113    }
114
115    #[test]
116    fn can_generate_alternative_addresses() {
117        assert_eq!(
118            0b100_1010,
119            SlaveAddr::Alternative(false).addr(DEVICE_BASE_ADDRESS)
120        );
121        assert_eq!(
122            0b100_1011,
123            SlaveAddr::Alternative(true).addr(DEVICE_BASE_ADDRESS)
124        );
125    }
126}