Monday, 13 November 2017

Touchscreen Raspberry Pi

Raspberry Pi is a great machine that people want to make amazing stuff with it, and one great think is to use the a visual interface that you can get from the hdmi port.
In my case I wanted to have a screen and touch sensor to interact with a custom GUI.


  • Innolux 7 with touch screen (link)
  • Raspberry Pi ( in my case version 2 )
  • Battery pack
  • USB to DC Jack


1) Driver
The good news is that Raspberry Pi has finally introduced a simple  system to enable touch screen with most common board on the market ( here a list ) but in my case I had to enable Egalax sensor.

To make sure your device is supported, just connect the usb and run ( make sure its the only device usb connected)
and you should see the model and manufacture of the sensor.

In my case everything is supported, if your sensor is not supported, try to poke them on github otherwise you are forced to recompile the kernel with the driver built-in.

The module kernel ( if supported ) has to be enabled by running the command and the sensor connected in the usb slot:
when the process will be completed, you should be able to access the device.

2) Testing
Make sure you have installed all the tools to test your sensor by running this command:
 sudo apt-get install evtest tslib libts-bin

When you have confirmed that, you have to find the right path to your device by looking into the folder /dev/input/.
The common path are  /dev/input/event0 or /dev/input/touchscreen, but make sure you dont have a keyboard or mouse connected otherwise it might be event1 , event2, etc.. 

After you have discovred the correct path, in my case /dev/input/event0, try to run the command:
sudo evtest /dev/input/event0
and you should see some values after you pressed the  touch screen.
If nothing happened, that means you sensor is not connected properly or the driver module is not loaded.

3) Calibrate
Now that you made sure that the device is plugged, module loaded and something is coming out from the device, you can run the first calibration command:
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/event0 ts_calibrate
after this command has been launch, you should see a calibration screen where you are asked to press 5 different spots.

Once the calibration is completed, you should have a file at the location /etc/pointcal.

Double check that you can draw correctly with this command:
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/event0 ts_test
then you are ready to go!

4) Issues
You might have some problems by using the device Egalax ( as I had ), which is the missing module in the latest build of  libts-bin ( probably if this guide is a few weeks older, this problem would have been solved ).

I couldn't run the calibration test because the software would close by saying "sensor not supported", this is because the build dint have the Egalax driver embedded in the build.
What you have to do, is to make sure libts-bin is removed from your system and purged all references, usually by using:
apt-get remove --purge libts-bin

Make sure you got all the software to compile the source code of libts:
sudo apt-get install git automake libtool

Checkout the repository ( apparently the patch for the driver has been included on the main repo ):
git clone git://
cd libts
and compile
sudo make install

then follow the guide here that is just moving the library in the right folder: (pointless to re-write what this guy did best )

Once you finished, you should be able to run ts_calibrate without any errors.

Also, you might want to change some parameters into /etc/ts.conf.

Pygame without X server

You small game, application, script or else dont always need to have a X server running just to access to the graphical unit.
The trick is to use pyscope, offered by Adafruit ( link ), that is a simple class which makes sure your software will inject the output of pygame into the frame buffer.

In my case I had to add few environment variables to make sure that the touch screen would work with pygame:

#Reference to the calibration file
os.putenv('TSLIB_CALIBFILE', '/etc/pointercal')
#Point to the right library folder
os.putenv('TSLIB_PLUGINDIR', '/usr/lib/arm-linux-gnueabihf/ts')
#Setup the touch screen
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/event0')
otherwise the pointer would be jumpy or not following correctly as the ts_test was showing.

Saturday, 5 November 2016


I wanted to send AGPS data to my UBLOX CAM-M8, so get a faster fix as the chip doesnt have a eeprom to save the data and not even a battery backup.
I understood that every chip needs to download a map of where all the satellites are in the space and then based on that information, the chip can triangulate the position.

The great information is that UBLOX provides a free access to the AGPS servies which you will need to provide a close location ( latitude and longitude) and then a file is generated.

To send this file, you will need to parse the data and send the message via Serial and because I couldnt find anywhere a sample code, I tried to code something which actually worked.

The real sending would be too check the ack message, but this worked fine for me.

Saturday, 7 November 2015

PN53x with Raspberry Pi and Python

Install libnfc on your raspberry pi by following Adafruit guide , make sure you are using the Serial pins and shorted the right pins on the board, check the version of the PN53x to short the right ones.

Install the library NFCPY which allows you to read and write through libnfc a nfc tag.

To decode a message:

To write a message

Saturday, 18 July 2015

[Arduino] Timestamp function

Arduino don't have the function which convert hhmmss and ddmmyy to unix timestamp and because I was looking to combine my data to one universal time, I did't find a method to achieve that.

I came across to the library TIME which is a good library but because I had to optimise my code and have the sketch as small as possible ( developing for Arduino pro mini ), I extracted the necessary function  and variables to generate timestamp.
The file is available on my github here and this is how you can use the function:

From the date:01.06.2015 and time 20:10:00 you will have call the function like this:

and it will return a long number which is unix timestamp.

Saturday, 13 December 2014

Remove Passphrase from SSH Key

Most of the time you create a ssh key for your GIT or SSH login and you wished the system wouldn't ask the password each time, and here there is a solution to it.

openssl rsa -in private_key_with_pass_phrase -out private_key_without_pass_phrase
WARNING: a pass phrase is an added layer of security in case you loose control of your private key. Think carefully about removing the password.

Just replace the old key with the new one and you solved your problem.

Sunday, 26 October 2014

J/Box2D first steps

I came across to use JBox2D and I didnt know anything about how a physical engine works, so I am writing some helpful tips here.

Scale and dimensions

Firstable you have to keep on your mind that Box2D works with meters, so forget pixel or dpi, you will need to use some functions to convert these measurements to something that the engine can understand.
In general people use a scale, because the engine works in a world of 100x100 meters, so its better to have it as small as possible. The common scale is 30.

Here a simple way to convert pixel to Box2D dimensions:

Create a simple ball

Let's start with the second step, you have to define all properties of the object, if its a ball, it has to have  round shape and be elastic.

So you have to define the body definition, if the object will be static or dynamic  and set the position in the world:

The position might be tricky to understand, but here is a simple solution:
When you want to draw something in a canvas, you draw something from position (X,Y), so if you want to draw something at the top left corner, you will choose (0,0).
So in this thinking you would put new Vec2(0,0) in this case, but this will be WRONG because in Box2D everything works in another way, you have to tell the position of the centre of the object.

If you object is  10x10 and you want to position to the top left corner, you position will be (5,5).

To continue the body creation, you have to tell the world that this object exists in the engine, so you have to call the method "createBody"

Then tell at the body, what kind of shape your object will be:

In this case we create a CircleShape, because its a ball. In other case you have to use PolygonShape to create a special shape or a simple square.

Another tricky to understand how to create a PolygonShape for a square is the follow:

You have to use "setAsBox", and the dimension that you put inside must to be half of the width and half of the height, because the BodyDefinition has the position of the centre of the object.

Last thing to complete the creation of the ball in the world is the Fixture Definition:

You can change the density, that is the weight of the object, the friction between objects and the restitution that is the elasticity. Play with some values and you will find out what better works with your project.


Joints are parts that connects two body parts, there are different ones.
There is a joint to simulate a rope connection, an elastic connection, a rotation connection, etc...
You can find more information around in the web, it is very simple to use them but tricky to find the right values or anchor points.

Friday, 4 October 2013

Concatenate a String with a Float

I came up with a very simple and difficult problem with my little Arduino, I couldn't concat a string and a float number, after a few hours on Google I found out the solution:

Helpful link: