<< PiJava overview: Java 11 and JavaFX 11 on Raspberry PI
PiJava - Part 5: Running the minimal JavaFX 11 application on Raspberry PI >>

PiJava - Part 6: JavaFX 11 application on Raspberry PI with TilesFX and GPIO

2019-04-15 10:46:41
TL;DR; Combining the previous 5 posts, adding TilesFX and GPIO access into one "dashboard application" for the PI.

What we will do

Based on the previous blog posts
So there is one "small" step remaining: build something which actually does something on the PI, talking to the GPIO's and show what's happening.
Spoiler alert: this is what's is going to look like:



Wiring

As this is just a proof-of-concept, I'm not doing anything fancy at all, just a LED we can turn on and off, and a button to read out the state of it. So a simple setup is used:
  • GPIO22 (WiringPi n° 3) > resistor > LED > ground
  • 3.3V > push button > GPIO24 (WiringPi n° 5)



Java to GPIO

At first I wanted to use PI4J: "a friendly object-oriented I/O API and implementation libraries for Java Programmers to access the full I/O capabilities of the Raspberry Pi platform". But a Java 11 version is not ready yet, so that will be for a next blog series... ;-)

For this quick demo, I just used basic gpio commands which you can also use in terminal to toggle the led on GPIO22 = WiringPi number 3 (1 on, 0 off):
$ gpio mode 3 out 
$ gpio write 3 1
$ gpio write 3 0
And read the button state on GPIO24 = WiringPi number 5 (1 pressed, 0 not pressed):
$ gpio mode 5 in 
$ gpio read 5
1

Install TilesFX module on the PI

The application as a single JAR file (which is only 9.7kb) will need Java 11 and JavaFX 11 to be available on the board, this is what has been done already in the previous blogs (part 2 and 5).

But we will also need the TilesFX module, a great library made by Gerrit Grunwald. We can easily download this from Maven and put it side-by-side with Java and JavaFX in the opt-directory. This library doesn't contain any platform specific code, so again the JAR can be used on both PC and Linux/PI. A script file is available here.
# Make sure we are in the home directory
cd /home/pi

# Download the TilesFX module from Maven
wget -O tilesfx-11.1.jar http://central.maven.org/maven2/eu/hansolo/tilesfx/11.1/tilesfx-11.1.jar

# Make a directory in opt
sudo mkdir /opt/tilesfx11

# Move the downloaded file to opt
sudo mv tilesfx-11.1.jar /opt/tilesfx11

The code

Based on the minimal JavaFX application from blog part 4, two additional classes were added. You can find the full source on GitHub, so a short overview here:

FxScreen.java


This class generates the view with multiple tiles from the TilesFX-library:
  • two as demo only (TEXT and CLOCK)
  • close button to exit the application (CUSTOM)
  • a toggle button to put the led on and off (SWITCH)
  • and two line charts (SMOOTHED_CHART), one with dummy temperature measurements, one with value 0 or 100 depending on the button state

Check the source in GitHub for the full code. As an example, this is the limited code we need to build the toggle tile and change the GPIO state of the led:
var ledSwitchTile = TileBuilder.create()
   .skinType(SkinType.SWITCH)
   .prefSize(250, 200)
   .title("Gpio " + PIN_LED)
   .roundedCorners(false)
   .build();
ledSwitchTile.setOnSwitchReleased(e -> Gpio.setPinState(PIN_LED, ledSwitchTile.isActive()));

Gpio.java


The Gpio class has an execute function, which uses Process to send the commands identical to how we can use them in the terminal.
private static String execute(String cmd) {
   ...
   Process p = Runtime.getRuntime().exec(cmd);
   ...
}
E.g. this is the function to toggle a pin:
public static void setPinState(final int pin, final boolean on) {
    execute("gpio write " + pin + (on ? " 1" : " 0"));
}

Run it!

We will run the application with Java 11.0.2 so don't need to add a reference to the JavaFX module as it is integrated. But we do need to link to the TilesFX module we downloaded before. A script file is available here.
sudo /opt/jdk-11.0.2/bin/java \
	--module-path=/opt/tilesfx11 \
	--add-modules=eu.hansolo.tilesfx \
	-jar /home/pi/git/PiJava/out/pijava-0.1-SNAPSHOT.jar \
	-Dprism.verbose=true \
	-Dembedded=monocle \
	-Dglass.platform=Monocle
And here it is, in full action:

Conclusion

I wanted to be able to run Java, JavaFX, TilesFX on a PI and talk to the GPIO's, so mission accomplished! Many possible paths from here. WebView from JavaFX doesn't work on the PI and PI4J can use some help to bring it to Java 11 and Java 12 is already available...... So to be continued...