pub struct Opt3001<I2C, D> { /* private fields */ }Expand description
An OPT3001 on an I2C bus, measuring illuminance on demand in single-shot mode.
init checks the manufacturer and device id registers and
writes the configuration with the part in shutdown, its power-on state.
measure writes the single-shot mode, which starts one
conversion at the configured integration time, waits that time, polls the
conversion-ready flag, which a read of the configuration register clears, then
reads the result register. The part returns itself to shutdown when the
conversion ends. As a [Sensor] its reading is the Reading; the lux value
is selected with [Sensor::map].
§Examples
A greenhouse’s light, read from an OPT3001 that is not plugged in yet: the simulated part sees 320 lux, and the driver hands the reading back in lux.
use pamoja_core::Sensor;
use pamoja_hal::script::{block_on, DelayLog};
use pamoja_sensors::opt3001::{sim, Opt3001, I2C_ADDRESS_GND};
let part = sim::reporting(I2C_ADDRESS_GND, 320.0);
let sensor = Opt3001::new(part, I2C_ADDRESS_GND, DelayLog::new());
let mut lux = sensor.map(|reading| reading.lux());
let reading = block_on(lux.read())?;
assert!((reading - 320.0).abs() < 0.5);Implementations§
Source§impl<I2C, D> Opt3001<I2C, D>
impl<I2C, D> Opt3001<I2C, D>
Sourcepub fn new(bus: I2C, address: u8, delay: D) -> Self
pub fn new(bus: I2C, address: u8, delay: D) -> Self
Wraps an I2C bus and the part’s address.
§Arguments
bus- the I2C bus the part is on.address- the address the ADDR pin selects, one of theI2C_ADDRESS_*constants.delay- the timer that paces the conversions.
§Returns
The driver, with the 800 ms integration time and automatic full-scale range, the part’s reset settings.
Sourcepub fn with_conversion_time(self, conversion_time: ConversionTime) -> Self
pub fn with_conversion_time(self, conversion_time: ConversionTime) -> Self
Sourcepub fn with_range(self, range_number: u8) -> Self
pub fn with_range(self, range_number: u8) -> Self
Sets the full-scale range number, or automatic range selection.
§Arguments
range_number- 0 toRANGE_MAXfor a fixed range, orRANGE_AUTOMATIC.
§Returns
The driver.
Sourcepub fn configuration(&self) -> Configuration
pub fn configuration(&self) -> Configuration
Returns the configuration the driver writes, with the part in shutdown.
Source§impl<I2C: I2c, D: DelayNs> Opt3001<I2C, D>
impl<I2C: I2c, D: DelayNs> Opt3001<I2C, D>
Sourcepub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
pub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
Checks the part’s identity and writes the settings with the part in shutdown.
§Errors
Returns DriverError::Bus if the bus fails, and DriverError::Sensor
with SensorError::Identity if either id register is not an OPT3001’s.
Sourcepub fn measure(&mut self) -> Result<Reading, DriverError<I2C::Error>>
pub fn measure(&mut self) -> Result<Reading, DriverError<I2C::Error>>
Runs one conversion and returns the result.
Initializes the part first if init has not run.
§Returns
The result register as a Reading.
§Errors
Returns DriverError::Bus if the bus fails, DriverError::Timeout if
the conversion-ready flag never sets, or any error of init.
Sourcepub fn set_limits(
&mut self,
low_milli_lux: u32,
high_milli_lux: u32,
) -> Result<(), DriverError<I2C::Error>>
pub fn set_limits( &mut self, low_milli_lux: u32, high_milli_lux: u32, ) -> Result<(), DriverError<I2C::Error>>
Writes the low and high limit registers the interrupt pin compares against.
§Arguments
low_milli_lux- the low limit in millilux.high_milli_lux- the high limit in millilux.
§Errors
Returns DriverError::Bus if the bus fails.