public class GpioDevice extends Object implements AutoCloseable
Nowadays, a single machine can have several GPIO chips providing lines. Typically, such a device is available at '/dev/gpiochipX' where `X` is the number of the chip.
Only one process can request a specific GPIO device at once. The user can then retrieve information about the chip itself or a given GPIO line.
A handle can be requested and obtained for driving one or several GPIO lines. When several lines are handled at once, whether doing any kind of IO is atomic depends on the underlying driver. For instance, there is no garantee that writing to several lines at once will indeed happen at the exact time for each line. This fact is opaque to this library and user space in general. Reading and writing the state of the lines is done using an additional buffer.
An input line can be monitored for events by requesting an event handle. This event handle can be used for blocking until a declared event happens, such as the line transitioning from low to high. Once an event handle is obtained, the requested events are queued by the kernel until read by the user. However, Linux not being a real-time operating system out of the box, such interrupts from user space should not be used for highly critical tasks. For such matters, a simple microcontroller is prefered.
Instead of using one thread per monitored pin, the user can start an event watcher and add several event handles. The event watcher can then be used to wait until one of the registered lines transitions to its relevant state, effectively using one thread for monitoring several lines at once.
A GPIO line, just like a GPIO device, can only be requested and handled by one instance at a time. When a handle is closed, all associated resources are cleaned-up by the kernel. A GPIO line is associated with a consumer, an optional string provided by the user describing "who" is controlling that line.
Closing a GPIO device does not close the acquired handles. Hence, it is a good practise to close it when it is not needed anymore.
| Constructor and Description |
|---|
GpioDevice(int number)
Opens a GPIO device by number.
|
GpioDevice(String path)
Opens the GPIO device located at the given path.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this GPIO device.
|
GpioChipInfo |
requestChipInfo()
Obtains information about this GPIO device.
|
GpioChipInfo |
requestChipInfo(GpioChipInfo info)
Obtains information about this GPIO device and writes it to `
info`. |
GpioEventHandle |
requestEvent(GpioEventRequest request)
Obtains a GPIO event handle for a GPIO line.
|
GpioHandle |
requestHandle(GpioHandleRequest request)
Obtains a GPIO handle for driving the requested GPIO lines.
|
GpioLineInfo |
requestLineInfo(int line)
Obtains information about a particular GPIO line from this GPIO device.
|
GpioLineInfo |
requestLineInfo(int line,
GpioLineInfo info)
Obtains information about a particular GPIO line from this GPIO device and writes it to `
info`. |
public GpioDevice(String path) throws LinuxException
path - Path to the GPIO device.LinuxException - When something fails on the native side.public GpioDevice(int number)
throws LinuxException
number - Number of the GPIO device.LinuxException - When something fails on the native side.public void close()
throws LinuxException
close in interface AutoCloseableLinuxException - When something fails on the native side.public GpioChipInfo requestChipInfo() throws LinuxException
LinuxException - When something fails on the native side.public GpioChipInfo requestChipInfo(GpioChipInfo info) throws LinuxException
info`.info - Where the data will be written.LinuxException - When something fails on the native side.public GpioLineInfo requestLineInfo(int line) throws LinuxException
line - The number of the line.LinuxException - When something fails on the native side.public GpioLineInfo requestLineInfo(int line, GpioLineInfo info) throws LinuxException
info`.line - The number of the line.info - Where the data will be written.LinuxException - When something fails on the native side.public GpioHandle requestHandle(GpioHandleRequest request) throws LinuxException
request - Request specifying what lines will be handled and how.LinuxException - When something fails on the native side.public GpioEventHandle requestEvent(GpioEventRequest request) throws LinuxException
This handle can be used to read the current value of the line or wait for an interrupt.
request - Request specifying which line will be monitored and how.LinuxException - When something fails on the native side.