Class GpioUtil
- java.lang.Object
-
- com.pi4j.wiringpi.GpioUtil
-
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:
- pi4j
- wiringPi
This library depends on the wiringPi native system library. (developed by Gordon Henderson @ http://wiringpi.com/)
- Author:
- Robert Savage (http://www.savagehomeautomation.com)
- See Also:
- https://pi4j.com/, http://wiringpi.com/
-
-
Field Summary
Fields Modifier and Type Field Description static int
DIRECTION_HIGH
GPIO PIN DIRECTIONstatic int
DIRECTION_IN
GPIO PIN DIRECTIONstatic int
DIRECTION_LOW
GPIO PIN DIRECTIONstatic int
DIRECTION_OUT
GPIO PIN DIRECTIONstatic int
EDGE_BOTH
GPIO PIN EDGE DETECTIONstatic int
EDGE_FALLING
GPIO PIN EDGE DETECTIONstatic int
EDGE_NONE
GPIO PIN EDGE DETECTIONstatic int
EDGE_RISING
GPIO PIN EDGE DETECTION
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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 supportedstatic 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.
-
-
-
Field Detail
-
DIRECTION_IN
public static final int DIRECTION_IN
GPIO PIN DIRECTION
GPIO pin constant for IN direction for reading pin states
-
DIRECTION_OUT
public static final int DIRECTION_OUT
GPIO PIN DIRECTION
GPIO pin constant for OUT direction for writing digital pin states (0/1).
-
DIRECTION_HIGH
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).
-
DIRECTION_LOW
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).
-
EDGE_NONE
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.
- See Also:
setEdgeDetection(int,int)
, Constant Field Values
-
EDGE_BOTH
public 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.
- See Also:
setEdgeDetection(int,int)
, Constant Field Values
-
EDGE_RISING
public 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.
- See Also:
setEdgeDetection(int,int)
, Constant Field Values
-
EDGE_FALLING
public 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.
- See Also:
setEdgeDetection(int,int)
, Constant Field Values
-
-
Method Detail
-
export
public static void export(int pin, int direction) throws RuntimeException
This method will export the selected GPIO pin.
This method required root permissions access.
- Parameters:
pin
- GPIO pin number (not header pin number; not wiringPi pin number)direction
- pin direction- Throws:
RuntimeException
- See Also:
DIRECTION_IN
,DIRECTION_OUT
-
unexport
public static void unexport(int pin) throws RuntimeException
This method will unexport the selected GPIO pin.
This method required root permissions access.
- Parameters:
pin
- GPIO pin number (not header pin number; not wiringPi pin number)- Throws:
RuntimeException
-
isExported
public static boolean isExported(int pin) throws RuntimeException
This method determines if the requested GPIO pin is already exported.
- Parameters:
pin
- GPIO pin number (not header pin number; not wiringPi pin number)- Returns:
- A return value of '0' represents that the pin is not exported. A return value of '1' represents that the pin is exported.
- Throws:
RuntimeException
-
setEdgeDetection
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.
- Parameters:
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:- EDGE_NONE
- EDGE_BOTH
- EDGE_RISING
- EDGE_FALLING
- Returns:
- A return value of '0' represents success. Errors are returned as negative numbers.
- Throws:
RuntimeException
- See Also:
EDGE_NONE
,EDGE_BOTH
,EDGE_RISING
,EDGE_FALLING
-
getEdgeDetection
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.
- Parameters:
pin
- GPIO pin number (not header pin number; not wiringPi pin number)- Returns:
- The edge condition detected on the selected pin: none, rising, falling, or both.
The following constants are provided for use with this parameter:
- EDGE_NONE
- EDGE_BOTH
- EDGE_RISING
- EDGE_FALLING
- Throws:
RuntimeException
- See Also:
EDGE_NONE
,EDGE_BOTH
,EDGE_RISING
,EDGE_FALLING
-
setDirection
public static boolean setDirection(int pin, int direction) throws RuntimeException
This method will set the selected GPIO pin's export direction.
- Parameters:
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:- DIRECTION_IN
- DIRECTION_OUT
- Returns:
- A return value of '0' represents success. Errors are returned as negative numbers.
- Throws:
RuntimeException
- See Also:
DIRECTION_IN
,DIRECTION_OUT
-
getDirection
public static int getDirection(int pin) throws RuntimeException
This method will get the selected GPIO pin's export direction.
- Parameters:
pin
- GPIO pin number (not header pin number; not wiringPi pin number)- Returns:
- The GPIO pin's configured export direction is returned: IN (0), OUT (1). The
following constants are provided for use with this parameter:
- DIRECTION_IN
- DIRECTION_OUT
- Throws:
RuntimeException
- See Also:
DIRECTION_IN
,DIRECTION_OUT
-
isPinSupported
public static int isPinSupported(int pin) throws RuntimeException
This method will return a value of '1' if the pin is supported
- Parameters:
pin
- pin number- Returns:
- '1' is the pin is supported, else '0'
- Throws:
RuntimeException
-
isPrivilegedAccessRequired
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/**'
- Returns:
- 'true' if privileged access is required; else returns 'false'.
- Throws:
RuntimeException
-
enableNonPrivilegedAccess
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.
- Throws:
RuntimeException
-
-