Struct sysfs_gpio::Pin [] [src]

pub struct Pin {
    // some fields omitted


impl Pin

Create a new Pin with the provided pin_num

This function does not export the provided pin_num.

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.

Get the pin number

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.


use sysfs_gpio::{Pin, Direction};

let gpio = Pin::new(24);
let res = gpio.with_exported(|| {
    println!("At this point, the Pin is exported");
    // ...

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).

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.


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


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),

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.

Get the pin number for the Pin

Get the direction of the Pin

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.

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).

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).

Get the currently configured edge for this pin

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

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.

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.

Trait Implementations

impl Eq for Pin

impl PartialEq for Pin

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Debug for Pin

Formats the value using the given formatter.

impl Clone for Pin

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more