Pin

Struct Pin 

pub struct Pin {
    pub(crate) pin_num: u64,
}

Fields§

§pin_num: u64

Implementations§

§

impl Pin

pub fn new(pin_num: u64) -> Pin

Create a new Pin with the provided pin_num

This function does not export the provided pin_num.

pub fn from_path<T>(path: T) -> Result<Pin, Error>
where T: AsRef<Path>,

Create a new Pin with the provided path

This form is useful when there are other scripts which may have already exported the GPIO and created a symlink with a nice name that you already have reference to. Otherwise, it is generally preferrable to use new directly.

The provided path must be either the already exported directory for a GPIO or a symlink to one. If the directory does not look sufficiently like this (i.e. does not resolve to a path starting with /sys/class/gpioXXX), then this function will return an error.

pub fn get_pin_num(&self) -> u64

Get the pin number

pub fn with_exported<F>(&self, closure: F) -> Result<(), Error>
where F: FnOnce() -> Result<(), Error>,

Run a closure with the GPIO exported

Prior to the provided closure being executed, the GPIO will be exported. After the closure execution is complete, the GPIO will be unexported.

§Example
use sysfs_gpio::{Pin, Direction};

let gpio = Pin::new(24);
let res = gpio.with_exported(|| {
    println!("At this point, the Pin is exported");
    gpio.set_direction(Direction::Low)?;
    gpio.set_value(1)?;
    // ...
    Ok(())
});

pub fn is_exported(&self) -> bool

Determines whether the GPIO is exported

This function will error out if the kernel does not support the GPIO sysfs interface (i.e. /sys/class/gpio does not exist).

pub fn export(&self) -> Result<(), Error>

Export the GPIO

This is equivalent to echo N > /sys/class/gpio/export with the exception that the case where the GPIO is already exported is not an error.

§Errors

The main cases in which this function will fail and return an error are the following:

  1. The system does not support the GPIO sysfs interface
  2. The requested GPIO is out of range and cannot be exported
  3. The requested GPIO is in use by the kernel and cannot be exported by use in userspace
§Example
use sysfs_gpio::Pin;

let gpio = Pin::new(24);
match gpio.export() {
    Ok(()) => println!("Gpio {} exported!", gpio.get_pin()),
    Err(err) => println!("Gpio {} could not be exported: {}", gpio.get_pin(), err),
}

pub fn unexport(&self) -> Result<(), Error>

Unexport the GPIO

This function will unexport the provided by from syfs if it is currently exported. If the pin is not currently exported, it will return without error. That is, whenever this function returns Ok, the GPIO is not exported.

pub fn get_pin(&self) -> u64

Get the pin number for the Pin

pub fn get_direction(&self) -> Result<Direction, Error>

Get the direction of the Pin

pub fn set_direction(&self, dir: Direction) -> Result<(), Error>

Set this GPIO as either an input or an output

The basic values allowed here are Direction::In and Direction::Out which set the Pin as either an input or output respectively. In addition to those, two additional settings of Direction::High and Direction::Low. These both set the Pin as an output but do so with an initial value of high or low respectively. This allows for glitch-free operation.

Note that this entry may not exist if the kernel does not support changing the direction of a pin in userspace. If this is the case, you will get an error.

pub fn get_value(&self) -> Result<u8, Error>

Get the value of the Pin (0 or 1)

If successful, 1 will be returned if the pin is high and 0 will be returned if the pin is low (this may or may not match the signal level of the actual signal depending on the GPIO “active_low” entry).

pub fn set_value(&self, value: u8) -> Result<(), Error>

Set the value of the Pin

This will set the value of the pin either high or low. A 0 value will set the pin low and any other value will set the pin high (1 is typical).

pub fn get_edge(&self) -> Result<Edge, Error>

Get the currently configured edge for this pin

This value will only be present if the Pin allows for interrupts.

pub fn set_edge(&self, edge: Edge) -> Result<(), Error>

Set the edge on which this GPIO will trigger when polled

The configured edge determines what changes to the Pin will result in poll() returning. This call will return an Error if the pin does not allow interrupts.

pub fn get_active_low(&self) -> Result<bool, Error>

Get polarity of the Pin (true is active low)

pub fn set_active_low(&self, active_low: bool) -> Result<(), Error>

Set the polarity of the Pin (true is active low)

This will affect “rising” and “falling” edge triggered configuration.

pub fn get_poller(&self) -> Result<PinPoller, Error>

Get a PinPoller object for this pin

This pin poller object will register an interrupt with the kernel and allow you to poll() on it and receive notifications that an interrupt has occured with minimal delay.

pub fn get_async_poller(&self) -> Result<AsyncPinPoller, Error>

Get an AsyncPinPoller object for this pin

The async pin poller object can be used with the mio crate. You should probably call set_edge() before using this.

This method is only available when the mio-evented crate feature is enabled.

pub fn get_stream(&self) -> Result<PinStream, Error>

Get a Stream of pin interrupts for this pin

The PinStream object can be used with the tokio crate. You should probably call set_edge() before using this.

This method is only available when the async-tokio crate feature is enabled.

pub fn get_value_stream(&self) -> Result<PinValueStream, Error>

Get a Stream of pin values for this pin

The PinStream object can be used with the tokio crate. You should probably call set_edge(Edge::BothEdges) before using this.

Note that the values produced are the value of the pin as soon as we get to handling the interrupt in userspace. Each time this stream produces a value, a change has occurred, but it could end up producing the same value multiple times if the value has changed back between when the interrupt occurred and when the value was read.

This method is only available when the async-tokio crate feature is enabled.

Trait Implementations§

§

impl Clone for Pin

§

fn clone(&self) -> Pin

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Pin

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl PartialEq for Pin

§

fn eq(&self, other: &Pin) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Copy for Pin

§

impl Eq for Pin

§

impl StructuralPartialEq for Pin

Auto Trait Implementations§

§

impl Freeze for Pin

§

impl RefUnwindSafe for Pin

§

impl Send for Pin

§

impl Sync for Pin

§

impl Unpin for Pin

§

impl UnwindSafe for Pin

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.