Sensor

Trait Sensor 

pub trait Sensor: Send + Sync {
    // Required methods
    fn trigger_measurement(&self) -> Result<(), TriggerMeasurementError>;
    fn wait_for_reading(&'static self) -> ReadingWaiter ;
    fn reading_channels(&self) -> ReadingChannels;
    fn set_mode(&self, mode: Mode) -> Result<State, SetModeError>;
    fn state(&self) -> State;
    fn categories(&self) -> &'static [Category];
    fn label(&self) -> Option<&'static str>;
    fn display_name(&self) -> Option<&'static str>;
    fn part_number(&self) -> Option<&'static str>;
    fn version(&self) -> u8;
}
Available on crate feature sensors only.
Expand description

This trait must be implemented by sensor drivers.

See the module level documentation for more.

Required Methods§

fn trigger_measurement(&self) -> Result<(), TriggerMeasurementError>

Triggers a measurement. Clears the previous reading.

To obtain readings from every sensor drivers this method can be called in a loop over all sensors returned by Registry::sensors(), before obtaining the readings with Self::wait_for_reading() in a second loop, so that the measurements happen concurrently.

§For implementors

This method should return quickly.

§Errors

Returns TriggerMeasurementError::NonEnabled if the sensor driver is not enabled.

fn wait_for_reading(&'static self) -> ReadingWaiter

Waits for the reading and returns it asynchronously. Depending on the sensor device and the sensor driver, this may use a sensor interrupt or data polling. Interpretation of the reading requires data from Sensor::reading_channels() as well. See the module level documentation for more.

§Note

It is necessary to trigger a measurement by calling Sensor::trigger_measurement() beforehand, even if the sensor device carries out periodic measurements on its own.

§Errors

fn reading_channels(&self) -> ReadingChannels

Provides information about the reading returned by Sensor::wait_for_reading().

fn set_mode(&self, mode: Mode) -> Result<State, SetModeError>

Sets the sensor driver mode and returns the previous state. Allows to put the sensor device to sleep if supported.

§Errors

Returns SetModeError::Uninitialized if the sensor driver is not initialized.

fn state(&self) -> State

Returns the current sensor driver state.

fn categories(&self) -> &'static [Category]

Returns the categories the sensor device is part of.

fn label(&self) -> Option<&'static str>

String label of the sensor driver instance. For instance, in the case of a temperature sensor, this allows to specify whether this specific sensor device is placed indoor or outdoor.

fn display_name(&self) -> Option<&'static str>

Returns a human-readable name of the sensor driver. For instance, “push button” and “3-axis accelerometer” are appropriate display names.

§Note

Different sensor drivers for the same sensor device may have different display names.

fn part_number(&self) -> Option<&'static str>

Returns the sensor device part number. Returns None when the sensor device does not have a part number. For instance, “DS18B20” is a valid part number.

fn version(&self) -> u8

Returns the sensor driver version number.

Implementors§