Skip to main content

Led

Struct Led 

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

Monochrome LED with polarity chosen at construction time.

Stores a PolarityMode value — each operation incurs one match which is negligible on Cortex-M.

No internal state cache — is_on() reads the hardware register (ODR) directly and applies the polarity conversion. In the embassy ecosystem OutputPin::Error is Infallible, so unwrapping results is safe.

§Examples

use status_led::{Led, PolarityMode};

let mut led = Led::new(pin, PolarityMode::ActiveLow).unwrap();
led.on().unwrap();
led.toggle().unwrap();
assert!(led.is_on().unwrap());

Implementations§

Source§

impl<P> Led<P>

Source

pub fn from_pin(pin: P, polarity: PolarityMode) -> Self

Build from an already-configured pin without changing its level.

Prefer this when the HAL already set the pin to a known state (e.g. Output::new(pin, Level::High, Speed::Low) for an active-low LED).

Source§

impl<P: OutputPin> Led<P>

Source

pub fn new(pin: P, polarity: PolarityMode) -> Result<Self, P::Error>

Build and force the pin to the logical OFF state.

Safest default — guarantees the LED starts dark regardless of the pin’s reset state.

Source

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

Turn the LED on (logical ON → physical level determined by polarity).

Source

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

Turn the LED off (logical OFF → physical level determined by polarity).

Source

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

Set the logical state: true = ON, false = OFF.

Source§

impl<P: StatefulOutputPin> Led<P>

Source

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

Read the logical state from the hardware register.

Reads the physical pin level (ODR register on STM32), then converts through the polarity. Requires &mut self because StatefulOutputPin::is_set_high does — in practice the read is a single register access with no side effects.

Source

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

Inverse of is_on.

Source

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

Toggle the logical state.

Delegates to StatefulOutputPin::toggle. On embassy this is a single bit-band operation (ODR ^= 1 << n) — unconditionally flips the physical pin, which is always correct regardless of polarity.

Source§

impl<P> Led<P>

Source

pub fn polarity(&self) -> PolarityMode

Return the current polarity.

Source

pub fn release(self) -> P

Consume the Led and return the underlying pin.

Trait Implementations§

Source§

impl<P> Debug for Led<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 Led<P>
where P: Freeze,

§

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

§

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

§

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

§

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

§

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

§

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