# GpioDriver

Summary:[Inherited Constants](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#inhconstants)\|[Methods](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#)  

# GpioDriver

`
public


interface
GpioDriver
`  
`


implements

`[Gpio](https://developer.android.com/reference/com/google/android/things/pio/Gpio)`


`

|-----------------------------------------------------|
| com.google.android.things.userdriver.pio.GpioDriver |

<br />

*** ** * ** ***

GPIO user driver. A GpioDriver must implement all[Gpio](https://developer.android.com/reference/com/google/android/things/pio/Gpio)functions, but additionally this class provides an open() function that will be called whenever a process obtains ownership of the GPIO, to match the existing close() function.

## Summary

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ### Inherited constants                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#)From interface` `[com.google.android.things.pio.Gpio](https://developer.android.com/reference/com/google/android/things/pio/Gpio)` ` |-------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `int` | [ACTIVE_HIGH](https://developer.android.com/reference/com/google/android/things/pio/Gpio#ACTIVE_HIGH) [ActiveType](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setActiveType(int))constant to select active high, which means that logical`true`corresponds with high voltage.                                                                                                                                                               | | `int` | [ACTIVE_LOW](https://developer.android.com/reference/com/google/android/things/pio/Gpio#ACTIVE_LOW) [ActiveType](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setActiveType(int))constant to select active low, which means that logical`true`corresponds with low voltage.                                                                                                                                                                   | | `int` | [DIRECTION_IN](https://developer.android.com/reference/com/google/android/things/pio/Gpio#DIRECTION_IN) [Direction](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setDirection(int))constant used to configure a GPIO pin as an input.                                                                                                                                                                                                         | | `int` | [DIRECTION_OUT_INITIALLY_HIGH](https://developer.android.com/reference/com/google/android/things/pio/Gpio#DIRECTION_OUT_INITIALLY_HIGH) [Direction](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setDirection(int))constant used to configure a GPIO pin as an output and initialize its state to`true`(equivalent of calling[setValue(true)](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setValue(boolean))). | | `int` | [DIRECTION_OUT_INITIALLY_LOW](https://developer.android.com/reference/com/google/android/things/pio/Gpio#DIRECTION_OUT_INITIALLY_LOW) [Direction](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setDirection(int))constant used to configure a GPIO pin as an output and initialize its state to`false`(equivalent of calling[setValue(false)](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setValue(boolean))). | | `int` | [EDGE_BOTH](https://developer.android.com/reference/com/google/android/things/pio/Gpio#EDGE_BOTH) [EdgeType](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setEdgeTriggerType(int))constant used to configure the GPIO interrupt to trigger on any edge (when[getValue()](https://developer.android.com/reference/com/google/android/things/pio/Gpio#getValue())changes state in either direction).                                            | | `int` | [EDGE_FALLING](https://developer.android.com/reference/com/google/android/things/pio/Gpio#EDGE_FALLING) [EdgeType](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setEdgeTriggerType(int))constant used to configure the GPIO interrupt to trigger on falling edge (when[getValue()](https://developer.android.com/reference/com/google/android/things/pio/Gpio#getValue())goes from`true`to`false`).                                           | | `int` | [EDGE_NONE](https://developer.android.com/reference/com/google/android/things/pio/Gpio#EDGE_NONE) [EdgeType](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setEdgeTriggerType(int))constant used to configure the GPIO interrupt to never trigger.                                                                                                                                                                                             | | `int` | [EDGE_RISING](https://developer.android.com/reference/com/google/android/things/pio/Gpio#EDGE_RISING) [EdgeType](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setEdgeTriggerType(int))constant used to configure the GPIO interrupt to trigger on rising edge (when[getValue()](https://developer.android.com/reference/com/google/android/things/pio/Gpio#getValue())goes from`false`to`true`).                                              | |

|                                                                                                                                                                                                                                                                ### Public methods                                                                                                                                                                                                                                                                 ||
|-------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract void`                                                              | ` `[close](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#close())`() ` Closes the GPIO pin.                                                                                                                                                                                                                                                                                                                          |
| ` default `[String](https://developer.android.com/reference/java/lang/String) | ` `[getName](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#getName())`() ` Returns null.                                                                                                                                                                                                                                                                                                                             |
| ` abstract void`                                                              | ` `[open](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#open())`() ` Opens the GPIO pin.                                                                                                                                                                                                                                                                                                                             |
| ` abstract void`                                                              | ` `[registerGpioCallback](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#registerGpioCallback(android.os.Handler, com.google.android.things.pio.GpioCallback))`(`[Handler](https://developer.android.com/reference/android/os/Handler.html)` handler, `[GpioCallback](https://developer.android.com/reference/com/google/android/things/pio/GpioCallback)` callback) ` Registers a callback for GPIO edge interrupts. |
| ` abstract void`                                                              | ` `[unregisterGpioCallback](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#unregisterGpioCallback(com.google.android.things.pio.GpioCallback))`(`[GpioCallback](https://developer.android.com/reference/com/google/android/things/pio/GpioCallback)` callback) ` Unregisters an interrupt callback.                                                                                                                   |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#)From interface` `[com.google.android.things.pio.Gpio](https://developer.android.com/reference/com/google/android/things/pio/Gpio)` ` |-------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void`                                                              | ` `[close](https://developer.android.com/reference/com/google/android/things/pio/Gpio#close())`() ` Releases the GPIO pin.                                                                                                                                                                                                                                                                                                                        | | ` default `[String](https://developer.android.com/reference/java/lang/String) | ` `[getName](https://developer.android.com/reference/com/google/android/things/pio/Gpio#getName())`() ` Returns the I/O name.                                                                                                                                                                                                                                                                                                                     | | ` abstract boolean`                                                           | ` `[getValue](https://developer.android.com/reference/com/google/android/things/pio/Gpio#getValue())`() ` Gets the current value of the GPIO pin (for GPIO pins configured as input only).                                                                                                                                                                                                                                                        | | ` default void`                                                               | ` `[registerGpioCallback](https://developer.android.com/reference/com/google/android/things/pio/Gpio#registerGpioCallback(com.google.android.things.pio.GpioCallback))`(`[GpioCallback](https://developer.android.com/reference/com/google/android/things/pio/GpioCallback)` callback) ` Registers a callback for GPIO edge interrupts.                                                                                                           | | ` abstract void`                                                              | ` `[registerGpioCallback](https://developer.android.com/reference/com/google/android/things/pio/Gpio#registerGpioCallback(android.os.Handler, com.google.android.things.pio.GpioCallback))`(`[Handler](https://developer.android.com/reference/android/os/Handler.html)` handler, `[GpioCallback](https://developer.android.com/reference/com/google/android/things/pio/GpioCallback)` callback) ` Registers a callback for GPIO edge interrupts. | | ` abstract void`                                                              | ` `[setActiveType](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setActiveType(int))`(int activeType) ` Sets the active level.                                                                                                                                                                                                                                                                                       | | ` abstract void`                                                              | ` `[setDirection](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setDirection(int))`(int direction) ` Sets the direction of the GPIO pin.                                                                                                                                                                                                                                                                             | | ` abstract void`                                                              | ` `[setEdgeTriggerType](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setEdgeTriggerType(int))`(int edgeTriggerType) ` Sets the interrupt trigger type.                                                                                                                                                                                                                                                              | | ` abstract void`                                                              | ` `[setValue](https://developer.android.com/reference/com/google/android/things/pio/Gpio#setValue(boolean))`(boolean value) ` Sets the value of the GPIO pin (for GPIO pins configured as output only).                                                                                                                                                                                                                                           | | ` abstract void`                                                              | ` `[unregisterGpioCallback](https://developer.android.com/reference/com/google/android/things/pio/Gpio#unregisterGpioCallback(com.google.android.things.pio.GpioCallback))`(`[GpioCallback](https://developer.android.com/reference/com/google/android/things/pio/GpioCallback)` callback) ` Unregisters an interrupt callback.                                                                                                                   | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#)From interface` `[java.io.Closeable](https://developer.android.com/reference/java/io/Closeable)` ` |------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/io/Closeable#close())`() ` Closes this stream and releases any system resources associated with it. | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#)From interface` `[java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable)` ` |------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/lang/AutoCloseable#close())`() ` Closes this resource, relinquishing any underlying resources. | ||

## Public methods

### close

```
void close ()
```

Closes the GPIO pin. This will be called when the GPIO is closed by its owner. Any resources that need to be cleaned up should be done so here. This method may throw IOException to indicate I/O failure, but the GPIO will still be considered closed and may attempt to be re-opened in the future.

<br />

|                                                                 Throws                                                                  ||
|----------------------------------------------------------------------------|-------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | on I/O failure; the driver will still be considered closed. |

### getName

```
String getName ()
```

Returns null. I/O user drivers can be registered under any name so this returns null by default. Child classes may override this if other behavior is desired.

<br />

|                                  Returns                                   ||
|--------------------------------------------------------------------|--------|
| [String](https://developer.android.com/reference/java/lang/String) | <br /> |

### open

```
void open ()
```

Opens the GPIO pin. This will be called when ownership of a GPIO is obtained. The driver should do any initialization needed to configure the GPIO pin for use here.

<br />

|                                                           Throws                                                           ||
|----------------------------------------------------------------------------|------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | on I/O failure; the driver will remain closed. |

### registerGpioCallback

```
void registerGpioCallback (Handler handler, 
                GpioCallback callback)
```

Registers a callback for GPIO edge interrupts. This API is shared with a standard[Gpio](https://developer.android.com/reference/com/google/android/things/pio/Gpio), but for a GpioDriver some simplifying assumptions can be made:

- Only one[GpioCallback](https://developer.android.com/reference/com/google/android/things/pio/GpioCallback)will ever be registered at a time.
- The Gpio argument to the GpioCallback methods are not needed, so any value (or`null`) may be provided when making callbacks.
- The return value from[onGpioEdge(Gpio)](https://developer.android.com/reference/com/google/android/things/pio/GpioCallback#onGpioEdge(com.google.android.things.pio.Gpio))will always be true and can be safely ignored.
- The driver is allowed but not required to post callbacks on the provided Handler.
- Currently the error value passed to[onGpioError(Gpio, int)](https://developer.android.com/reference/com/google/android/things/pio/GpioCallback#onGpioError(com.google.android.things.pio.Gpio, int))is ignored and`OsConstants.ENODEV`is always used instead.

##### Example: Shared GPIO interrupt line

This is common with I2C-to-GPIO expanders, where a bank of GPIOs will share one common interrupt line. The idea is that the GpioDriver receives the common interrupt signal, figures out which of the expander's GPIOs it actually came from, and then dispatch the interrupt to that particular GpioDriver.  

     // Implements a single GPIO on the expander.
     public class SharedInterruptGpioDriver implements GpioDriver {
         // The underlying GPIO shared between the whole GPIO bank.
         private Gpio mInterruptGpio;

         // Our callback implementation to handle shared GPIO interrupts.
         private SharedGpioCallback mSharedCallback;

         @Override
         public void registerGpioCallback(Handler handler, GpioCallback callback)
                 throws IOException {
             mSharedCallback = new SharedGpioCallback(callback);
             mInterruptGpio.registerGpioCallback(handler, mSharedCallback);
         }

         @Override
         public void unregisterGpioCallback(GpioCallback callback) {
             mInterruptGpio.unregisterGpioCallback(mSharedCallback);
             mSharedCallback = null;
         }

         // Intercept the shared GPIO callback and if it was meant for us, dispatch to the
         // user-facing callback.
         private class SharedGpioCallback implements GpioCallback {
             // The callback registers on this GPIO driver that we dispatch events to.
             private final GpioCallback mRegisteredCallback;

             public SharedGpioCallback(GpioCallback registeredCallback) {
                 mRegisteredCallback = registeredCallback;
             }

             @Override
             public boolean onGpioEdge(Gpio gpio) {
                 // Do any checking necessary to determine if this is the GPIO that fired.
                 //
                 // This is for example purposes only and may be inefficient since each GPIO
                 // checks the interrupt - better would be a single interrupt handler which
                 // dispatches to the proper GPIO driver.
                 if (thisGpioHadInterrupt()) {
                     return mRegisteredCallback.onGpioEdge(gpio);
                 }

                 // If it wasn't meant for this GPIO, do nothing and return true to keep
                 // listening for events.
                 return true;
             }

             @Override
             public void onGpioError(Gpio gpio, int error) {
                 // The underlying interrupt GPIO failed, we won't get any more callbacks.
                 mRegisteredCallback.onGpioError(gpio, error);
             }
         }
     }
     
<br />

<br />

|                                                                           Parameters                                                                           ||
|------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
| `handler`  | `Handler`: Handler to optionally run callbacks on. Provided as a convenience if needed, but it's not required to execute callbacks on this. <br /> |
| `callback` | `GpioCallback`: GpioCallback to register; only one will ever be registered at a time. <br />                                                       |

|                                                        Throws                                                        ||
|----------------------------------------------------------------------------|------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if this GPIO doesn't support interrupts. |

### unregisterGpioCallback

```
void unregisterGpioCallback (GpioCallback callback)
```

Unregisters an interrupt callback. Will only be called if a GpioCallback has been previously registered.

<br />

|                           Parameters                           ||
|------------|----------------------------------------------------|
| `callback` | `GpioCallback`: GpioCallback to unregister. <br /> |

**See also:**

- [ERROR(/#registerGpioCallback(GpioCallback, Handler))](https://developer.android.com/)  
-

  ## Interfaces

  - [GpioDriver](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver)
  - [I2cBusDriver](https://developer.android.com/reference/com/google/android/things/userdriver/pio/I2cBusDriver)
  - [PwmDriver](https://developer.android.com/reference/com/google/android/things/userdriver/pio/PwmDriver)
  - [SpiBusDriver](https://developer.android.com/reference/com/google/android/things/userdriver/pio/SpiBusDriver)
  - [UartDeviceDriver](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver)
-

  ## Classes

  - [PioDriverManager](https://developer.android.com/reference/com/google/android/things/userdriver/pio/PioDriverManager)