Skip to content

Commit

Permalink
Extract types into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
eldruin committed Jun 7, 2021
1 parent bb4709a commit 2659287
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 124 deletions.
128 changes: 4 additions & 124 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,107 +129,6 @@

use embedded_hal::blocking::i2c;

/// All possible errors in this crate
#[derive(Debug)]
pub enum Error<E> {
/// I²C bus error.
I2C(E),
/// A manual-configuration-mode-only was attempted while in automatic
/// configuration mode.
OperationNotAvailable,
}

/// Measurement mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MeasurementMode {
/// Once every 800ms mode (default).
///
/// Measures lux intensity every 800ms regardless of the integration time.
/// Sensor operates on lowest possible supply current.
OnceEvery800ms,
/// Continuous mode.
///
/// Continuously measures lux intensity. As soon as a reading finishes,
/// the next one begins. The actual cadence depends on the integration
/// time selected.
Continuous,
}

/// Configuration mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConfigurationMode {
/// Automatic mode (default).
///
/// On-chip algorithm selects the integration time (100ms - 800ms) and
/// the current division ratio
Automatic,
/// Manual mode.
///
/// The user can select the integration time and the current division
/// ratio manually.
Manual,
}

/// Integration time
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum IntegrationTime {
/// 6.25ms. (Only in manual mode)
_6_25ms,
/// 12.5ms. (Only in manual mode)
_12_5ms,
/// 25ms. (Only in manual mode)
_25ms,
/// 50ms. (Only in manual mode)
_50ms,
/// 100ms. (Preferred mode for high-brightness applications)
_100ms,
/// 200ms
_200ms,
/// 400ms
_400ms,
/// 800ms. (Preferred mode for boosting low-light sensitivity)
_800ms,
}

/// Current division ratio
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CurrentDivisionRatio {
/// No current division (default).
///
/// All the photodiode current goes to the ADC.
One,
/// 1/8 current division ratio.
///
/// Only 1/8 of the photodiode current goes to the ADC. This mode is used in
/// high-brightness situations.
OneEighth,
}

/// Possible slave addresses
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SlaveAddr {
/// Default slave address
Default,
/// Alternative slave address providing bit value for A0
Alternative(bool),
}

impl Default for SlaveAddr {
/// Default slave address
fn default() -> Self {
SlaveAddr::Default
}
}

impl SlaveAddr {
fn addr(self, default: u8) -> u8 {
match self {
SlaveAddr::Default => default,
SlaveAddr::Alternative(a0) => default | a0 as u8,
}
}
}

const DEVICE_BASE_ADDRESS: u8 = 0b100_1010;

struct Register;
Expand Down Expand Up @@ -262,6 +161,10 @@ pub struct Max44009<I2C> {

mod configuration;
mod reading;
mod types;
pub use types::{
ConfigurationMode, CurrentDivisionRatio, Error, IntegrationTime, MeasurementMode, SlaveAddr,
};

impl<I2C, E> Max44009<I2C>
where
Expand All @@ -281,26 +184,3 @@ where
self.i2c
}
}

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

#[test]
fn can_get_default_address() {
let addr = SlaveAddr::default();
assert_eq!(DEVICE_BASE_ADDRESS, addr.addr(DEVICE_BASE_ADDRESS));
}

#[test]
fn can_generate_alternative_addresses() {
assert_eq!(
0b100_1010,
SlaveAddr::Alternative(false).addr(DEVICE_BASE_ADDRESS)
);
assert_eq!(
0b100_1011,
SlaveAddr::Alternative(true).addr(DEVICE_BASE_ADDRESS)
);
}
}
126 changes: 126 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//! Public types

/// All possible errors in this crate
#[derive(Debug)]
pub enum Error<E> {
/// I²C bus error.
I2C(E),
/// A manual-configuration-mode-only was attempted while in automatic
/// configuration mode.
OperationNotAvailable,
}

/// Measurement mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MeasurementMode {
/// Once every 800ms mode (default).
///
/// Measures lux intensity every 800ms regardless of the integration time.
/// Sensor operates on lowest possible supply current.
OnceEvery800ms,
/// Continuous mode.
///
/// Continuously measures lux intensity. As soon as a reading finishes,
/// the next one begins. The actual cadence depends on the integration
/// time selected.
Continuous,
}

/// Configuration mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConfigurationMode {
/// Automatic mode (default).
///
/// On-chip algorithm selects the integration time (100ms - 800ms) and
/// the current division ratio
Automatic,
/// Manual mode.
///
/// The user can select the integration time and the current division
/// ratio manually.
Manual,
}

/// Integration time
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum IntegrationTime {
/// 6.25ms. (Only in manual mode)
_6_25ms,
/// 12.5ms. (Only in manual mode)
_12_5ms,
/// 25ms. (Only in manual mode)
_25ms,
/// 50ms. (Only in manual mode)
_50ms,
/// 100ms. (Preferred mode for high-brightness applications)
_100ms,
/// 200ms
_200ms,
/// 400ms
_400ms,
/// 800ms. (Preferred mode for boosting low-light sensitivity)
_800ms,
}

/// Current division ratio
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CurrentDivisionRatio {
/// No current division (default).
///
/// All the photodiode current goes to the ADC.
One,
/// 1/8 current division ratio.
///
/// Only 1/8 of the photodiode current goes to the ADC. This mode is used in
/// high-brightness situations.
OneEighth,
}

/// Possible slave addresses
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SlaveAddr {
/// Default slave address
Default,
/// Alternative slave address providing bit value for A0
Alternative(bool),
}

impl Default for SlaveAddr {
/// Default slave address
fn default() -> Self {
SlaveAddr::Default
}
}

impl SlaveAddr {
pub(crate) fn addr(self, default: u8) -> u8 {
match self {
SlaveAddr::Default => default,
SlaveAddr::Alternative(a0) => default | a0 as u8,
}
}
}

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

#[test]
fn can_get_default_address() {
let addr = SlaveAddr::default();
assert_eq!(DEVICE_BASE_ADDRESS, addr.addr(DEVICE_BASE_ADDRESS));
}

#[test]
fn can_generate_alternative_addresses() {
assert_eq!(
0b100_1010,
SlaveAddr::Alternative(false).addr(DEVICE_BASE_ADDRESS)
);
assert_eq!(
0b100_1011,
SlaveAddr::Alternative(true).addr(DEVICE_BASE_ADDRESS)
);
}
}

0 comments on commit 2659287

Please sign in to comment.