Gpio
public
interface
Gpio
implements
Closeable
com.google.android.things.pio.Gpio |
Known Indirect 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
|
int |
ACTIVE_LOW
|
int |
DIRECTION_IN
|
int |
DIRECTION_OUT_INITIALLY_HIGH
|
int |
DIRECTION_OUT_INITIALLY_LOW
|
int |
EDGE_BOTH
|
int |
EDGE_FALLING
|
int |
EDGE_NONE
|
int |
EDGE_RISING
|
Public methods | |
---|---|
abstract
void
|
close()
Releases the GPIO pin. |
default
String
|
getName()
Returns the I/O name. |
abstract
boolean
|
getValue()
Gets the current value of the GPIO pin (for GPIO pins configured as input only). |
default
void
|
registerGpioCallback(GpioCallback callback)
Registers a callback for GPIO edge interrupts. |
abstract
void
|
registerGpioCallback(Handler handler, GpioCallback callback)
Registers a callback for GPIO edge interrupts. |
abstract
void
|
setActiveType(int activeType)
Sets the active level. |
abstract
void
|
setDirection(int direction)
Sets the direction of the GPIO pin. |
abstract
void
|
setEdgeTriggerType(int edgeTriggerType)
Sets the interrupt trigger type. |
abstract
void
|
setValue(boolean value)
Sets 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
interface
java.io.Closeable
| |
From
interface
java.lang.AutoCloseable
|
Constants
ACTIVE_HIGH
int ACTIVE_HIGH
ActiveType
constant to select active high, which means that
logical true
corresponds with high voltage.
Constant Value: 1 (0x00000001)
ACTIVE_LOW
int ACTIVE_LOW
ActiveType
constant to select active low, which means that
logical true
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 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 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 getValue()
changes state in either direction).
Constant Value: 3 (0x00000003)
EDGE_FALLING
int EDGE_FALLING
EdgeType
constant used to configure the GPIO interrupt to
trigger on falling edge (when getValue()
goes from true
to 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 getValue()
goes from false
to true
).
Constant Value: 1 (0x00000001)
Public methods
close
void close ()
Releases the GPIO pin.
This must be called in order for the GPIO pin to be released and be available to other users. Closing a GPIO does not change its state, for example if it closed while outputting a high signal, it will continue to output high until another user opens it and changes its state.
Throws | |
---|---|
IOException |
if the GPIO is already closed. |
getValue
boolean getValue ()
Gets the current value of the GPIO pin (for GPIO pins configured as input only).
Returns | |
---|---|
boolean |
the state of the GPIO pin. Return value is undefined if the GPIO is configured as an output. |
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 |
registerGpioCallback
void registerGpioCallback (Handler handler, GpioCallback callback)
Registers a callback for GPIO edge interrupts.
The type of edges to watch for must be first set with setEdgeTriggerType(int)
.
Parameters | |
---|---|
handler |
Handler : Handler to run callbacks on, or null to use the current thread. |
callback |
GpioCallback : GpioCallback to register. |
Throws | |
---|---|
|
IOException |
IOException |
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)
Sets 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)
Sets the interrupt trigger type.
The trigger type is based on the logical pin value, not the physical voltage. For
example, EDGE_RISING
will always trigger on a false
-to-true
transition, but if the GPIO is configured as ACTIVE_LOW
this will actually be
caused by a drop from high to low voltage.
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)
Sets 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. |
Interfaces
Classes