esp_hal::gpio

Struct Input

Source
pub struct Input<'d, P = AnyPin> { /* private fields */ }
Expand description

Digital input.

This driver configures the GPIO pin to be an input. Input drivers read the voltage of their pins and convert it to a logical Level.

Implementations§

Source§

impl<P> Input<'_, P>
where P: InputPin,

Source

pub async fn wait_for_high(&mut self)

Wait until the pin is high. If it is already high, return immediately.

Source

pub async fn wait_for_low(&mut self)

Wait until the pin is low. If it is already low, return immediately.

Source

pub async fn wait_for_rising_edge(&mut self)

Wait for the pin to undergo a transition from low to high.

Source

pub async fn wait_for_falling_edge(&mut self)

Wait for the pin to undergo a transition from high to low.

Source

pub async fn wait_for_any_edge(&mut self)

Wait for the pin to undergo any transition, i.e low to high OR high to low.

Source§

impl<'d> Input<'d>

Source

pub fn new(pin: impl Peripheral<P = impl InputPin> + 'd, pull: Pull) -> Self

Creates a new, type-erased GPIO input.

The pull parameter configures internal pull-up or pull-down resistors.

§Example

The following example configures GPIO5 to read a button press. The example assumes that the button is connected such that the pin is low when the button is pressed.

use esp_hal::gpio::{Level, Input, Pull};
use esp_hal::delay::Delay;

fn print_when_pressed(button: &mut Input<'_>, delay: &mut Delay) {
    let mut was_pressed = false;
    loop {
        let is_pressed = button.is_low();
        if is_pressed && !was_pressed {
            println!("Button pressed!");
        }
        was_pressed = is_pressed;
        delay.delay_millis(100);
    }
}

let mut button = Input::new(
    peripherals.GPIO5,
    Pull::Up,
);
let mut delay = Delay::new();

print_when_pressed(&mut button, &mut delay);
Source§

impl<'d, P> Input<'d, P>
where P: InputPin,

Source

pub fn new_typed(pin: impl Peripheral<P = P> + 'd, pull: Pull) -> Self

Creates a new, typed GPIO input.

The pull parameter configures internal pull-up or pull-down resistors.

This constructor is useful when you want to limit which GPIO pin can be used for a particular function.

§Example

The following example configures GPIO5 to read a button press. The example assumes that the button is connected such that the pin is low when the button is pressed.

use esp_hal::gpio::{GpioPin, Level, Input, Pull};
use esp_hal::delay::Delay;

fn print_when_pressed(
    button: &mut Input<'_, GpioPin<5>>,
    delay: &mut Delay,
) {
    let mut was_pressed = false;
    loop {
        let is_pressed = button.is_low();
        if is_pressed && !was_pressed {
            println!("Button pressed!");
        }
        was_pressed = is_pressed;
        delay.delay_millis(100);
    }
}

let mut button = Input::new_typed(
    peripherals.GPIO5,
    Pull::Up,
);
let mut delay = Delay::new();

print_when_pressed(&mut button, &mut delay);
Source

pub fn peripheral_input(&self) -> InputSignal

Returns a peripheral input connected to this pin.

The input signal can be passed to peripherals in place of an input pin.

Source

pub fn is_high(&self) -> bool

Get whether the pin input level is high.

Source

pub fn is_low(&self) -> bool

Get whether the pin input level is low.

Source

pub fn level(&self) -> Level

Get the current pin input level.

Source

pub fn listen(&mut self, event: Event)

Listen for interrupts

Source

pub fn unlisten(&mut self)

Stop listening for interrupts

Source

pub fn clear_interrupt(&mut self)

Clear the interrupt status bit for this Pin

Source

pub fn is_interrupt_set(&self) -> bool

Checks if the interrupt status bit for this Pin is set

Source

pub fn wakeup_enable(&mut self, enable: bool, event: WakeEvent)

Enable as a wake-up source.

This will unlisten for interrupts

Source§

impl<P> Input<'_, P>
where P: InputPin + OutputPin,

Source

pub fn split(self) -> (InputSignal, OutputSignal)

Split the pin into an input and output signal.

Peripheral signals allow connecting peripherals together without using external hardware.

Source

pub fn into_peripheral_output(self) -> OutputSignal

Turns the pin object into a peripheral output.

The output signal can be passed to peripherals in place of an output pin.

Trait Implementations§

Source§

impl<P> ErrorType for Input<'_, P>
where P: InputPin,

Source§

type Error = Infallible

Error type
Source§

impl<P> InputPin for Input<'_, P>
where P: InputPin,

Source§

type Error = Infallible

Error type
Source§

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

Is the input pin high?
Source§

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

Is the input pin low?
Source§

impl<P> InputPin for Input<'_, P>
where P: InputPin,

Source§

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

Is the input pin high?
Source§

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

Is the input pin low?
Source§

impl<'d, P> Peripheral for Input<'d, P>

Source§

type P = Flex<'d, P>

Peripheral singleton type
Source§

unsafe fn clone_unchecked(&self) -> Self::P

Unsafely clone (duplicate) a peripheral singleton. Read more
Source§

fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>
where Self: 'a,

Convert a value into a PeripheralRef. Read more
Source§

fn map_into<U>(self) -> U
where Self::P: Into<U>, U: Peripheral<P = U>,

Map the peripheral using Into. Read more
Source§

impl<P> Wait for Input<'_, P>
where P: InputPin,

Source§

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

Wait until the pin is high. If it is already high, return immediately. Read more
Source§

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

Wait until the pin is low. If it is already low, return immediately. Read more
Source§

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

Wait for the pin to undergo a transition from low to high. Read more
Source§

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

Wait for the pin to undergo a transition from high to low. Read more
Source§

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

Wait for the pin to undergo any transition, i.e low to high OR high to low.

Auto Trait Implementations§

§

impl<'d, P> Freeze for Input<'d, P>
where P: Freeze,

§

impl<'d, P> RefUnwindSafe for Input<'d, P>
where P: RefUnwindSafe,

§

impl<'d, P> Send for Input<'d, P>
where P: Send,

§

impl<'d, P> Sync for Input<'d, P>
where P: Sync,

§

impl<'d, P> Unpin for Input<'d, P>
where P: Unpin,

§

impl<'d, P = AnyPin> !UnwindSafe for Input<'d, P>

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.