Skip to main content

Contact

Struct Contact 

Source
pub struct Contact<P> { /* private fields */ }
Expand description

An on/off part on an input line: a button, a float switch, a motion detector.

§Examples

use pamoja_core::Sensor;
use pamoja_gpio::pin::Polarity;
use pamoja_gpio::switch::Contact;
use pamoja_hal::digital::PinState;
use pamoja_hal::script::{block_on, PinScript};

// A float switch pulled up, which closes to ground when the tank is full.
let line = PinScript::new([PinState::High, PinState::Low]);
let mut full = Contact::new(line, Polarity::ActiveLow);
assert!(!block_on(full.read())?);
assert!(block_on(full.read())?);

Implementations§

Source§

impl<P> Contact<P>

Source

pub fn new(pin: P, polarity: Polarity) -> Contact<P>

Wraps an input line.

§Arguments
  • pin - the input line.
  • polarity - which level means the contact is asserted.
§Returns

The contact.

Source

pub fn active_high(pin: P) -> Contact<P>

Wraps a line that reads high when asserted.

§Arguments
  • pin - the input line.
§Returns

The contact.

Source

pub fn active_low(pin: P) -> Contact<P>

Wraps a line pulled up that a closing contact takes low, the common wiring for a button or a float switch.

§Arguments
  • pin - the input line.
§Returns

The contact.

Source

pub fn polarity(&self) -> Polarity

Returns the polarity the contact reads with.

Source

pub fn release(self) -> P

Gives back the line.

§Returns

The pin the contact was built from.

Source§

impl<P: InputPin> Contact<P>

Source

pub fn level(&mut self) -> Result<Level, P::Error>

Reads the physical level of the line.

§Returns

The level, before the polarity is applied.

§Errors

Returns the pin’s error if the line cannot be read.

Source

pub fn is_asserted(&mut self) -> Result<bool, P::Error>

Reads whether the contact is asserted.

§Returns

true when the line sits at the polarity’s asserted level.

§Errors

Returns the pin’s error if the line cannot be read.

Trait Implementations§

Source§

impl<P: Debug> Debug for Contact<P>

Source§

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

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

impl<P> Sensor for Contact<P>
where P: InputPin + Send, P::Error: Debug,

Source§

type Reading = bool

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

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

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<P> Freeze for Contact<P>
where P: Freeze,

§

impl<P> RefUnwindSafe for Contact<P>
where P: RefUnwindSafe,

§

impl<P> Send for Contact<P>
where P: Send,

§

impl<P> Sync for Contact<P>
where P: Sync,

§

impl<P> Unpin for Contact<P>
where P: Unpin,

§

impl<P> UnsafeUnpin for Contact<P>
where P: UnsafeUnpin,

§

impl<P> UnwindSafe for Contact<P>
where P: 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.