Skip to main content

Switch

Struct Switch 

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

An on/off part on an output line: a relay, a valve, a lamp.

§Examples

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

// A relay board whose inputs are pulled low to energize the coil.
let mut valve = Switch::new(PinScript::new([]), Polarity::ActiveLow);
block_on(valve.apply(true))?;
block_on(valve.apply(false))?;
assert_eq!(valve.release().driven(), [PinState::Low, PinState::High]);

Implementations§

Source§

impl<P> Switch<P>

Source

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

Wraps an output line, without driving it.

§Arguments
  • pin - the output line.
  • polarity - which level asserts the part.
§Returns

The switch, recorded as not asserted until it is first driven.

Source

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

Wraps a line that asserts high.

§Arguments
  • pin - the output line.
§Returns

The switch.

Source

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

Wraps a line that asserts low, the shape of most relay boards.

§Arguments
  • pin - the output line.
§Returns

The switch.

Source

pub fn polarity(&self) -> Polarity

Returns the polarity the switch drives with.

Source

pub fn is_asserted(&self) -> bool

Reports whether the part was last driven asserted.

§Returns

true after a successful set(true), false after set(false) or before the line is first driven.

Source

pub fn release(self) -> P

Gives back the line.

§Returns

The pin the switch was built from.

Source§

impl<P: OutputPin> Switch<P>

Source

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

Drives the part asserted or not, at the level its polarity maps to.

§Arguments
  • asserted - true to energize, false to release.
§Errors

Returns the pin’s error if the line cannot be driven; the recorded state is left unchanged.

Trait Implementations§

Source§

impl<P> Actuator for Switch<P>
where P: OutputPin + Send, P::Error: Debug,

Source§

type Command = bool

The command accepted by a single application, for example a setpoint.
Source§

async fn apply(&mut self, asserted: bool) -> Result<()>

Applies a command to the actuator. Read more
§

fn map_command<F, C>(self, convert: F) -> MapCommand<Self, F, C>
where Self: Sized, F: FnMut(C) -> Self::Command,

Wraps this actuator so every command passes through convert first. Read more
Source§

impl<P: Debug> Debug for Switch<P>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P> Freeze for Switch<P>
where P: Freeze,

§

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

§

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

§

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

§

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

§

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

§

impl<P> UnwindSafe for Switch<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.