Skip to content

helins/linux-gpio.java

Repository files navigation

Linux-GPIO

Maven Central

Javadoc

CircleCI

Handle GPIO lines on a Linux system in a portable and fast way from the JVM.

  • Control several lines at once
  • Surprisingly fast
  • Better support for interrupts than others usual methods
  • Standard

Rationale

Handling GPIO lines from user space on a Linux system is typically done by writing directly to '/dev/mem' or to the more permissive '/dev/gpiomem'. While being fast, this approach is very closely thight to the hardware which means the code is not portable. Another way is to use the sysfs method where each pin is exported and represented in the file system. Users can read and write specific files for configuration and IO. While being standard, this method is slow and deprecated. Both methods present caveats. For instance, there is no way to claim pins nor any kind of automatic clean-up.

Since Linux 4.8, a new API is provided. It is not widely known nor used in spite of the fact it is standard and fast. Certain platforms such as Raspbian on the Raspberry Pi already support this API. This Java library wraps the C API in order to provide a more idiomatic interface accessible from the JVM while trying not to be too opiniated.

Usage

Any accessed GPIO device need at least read permission (which is enough even for writing). Typically, only the root user has access, hence permissions need to be set. For instance, for device 0 on a Raspberry Pi (user pi) :

$ chown root:pi /dev/gpiochip0
$ chmod g+r /dev/gpiochip0

The javadoc provide all details.

Have a look at the examples.

Run an example where $EXAMPLE is the name of an example such as AlternatingLeds :

$ ./gradlew :examples:$EXAMPLE:run

Run tests :

$ ./gradlew test

Run tests on the Raspberry Pi 3 (or similar), will test some IO :

$ ./gradlew test -DonRaspberry=true

Resources

License

Copyright © 2018 Adam Helinski

Licensed under the term of the Mozilla Public License 2.0, see LICENSE.