pub struct Output<'d> { /* private fields */ }
Expand description
Push-pull digital output.
This driver configures the GPIO pin to be an output driver.
Implementations§
Source§impl<'d> Output<'d>
impl<'d> Output<'d>
Sourcepub fn new(
pin: impl OutputPin + 'd,
initial_level: Level,
config: OutputConfig,
) -> Self
pub fn new( pin: impl OutputPin + 'd, initial_level: Level, config: OutputConfig, ) -> Self
Creates a new GPIO output driver.
The initial_level
parameter sets the initial output level of the pin.
The config
parameter sets the drive mode, drive strength, and pull
direction of the pin.
§Example
The following example configures GPIO5
to pulse a LED once. The
example assumes that the LED is connected such that it is on when
the pin is low.
use esp_hal::{
delay::Delay,
gpio::{Level, Output, OutputConfig},
};
fn blink_once(led: &mut Output<'_>, delay: &mut Delay) {
led.set_low();
delay.delay_millis(500);
led.set_high();
}
let config = OutputConfig::default();
let mut led = Output::new(peripherals.GPIO5, Level::High, config);
let mut delay = Delay::new();
blink_once(&mut led, &mut delay);
Sourcepub fn into_peripheral_output(self) -> OutputSignal<'d>
Available on crate feature unstable
only.
pub fn into_peripheral_output(self) -> OutputSignal<'d>
unstable
only.Turns the pin object into a peripheral output.
The output signal can be passed to peripherals in place of an output pin.
Note that the signal returned by this function is frozen.
§Example
use esp_hal::gpio::{Level, Output, OutputConfig};
let pin1_gpio = Output::new(peripherals.GPIO1, Level::High, config);
let output = pin1_gpio.into_peripheral_output();
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn apply_config(&mut self, config: &OutputConfig)
pub fn apply_config(&mut self, config: &OutputConfig)
Change the configuration.
§Example
use esp_hal::gpio::{DriveMode, Level, Output, OutputConfig};
let mut pin = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
pin.apply_config(&OutputConfig::default().with_drive_mode(DriveMode::OpenDrain));
Sourcepub fn set_high(&mut self)
pub fn set_high(&mut self)
Set the output as high.
§Example
use esp_hal::gpio::{Level, Output, OutputConfig};
let mut pin = Output::new(peripherals.GPIO5, Level::Low, OutputConfig::default());
pin.set_high();
Sourcepub fn set_low(&mut self)
pub fn set_low(&mut self)
Set the output as low.
§Example
use esp_hal::gpio::{Level, Output, OutputConfig};
let mut pin = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
pin.set_low();
Sourcepub fn set_level(&mut self, level: Level)
pub fn set_level(&mut self, level: Level)
Set the output level.ç
§Example
use esp_hal::gpio::{Level, Output, OutputConfig};
let mut pin = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
pin.set_level(Level::Low);
Sourcepub fn is_set_high(&self) -> bool
pub fn is_set_high(&self) -> bool
Returns whether the pin is set to high level.
This function reads back the value set using set_level
, set_high
or
set_low
. It does not need the input stage to be enabled.
§Example
use esp_hal::gpio::{Level, Output, OutputConfig};
let pin = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
let is_high = pin.is_set_high();
Sourcepub fn is_set_low(&self) -> bool
pub fn is_set_low(&self) -> bool
Returns whether the pin is set to low level.
This function reads back the value set using set_level
, set_high
or
set_low
. It does not need the input stage to be enabled.
§Example
use esp_hal::gpio::{Level, Output, OutputConfig};
let pin = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
let is_low = pin.is_set_low();
Sourcepub fn output_level(&self) -> Level
pub fn output_level(&self) -> Level
Returns which level the pin is set to.
This function reads back the value set using set_level
, set_high
or
set_low
. It does not need the input stage to be enabled.
§Example
use esp_hal::gpio::{Level, Output, OutputConfig};
let pin = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
let level = pin.output_level();
Sourcepub fn toggle(&mut self)
pub fn toggle(&mut self)
Toggles the pin output.
If the pin was previously set to high, it will be set to low, and vice versa.
§Example
use esp_hal::gpio::{Level, Output, OutputConfig};
let mut pin = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
pin.toggle();
Trait Implementations§
Source§impl<'d> From<Output<'d>> for OutputSignal<'d>
Available on crate feature unstable
only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
impl<'d> From<Output<'d>> for OutputSignal<'d>
unstable
only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§impl StatefulOutputPin for Output<'_>
impl StatefulOutputPin for Output<'_>
impl<'d> PeripheralOutput<'d> for Output<'d>
unstable
only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
impl<'d> PeripheralSignal<'d> for Output<'d>
unstable
only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.