public interface GPIOPort extends Device<GPIOPort>
GPIOPort
interface provides methods for controlling a GPIO port.
Each GPIO port is identified by a numerical ID and optionally by a name.
A GPIO port is a platform-defined grouping of GPIO pins that may be configured for output or
input. Output ports are both writable and readable while input ports are only readable. Note that
whether GPIO pins that are part of a GPIO port can be retrieved and controlled individually as
GPIOPin
instances depends on the hardware and platform configuration (and especially
whether the GPIO pins can be shared through different abstractions).
A GPIO port may be identified by the numerical ID and by the name (if any defined) that
correspond to its registered configuration. A GPIOPort
instance can be opened by a call
to one of the DeviceManager.open(id,...)
methods using
its ID or by a call to one of the
DeviceManager.open(name,...)
methods using its name. When a GPIOPort
instance is
opened with an ad-hoc GPIOPortConfig
configuration (which includes its hardware
addressing information) using one of the
DeviceManager.open(config,...)
it is not assigned any ID nor name.
Once opened, an application can obtain the current value of a GPIO port by calling the
getValue
method and set its value by calling the setValue
method. getMaxValue
method. An attempt to set a
GPIO port with a value that exceeds its maximum range value will result in an
IllegalArgumentException
being thrown.
An application can either monitor a GPIO port value changes using polling or can register a
PortListener
instance which will get asynchronously notified of any value changes. To
register a PortListener
instance, the application must call the setInputListener
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 port configured for input. An attempt to set a listener on a GPIO port configured for output
will result in an UnsupportedOperationException
being thrown.
When an application is no longer using a GPIO port it should call the close
method
to close the GPIO port. Any further attempt to set or get the value of a GPIO port which has been
closed will result in a ClosedDeviceException
been thrown.
Note that the initial direction of a GPIO port which may be used for output or input as well as
the initial value of a GPIO port set for output is configuration-specific. An application should
always initially set the GPIO port's direction; or first query the GPIO port's direction then set
it if necessary.
Note that the configuration may allow for some GPIO ports to be set by an application for either
output or input while others may be used for input only or output only and their direction cannot
be changed by an application. Note also that asynchronous notification of port value changes is
only loosely tied to hardware-level interrupt requests. The platform does not guarantee
notification in a deterministic/timely manner.PortListener
,
GPIOPortPermission
Modifier and Type | Field and Description |
---|---|
static int |
INPUT
Input port direction.
|
static int |
OUTPUT
Output port direction.
|
BIG_ENDIAN, LITTLE_ENDIAN, MIXED_ENDIAN
Modifier and Type | Method and Description |
---|---|
int |
getDirection()
Returns the current direction of this GPIO port.
|
int |
getMaxValue()
Returns the maximum value of this GPIO port.
|
int |
getValue()
Returns the current value of this GPIO port.
|
void |
setDirection(int direction)
Sets this GPIO port for output or input.
|
void |
setInputListener(PortListener listener)
Registers a
PortListener instance which will get asynchronously notified when this
GPIO port's value changes. |
void |
setValue(int value)
Sets the value of this GPIO port.
|
static final int INPUT
static final int OUTPUT
int getDirection() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
setDirection
the device configuration-specific default value
is returned.OUTPUT
if this GPIO port is currently set as output;
INPUT
otherwise (the GPIO port is set as input).java.io.IOException
- if some other I/O error occurs.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another
application.ClosedDeviceException
- if the device has been closed.int getMaxValue() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
java.io.IOException
- if some other I/O error occurs.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another
application.ClosedDeviceException
- if the device has been closed.int getValue() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
setValue
the device configuration-specific default value is returned.
This method can be called on both output and input ports.java.io.IOException
- if an I/O error occurred such as the port is not readable.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another
application.ClosedDeviceException
- if the device has been closed.void setDirection(int direction) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UnsupportedOperationException
being thrown.direction
- OUTPUT
for output; INPUT
for input.java.lang.IllegalArgumentException
- if direction
is not equal to OUTPUT
or
INPUT
.java.lang.UnsupportedOperationException
- if this GPIO port cannot be configured for the desired direction.java.lang.SecurityException
- if the caller does not have the required permission.java.io.IOException
- if some other I/O error occurs.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another
application.ClosedDeviceException
- if the device has been closed.void setInputListener(PortListener listener) throws java.io.IOException, ClosedDeviceException
PortListener
instance which will get asynchronously notified when this
GPIO port's value changes. Notification will automatically begin after registration
completes.
A listener can only be registered for a GPIO port currently configured for input.
If this GPIOPort
is open in DeviceManager.SHARED
access mode and if this
GPIOPort
is currently configured for input, the listeners registered by all the
applications sharing the underlying GPIO port will get notified when its value changes.
If listener
is null
then the previously registered listener is removed.
Only one listener can be registered at a particular time.listener
- the PortListener
instance to be notified when this GPIO port's value
changes.java.io.IOException
- if some other I/O error occurs.java.lang.UnsupportedOperationException
- if this GPIO port is currently configured for output.java.lang.IllegalStateException
- if listener
is not null
and a listener is already registered.ClosedDeviceException
- if the device has been closed.void setValue(int value) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UnsupportedOperationException
being thrown.value
- the new port value.java.lang.IllegalArgumentException
- if the new value exceeds this GPIO port's maximum value.java.lang.UnsupportedOperationException
- if trying to set the value for a port configured for input.java.io.IOException
- if an I/O error occurred such as the port is not writable.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another
application.ClosedDeviceException
- if the device has been closed.Copyright © 2012, 2014, Oracle and/or its affiliates. All rights reserved.
Legal Notices