public class Gpio extends Object
Some of the functions in the WiringPi library are designed to mimic those in the Arduino Wiring system. There are relatively easy to use and should present no problems for anyone used to the Arduino system, or C programming in-general.
The main difference is that unlike the Arduino system, the main loop of the program is not provided for you and you need to write it yourself. This is often desirable in a Linux system anyway as it can give you access to command-line arguments and so on. See the examples page for some simple examples and a Makefile to use.
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 @ https://projects.drogon.net/)
Modifier and Type | Field and Description |
---|---|
static int |
HIGH
GPIO pin state constant for HIGH/ON/+3.3VDC
|
static int |
INPUT
GPIO pin constant for INPUT direction for reading pin states
|
static int |
LOW
GPIO pin state constant for LOW/OFF/0VDC
|
static int |
NUM_PINS
The total number of GPIO pins available in the WiringPi library.
|
static int |
OUTPUT
GPIO pin constant for OUTPUT direction for writing digital pin states (0/1)
|
static int |
PUD_DOWN
GPIO constant to enable the pull-down resistor mode on a GPIO pin.
|
static int |
PUD_OFF
GPIO constant to disable the pull-up or pull-down resistor mode on a GPIO pin.
|
static int |
PUD_UP
GPIO constant to enable the pull-up resistor mode on a GPIO pin.
|
static int |
PWM_OUTPUT
GPIO pin constant for PWM_OUTPUT direction for writing analog pin states
|
Modifier and Type | Method and Description |
---|---|
static void |
delay(long howLong)
This causes program execution to pause for at least howLong milliseconds.
|
static void |
delayMicroseconds(long howLong)
This causes program execution to pause for at least howLong microseconds.
|
static int |
digitalRead(int pin)
This function returns the value read at the given pin.
|
static void |
digitalWrite(int pin,
boolean value)
Writes the value HIGH or LOW ('true' or 'false') to the given pin which must have been
previously set as an output.
|
static void |
digitalWrite(int pin,
int value)
Writes the value HIGH or LOW (1 or 0) to the given pin which must have been previously set as
an output.
|
static long |
millis()
This returns a number representing the number if milliseconds since your program called one
of the wiringPiSetup functions.
|
static int |
piBoardRev()
[Hardware]
|
static int |
piHiPri(int priority)
This attempts to shift your program (or thread in a multi-threaded program) to a higher
priority and enables a real-time scheduling.
|
static void |
pinMode(int pin,
int mode)
This sets the mode of a pin to either INPUT, OUTPUT, or PWM_OUTPUT.
|
static void |
pullUpDnControl(int pin,
int pud)
This sets the pull-up or pull-down resistor mode on the given pin, which should be set as an
input.
|
static void |
pwmWrite(int pin,
int value)
Writes the value to the PWM register for the given pin.
|
static int |
waitForInterrupt(int pin,
int timeout)
[Interrupts]
|
static int |
wiringPiSetup()
This initializes the wiringPi system and assumes that the calling program is going to be
using the wiringPi pin numbering scheme.
|
static int |
wiringPiSetupGpio()
This setup function is identical to wiringPiSetup(), however it allows the calling programs
to use the Broadcom GPIO pin numbers directly with no re-mapping.
|
static int |
wiringPiSetupSys()
This initializes the wiringPi system but uses the /sys/class/gpio interface rather than
accessing the hardware directly.
|
static int |
wpiPinToGpio(int wpiPin)
[Hardware]
|
public static final int NUM_PINS
public static final int INPUT
pinMode(int,int)
,
Constant Field Valuespublic static final int OUTPUT
pinMode(int,int)
,
Constant Field Valuespublic static final int PWM_OUTPUT
pinMode(int,int)
,
Constant Field Valuespublic static final int LOW
digitalWrite(int,int)
,
Constant Field Valuespublic static final int HIGH
digitalWrite(int,int)
,
Constant Field Valuespublic static final int PUD_OFF
waitForInterrupt(int,int)
,
Constant Field Valuespublic static final int PUD_DOWN
waitForInterrupt(int,int)
,
Constant Field Valuespublic static final int PUD_UP
waitForInterrupt(int,int)
,
Constant Field Valuespublic static int wiringPiSetup()
This initializes the wiringPi system and assumes that the calling program is going to be using the wiringPi pin numbering scheme. This is a simplified numbering scheme which provides a mapping from virtual pin numbers 0 through 16 to the real underlying Broadcom GPIO pin numbers. See the pins page for a table which maps the wiringPi pin number to the Broadcom GPIO pin number to the physical location on the edge connector.
This function needs to be called with root privileges.
public static int wiringPiSetupSys()
This initializes the wiringPi system but uses the /sys/class/gpio interface rather than accessing the hardware directly. This can be called as a non-root user provided the GPIO pins have been exported before-hand using the gpio program. Pin number in this mode is the native Broadcom GPIO numbers.
Also note that some functions (noted below) have no effect when using this mode as they're not currently possible to action unless called with root privileges.
public static int wiringPiSetupGpio()
This setup function is identical to wiringPiSetup(), however it allows the calling programs to use the Broadcom GPIO pin numbers directly with no re-mapping.
This function needs to be called with root privileges.
public static void pinMode(int pin, int mode)
This sets the mode of a pin to either INPUT, OUTPUT, or PWM_OUTPUT. Note that only wiringPi pin 1 (GPIO-18) supports PWM output. The pin number is the number obtained from the pins table.
This function has no effect when in Sys mode.
pin
- The GPIO pin number. (Depending on how wiringPi was initialized, this may
be the wiringPi pin number or the Broadcom GPIO pin number.)mode
- Pin mode/direction to apply to the selected pin.The following constants are
provided for use with this parameter:
INPUT
,
OUTPUT
,
PWM_OUTPUT
,
https://projects.drogon.net/raspberry-pi/wiringpi/functions/public static void pullUpDnControl(int pin, int pud)
pin
- The GPIO pin number. (Depending on how wiringPi was initialized, this may
be the wiringPi pin number or the Broadcom GPIO pin number.)pud
- Pull Up/Down internal pin resistance.The following constants are provided for
use with this parameter:
PUD_OFF
,
PUD_DOWN
,
PUD_UP
,
https://projects.drogon.net/raspberry-pi/wiringpi/functions/public static void digitalWrite(int pin, int value)
Writes the value HIGH or LOW (1 or 0) to the given pin which must have been previously set as an output.
pin
- The GPIO pin number. (Depending on how wiringPi was initialized, this may
be the wiringPi pin number or the Broadcom GPIO pin number.)value
- The pin state to write to the selected pin.The following constants are
provided for use with this parameter:
HIGH
,
LOW
,
https://projects.drogon.net/raspberry-pi/wiringpi/functions/public static void digitalWrite(int pin, boolean value)
Writes the value HIGH or LOW ('true' or 'false') to the given pin which must have been previously set as an output.
pin
- The GPIO pin number. (Depending on how wiringPi was initialized, this may
be the wiringPi pin number or the Broadcom GPIO pin number.)value
- The pin boolean state to write to the selected pin.public static void pwmWrite(int pin, int value)
Writes the value to the PWM register for the given pin. The value must be between 0 and 1024. (Again, note that only pin 1 supports PWM)
This function has no effect when in Sys mode
pin
- The GPIO pin number. (Depending on how wiringPi was initialized, this may
be the wiringPi pin number or the Broadcom GPIO pin number.)value
- The analog value to write to the selected pin. (The value must be between
0 and 1024.)public static int digitalRead(int pin)
This function returns the value read at the given pin. It will be HIGH or LOW (1 or 0) depending on the logic level at the pin.
pin
- The GPIO pin number. (Depending on how wiringPi was initialized, this may
be the wiringPi pin number or the Broadcom GPIO pin number.)public static void delay(long howLong)
This causes program execution to pause for at least howLong milliseconds. Due to the multi-tasking nature of Linux it could be longer. Note that the maximum delay is an unsigned 32-bit integer or approximately 49 days.
howLong
- The number of milliseconds to delay the main program thread.public static long millis()
This returns a number representing the number if milliseconds since your program called one of the wiringPiSetup functions. It returns an unsigned 32-bit number which wraps after 49 days.
public static void delayMicroseconds(long howLong)
This causes program execution to pause for at least howLong microseconds. Due to the multi-tasking nature of Linux it could be longer. Note that the maximum delay is an unsigned 32-bit integer microseconds or approximately 71 minutes.
howLong
- The number of microseconds to delay the main program thread.public static int piHiPri(int priority)
This attempts to shift your program (or thread in a multi-threaded program) to a higher priority and enables a real-time scheduling. The priority parameter should be from 0 (the Default) to 99 (the maximum). This won't make your program go any faster, but it will give it a bigger slice of time when other programs are running. The priority parameter works relative to others and so you can make one program priority 1 and another priority 2 and it will have the same effect as setting one to 10 and the other to 90 (as long as no other programs are running with elevated priorities)
The return value is 0 for success and -1 for error. If an error is returned, the program should then consult the errno global variable, as per the usual conventions.
Note: Only programs running as root can change their priority. If called from a non-root program then nothing happens.
priority
- The priority parameter should be from 0 (the Default) to 99 (the maximum)public static int waitForInterrupt(int pin, int timeout)
[Interrupts]
With a newer kernel patched with the GPIO interrupt handling code, you can now wait for an interrupt in your program. This frees up the processor to do other tasks while you're waiting for that interrupt. The GPIO can be set to interrupt on a rising, falling or both edges of the incoming signal.
int waitForInterrupt (int pin, int timeOut)
When called, it will wait for an interrupt event to happen on that pin and your program will be stalled. The timeOut parameter is given in milliseconds, or can be -1 which means to wait forever.
Before you call waitForInterrupt, you must first initialize the GPIO pin and at present the only way to do this is to use the gpio program, either in a script, or using the system() call from inside your program.
e.g. We want to wait for a falling-edge interrupt on GPIO pin 0, so to setup the hardware, we need to run:
gpio edge 0 falling
pin
- The GPIO pin number. (Depending on how wiringPi was initialized, this may
be the wiringPi pin number or the Broadcom GPIO pin number.)timeout
- The number of milliseconds to wait before timing out. A value of '-1' will
disable the timeout.public static int piBoardRev()
[Hardware]
This method provides the board revision as determined by the wiringPi library.
public static int wpiPinToGpio(int wpiPin)
[Hardware]
This method provides the edge GPIO pin number for the requested wiringPi pin number.
Copyright © 2012-2019 Pi4J. All Rights Reserved.