public interface GPIOPin extends Peripheral
GPIOPin
interface provides methods for controlling a GPIO pin.
Each GPIO pin is identified by a numerical ID and by a name.
A GPIO pin may be configured for output or input. Output pins are both writable and readable while input pins are
only readable.
A GPIOPin
instance can be opened by a call to one of the PeripheralManager.open()
methods.
Once opened, an application can obtain the current value of a GPIO pin by calling the getValue()
method and
set its value by calling the setValue(boolean)
method.
An application can either monitor a GPIO pin value changes using polling or can register a PinListener
instance which will get asynchronously notified of any pin value changes. To register a PinListener
instance,
the application must call the setInputListener(PinListener)
method. The registered listener can later on be
removed by calling the same method with a null
listener parameter. Asynchronous notification is only
supported for GPIO pin configured for input. An attempt to set a listener on a GPIO pin configured for output will
result in an InvalidOperationException
being thrown.
When an application is no longer using a GPIO pin it should call the GPIOPin.close()
method to
release the GPIO pin. Any further attempt to set or get the value of a GPIO pin which has been closed will result in
a PeripheralNotAvailableException
been thrown.
Note that the initial direction of a GPIO pin which may be used for output or input as well as the initial value of a
GPIO pin set for output is configuration-specific. An application should always initially set the GPIO pin's
direction; or first query the GPIO pin's direction then set it if necessary.
Note that the configuration may allow for some GPIO pins to be set by the application for either output or input
while others may be used for input only or output only and their direction cannot be changed by the application. Note
also that asynchronous notification of pin value changes is only loosely tied to hardware-level interrupt requests.
The platform does not guarantee notification in a deterministic/timely manner.PinListener
Modifier and Type | Field and Description |
---|---|
static int |
INPUT
Input port direction.
|
static int |
OUTPUT
Output port direction.
|
BIG_ENDIAN, LITTLE_ENDIAN, MIXED_ENDIAN, UNDEFINED_ID
Modifier and Type | Method and Description |
---|---|
int |
getDirection()
Returns the current direction of this GPIO pin.
|
boolean |
getValue()
Returns the current value of this GPIO pin.
|
void |
setDirection(int direction)
Sets this GPIO pin for output or input.
|
void |
setInputListener(PinListener listener)
Registers a
PinListener instance which will get asynchronously notified when this GPIO pin's value
changes and according to the current trigger mode (see GPIOPinConfig.getTrigger() ). |
void |
setValue(boolean value)
Sets the value of this GPIO pin.
|
close, getID, getName, getProperties, isOpen
static final int INPUT
static final int OUTPUT
int getDirection() throws java.io.IOException, PeripheralNotAvailableException
setDirection(int)
the peripheral configuration-specific default value is returned.OUTPUT
if this GPIO pin is currently set as output; INPUT
otherwise (the
GPIO pin is set as input).java.io.IOException
- if an IO error occurred.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).boolean getValue() throws java.io.IOException, PeripheralNotAvailableException
setValue(boolean)
the peripheral configuration-specific default value is returned.
This method can be called on both output and input pins.java.io.IOException
- if an IO error occurred such as the pin is not readable.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).void setDirection(int direction) throws java.io.IOException, PeripheralNotAvailableException
InvalidOperationException
being thrown.direction
- OUTPUT
for output; INPUT
for input.java.io.IOException
- if an IO error occurred.InvalidOperationException
- if this GPIO pin cannot be configured for the desired direction.java.lang.IllegalArgumentException
- if direction
is not equal to OUTPUT
or INPUT
.java.lang.SecurityException
- if the caller does not have the required permission.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).void setInputListener(PinListener listener) throws java.io.IOException, PeripheralNotAvailableException
PinListener
instance which will get asynchronously notified when this GPIO pin's value
changes and according to the current trigger mode (see GPIOPinConfig.getTrigger()
). Notification will automatically begin
after registration completes.
A listener can only be registered for a GPIO pin currently configured for input.
If listener
is null
or if the pin's direction is changed from input to output then the previously
registered listener is removed.
Only one listener can be registered at a particular time.listener
- the PinListener
instance to be notified when this GPIO pin's value changes.java.io.IOException
- if an IO error occurred.InvalidOperationException
- if this GPIO pin is currently configured for output.InvalidStateException
- if listener
is not null
and a listener is already registered.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).void setValue(boolean value) throws java.io.IOException, PeripheralNotAvailableException
InvalidOperationException
being thrown.value
- the new pin value: true
for high, false
for low.InvalidOperationException
- if trying to set the value for pin configured for input.java.io.IOException
- if an IO error occurred such as the pin is not writable.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).Copyright (c) 2012, Oracle and/or its affiliates. All Rights Reserved. Use of this specification is subject to license terms.