Skip to content

Most visited

Recently visited

navigation

Gpio

Gpio

public abstract class Gpio
extends Object implements Closeable

java.lang.Object
   ↳ com.google.android.things.pio.Gpio
Known Direct Subclasses


Controls a GPIO pin. General-purpose input/output (GPIO) pins are physical pins on an integrated circuit that can be controlled via software. A GPIO pin can be configured as input to read the voltage value or as output to change the voltage value. It only understands logic values (true/false) which maps physically to a low value (the pin has the same voltage as Ground) or high value (the pin has the same voltage as IOREF). GPIOs are frequently used to communicate with simple peripherals like sensors and actuators. Opening a GPIO pin takes ownership of it for the whole system, preventing anyone else from opening/accessing the GPIO until you call close(). Forgetting to call close() will prevent anyone (including the same process/app) from using the GPIO.

Summary

Constants

int ACTIVE_HIGH

ActiveType constant to select active high, which means that a true value corresponds with high voltage.

int ACTIVE_LOW

ActiveType constant to select active low, which means that a true value corresponds with low voltage.

int DIRECTION_IN

Direction constant used to configure a GPIO pin as an input.

int DIRECTION_OUT_INITIALLY_HIGH

Direction constant used to configure a GPIO pin as an output and initialize its state to high/true (equivalent of calling setValue(true)).

int DIRECTION_OUT_INITIALLY_LOW

Direction constant used to configure a GPIO pin as an output and initialize its state to low/false (equivalent of calling setValue(false)).

int EDGE_BOTH

EdgeType constant used to configure the GPIO interrupt to trigger on any edge (when the GPIO change state from high/true to low/false or from low/false to high/true).

int EDGE_FALLING

EdgeType constant used to configure the GPIO interrupt to trigger on falling edge (when the GPIO goes from high/true to low/false).

int EDGE_NONE

EdgeType constant used to configure the GPIO interrupt to never trigger.

int EDGE_RISING

EdgeType constant used to configure the GPIO interrupt to trigger on rising edge (when the GPIO goes from low/false to high/true).

Public constructors

Gpio()

Public methods

abstract void close()

Close and release the GPIO pin.

abstract boolean getValue()

Get the current value of the GPIO pin (for GPIO pins configured as input only).

final void registerGpioCallback(GpioCallback callback)

Registers a callback for GPIO edge interrupts.

abstract void registerGpioCallback(GpioCallback callback, Handler handler)

Registers a callback for GPIO edge interrupts.

abstract void setActiveType(int activeType)

Sets the active level.

abstract void setDirection(int direction)

Set the direction of the GPIO pin.

abstract void setEdgeTriggerType(int edgeTriggerType)

Set the interrupt trigger type.

abstract void setValue(boolean value)

Set the value of the GPIO pin (for GPIO pins configured as output only).

abstract void unregisterGpioCallback(GpioCallback callback)

Unregisters an interrupt callback.

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Constants

ACTIVE_HIGH

int ACTIVE_HIGH

ActiveType constant to select active high, which means that a true value corresponds with high voltage.

Constant Value: 1 (0x00000001)

ACTIVE_LOW

int ACTIVE_LOW

ActiveType constant to select active low, which means that a true value corresponds with low voltage.

Constant Value: 0 (0x00000000)

DIRECTION_IN

int DIRECTION_IN

Direction constant used to configure a GPIO pin as an input.

Constant Value: 0 (0x00000000)

DIRECTION_OUT_INITIALLY_HIGH

int DIRECTION_OUT_INITIALLY_HIGH

Direction constant used to configure a GPIO pin as an output and initialize its state to high/true (equivalent of calling setValue(true)).

Constant Value: 1 (0x00000001)

DIRECTION_OUT_INITIALLY_LOW

int DIRECTION_OUT_INITIALLY_LOW

Direction constant used to configure a GPIO pin as an output and initialize its state to low/false (equivalent of calling setValue(false)).

Constant Value: 2 (0x00000002)

EDGE_BOTH

int EDGE_BOTH

EdgeType constant used to configure the GPIO interrupt to trigger on any edge (when the GPIO change state from high/true to low/false or from low/false to high/true).

Constant Value: 3 (0x00000003)

EDGE_FALLING

int EDGE_FALLING

EdgeType constant used to configure the GPIO interrupt to trigger on falling edge (when the GPIO goes from high/true to low/false).

Constant Value: 2 (0x00000002)

EDGE_NONE

int EDGE_NONE

EdgeType constant used to configure the GPIO interrupt to never trigger.

Constant Value: 0 (0x00000000)

EDGE_RISING

int EDGE_RISING

EdgeType constant used to configure the GPIO interrupt to trigger on rising edge (when the GPIO goes from low/false to high/true).

Constant Value: 1 (0x00000001)

Public constructors

Gpio

Gpio ()

Public methods

close

void close ()

Close and release the GPIO pin. This must be called in order for the GPIO pin to be released and be available to other users.

Throws
IOException if the GPIO is already closed.

getValue

boolean getValue ()

Get the current value of the GPIO pin (for GPIO pins configured as input only). Returns an unpredictable value when the GPIO is configured as output.

Returns
boolean
Throws
IOException

registerGpioCallback

void registerGpioCallback (GpioCallback callback)

Registers a callback for GPIO edge interrupts. Equivalent to registerGpioCallback(callback, null).

Parameters
callback GpioCallback: GpioCallback to register.
Throws
IOException
IOException

See also:

registerGpioCallback

void registerGpioCallback (GpioCallback callback, 
                Handler handler)

Registers a callback for GPIO edge interrupts. The type of edges to watch for must be first set with setEdgeTriggerType(int).

Parameters
callback GpioCallback: GpioCallback to register.
handler Handler: Handler to run callbacks on, or null to use the current thread.
Throws
IOException
IOException

See also:

setActiveType

void setActiveType (int activeType)

Sets the active level.

Parameters
activeType int: One of ACTIVE_LOW or ACTIVE_HIGH.
Throws
IOException
IllegalArgumentException
IOException

setDirection

void setDirection (int direction)

Set the direction of the GPIO pin.

Parameters
direction int: Direction to set. One of DIRECTION_IN, DIRECTION_OUT_INITIALLY_HIGH, DIRECTION_OUT_INITIALLY_LOW.
Throws
IOException
IllegalArgumentException
IOException

setEdgeTriggerType

void setEdgeTriggerType (int edgeTriggerType)

Set the interrupt trigger type.

Parameters
edgeTriggerType int: Type of trigger to use for interrupt. One of EDGE_NONE, EDGE_RISING, EDGE_FALLING, EDGE_BOTH.
Throws
IOException
IllegalArgumentException
IOException

setValue

void setValue (boolean value)

Set the value of the GPIO pin (for GPIO pins configured as output only).

Parameters
value boolean: Value to set.
Throws
IOException

unregisterGpioCallback

void unregisterGpioCallback (GpioCallback callback)

Unregisters an interrupt callback.

Parameters
callback GpioCallback: GpioCallback to unregister.

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.