public interface GPIOPin extends Device<GPIOPin>
GPIOPin
interface provides methods for controlling a GPIO pin.
A GPIO pin may be identified by the numerical ID and by the name (if any defined) that correspond
to its registered configuration. A GPIOPin
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 GPIOPin
instance is
opened with an ad-hoc GPIOPinConfig
configuration (which includes its hardware addressing
information) using one of the
DeviceManager.open(config,...)
it is not assigned any ID nor name.
A GPIO pin may be configured for output or input. Output pins are both writable and readable
while input pins are only readable.
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
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
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 UnsupportedOperationException
being thrown.
When an application is no longer using a GPIO pin it should call the close
method
to close the GPIO pin. Any further attempt to set or get the value of a GPIO pin which has been
closed will result in a ClosedDeviceException
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
,
GPIOPinPermission
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 pin.
|
int |
getTrigger()
Returns the current pin interrupt trigger.
|
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 getTrigger ). |
void |
setTrigger(int trigger)
Sets this GPIO pin trigger mode.
|
void |
setValue(boolean value)
Sets the value of this GPIO pin.
|
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 pin is currently set as output;
INPUT
otherwise (the GPIO pin 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 getTrigger() throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
setTrigger
the device configuration-specific default value is
returned. Note that setting the trigger mode to GPIOPinConfig.TRIGGER_NONE
disables
the notification of the PinListener
instance (see setInputListener
without unregistering it.GPIOPinConfig.TRIGGER_NONE
,
GPIOPinConfig.TRIGGER_FALLING_EDGE
, GPIOPinConfig.TRIGGER_RISING_EDGE
, GPIOPinConfig.TRIGGER_BOTH_EDGES
, GPIOPinConfig.TRIGGER_HIGH_LEVEL
,
GPIOPinConfig.TRIGGER_LOW_LEVEL
, GPIOPinConfig.TRIGGER_BOTH_LEVELS
.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.boolean 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 pins.java.io.IOException
- if an I/O error occurred such as the pin 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.io.IOException
- if some other I/O error occurs.java.lang.UnsupportedOperationException
- 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.UnavailableDeviceException
- if this device is not currently available - such as it is locked by another
application.ClosedDeviceException
- if the device has been closed.void setTrigger(int trigger) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
trigger
- the interrupt trigger events, one of: GPIOPinConfig.TRIGGER_NONE
,
GPIOPinConfig.TRIGGER_FALLING_EDGE
,
GPIOPinConfig.TRIGGER_RISING_EDGE
,
GPIOPinConfig.TRIGGER_BOTH_EDGES
, GPIOPinConfig.TRIGGER_HIGH_LEVEL
, GPIOPinConfig.TRIGGER_LOW_LEVEL
,
GPIOPinConfig.TRIGGER_BOTH_LEVELS
.java.io.IOException
- if an IO error occurred.java.lang.UnsupportedOperationException
- if this GPIO pin cannot be configured with the desired trigger mode.java.lang.IllegalArgumentException
- if trigger
is not one of the defined values.java.lang.SecurityException
- if the caller does not have the required permission.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(PinListener listener) throws java.io.IOException, ClosedDeviceException
PinListener
instance which will get asynchronously notified when this
GPIO pin's value changes and according to the current trigger mode (see getTrigger
). Notification will automatically begin after registration completes.
If this GPIOPin
is open in DeviceManager.SHARED
access mode and if this
GPIOPin
is currently configured for input, the listeners registered by all the
applications sharing the underlying GPIO pin will get notified when its value changes.
A listener can only be registered for a GPIO pin currently configured for input.
If listener
is null
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 some other I/O error occurs.java.lang.UnsupportedOperationException
- if this GPIO pin 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(boolean value) throws java.io.IOException, UnavailableDeviceException, ClosedDeviceException
UnsupportedOperationException
being thrown.value
- the new pin value: true
for high, false
for low.java.lang.UnsupportedOperationException
- if trying to set the value for pin configured for input.java.io.IOException
- if an I/O error occurred such as the pin 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