Skip to content

Commit

Permalink
Support interrupt pin modes
Browse files Browse the repository at this point in the history
  • Loading branch information
eldruin committed Apr 7, 2020
1 parent 4b55fd0 commit 038bca2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/device_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
BitFlags, Config, Error, Isl29125, Measurement, OperatingMode, Range, Register, Resolution,
BitFlags, Config, Error, InterruptPinMode, Isl29125, Measurement, OperatingMode, Range,
Register, Resolution,
};
use embedded_hal::blocking::i2c;

Expand Down Expand Up @@ -69,6 +70,15 @@ where
self.set_config1(config1)
}

/// Set interrupt pin (INT) mode (Interrupt / Synced conversion start)
pub fn set_interrupt_pin_mode(&mut self, mode: InterruptPinMode) -> Result<(), Error<E>> {
let config1 = match mode {
InterruptPinMode::Interrupt => self.config1.with_low(BitFlags::SYNC),
InterruptPinMode::SyncStart => self.config1.with_high(BitFlags::SYNC),
};
self.set_config1(config1)
}

fn set_config1(&mut self, config1: Config) -> Result<(), Error<E>> {
self.write_register(Register::CONFIG1, config1.bits)?;
self.config1 = config1;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

mod device_impl;
mod types;
pub use crate::types::{Error, Measurement, OperatingMode, Range, Resolution};
pub use crate::types::{Error, InterruptPinMode, Measurement, OperatingMode, Range, Resolution};
mod register_address;
use crate::register_address::{BitFlags, Register};

Expand Down
1 change: 1 addition & 0 deletions src/register_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ impl Register {

pub struct BitFlags;
impl BitFlags {
pub const SYNC: u8 = 1 << 5;
pub const RESOLUTION: u8 = 1 << 4;
pub const RANGE: u8 = 1 << 3;
}
Expand Down
20 changes: 20 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ impl Default for Range {
}
}

/// 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
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -93,4 +108,9 @@ mod tests {
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());
}
}
1 change: 1 addition & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Register {

pub struct BitFlags;
impl BitFlags {
pub const SYNC: u8 = 1 << 5;
pub const RESOLUTION: u8 = 1 << 4;
pub const RANGE: u8 = 1 << 3;
}
Expand Down
17 changes: 16 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod common;
use crate::common::{destroy, new, BitFlags as BF, Register, ADDR};
use embedded_hal_mock::i2c::Transaction as I2cTrans;
use isl29125::{OperatingMode, Range, Resolution};
use isl29125::{InterruptPinMode, OperatingMode, Range, Resolution};

#[test]
fn can_create_and_destroy() {
Expand Down Expand Up @@ -100,3 +100,18 @@ set_test!(
BF::RANGE,
Range::Lux10000
);

set_test!(
set_int_mode_int,
set_interrupt_pin_mode,
CONFIG1,
0,
InterruptPinMode::Interrupt
);
set_test!(
set_int_mode_sync,
set_interrupt_pin_mode,
CONFIG1,
BF::SYNC,
InterruptPinMode::SyncStart
);

0 comments on commit 038bca2

Please sign in to comment.