Skip to main content

Opt3001

Struct Opt3001 

Source
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>

Source

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 the I2C_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.

Source

pub fn with_conversion_time(self, conversion_time: ConversionTime) -> Self

Sets the integration time of each conversion.

§Arguments
  • conversion_time - 100 ms for speed, 800 ms for resolution.
§Returns

The driver.

Source

pub fn with_range(self, range_number: u8) -> Self

Sets the full-scale range number, or automatic range selection.

§Arguments
§Returns

The driver.

Source

pub fn configuration(&self) -> Configuration

Returns the configuration the driver writes, with the part in shutdown.

Source

pub fn release(self) -> (I2C, D)

Gives back the bus and the delay.

§Returns

The bus and delay the driver was built from.

Source§

impl<I2C: I2c, D: DelayNs> Opt3001<I2C, D>

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<I2C: Debug, D: Debug> Debug for Opt3001<I2C, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I2C, D> Sensor for Opt3001<I2C, D>
where I2C: I2c + Send, I2C::Error: Debug, D: DelayNs + Send,

Source§

type Reading = Reading

The value produced by a single read, for example a temperature or a fix.
Source§

async fn read(&mut self) -> Result<Reading>

Takes a single reading from the sensor. Read more
§

fn map<F, T>(self, select: F) -> Map<Self, F>
where Self: Sized, F: FnMut(Self::Reading) -> T,

Wraps this sensor so every reading passes through select first. Read more

Auto Trait Implementations§

§

impl<I2C, D> Freeze for Opt3001<I2C, D>
where I2C: Freeze, D: Freeze,

§

impl<I2C, D> RefUnwindSafe for Opt3001<I2C, D>

§

impl<I2C, D> Send for Opt3001<I2C, D>
where I2C: Send, D: Send,

§

impl<I2C, D> Sync for Opt3001<I2C, D>
where I2C: Sync, D: Sync,

§

impl<I2C, D> Unpin for Opt3001<I2C, D>
where I2C: Unpin, D: Unpin,

§

impl<I2C, D> UnsafeUnpin for Opt3001<I2C, D>
where I2C: UnsafeUnpin, D: UnsafeUnpin,

§

impl<I2C, D> UnwindSafe for Opt3001<I2C, D>
where I2C: UnwindSafe, D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.