Using the Raspberry Pi Touch Display with OpenFL

I really like the new Raspberry pi display.

One of the things I was really looking forward to was using it with OpenFL.
But out of the box SDL2 wasn't supporting the display in the mode I am running OpenFL namely without x11.

So I decided to do a little digging.
I found that mouse and keyboard devices are being handled in sdl_udev and sdl_evdev.
Basically udev was looking for and identifying input devices and evdev was handling incoming events from identified devices.

I could see that udev was finding an ID_INPUT_TOUCHSCREEN device, but there was nothing more than a comment after that.
So I decided to try and trick udev by identifying the touchscreen as a combined mouse and keyboard device.

Immediately I could move the mouse pointer but a touch triggered a key-event with an unhandled value of 330.
( later I found that this key is defined in linux/input.h as BTN_TOUCH )
So I added the button to the if statement that was catching the mousebutton values in SDL_evdev.c
and cheated a bit by changing it to mouse-button 0

I thought that it was working until I noticed something strange. It felt like the mousedown event was out of sync.
So I opened the utility evtest on an ssh session to see what was going on.
If found that the touchscreen was firing 2 unhandled events before the BTN_TOUCH event:

the code was only handling the ABS_X events so indeed the Touch event came before the x en y positions where handled.
So I added these 2 new events:

Et Voila!, the touch screen is working as a mouse.

For proper multitouch a different approach is needed.
But for testing this hack works quite well.



Using the GPIO ports with OpenFL

Over the last few weeks I have been exploring OpenFL and Haxe.
I was particularly interested in getting OpenFL to run on the Raspberry Pi.
One of the cool things about the Raspberry Pi of course is the GPIO-ports.
So after I got a version of OpenFL running on the Pi it was time to take the next step and look into the development of native extensions for OpenFL.

I don't really have a background in c or c++, but I'm able to read through other people's code if it's not to complicated.
I had a look at the hxSerial extension and for the most part was able to see what was going on.

I had already been playing around with the wiringPi library and the c-examples that came with it, and I knew how to blink an led and how to read from a digital input port.
I had also looked at the wiringPi wrapper for nodejs and I saw that is was mainly just translating javascript types to native c++ types.
Last but not least I had a look at a post on
So by combining the things I saw in these examples I took the first step towards creating my hxPiGpio extension.

I'll probably make a separate post in the near future on how to create an openfl c++ extension. But for now I'm focusing on how to use my Extension.

I wrapped the wiringPi setup functions , the digitalRead and digitalWrite functions and some static inline vars for the pinmodes.

If you would like to try out the extension follow these steps: (WARNING EXPERIMENTAL CODE)

First of all you need to run Raspbian Jessie! if you want to be able to test using the 'openfl test linux' command.
This is because on Wheezy the gpio ports require the use of the sudo command, while on Jessie you can access them without the sudo command if you add a file to /etc/dev/rules.d/

Next of course get openfl working through the steps of my earlier post.

If that's working go ahead and install wiringPi and make sure you are using version 2.29 following the steps on this page
do not forget to add the file to /etc/dev/rules.d/ as mentioned and shutdown your pi.

While your pi is off it's a good time to connect an led and button in the following fashion and restart your pi.
( at your own risk of course, I am not responsible for any damage to your pi )


Checkout the extension from my github page to the directory where you keep the development haxelib modules
( I keep the ones I get from 'haxelib install ..', separate from the ones I compile from source)
Now compile the extension

now you checkout the testProject in the directory where you keep your projects, and compile

hopefully, if I didn't miss any steps, you'll see something like this:
and you will be able to click on the led on screen and have it turn on the actual led, and press the button to toggle the color-version of the raspi logo.

the latest version of this testproject is trying to connect to SPI so it might not work if you have not enabled it in raspi-config.
( haven't tried this yet with spi disabled)
You can probably also comment out the line in Main.hx that says

This is all still experimental / proof of concept. I'm not concerned with coding conventions or even proper code at the moment.
That will happen if I decide to put the extension up on haxelib and my c++ skills have improved.

Feel free to fork and improve the code if you have more experience in c++