pub struct Request { /* private fields */ }
Expand description
An active request of a set of lines.
Requests are built by the Builder
, which itself can be constructed by builder
.
Output Lifetime
The value of an output line is only guaranteed for the lifetime of the request. If the request is dropped then the output value becomes indeterminate - it may remain unchanged or it may reset to the default value (depending on a number of factors including the kernel driver for your hardware and whether other lines on the chip are still requested).
So keep the request alive to be sure of the output value.
Event Buffering
Events are buffered both in the kernel and in user space.
The purpose of the kernel event buffer is to store events that may occur in a burst at
a faster rate than they can be handled by the user space process.
The kernel event buffer can be customised to some extent by the with_kernel_event_buffer_size
option.
A number of options are available for user space buffering, i.e. where the events are to be written when they are read from the kernel.
The read_edge_event
reads a single event onto the stack.
The EdgeEventBuffer
provides buffering on the heap, and can reduce reading overheads
by reading multiple events from the kernel at once. The edge_events
iterator uses an
EdgeEventBuffer, the size of which is controlled by with_user_event_buffer_size
It is also possible to read multiple events into a user specified location using
read_edge_events_into_slice
.
As with EdgeEventBuffer
this may reduce read overheads when reading a burst of events
from the kernel. The edge_event_size
method provides the size required to store a
single event to allow sizing of custom slices.
Reading Output Values
Note that reading back output values using value
or values
is dependent on driver
and hardware support and so cannot be guaranteed to work, though frequently it does.
Test with your particular hardware to be sure.
Implementations
sourceimpl Request
impl Request
sourcepub fn builder() -> Builder
pub fn builder() -> Builder
Start building a new request.
Examples
let req = Request::builder()
.on_chip("/dev/gpiochip0")
.with_lines(&[3,5])
.request()?;
let mut values = Values::default();
req.values(&mut values)?;
sourcepub fn from_config(config: Config) -> Builder
pub fn from_config(config: Config) -> Builder
Start building a new request using the provided config.
Examples
let mut cfg = Config::default();
cfg.with_line(5).as_output(Value::Active);
let req = Request::from_config(cfg)
.on_chip("/dev/gpiochip0")
.request()?;
sourcepub fn values(&self, values: &mut Values) -> Result<()>
pub fn values(&self, values: &mut Values) -> Result<()>
Get the values for a subset of the requested lines.
The keys indicate the lines to get. Keys that are not requested offsets are ignored. If no keys are set then all requested lines are returned.
Examples
let req = Request::builder()
.on_chip("/dev/gpiochip0")
.with_lines(&[3,5,6,8])
.request()?;
// subset of lines
let mut values = Values::from_offsets(&[3,8]);
req.values(&mut values)?;
// all requested lines
let mut values = Values::default();
req.values(&mut values)?;
sourcepub fn value(&self, offset: Offset) -> Result<Value>
pub fn value(&self, offset: Offset) -> Result<Value>
Get the value for one line in the request.
Examples
let req = Request::builder()
.on_chip("/dev/gpiochip0")
.with_lines(&[3,5])
.request()?;
let v5 = req.value(5)?;
sourcepub fn set_values(&self, values: &Values) -> Result<()>
pub fn set_values(&self, values: &Values) -> Result<()>
Set the values for a subset of the requested lines.
Examples
let req = Request::builder()
.on_chip("/dev/gpiochip0")
.with_lines(&[3,5,6,8])
.as_output(Active)
.request()?;
let mut values = Values::default();
values.set(5, Inactive).set(6, Inactive);
req.set_values(&values)?;
sourcepub fn set_value(&self, offset: Offset, value: Value) -> Result<()>
pub fn set_value(&self, offset: Offset, value: Value) -> Result<()>
Set the value for one line in the request.
Examples
let mut cfg = Config::default();
cfg.with_line(5).as_output(Value::Active);
let req = Request::from_config(cfg)
.on_chip("/dev/gpiochip0")
.request()?;
req.set_value(5,Value::Inactive)?;
sourcepub fn config(&self) -> Config
pub fn config(&self) -> Config
Get a snapshot of the requested configuration.
This is the configuration currently applied to the hardware.
sourcepub fn line_config(&self, offset: Offset) -> Option<Config>
pub fn line_config(&self, offset: Offset) -> Option<Config>
Get a snapshot of the requested configuration for a particular line.
This is the configuration currently applied to the line.
sourcepub fn reconfigure(&self, new_cfg: &Config) -> Result<()>
pub fn reconfigure(&self, new_cfg: &Config) -> Result<()>
Reconfigure the request with the an updated configuration.
Note that lines cannot be added or removed from the request.
Any additional lines in new_cfg
will be ignored, and any missing
lines will retain their existing configuration.
sourcepub fn edge_events(&self) -> EdgeEventBuffer<'_>ⓘNotable traits for EdgeEventBuffer<'a>impl<'a> Iterator for EdgeEventBuffer<'a> type Item = Result<EdgeEvent>;
pub fn edge_events(&self) -> EdgeEventBuffer<'_>ⓘNotable traits for EdgeEventBuffer<'a>impl<'a> Iterator for EdgeEventBuffer<'a> type Item = Result<EdgeEvent>;
An iterator for events from the request.
By default the events are read from the kernel individually.
The iterator can be backed by a user space buffer using the
Builder.with_user_event_buffer_size
option in the builder.
Examples
let req = Request::builder()
.on_chip("/dev/gpiochip0")
.with_consumer("watcher")
.with_line(23)
.with_edge_detection(EdgeDetection::BothEdges)
.request()?;
for event in req.edge_events() {
println!("{:?}", event?);
}
sourcepub fn has_edge_event(&self) -> Result<bool>
pub fn has_edge_event(&self) -> Result<bool>
Returns true when the request has edge events available to read using read_edge_event
.
sourcepub fn wait_edge_event(&self, timeout: Duration) -> Result<bool>
pub fn wait_edge_event(&self, timeout: Duration) -> Result<bool>
Wait for an edge event to be available.
Returns true if read_edge_event
will return an event without blocking.
sourcepub fn read_edge_event(&self) -> Result<EdgeEvent>
pub fn read_edge_event(&self) -> Result<EdgeEvent>
Read a single edge event from the request.
Will block until an edge event is available.
This is a convenience function.
Reading events using edge_events
or a buffer created using new_edge_event_buffer
may be more performant.
sourcepub fn new_edge_event_buffer(&self, capacity: usize) -> EdgeEventBuffer<'_>ⓘNotable traits for EdgeEventBuffer<'a>impl<'a> Iterator for EdgeEventBuffer<'a> type Item = Result<EdgeEvent>;
pub fn new_edge_event_buffer(&self, capacity: usize) -> EdgeEventBuffer<'_>ⓘNotable traits for EdgeEventBuffer<'a>impl<'a> Iterator for EdgeEventBuffer<'a> type Item = Result<EdgeEvent>;
Create an edge event buffer.
capacity
- The number of events that can be buffered.
sourcepub fn read_edge_events_into_slice(&self, buf: &mut [u8]) -> Result<usize>
pub fn read_edge_events_into_slice(&self, buf: &mut [u8]) -> Result<usize>
Read edge events from the kernel into a user space [u8]
slice.
This is a helper function for the special case where the user prefers to manage the buffer containing raw events, e.g. to place it in a specific location in memory.
This will read in edge_event_size
sized chunks so buf
must be at least
as large as one event.
This function will block if no events are available to read.
buf
- The slice to contain the raw events.
sourcepub fn edge_event_from_slice(&self, buf: &[u8]) -> Result<EdgeEvent>
pub fn edge_event_from_slice(&self, buf: &[u8]) -> Result<EdgeEvent>
Read an edge event from a [u8]
slice.
This is a helper function for the special case where the user prefers to manage the buffer containing raw events, e.g. to place it in a specific location in memory.
Assumes the buffer has been previously populated by a call to
read_edge_events_into_slice
.
buf
- The slice containing the raw event.
sourcepub fn edge_event_size(&self) -> usize
pub fn edge_event_size(&self) -> usize
The number of bytes required to buffer a single event read from the request.
This can be used to size an external [u8]
slice to read events into.
The slice size should be a multiple of this size.