Crate opt300x[][src]

Expand description

This is a platform agnostic Rust driver for the OPT300x ambient light sensors using the embedded-hal traits.

This driver allows you to:

Introductory blog post

The devices

This driver is compatible with the devices OPT3001, OPT3002, OPT3004, OPT3006 and OPT3007.

The OPT3001 is a sensor that measures the intensity of visible light. The spectral response of the sensor tightly matches the photopic response of the human eye and includes significant infrared rejection.

The OPT3001 is a single-chip lux meter, measuring the intensity of light as visible by the human eye. The precision spectral response and strong IR rejection of the device enables the OPT3001 to accurately meter the intensity of light as seen by the human eye regardless of light source. The strong IR rejection also aids in maintaining high accuracy when industrial design calls for mounting the sensor under dark glass for aesthetics. The OPT3001 is designed for systems that create light-based experiences for humans, and an ideal preferred replacement for photodiodes, photoresistors, or other ambient light sensors with less human eye matching and IR rejection.

Measurements can be made from 0.01 lux up to 83k lux without manually selecting full-scale ranges by using the built-in, full-scale setting feature. This capability allows light measurement over a 23-bit effective dynamic range.

The digital operation is flexible for system integration. Measurements can be either continuous or single-shot. The control and interrupt system features autonomous operation, allowing the processor to sleep while the sensor searches for appropriate wake-up events to report via the interrupt pin. The digital output is reported over an I2C- and SMBus-compatible, two-wire serial interface.

The low power consumption and low power-supply voltage capability of the OPT3001 enhance the battery life of battery-powered systems.

Datasheets:

Application Guide:

Usage examples (see also examples folder)

To use this driver, import this crate and an embedded_hal implementation, then instantiate the appropriate device.

In the following examples an instance of the device OPT3001 will be created. Other devices can be created with similar methods like: Opt300x::new_opt3002(...).

Create a driver instance for the OPT3001

use linux_embedded_hal::I2cdev;
use opt300x::{Opt300x, SlaveAddr};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let sensor = Opt300x::new_opt3001(dev, address);

Provide an alternative address

use linux_embedded_hal::I2cdev;
use opt300x::{Opt300x, SlaveAddr};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::Alternative(true, false);
let mut sensor = Opt300x::new_opt3001(dev, address);

Read lux in one-shot measurements

use linux_embedded_hal::I2cdev;
use nb;
use opt300x::{Opt300x, SlaveAddr};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut sensor = Opt300x::new_opt3001(dev, address);
loop {
    let m = nb::block!(sensor.read_lux()).unwrap();
    println!("lux: {:2}", m.result);
}

Change into continuous mode and read lux

use linux_embedded_hal::I2cdev;
use opt300x::{Opt300x, SlaveAddr};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let sensor = Opt300x::new_opt3001(dev, address);
let mut sensor = sensor.into_continuous().ok().unwrap();
loop {
    let lux = sensor.read_lux().unwrap();
    println!("lux: {:2}", lux);
}

Set the integration time and manual lux range

use linux_embedded_hal::I2cdev;
use opt300x::{IntegrationTime, LuxRange, Opt300x, SlaveAddr};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut sensor = Opt300x::new_opt3001(dev, address);
sensor.set_integration_time(IntegrationTime::Ms800).unwrap();
sensor.set_lux_range(LuxRange::Manual(2)).unwrap();
sensor.enable_exponent_masking().unwrap();

Configure interrupts

use linux_embedded_hal::I2cdev;
use opt300x::{
    ComparisonMode, FaultCount, InterruptPinPolarity, Opt300x, SlaveAddr
};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut sensor = Opt300x::new_opt3001(dev, address);
sensor.set_comparison_mode(ComparisonMode::LatchedWindow).unwrap();
sensor.set_interrupt_pin_polarity(InterruptPinPolarity::High).unwrap();
sensor.set_fault_count(FaultCount::Four).unwrap();
sensor.set_low_limit_raw(1, 127).unwrap();
sensor.set_high_limit_raw(3, 50).unwrap();
loop {
    let status = sensor.read_status().unwrap();
    println!("status {:?}", status);
}

Enable end of conversion mode

use linux_embedded_hal::I2cdev;
use opt300x::{Opt300x, SlaveAddr};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut sensor = Opt300x::new_opt3001(dev, address);
sensor.enable_end_of_conversion_mode().unwrap();
loop {
    let status = sensor.read_status().unwrap();
    println!("status {:?}", status);
}

Modules

Mode marker

Structs

One-shot measurement

OPT300x device driver

Conversion status

Enums

Result comparison mode for interrupt reporting

Errors in this crate

Fault count

Integration time

Interrupt pin polarity (active state)

Lux range

Error type for mode changes.

Possible slave addresses