Skip to main content

Trigger

Struct Trigger 

Source
pub struct Trigger { /* private fields */ }
Expand description

A threshold with hysteresis that reports the moment it is crossed.

This is the helper behind “when the tank drops below 20 percent”: it holds a line, says once when a reading crosses it, and says once more when the reading has come back far enough to count. The hysteresis is the release band on the far side of the line, so a reading hovering at the threshold cannot fire the trigger over and over. Between the line and the release band the trigger holds its state.

A Thermostat answers the same question as a level to hold; a trigger answers it as an event to act on, which is what a rule wants.

§Examples

use pamoja_kit::{Edge, Trigger};

// Water when the bed is drier than 30, and stop once it is wetter than 35.
let mut dry = Trigger::below(30.0, 5.0);
assert_eq!(dry.update(42.0), None); // well above the line
assert_eq!(dry.update(28.0), Some(Edge::Set)); // crossed it: fire once
assert_eq!(dry.update(33.0), None); // inside the release band: hold
assert!(dry.is_set());
assert_eq!(dry.update(36.0), Some(Edge::Cleared)); // past the band: clear once
assert_eq!(dry.update(20.0), Some(Edge::Set)); // and it can fire again

Implementations§

Source§

impl Trigger

Source

pub fn above(threshold: f32, hysteresis: f32) -> Self

Creates a trigger that fires when a reading rises above a line.

It sets when the reading exceeds threshold and clears once the reading has fallen below threshold less hysteresis.

§Arguments
  • threshold - the line a rising reading crosses. One that is not a number never fires.
  • hysteresis - how far below the line the reading must fall to clear; its magnitude is used, and one that is not a number is taken as zero.
§Returns

A trigger that starts cleared.

Source

pub fn below(threshold: f32, hysteresis: f32) -> Self

Creates a trigger that fires when a reading falls below a line.

It sets when the reading drops under threshold and clears once the reading has risen above threshold plus hysteresis.

§Arguments
  • threshold - the line a falling reading crosses. One that is not a number never fires.
  • hysteresis - how far above the line the reading must rise to clear; its magnitude is used, and one that is not a number is taken as zero.
§Returns

A trigger that starts cleared.

Source

pub fn update(&mut self, reading: f32) -> Option<Edge>

Feeds a reading in and reports whether it changed the trigger’s state.

A reading that is not a finite number, such as the NaN a failed sensor reports, is ignored: it neither sets nor clears the trigger.

§Arguments
  • reading - the latest measured value.
§Returns

Edge::Set the moment the reading crosses the line, Edge::Cleared the moment it comes back past the release band, and None while nothing changed.

Source

pub fn is_set(&self) -> bool

Returns whether the condition currently holds.

§Returns

true from the reading that set the trigger until the one that clears it.

Source

pub fn threshold(&self) -> f32

Returns the line the trigger watches.

§Returns

The threshold it was created with.

Source

pub fn hysteresis(&self) -> f32

Returns the release band on the far side of the line.

§Returns

The hysteresis it was created with, as a magnitude.

Source

pub fn watches_above(&self) -> bool

Returns whether the trigger watches a rising reading.

§Returns

true for above, false for below.

Trait Implementations§

Source§

impl Clone for Trigger

Source§

fn clone(&self) -> Trigger

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Trigger

Source§

impl Debug for Trigger

Source§

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

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

impl PartialEq for Trigger

Source§

fn eq(&self, other: &Trigger) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Trigger

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.