switch_hal

Struct Switch

source
pub struct Switch<IoPin, ActiveLevel> { /* private fields */ }
Expand description

Concrete implementation for InputSwitch and OutputSwitch

§Type Params

  • IoPin must be a type that implements either of the InputPin or OutputPin traits.
  • ActiveLevel indicates whether the Switch is ActiveHigh or ActiveLow. ActiveLevel is not actually stored in the struct. It’s PhantomData used to indicate which implementation to use.

Implementations§

source§

impl<IoPin, ActiveLevel> Switch<IoPin, ActiveLevel>

source

pub fn new(pin: IoPin) -> Self

Constructs a new Switch from a concrete implementation of an InputPin or OutputPin

Prefer the IntoSwitch trait over calling new directly.

§Examples

Active High

use switch_hal::{ActiveHigh, OutputSwitch, Switch};
let mut led = Switch::<_, ActiveHigh>::new(pin);

ActiveLow

use switch_hal::{ActiveLow, OutputSwitch, Switch};
let mut led = Switch::<_, ActiveLow>::new(pin);

stm32f3xx-hal

// Example for the stm32f303
use stm32f3xx_hal::gpio::gpioe;
use stm32f3xx_hal::gpio::{PushPull, Output};
use stm32f3xx_hal::stm32;

use switch_hal::{ActiveHigh, Switch};

let device_periphs = stm32::Peripherals::take().unwrap();
let gpioe = device_periphs.GPIOE.split(&mut reset_control_clock.ahb);

let led = Switch::<_, ActiveHigh>::new(
    gpioe
    .pe9
    .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper)
)
source

pub fn into_pin(self) -> IoPin

Consumes the Switch and returns the underlying InputPin or OutputPin.

This is useful fore retrieving the underlying pin to use it for a different purpose.

§Examples
use switch_hal::{OutputSwitch, Switch, IntoSwitch};
let mut led = pin.into_active_high_switch();
led.on().ok();
let mut pin = led.into_pin();
// do something else with the pin

Trait Implementations§

source§

impl<T: InputPin> InputSwitch for Switch<T, ActiveHigh>

source§

type Error = <T as InputPin>::Error

source§

fn is_active(&self) -> Result<bool, Self::Error>

Returns true if the swich has been activated, otherwise false i.e. if a button is currently pressed, returns true Read more
source§

impl<T: InputPin> InputSwitch for Switch<T, ActiveLow>

source§

type Error = <T as InputPin>::Error

source§

fn is_active(&self) -> Result<bool, Self::Error>

Returns true if the swich has been activated, otherwise false i.e. if a button is currently pressed, returns true Read more
source§

impl<T: OutputPin> OutputSwitch for Switch<T, ActiveHigh>

source§

type Error = <T as OutputPin>::Error

source§

fn on(&mut self) -> Result<(), Self::Error>

Turns the switch on Read more
source§

fn off(&mut self) -> Result<(), Self::Error>

Turns the switch off Read more
source§

impl<T: OutputPin> OutputSwitch for Switch<T, ActiveLow>

source§

type Error = <T as OutputPin>::Error

source§

fn on(&mut self) -> Result<(), Self::Error>

Turns the switch on Read more
source§

fn off(&mut self) -> Result<(), Self::Error>

Turns the switch off Read more
source§

impl<T: OutputPin + StatefulOutputPin> StatefulOutputSwitch for Switch<T, ActiveHigh>

source§

type Error = <T as OutputPin>::Error

source§

fn is_on(&mut self) -> Result<bool, Self::Error>

Checks whether the switch is on Read more
source§

fn is_off(&mut self) -> Result<bool, Self::Error>

Checks whether the switch is off Read more
source§

impl<T: OutputPin + StatefulOutputPin> StatefulOutputSwitch for Switch<T, ActiveLow>

source§

type Error = <T as OutputPin>::Error

source§

fn is_on(&mut self) -> Result<bool, Self::Error>

Checks whether the switch is on Read more
source§

fn is_off(&mut self) -> Result<bool, Self::Error>

Checks whether the switch is off Read more
source§

impl<T: OutputPin + ToggleableOutputPin, ActiveLevel> ToggleableOutputSwitch for Switch<T, ActiveLevel>

source§

type Error = <T as ToggleableOutputPin>::Error

source§

fn toggle(&mut self) -> Result<(), Self::Error>

Toggles the current state of the OutputSwitch Read more

Auto Trait Implementations§

§

impl<IoPin, ActiveLevel> Freeze for Switch<IoPin, ActiveLevel>
where IoPin: Freeze,

§

impl<IoPin, ActiveLevel> RefUnwindSafe for Switch<IoPin, ActiveLevel>
where IoPin: RefUnwindSafe, ActiveLevel: RefUnwindSafe,

§

impl<IoPin, ActiveLevel> Send for Switch<IoPin, ActiveLevel>
where IoPin: Send, ActiveLevel: Send,

§

impl<IoPin, ActiveLevel> Sync for Switch<IoPin, ActiveLevel>
where IoPin: Sync, ActiveLevel: Sync,

§

impl<IoPin, ActiveLevel> Unpin for Switch<IoPin, ActiveLevel>
where IoPin: Unpin, ActiveLevel: Unpin,

§

impl<IoPin, ActiveLevel> UnwindSafe for Switch<IoPin, ActiveLevel>
where IoPin: UnwindSafe, ActiveLevel: 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> IntoSwitch for T

source§

fn into_switch<ActiveLevel>(self) -> Switch<T, ActiveLevel>

Consumes the IoPin returning a Switch of the appropriate ActiveLevel. Read more
source§

fn into_active_low_switch(self) -> Switch<Self, ActiveLow>
where Self: Sized,

Consumes the IoPin returning a Switch<IoPin, ActiveLow>. Read more
source§

fn into_active_high_switch(self) -> Switch<Self, ActiveHigh>
where Self: Sized,

Consumes the IoPin returning a Switch<IoPin, ActiveHigh>. Read more
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.

Layout§

Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.