public class GpioUtil extends Object
This utility class is provided to export, unexport, and manipulate pin direction.
Before using the Pi4J library, you need to ensure that the Java VM in configured with access to the following system libraries:
This library depends on the wiringPi native system library. (developed by Gordon Henderson @ http://wiringpi.com/)
Modifier and Type | Field and Description |
---|---|
static int |
DIRECTION_HIGH
GPIO PIN DIRECTION
|
static int |
DIRECTION_IN
GPIO PIN DIRECTION
|
static int |
DIRECTION_LOW
GPIO PIN DIRECTION
|
static int |
DIRECTION_OUT
GPIO PIN DIRECTION
|
static int |
EDGE_BOTH
GPIO PIN EDGE DETECTION
|
static int |
EDGE_FALLING
GPIO PIN EDGE DETECTION
|
static int |
EDGE_NONE
GPIO PIN EDGE DETECTION
|
static int |
EDGE_RISING
GPIO PIN EDGE DETECTION
|
Modifier and Type | Method and Description |
---|---|
static void |
enableNonPrivilegedAccess()
This method is used to enable non-privileged access to the GPIO pins on
to system.
|
static void |
export(int pin,
int direction)
This method will export the selected GPIO pin.
|
static int |
getDirection(int pin)
This method will get the selected GPIO pin's export direction.
|
static int |
getEdgeDetection(int pin)
This method will get the selected GPIO pin's edge detection setting.
|
static boolean |
isExported(int pin)
This method determines if the requested GPIO pin is already exported.
|
static int |
isPinSupported(int pin)
This method will return a value of '1' if the pin is supported
|
static boolean |
isPrivilegedAccessRequired()
This method will return a value of '1' if Privileged access is required.
|
static boolean |
setDirection(int pin,
int direction)
This method will set the selected GPIO pin's export direction.
|
static boolean |
setEdgeDetection(int pin,
int edge)
This method will set the selected GPIO pin's edge detection.
|
static void |
unexport(int pin)
This method will unexport the selected GPIO pin.
|
public static final int DIRECTION_IN
GPIO PIN DIRECTION
GPIO pin constant for IN direction for reading pin states
public static final int DIRECTION_OUT
GPIO PIN DIRECTION
GPIO pin constant for OUT direction for writing digital pin states (0/1).
public static final int DIRECTION_HIGH
GPIO PIN DIRECTION
GPIO pin constant for OUT direction with an initial default HIGH value for writing digital pin states (0/1).
public static final int DIRECTION_LOW
GPIO PIN DIRECTION
GPIO pin constant for OUT direction with an initial default LOW value for writing digital pin states (0/1).
public static final int EDGE_NONE
GPIO PIN EDGE DETECTION
This constant is provided as an edge detection mode for use with the 'edge' method. This constants instructs the edge detection to be disabled.
setEdgeDetection(int,int)
,
Constant Field Valuespublic static final int EDGE_BOTH
GPIO PIN EDGE DETECTION
This constant is provided as an edge detection mode for use with the 'edge' method. This constants instructs the edge detection to only look for rising and falling pins states; pins changing from LOW to HIGH or HIGH to LOW.
setEdgeDetection(int,int)
,
Constant Field Valuespublic static final int EDGE_RISING
GPIO PIN EDGE DETECTION
This constant is provided as an edge detection mode for use with the 'edge' method. This constants instructs the edge detection to only look for rising pins states; pins changing from LOW to HIGH.
setEdgeDetection(int,int)
,
Constant Field Valuespublic static final int EDGE_FALLING
GPIO PIN EDGE DETECTION
This constant is provided as an edge detection mode for use with the 'edge' method. This constants instructs the edge detection to only look for falling pins states; pins changing from HIGH to LOW.
setEdgeDetection(int,int)
,
Constant Field Valuespublic static void export(int pin, int direction) throws RuntimeException
This method will export the selected GPIO pin.
This method required root permissions access.
pin
- GPIO pin number (not header pin number; not wiringPi pin number)direction
- pin directionRuntimeException
DIRECTION_IN
,
DIRECTION_OUT
public static void unexport(int pin) throws RuntimeException
This method will unexport the selected GPIO pin.
This method required root permissions access.
pin
- GPIO pin number (not header pin number; not wiringPi pin number)RuntimeException
public static boolean isExported(int pin) throws RuntimeException
This method determines if the requested GPIO pin is already exported.
pin
- GPIO pin number (not header pin number; not wiringPi pin number)RuntimeException
public static boolean setEdgeDetection(int pin, int edge) throws RuntimeException
This method will set the selected GPIO pin's edge detection. Edge detection instructs when the hardware GPIO changes raise interrupts on the system.
NOTE: Calling this method will automatically export the pin and set the pin direction to INPUT. This method required root permissions access.
pin
- GPIO pin number (not header pin number; not wiringPi pin number)edge
- The edge condition to detect: none, rising, falling, or both. The following
constants are provided for use with this parameter:
RuntimeException
EDGE_NONE
,
EDGE_BOTH
,
EDGE_RISING
,
EDGE_FALLING
public static int getEdgeDetection(int pin) throws RuntimeException
This method will get the selected GPIO pin's edge detection setting. Edge detection instructs when the hardware GPIO changes raise interrupts on the system.
pin
- GPIO pin number (not header pin number; not wiringPi pin number)RuntimeException
EDGE_NONE
,
EDGE_BOTH
,
EDGE_RISING
,
EDGE_FALLING
public static boolean setDirection(int pin, int direction) throws RuntimeException
This method will set the selected GPIO pin's export direction.
pin
- GPIO pin number (not header pin number; not wiringPi pin number)direction
- The export direction to apply: IN, OUT. The following constants are provided
for use with this parameter:
RuntimeException
DIRECTION_IN
,
DIRECTION_OUT
public static int getDirection(int pin) throws RuntimeException
This method will get the selected GPIO pin's export direction.
pin
- GPIO pin number (not header pin number; not wiringPi pin number)RuntimeException
DIRECTION_IN
,
DIRECTION_OUT
public static int isPinSupported(int pin) throws RuntimeException
This method will return a value of '1' if the pin is supported
pin
- pin numberRuntimeException
public static boolean isPrivilegedAccessRequired() throws RuntimeException
This method will return a value of '1' if Privileged access is required. This method will return a value of '0' if Privileged access is NOT required. Privileged access is required if any of the the following conditions are not met: - You are running with Linux kernel version 4.1.7 or greater - The Device Tree is enabled - The 'bcm2835_gpiomem' kernel module loaded. - Udev rules are configured to permit write access to '/sys/class/gpio/**'
RuntimeException
public static void enableNonPrivilegedAccess() throws RuntimeException
This method is used to enable non-privileged access to the GPIO pins on to system. This method will throw a runtime exception if privileged access is required. You can test for required access using the 'isPrivilegedAccessRequired()' method. Please note when non-privileged access is enabled, you will not be able to use any hardware PWM or CLOCK functions.
RuntimeException
Copyright © 2012–2019 Pi4J. All rights reserved.