Wednesday, February 19, 2014

Fun with JavaFX on Raspberry Pi again...

Hi there,

Long time no post...I know...but today it's time for another blogpost. This time it's about a little lcd display for the Raspberry Pi that I've found on the web. I really like the embedded computing scene with all it's nice little projects that solve little problems but what I don't like is all the loose wires and breadboards lying around. The reason for that is that it's really hard to take this stuff with you to conferences and show use them for demos. This stuff is just to fragile and I really don't want to take a soldering iron with me to fix problems before my conference session :)
Long story short...I like everything that is small, robust and where I don't see many cables.
With this on my mind I'm skimming the web from time to time searching for new gadgets that might be useful and in the beginning of last week I've found the following little lcd display


The really nice thing about that display is it's size and that you can plug it directly on the Raspberry Pi. And because it's really thin it will fit in a standard Raspberry Pi housing. But before you start to drill holes into your existing Pi case you should think about to buy an already prepared one like the following from TEK-BERRY


Sometimes things are sooooo easy :)
Of course there are many displays available for the Raspberry Pi but in this case I read something about FrameBuffer and X-Windows support which brought JavaFX on my mind. 

The Lcd display and the case are available at Watterott.com

LCD Display

TEK-BERRY Case

So I ordered a display (30 €) and a case (9 €) and when it arrived last Saturday I've put everything together. 
To get the LCD display running you need a special Raspbian image and modify some settings.
A detailed instruction can be found on GitHub and the image can be downloaded here.

When you follow the instructions the lcd display will be used as standard display on the console and also with support for x-windows.

The next thing was to figure out how to get JavaFX running on that display. Because there are frame buffer drivers available for the display it should somehow be possible to get JavaFX running on this display from the command line but I was not able to figure out how to do that (to be honest I only spent 2 hours on it). 
And because I knew that x-windows works on the device I decided to start my JavaFX application from x-windows because there I also have touch support. The LCD supports simple touch events (afaik 2 point touch) but no swipes etc. 

When using x-windows with an attached touchscreen you will always see the mouse pointer (even if no mouse is attached). To get rid of the mouse pointer in x-windows you might want to use unclutter that will hide the mouse pointer after a defined interval and only makes it visible when the user touches the display. To install unclutter you just have to type

sudo apt-get install unclutter

You can use unclutter as follows

unclutter -idle 0.01 -root

which defines the number of seconds before the pointer will disappear (1/100 second in this case) but this won't work directly from the shell, instead we will use it in the next step.

To start a JavaFX application from the command line using x-windows you simply have to add a file named .xsession to your users home folder. And in this file you simply have to call your JavaFX jar file with the parameter -Djavafx.platform=gtk. So your .xsession file should look similar to the following

unclutter -idle 0.01 -root
java -Djavafx.platform=gtk YOUR_APPLICATION.JAR PACKAGE.MAIN

The nice thing about using a .xsession file is that as soon as you call startx on the command line it will start x-windows and directly load your application. If your application uses the fullscreen (which is 320x240 pixel in this case) only your application will popup on the screen. And even better, if you close your application also x-windows will be closed and you are back to console...nice isn't it...

If you are up to this step you will figure out that the x-axis of the touch events is mirrored which means if you touch the display on the left part the mouse pointer will appear on the right part of the display. To correct this behavior you have to open the file

/usr/share/X11/xorg.conf.d/10-edev.conf

with a texteditor (e.g. vim, nano etc.) and search for the last topic (should state touchscreen somewhere). Here you have to add the following line

Option "InvertX" "true"

If you don't use unclutter you have to invert the y-axis instead means you have to add the following line 

Option "InvertY" "true"

And that's it, with this setup you can run JavaFX on the embedded LCD display with touch support which makes the Raspberry Pi very useful as display for current weather, measured data, switch or whatever you have on your mind...

I use it as another display for my JavaFX everywhere demo where I monitor a sensor network with different devices. 



As an example I've tried to record a little video that you can find on youtube.

On the video you can see that I use some kind of swipe events to navigate between the screens of my application. But because the display doesn't support swipe events I've simply implemented them by using EventHandlers on the Scene that are listening for MousePressed and MouseReleased events.

So that's it for today...keep coding...

4 comments:

  1. Hi,

    I'm building an domotica platform with a server and client application, and i wonder if it is possible to use controls you have created in my client application. I did a clone from your bitbucket repository to check them out, and i really like them. My application is open source if that is a requirement, and off course a link back will be provided. Some screenshots of the client application are available over here: http://pidome.wordpress.com/screenshots/java-client-screenshots/

    Best regards,
    John.

    ReplyDelete
    Replies
    1. Hi John,
      Yep, I know your stuff and I like it, so feel free to take whatever you need and please let me know if you need something special.
      Cheers,
      Gerrit

      Delete
  2. Hi, Han. Very nice post! However, when I run a JavaFX application at the console prompt using -Djavafx.platform=gtk the controls (two buttons) don't render and I end up with a blank form. Without -Djavafx.platform=gtk it works.
    Even after creating the .xsession file and adding the line (with -Djavafx......) I still end up with the blank screen. Without -Djavafx..... in the .xsession file the controls render perfectly.
    I'm using the regular HDMI output though, with a regular (non touch) monitor. If I don't use -Djavafx.platform=gtk will I still get touch support?
    Thanks!

    ReplyDelete
    Replies
    1. Hi there,
      It seems you misunderstood the post. On the Pi with a standard hdmi display you don't need the gtk switch. Touch was not supported when using the gtk switch but without you always have touch support (as long as your display has touch support and it will be registered in the right way). So if you just want to use JavaFX on a hdmi display on the Pi, make sure that you boot the Pi into the console and call your app from the command line without the gtk switch.
      Cheers,
      Gerrit

      Delete