pub struct DedicatedGpioInputBundle<'lt> { /* private fields */ }Expand description
A bundle of dedicated GPIO input drivers.
An input bundle precomputes a channel mask from one or more DedicatedGpioInput
drivers. This lets you read multiple dedicated input channels with a single
low-level read (see DedicatedGpioInputBundle::levels).
Attaching a driver does not change any pin state. The bundle only stores a channel mask; it does not own pins or remember which GPIOs were connected.
§Relationship to pins
Dedicated input channels are fed by GPIO pins that were connected when creating
DedicatedGpioInput. The bundle operates on channels, not individual pins:
each bit in the returned mask corresponds to a dedicated input channel.
If you later reconfigure a GPIO pin (e.g. connect it to a different peripheral input), that changes what the channel reads. The bundle does not prevent such reconfiguration.
§Examples
use esp_hal::gpio::{
Input,
InputConfig,
dedicated::{DedicatedGpio, DedicatedGpioInput, DedicatedGpioInputBundle},
};
// Create channels:
let channels = DedicatedGpio::new(peripherals.GPIO_DEDICATED);
// Create input drivers for two channels:
let p0 = Input::new(peripherals.GPIO0, InputConfig::default());
let p1 = Input::new(peripherals.GPIO1, InputConfig::default());
let in0 = DedicatedGpioInput::new(channels.channel0, p0);
let in1 = DedicatedGpioInput::new(channels.channel1, p1);
// Build a bundle that reads channels 0 and 1:
let mut bundle = DedicatedGpioInputBundle::new();
bundle.enable_input(&in0).enable_input(&in1);
// Read both channels at once:
let bits = bundle.levels();
// bit 0 -> channel 0, bit 1 -> channel 1, ...Implementations§
Source§impl<'lt> DedicatedGpioInputBundle<'lt>
impl<'lt> DedicatedGpioInputBundle<'lt>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty dedicated GPIO input bundle.
A bundle is a logical grouping of one or more DedicatedGpioInput drivers.
Internally, it stores a precomputed channel mask which allows reading multiple
dedicated input channels efficiently.
The returned bundle initially contains no channels. Add inputs using
Self::enable_input.
§Notes
- Creating a bundle does not configure any hardware by itself.
Sourcepub fn mask(&self) -> u32
pub fn mask(&self) -> u32
Returns the channel mask of this bundle.
Each bit corresponds to one dedicated GPIO channel:
- bit 0 -> channel 0
- bit 1 -> channel 1
- …
A bit is set to 1 if that channel is currently included in the bundle.
§Example
use esp_hal::gpio::{
Input,
InputConfig,
dedicated::{DedicatedGpio, DedicatedGpioInput, DedicatedGpioInputBundle},
};
let channels = DedicatedGpio::new(peripherals.GPIO_DEDICATED);
let in0 = DedicatedGpioInput::new(
channels.channel0,
Input::new(peripherals.GPIO0, InputConfig::default()),
);
let in2 = DedicatedGpioInput::new(
channels.channel2,
Input::new(peripherals.GPIO2, InputConfig::default()),
);
let mut bundle = DedicatedGpioInputBundle::new();
bundle.enable_input(&in0).enable_input(&in2);
let mask = bundle.mask(); // bit 0 -> ch0, bit 2 -> ch2, ...
// For this bundle: mask == 0b0000_0101 (channels 0 and 2).
Sourcepub fn enable_input<'d>(
&mut self,
inp: &'lt DedicatedGpioInput<'d>,
) -> &mut Self
pub fn enable_input<'d>( &mut self, inp: &'lt DedicatedGpioInput<'d>, ) -> &mut Self
Attaches (enables) an already-configured dedicated input driver to this bundle. After
enabling, you will be able to read the input channels of inp via this bundle.
This method logically borrows the provided DedicatedGpioInput for the lifetime 'lt.
Multiple bundles may borrow the same input driver at the same time. Check examples in the
module-level documentation for more.
§Notes
- This function does not change any input state.
Sourcepub fn disable_input<'d>(
&mut self,
inp: &'lt DedicatedGpioInput<'d>,
) -> &mut Self
pub fn disable_input<'d>( &mut self, inp: &'lt DedicatedGpioInput<'d>, ) -> &mut Self
Disables a dedicated input driver in this bundle.
This updates the internal mask by clearing the channel bit(s) of inp. After disabling,
future bundle operations will no longer touch those channels.
§Notes
- This does not affect
inpitself. It only changes which channels future bundle operations will touch. - This does not end the lifetime-based borrow of
inp. Even after disabling,inpstill cannot be moved or dropped while this bundle exists. - You can re-enable it later via
Self::enable_input.
Sourcepub fn levels(&self) -> u32
pub fn levels(&self) -> u32
Reads the current state of the channels included by this bundle.
For channels outside the bundle mask, the corresponding bits are always 0.
§Example
If the bundle mask is 0b0000_1011 (channels 0, 1, and 3), then
levels() will only contain bits 0, 1, and 3, regardless of the state
of other channels.