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 @ http://wiringpi.com/)
Modifier and Type | Field and Description |
---|---|
static int |
ALT0
GPIO constant to define pin ALT modes
|
static int |
ALT1 |
static int |
ALT2 |
static int |
ALT3 |
static int |
ALT4 |
static int |
ALT5 |
static int |
GPIO_CLOCK
GPIO pin constant for GPIO_CLOCK pin mode
|
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 |
INT_EDGE_BOTH |
static int |
INT_EDGE_FALLING |
static int |
INT_EDGE_RISING |
static int |
INT_EDGE_SETUP
GPIO constants to define interrupt levels
|
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_MODE_BAL
GPIO constant to define PWM balanced mode.
|
static int |
PWM_MODE_MS
GPIO constant to define PWM mark:space mode.
|
static int |
PWM_OUTPUT
GPIO pin constant for PWM_OUTPUT direction for writing analog pin states
|
Modifier and Type | Method and Description |
---|---|
static int |
analogRead(int pin)
Core Functions
|
static void |
analogWrite(int pin,
int value)
Core Functions
|
static void |
delay(long howLong)
Timing Functions
|
static void |
delayMicroseconds(long howLong)
Timing Functions
|
static int |
digitalRead(int pin)
Core Functions
|
static void |
digitalWrite(int pin,
boolean value)
Core Functions
|
static void |
digitalWrite(int pin,
int value)
Core Functions
|
static void |
digitalWriteByte(int value)
This writes the 8-bit byte supplied to the first 8 GPIO pins.
|
static int |
getAlt(int pin)
[Hardware]
|
static void |
gpioClockSet(int pin,
int frequency)
[Hardware]
|
static long |
micros()
Timing Functions
|
static long |
millis()
Timing Functions
|
static int |
physPinToGpio(int physPin)
[Hardware]
|
static int |
piBoardRev()
[Hardware]
|
static int |
piHiPri(int priority)
Priority, Interrupt and Thread Functions
|
static void |
pinMode(int pin,
int mode)
Core Functions
|
static void |
pinModeAlt(int pin,
int mode)
Core Functions
|
static void |
pullUpDnControl(int pin,
int pud)
Core Functions
|
static void |
pwmSetClock(int divisor)
[PWM]
|
static void |
pwmSetMode(int mode)
[PWM]
|
static void |
pwmSetRange(int range)
[PWM]
|
static void |
pwmWrite(int pin,
int value)
Core Functions
|
static void |
setPadDrive(int group,
int value)
[Hardware]
|
static int |
waitForInterrupt(int pin,
int timeout)
Deprecated.
Note: Jan 2013: The waitForInterrupt() function is deprecated – you should use the newer
and easier to use wiringPiISR() function.
|
static void |
wiringPiClearISR(int pin)
Clear all WiringPiISR callbacks for this GPIO pin.
|
static int |
wiringPiISR(int pin,
int edgeType,
GpioInterruptCallback callback)
Priority, Interrupt and Thread Functions
|
static int |
wiringPiSetup()
Setup Functions
|
static int |
wiringPiSetupGpio()
Setup Functions
|
static int |
wiringPiSetupPhys()
Setup Functions
|
static int |
wiringPiSetupSys()
Setup Functions
|
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 GPIO_CLOCK
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 final int PWM_MODE_BAL
pwmSetMode(int)
,
Constant Field Valuespublic static final int PWM_MODE_MS
pwmSetMode(int)
,
Constant Field Valuespublic static final int ALT0
pinModeAlt(int,int)
,
Constant Field Valuespublic static final int ALT1
public static final int ALT2
public static final int ALT3
public static final int ALT4
public static final int ALT5
public static final int INT_EDGE_SETUP
public static final int INT_EDGE_FALLING
public static final int INT_EDGE_RISING
public static final int INT_EDGE_BOTH
public static int wiringPiSetup()
Setup Functions
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()
Setup Functions
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()
Setup Functions
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 int wiringPiSetupPhys()
Setup Functions
This setup function is identical to wiringPiSetup(), however it allows the calling programs to use the physical header pin numbers on the board GPIO header.
This function needs to be called with root privileges.
public static void pinMode(int pin, int mode)
Core Functions
This sets the mode of a pin to either INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK. Note that only wiringPi pin 1 (BCM_GPIO 18) supports PWM output and only wiringPi pin 7 (BCM_GPIO 4) supports CLOCK output modes.
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, the Broadcom GPIO pin number, or the board header 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
,
http://wiringpi.com/reference/core-functions/public static void pinModeAlt(int pin, int mode)
Core Functions
This method is an undocumented method in the WiringPi library that allows you to configure any PIN to any MODE.
pin
- pin numbermode
- Pin mode/direction to apply to the selected pin.The following constants are
provided for use with this parameter:
public static void pullUpDnControl(int pin, int pud)
Core Functions
This sets the pull-up or pull-down resistor mode on the given pin, which should be set as an input. Unlike the Arduino, the BCM2835 has both pull-up an down internal resistors. The parameter pud should be; PUD_OFF, (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v) This function has no effect when in Sys mode (see above) If you need to activate a pull-up/pull-down, then you can do it with the gpio program in a script before you start your program.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
,
http://wiringpi.com/reference/core-functions/public static void digitalWrite(int pin, int value)
Core Functions
Writes the value HIGH or LOW (1 or 0) to the given pin which must have been previously set as an output. WiringPi treats any non-zero number as HIGH, however 0 is the only representation of LOW.
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
,
http://wiringpi.com/reference/core-functions/public static void digitalWrite(int pin, boolean value)
Core Functions
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)
Core Functions
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)
Core Functions
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 int analogRead(int pin)
Core Functions
This returns the value read on the supplied analog input pin. You will need to register additional analog modules to enable this function for devices such as the Gertboard, quick2Wire analog board, etc.
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 analogWrite(int pin, int value)
Core Functions
This writes the given value to the supplied analog pin. You will need to register additional analog modules to enable this function for devices such as the Gertboard.
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 assign to the selected pin number.public static void delay(long howLong)
Timing Functions
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()
Timing Functions
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 long micros()
Timing Functions
This returns a number representing the number of microseconds since your program called one of the wiringPiSetup functions. It returns an unsigned 32-bit number which wraps after approximately 71 minutes.
public static void delayMicroseconds(long howLong)
Timing Functions
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. Delays under 100 microseconds are timed using a hard-coded loop continually polling the system time, Delays over 100 microseconds are done using the system nanosleep() function – You may need to consider the implications of very short delays on the overall performance of the system, especially if using threads.
howLong
- The number of microseconds to delay the main program thread.public static int piHiPri(int priority)
Priority, Interrupt and Thread Functions
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)
Priority, Interrupt and Thread Functions
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 wiringPiISR(int pin, int edgeType, GpioInterruptCallback callback)
Priority, Interrupt and Thread Functions
This function registers a function to received interrupts on the specified pin. The edgeType parameter is either INT_EDGE_FALLING, INT_EDGE_RISING, INT_EDGE_BOTH or INT_EDGE_SETUP. If it is INT_EDGE_SETUP then no initialisation of the pin will happen – it’s assumed that you have already setup the pin elsewhere (e.g. with the gpio program), but if you specify one of the other types, then the pin will be exported and initialised as specified. This is accomplished via a suitable call to the gpio utility program, so it need to be available
The pin number is supplied in the current mode – native wiringPi, BCM_GPIO, physical or Sys modes.
This function will work in any mode, and does not need root privileges to work.
The function will be called when the interrupt triggers. When it is triggered, it’s cleared in the dispatcher before calling your function, so if a subsequent interrupt fires before you finish your handler, then it won’t be missed. (However it can only track one more interrupt, if more than one interrupt fires while one is being handled then they will be ignored)
This function is run at a high priority (if the program is run using sudo, or as root) and executes concurrently with the main program. It has full access to all the global variables, open file handles and so on.
pin
- The GPIO pin number. (Depending on how wiringPi was initialized, this may
be the wiringPi pin number or the Broadcom GPIO pin number.)edgeType
- The type of pin edge event to watch for: INT_EDGE_FALLING, INT_EDGE_RISING, INT_EDGE_BOTH or INT_EDGE_SETUP.callback
- The callback interface implemented by the consumer. The 'callback' method of this interface
will be invoked when the wiringPiISR issues a callback signal.public static void wiringPiClearISR(int 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 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.
public static int physPinToGpio(int physPin)
[Hardware]
This returns the BCM_GPIO pin number of the supplied physical pin on the board header connector.
public static void digitalWriteByte(int value)
This writes the 8-bit byte supplied to the first 8 GPIO pins. It’s the fastest way to set all 8 bits at once to a particular value, although it still takes two write operations to the Pi’s GPIO hardware.
public static void pwmSetMode(int mode)
[PWM]
The PWM generator can run in 2 modes – balanced and mark:space. The mark:space mode is traditional, however the default mode in the Pi is balanced. You can switch modes by supplying the parameter: PWM_MODE_BAL or PWM_MODE_MS.
public static void pwmSetRange(int range)
[PWM]
This sets the range register in the PWM generator. The default is 1024.
public static void pwmSetClock(int divisor)
[PWM]
This sets the divisor for the PWM clock.
public static void setPadDrive(int group, int value)
[Hardware]
This sets the strength of the pad drivers for a particular group of pins. There are 3 groups of pins and the drive strength is from 0 to 7. Do not use this unless you know what you are doing.
public static int getAlt(int pin)
[Hardware]
This gets the ALT function level of the requested pin number
public static void gpioClockSet(int pin, int frequency)
[Hardware]
This sets the frequency of a GPIO pin
Copyright © 2012–2021 Pi4J. All rights reserved.