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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/// All possible errors in this crate
#[derive(Debug)]
pub enum Error<E> {
    /// I²C communication error
    I2C(E),
    /// Invalid input data provided
    InvalidInputData,
}

/// Measurement result
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Measurement {
    /// Red
    pub red: u16,
    /// Green
    pub green: u16,
    /// Blue
    pub blue: u16,
}

/// Operating mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OperatingMode {
    /// Power-Down (ADC conversion)
    PowerDown,
    /// Stand by (No ADC conversion)
    StandBy,
    /// Red only
    RedOnly,
    /// Green only
    GreenOnly,
    /// Blue only
    BlueOnly,
    /// Red/Green
    RedGreen,
    /// Green/Blue
    GreenBlue,
    /// Red/Green/Blue
    RedGreenBlue,
}

impl Default for OperatingMode {
    fn default() -> Self {
        OperatingMode::PowerDown
    }
}

/// ADC resolution
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Resolution {
    /// 12-bit resolution
    Bit12,
    /// 16-bit resolution (default)
    Bit16,
}

impl Default for Resolution {
    fn default() -> Self {
        Resolution::Bit16
    }
}

/// RGB data sensing range
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Range {
    /// 375 lux
    Lux375,
    /// 10000 lux
    Lux10000,
}

impl Default for Range {
    fn default() -> Self {
        Range::Lux375
    }
}

/// Interrupt pin (INT) mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum InterruptPinMode {
    /// Interrupts will be generated on exceeded thresholds. (ADC starts when writing to the config 1 register)
    Interrupt,
    /// INT pin is an input. ADC conversion starts on the rising edge at the INT pin.
    SyncStart,
}

impl Default for InterruptPinMode {
    fn default() -> Self {
        InterruptPinMode::Interrupt
    }
}

/// IR filtering range
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum IRFilteringRange {
    /// Lower range
    ///
    /// The value must be in the range `[0-63]` (no offset)
    Lower(u8),
    /// Higher range
    ///
    /// The value must be in the range `[0-63]` (this corresponds to 106-169)
    Higher(u8),
}

impl Default for IRFilteringRange {
    fn default() -> Self {
        IRFilteringRange::Lower(0)
    }
}

/// Interrupt threshold assignment
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum InterruptThresholdAssignment {
    /// No interrupt (default)
    None,
    /// Use interrupt thresholds on red channel data.
    Red,
    /// Use interrupt thresholds on green channel data.
    Green,
    /// Use interrupt thresholds on blue channel data.
    Blue,
}

impl Default for InterruptThresholdAssignment {
    fn default() -> Self {
        InterruptThresholdAssignment::None
    }
}

/// Fault count
///
/// Number of consecutive fault events necessary to trigger interrupt.
/// This is referred to as "persistence" in the documentation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FaultCount {
    /// One (default)
    One,
    /// Two
    Two,
    /// Four
    Four,
    /// Eight
    Eight,
}

impl Default for FaultCount {
    fn default() -> Self {
        FaultCount::One
    }
}

/// Status
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Status {
    /// Whether an interrupt was triggered
    pub interrupt_triggered: bool,
    /// Whether a conversion was completed
    pub conversion_completed: bool,
    /// Whether a power-down or brownout condition occurred
    pub brownout: bool,
    /// Wheter a color channel is under conversion
    pub converting: ConversionStatus,
}

/// RGB conversion status
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConversionStatus {
    /// No operation
    NoOperation,
    /// Red
    Red,
    /// Green
    Green,
    /// Blue
    Blue,
}

impl Default for ConversionStatus {
    fn default() -> Self {
        ConversionStatus::NoOperation
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_get_default_operating_mode() {
        assert_eq!(OperatingMode::PowerDown, OperatingMode::default());
    }

    #[test]
    fn can_get_default_resolution() {
        assert_eq!(Resolution::Bit16, Resolution::default());
    }

    #[test]
    fn can_get_default_range() {
        assert_eq!(Range::Lux375, Range::default());
    }

    #[test]
    fn can_get_default_int_pin_mode() {
        assert_eq!(InterruptPinMode::Interrupt, InterruptPinMode::default());
    }

    #[test]
    fn can_get_default_ir_filtering_range() {
        assert_eq!(IRFilteringRange::Lower(0), IRFilteringRange::default());
    }

    #[test]
    fn can_get_default_int_threshold_assignment() {
        assert_eq!(
            InterruptThresholdAssignment::None,
            InterruptThresholdAssignment::default()
        );
    }

    #[test]
    fn can_get_default_fault_count() {
        assert_eq!(FaultCount::One, FaultCount::default());
    }

    #[test]
    fn can_get_default_status() {
        assert_eq!(
            Status {
                interrupt_triggered: false,
                conversion_completed: false,
                brownout: false,
                converting: ConversionStatus::NoOperation
            },
            Status::default()
        );
    }
}