Class GpioUtil

    • 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 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.
    • 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