Raspberry Pi, RaspBMC & eGalax touchscreen

RaspBMC: Touchscreen calibration & extra features

Update: For OSMC please click

The following tutorial describes additional steps, after installation of RaspBMC, to enable calibration and extra functionality for the eGalax touchscreen in XBMC. By using a modified version of ts_calibrate for touchscreen calibration and python utility uinput-mapper, it’s possible to generate a user-input device even which handles the calibration transformations and gives additional BTN_LEFT/RIGHT functionality within XBMC on the Raspberry Pi.

The procedure requires to install the following:

ts_calibrate

uinput-mapper & Upstart script – uimapper.conf

The Upstart script uimapper.conf starts/stops the python script uinput-mapper whenever the input-device is added/removed allowing hot-plugging and grabbing of the device within XBMC.

The touchpad is then shown to set the correct event#. Unfortunately the calibration did not worked correctly (mouse pointer is still off) or hangs up when trieing to recalibrate.
It seems, some steps are missing to get the touchpad available to the system?

Sorry to hear you have problems, what model is it…the kernel modules should already be in the release as it uses the latest kernel.

Try to install evtest (sudo apt-get install evtest) so you can see the input events, you can also see it uimapper is running also. You can type ‘stop uimapper’ or ‘start uimapper’ to check if the pythonn script works ok. Uimapper grabs the device before XBMC does, so make sure when testing via SSH to stop XBMC also.

You were right about the uimapper. Nevertheless, the uimapper still won´t start automatically on system startup, and the display isn´t calibrated. i have to
stop xbmc
start uimapper
start xbmc
and everything works fine again.. until next reboot :/

I am using a Raspberry PI model B with 256MB RAM and the latest RaspBMC installed. The touchscreen did _not_ directly run out of the box after RaspBMC Installation. It is shown in lsusb as an eGalax touchscreen, but can not be found with …input/by-id or evtest

Hi Stefan, are you using the corect uimapper.conf file in the /etc/init/umapper.conf….I think I had to change the raspbmc uimapper.conf to start at boot the line that works for me should read….

start on startup or virtual-filesystems or (input-device-added SUBSYSTEM=input)

The calibration is only used when uimapper is running, it creates a user input device and adds the translations ans scales external;ly to xbmc. The calibration file is automatically created with ts_calibrate and stored in /etc/pointercal. Other functions of uimapper can be changed in the touchscreen.py file located in /scripts/uinput-mapper/configs folder; if you want to try other things.

Thanks alot, that was the trick
“start on startup or virtual-filesystems” was missing. Seems like I adopted a wrong config file when playing with Brantje´s install script for xbian.
Thanks for you great help and effort, the touchscreen works perfectly now

First of all thanks for this. Being new to this (RPi, Linux…) I was struggling quite a bit to get my touchscreen to work. Thanks to your efforts it is running now. I am using a small 8.4″ Touchscreen by cartft.com. My question now is, is there a way to increase the dead zone? If I understand correctly BTN_right is triggered by holding the finger within a certain radius. However due to my small touchscreen this is quite difficult. Or maybe is has nothing to do with the size at all. When I look at the pointercal the scaler value is quite big (65536), maybe this is the reason. On Xbian I had to manually adjust the pointercal, since after calibration there was still an offset (half the screen). On Raspbmc the calibration worked fine – no idea why.

First of all….make sure the resolution and overscan are set correctly in /boot/config.txt file in either xbian or raspbmc distro.

When running ts_calib, it will give you the info on the resolution that it is using for calib file.

The /etc/pointercal file generated automatically stores all offset and scale values in integer values (i.e. all are multiplied by the 65536 factor). Then when I use them later (in the file ‘/scripts/uinput-mapper/configs/touchscreen.py’) I recalculate the values. It is the same solution for both distros.
You can check when in xbmc for resolution and overscan settings in video calibration also.

But first, check you are using uimapper and not native egalax driver in xbmc.

if you can SSH your rpi from a pc, I use putty…

type

sudo su

stop xbmc
stop uimapper

apt-get install evtest

start uimapper

evtest

(check you can see your egalax device and a new userinput device uimapper….)

If you select uimapper, then pressing the screen will give you position feedback and you can check for BTN_RIGHT too.

Perfect. I increased the radius by 1.7 and it works much more reliable now. The calibration is still not working, however I want to connect both a touchscreen for control and my HD TV at the same time and I need some additional hardware for that. For testing and setup I am using the composite output right now, so this is not the final resolution. My goal is to use a splitter and scaler on the HDMI out. But splitter and scaler have not been delivered.

Thank you again for your help

Next thing is I also want to do more adjustments on the confluence skin so that the buttons are easier to hit on a small touchscreen.

Ok Michael, glad to hear it’s ok….bit weird about the calibration though!? I had to recompiled the code and packaged in a deb to add axis swap check, other than that it’s the original ‘ts_lib’ framebuffer calibration tool and should be spot on.

Hi Mark,
it’s very good now and work in lower resolution Perfectly,
one thing when i connect mouse and do the calibration it’s not work good and i discoverd that i must remove all usb hardware and leave only the usb touch screen cable it’s work nice now thank you
sorry for bad english it’s not my language

Again, this seems odd. I have a touch-pad/keyboard plugged all the time it should have zero effect on the touchscreen calibration, it only uses the info from that specific event.
I wonder if it’s a power issue, are you using a externally powered usb-hub, the rpi shouldn’t be used to power usb-hub
.
At least you can calibrate once successfully and then this should remain ok as and when you change resolution.

Hi. I’ve bought this screen for my Raspberry Pi: http://www.ebay.com/itm/New-SAMSUNG-7-LED-LCD-Touch-Screen-Monitor-Kit-Outdoor-/110659973152?ssPageName=ADME:L:OC:DE:3160
I’m using RaspBMC and I’ve got a little problem with the calibration. I followed the instructions. With event 2 set in ts_calib.sh, the calibration starts, but doesn’t react. When I install uimapper, it changes to event 3, which also works for the calibration, but after reboot, it goes back to event 2 with the effect, that that touchscreen doesn#t react at all under XBMC. So basically, I cannot calibrate the screen with the event set to event2 and can’t use the screen with event3. Also starting xbmc with the calibration made with event3 doesn’t affect the touchscreen input
Any ideas?

Hey, first I want to thank you for your great job!
I’m runnig RaspBMC on a 512MB Pi.
The only connected input device is an egalaxTouchscreen via USB.
I used you tutorial and so far all worked fine. evtest returned this:

You are correct, umapper.conf has started the python scripts, you can see here for the output of ps aux…

root 3122 18.0 0.8 6704 3124 ? R< 22:18 0:00 python ./input-read.
root 3123 17.0 0.7 6356 2940 ? R< 22:18 0:00 python ./input-creat
I think xbmc is grabbing the device before uimapper can and therefore not applying the calibrations.
When the pi is booted, can you SSH the pi…..then ‘stop xbmc’, ‘stop uimapper’, and ‘evtest’ to check if you can see egalax event. Then ‘start uimapper’ and then ‘evtest’ to see if it captures egalax and creates a ‘uimapper – touchscreen’ event; you can compare top-left/bot right values with the egalax event(stop uimapper) to check calibrations are working, or having an effect.
If uimapper is running, it has control of the egalax, then ‘start xbmc’ and the calibration should be ok, if the calib is incorrect, then the ts_calib values might be wrong…(overscan, default resolution, etc)
hope it helps, please let me know,
cheers,
mark

Ok, I will check later with the new raspbmc. You can test both the ‘./input-read.py’ and ‘./input-create.py’ programs on the command line, using the options from the uimapper.conf file (more info later). You will need to kill the process manually though, not via ‘uimapper stop’. I think input-create worked before, but you can check permission or just ‘chmod +x input-create.py’ to make sure it executes ok.

I will check this, but with uimapper running then only one press is necessary, It injects an extra BTN_TOUCH event if touch press is within 0.2 to 0.5 seconds; the duration can be altered inside the input_create.py file.

You can check with evtest too, you should see a extra BTN_TOUCH event, if you touch for longer then you get a BTN_RIGHT event.

It is working, but with the lag it makes it less responsive, I will investigate….

Hi,
great job! My display works fine. But I also have the problem with the “doubleclick”. I have to touch always twice.
I tried to to the same as Thomas, but I didn’t get it…Could you explain for a noob how to change that “inrage value”?
Many thanks!
Fredrik

Although these instructions are very easy to follow, I have troubles with my faytech touch screen (10 inch – eGalax driver).
The touch control works fine out of the box, but the calibration is not correct:
There seems to be a multiplicator from touch to mouse cursor position, as the top-left corner is correct but when moving down or right the mouse cursor moves faster.
That is why I wanted to do what you described here.

But when I do *sudo sh ts_calib.sh* the hairline cross appears on the screen but it does NOT react to any touching!

What am I missing?
Or is there another way to correct the *multiplicator*?

If ‘sudo sh ts_calib.sh’ doens’t work, then it cannot grab your device. Make sure xbmc & uimapper, or anyother program that might use the device are stopped. Also check the event number of your touchscreen, by using ‘evtest’.

Basically, ATM ,(until I have time to fix) you need to grab all additional events from the toucscreen in uimapper as dummy events.to prevent XBMC grabbing before uimapper. This is done inside /etc/uimapper.conf.

Nice try 😉
Seriously, yes. I am pretty sure that I changed ts_calib.sh to the correct event number. But I might have mixed up the instructions for raspBMC and xbian.
I just followed your instructions above with a new installation of raspBMC from noob and it worked like a charm !!
I don*t know what I did differently this time and I would like to use xbian, so I will try it again (with your instructions for xbian: http://markamc.cybaman.net/?p=50 or the script from brantje).
Just two more questions;
– The calibration is still not perfect: starting from the center, the pointer is too fast. Can I edit the calibration values manually (maybe using arithmetic means of multiple runs of ts_calib) or does ts_calib need more samples ?
– A left mouse click can be done by a double tap (hard, but managable), but how can you do a right mouse click (for context menues) ?

Firstly, if the calibration is off, then please check overscan and config.txt and make sure tvservice -s gives you the same screen resolution as ts-calib and xbmc.
If still off, then change the values in /etc/pointercal or change multitouch.py ot toucscreen.py equations to suit scale and offsets.

If uimapper is running then single click select and btn-right should work ok; you can demonstrate this in evtest by selecting uimapper, A touch event over .25s issues an extra btn_touch and longer than 0.5 sec issues a btn_right. Values changeable inside /scripts/uinput-mapper/input-create.py (section near file end, values may differ, it’s been a while since i looked!)

If the btn-right event isn’t available, then try using the the multitouch config file in uimapper. (i.e. there is no BTN_RIGHT event capability in evtest output, check it’s created in uiimapper with evtest)

Below is an example of a capacitive multitouch uimapper.conf file. He also changed the single click time to 0.02 secs to make it work better (this is NOT necessary in XBIAN since it uses Andrei’s improved single click select hard-coded method. (RASPBMC to change soon)

Yes I am familiar with Andrei’s work; I originally recommended to Xbian the use of his XBMC patch for the egalax (and other) touchscreens. They are still using his modified patch in the latest release which includes single press select, inconguntion with uimapper, you get ts_calibrtation, and BTN_RIGHT for easier navigation on any skin.

As described in tutorial, i wanted an solution that was external to XBMC to prevent the need for recompiling.

The solution I provide was working very well with Raspbmc but since august/sertember release it seems a little laggy in comparison to Xbian. I think this is the main cause of the slow reaction to the click.

I really need to have a look to try to resolve the problem. In the meantime, try xbian, it is really good support with on going development.

Hey there
first of all it´s a great description for a great project. So i tried it myself, i built it the same like the description but i have the problem that after calibrating the screen and restarting xbmc the screen won´t work correctly like it´s not calibrated (curser moves into different directions and stuff like this).
My hardware is:
raspberry pi rev 2 board
eGalax 7″ touchscreen

based on this article I did the same for GeexBox. Only thing I haven’t worked out is the calibration (wrong scaling) but I am working on it :-).
Do you want to create the same documentation also for the GeexBox for other users looking for this kind of information?

Glad it helped. Please take/use what you need. Unfortunately I have no time to create more documentation or continue to improve and develop this solution. Although I have a similar solution working using a rockchip rk3066 android tv too.

Just wanted to say thanks heaps for this. Managed to get my eGalax screen working, for some reason mine gave reversed x & y values so just switched them in your script and took button press delay down to 0.05 seconds and found it is now accurate and responsive in Raspbmc

But I still have a problem using the BTN_RIGHT event which is generated by the input-create.py when touching long enough. I want to use this event for the context menu in XBMC.
But since input-create.py first creates a BTN_TOUCH (which translates to mouseclick in XBMC) and then an additional BTN_RIGHT the context menu is always started on the wrong level.
Am I missing something important?

Hi Marek,
I have moved away from this solution. From memory it’s best to check every issued event with evtest and xbmc not running.

input-create will issue an exact copy of the events created by the TS input. So when a touch event to occurs a BTN_TOUCH = 1 (press) and BTN_TOUCH = 0 (release) event is generated.

Additionally, the time and distance are recorded between the ‘press/release’ event and extra BTN_RIGHT is forced where necessary.

Originally, I also added an extra BTN_TOUCH (time checked) to force a ‘double click’ for single press select. Since Andrei’s solution has been implemented into xbian/raspbmc (recently) this is no longer needed. Maybe this is the problem.

Edit input-create and comment-out the lines below by inserting a ‘#’ at the line start leave the spacing-indent as is (python etc).
Change two lines of code at approximately lines 139 and 141, the ‘d.fire_event(rev)’

I’m not really supporting this blog atm, although I am reluctant to remove it since the daily visits.

Sorry to hear you have problems, I have tested in the past on both distros without errors. The tutorial was an attempt to make it easy and avoid confusion and problems.
if possible all that is needed is to copy the files/structure to the script folder.
Perhaps you can separate the lines…

Hello! Thanks for the great tutorial and I almost got it to work. It is all done, but my calibration is still off for like 1cm. I think it has to do something with my resolution.

I got a touchscreen with 800x480p resolution, but that is not supported by XBMC so I use 720x480p. Now I also changed my /scripts/uinput-mapper/configs/touchscreen.py to 720x480p, but still the calibration is 1cm off.

Are there more resolution settings who I dont know? (I’m a raspberry n00b)

Have you tried changing the resolution of the pi, in the config file, then performing the calibration?. So the resolution is the same in both pi and xbmc.
It’s a while since I used this solution, but I think this can be found earlier in either the xbian or raspbmc blog. If all else fails, you can manually change the values in as described for fine tweaking, i needed to do this to compensate for a 6.5″ bezel around a 7″ screen.

Hi,
i had the same Error Message and solved it by running my start script with the command “./input-read.py /dev/input/event2 -D | ./input-create.py configs/touchscreen.py” as user root.
Maybe this helps somebody to save time finding out what that error means.

Hi,
I’m using Xbian 3.15.6 (Jul 21 2014) and have used the install.sh to get and execute the uimapper scripts. I can calibrate the screen but XBMC doesn’t use the scaled settings. On investigation I found that input-create.py is not creating a new event & I think it is complaining that an event is still in use:

Yep I think you are right, it looks like a ‘grab’ issue. Did you ‘stop xbmc’ too before evtest? this will hopefully release the device and allow capture in evtest to see if this is the problem.

Brantje (from XBIAN), hosts old files and did his own install script for xbian. I have changed things many times since and recommend the ‘long-hand’ way; this is good since it gives an idea of what’s being put where etc.

If you look at my uimapper.conf, it is slightly different for both distros of xbmc, i changed to get uimapper to grab the device before xbmc starts. Best to use the ‘long’ method with the newer files. Then you can see if uimapper works without xbmc in evtest.

Yes I’d stopped XBMC (just a login prompt in the eGalax screen). Running evtest only ever shows events0-2 for keyboard, mouse & eGalax. I’ve never seen a new event created by uimapper. Is there a way to determine which process is using an event?

If xbmc is stopped, and uimapper stopped, check to see no ‘stray’ processes using ‘ps aux’. If you find input-read.py running this will grab the device, despite input-create.py running or not. Kill the process using ID number.

You really need to prove egalax input using evtest, then check why input-create is failing when running ‘start uimapper’. See info after running with ‘dmesg’.

Again try the versions from my google drive, I think they might be different.

1. If evetest works and provides feedback with event2 egalax,
2. then try uimapper next. if that fails, look at dmesg.
3. try to test input-read.py..—…input create.py. commands from the conf file, obviously adding the correct config file etc.

think it might be input-read grabing, if not, check dmesg for info

Not tried in a while and have no test-bed to check, so can give ideas on past problems. If I find a way to check which process has control I’ll let you know.

It’s all working! I installed your scripts, didn’t need to calibrate again (I’d done that before), rebooted & it’s all good. Looking to use this in the kitchen as a family notice board & add extra plugins to XBMC for family appointments and maybe a feed in from a CCTV camera.

Hi, I’ve run your steps and the touchscreen is “useable” at this point in RaspBMC.

However, I have noticed that the touchscreen behavior is non-linear, and this makes it only partially usable. I can’t touch some areas of the screen even with calibration.

For example: Touching near the top of the LCD (1280×800 LCD, 7″) the pointer goes to the correct coordinates. However, the further down I go, the calibration/response to coordinates is increasingly off.

I can’t touch the “home” and other icons in the bottom bar in XBMC. Near the bottom, I can’t move touch pointer any further. Below 1/2 way down the LCD/touch layer, the coordinates are not as accurate as near the top, so I can’t precisely touch the XBMC options.

hi, i finally found something easy and clear!
But i have a problem, everything seems to works fine till…
i have an egalax 7″, now the pointer is ALMOST calibrated…
if i move the finger in the first quarter of the monitor, i see the pointer move accordingly but in the last quarter of the screen, any idea?
(latest noobs with raspbmc updated and rpi b 512 v2)

i installed everything according to your instructions, but it is not quite working right:

After having everything set up, the touchscreen is still “out of alignment”, but if i go to the console and stop xbmc and uimapper and run ts_test, everything is complelety fine (i just took the ts_calibrate.sh and replaced the last line with “ts_test”).

Manually executing the uimapper scripts ( sudo python ./input-read.py $UIMAPPER_DEV -D | .sudo python /input-create.py $UIMAPPER_CONF & ) does not display any errors, but if i try to run “cat /dev/uinput” it returns “no suitable device found”. Shouldn’t i be able to see at least some sort of output there?

Assuming there should be output there, i think the script either fails to register the /dev/uinput device (but then it should return with an OSError, which it seemingly doesn’t), or it just fails to post any events to the device (but shouldn’t cat at least be able to “connect to the device then?).

just fyi: i tried running the scripts and writing the output to txt files and encountered and error in the input_create.py where it ran into an EOF error in line 35… so i replaced the calls in the uimapper.conf with absolute filenames instead of the relative ones and the error vanished, but the rest of the problem persists…

with $UIMAPPER_DEV=”/dev/input/event1″ and $UIMAPPER_CONF=”/scripts/uinput-mapper/configs/touchscreen.py”

(I had “2> /home/pi/read.txt” and “&> /home/pi/create.txt” added to the end of the two calls respectively to check for error output but neither file got populated anymore after i fixed the relative/absolute path error)

after fiddling around a bit more and having a quick glance at the source of both tslib and uimapper, it looks to me like uimapper is completely disregarding the xymix/yxmix variables and is doing some other weird stuff… not knowing exactly how that works, it seems to me that this might be the source of my problems. unfortunately i know too little python to tell for sure or to try to code a solution.
but shouldn’t a EV_ABS event have both the ABS_X and ABS_Y values in any case? if so, why disregard them and use the formula “(x*xscale + xoffset)*xcalib/(scaler) + (xmres – xsres)” instead of “(xtemp*xscale + ytemp*xymix + xoffset)*xcalib / scaler” (xtemp/ytemp are the ABS_X / ABS_Y values)? as far as i can see from the tslib code, that is how they calculate the calibrated position. is there any possibility to get that corrected?

nevermind, removing the very weird bit a the end ( […] + (xmres – xsres) ) did the job for me… since i am not running at 720p (or 1280*720), that part messed up my calibration quite a bit… i don’t really see the point for that anyway?! the missing rest only accounts for maybe 6-7 pixels at worst for me, so it’s “accurate enough”.

I really have no time or equipment for testing anymore. At the time of creating the blog everything worked ok. If XBMC has been compiled with other updates since then this may create situations causing problems.

Your solution seems to work for many people so thumbs up for that, solid work. I do encounter some problems tho and hope that you can give me a hand.
I freshly installed RaspBMC and went through your solution, calibrated the eGalax screen, which seemed to work.
Then I installed uimapper and evtest and changed the uimapper event to the right one.
starting and stopping uimapper works and evtest shows uimapper as an extra event (I have 2 eGalax Touchscreen events and one uimapper) evtest also shows that it’s touched when I tap on the screen.
But when I start xbmc again it’s not at all calibrated.
Is it possible that I have to use an older version of RaspBMC?

I wonder if you could help me with that.
Thanks for your awesome work.

I used brantje’s script on Xbian and it worked like a charm. I’d like to use RaspBMC tho to get dac support

i found out, if you’re using a higher or lower resolution (instead of
1280×720) in config.txt, you’ll got problems with the calibration. the
mouse-pointer will appear a little bit beside the touched point.

i’ve been asking me, if there will be any possibility to run this calibration with osmc. i’ve tried it, but it doesn’t work. the calibration input seems to be ok, but the uimapper doesn’t start at all.

I can’t promise anything, I Haven’t looked at this for a very long time and not developing anymore due to time constrains and other projects. I may have a look into this in the next month or so, if I can setup a test rig again, with rpi2 also, but I cannot promise anything as yet.
I thought there was enough on here to help someone find a fix or other work on related sites using this solution.
cheers

markamc

NB: if it wont start on boot, look into the Upstart script – uimapper.conf, that used to be the issue. Please bear in mind this only worked when installing as tutorial described, brantje script used different file versions and only for xbian.

i got it running (kind of)…
i extracted the important parts from the upstart script and ran them without systemd as a normal script and it works.
Its very ugly because i need to do this after every boot… as systemd is still a mystery to me i have lots of reading up ahead.
I plan to tinker the next few days with the systemd part and i will post everything here once im satisfied (hopefully i manage it until sunday evening)

Hi Mark, thanks for you’re help on this touchscreen issue. I find it strange that it isn’t just built in by now… Anyways, I have a few questions before I start diving into this. I’m building a stand alone device that will run one python script 24/7. Is there a way to just import libraries youve made into my script? The PI is currently running raspbian, but soon it will boot straight to my script. Also, do you know of a way to get rid of the arrow so the user will just press on buttons like a tablet?

Sorry these may be silly questions, I’m fairly new to python and development.

If python is installed then this script should start as-is; if you using the upstart script. If the system is not using upstart then follow procedure as normal and use run the script by calling the following, from another script or on-boot or even within your script, once.

Here is a solution to get working with RPI2 and OSMC latest , including ‘hotplug’.
I will prepare another full tutorial soon since things have changed slightly with jessie.

Disclaimer :
At the moment it works and has not failed in testing, most things i.e. calibration remain the same, except ‘uimapper.conf’, only thing I have changed is using the systemd service to monitor input devices and execute the python script. There may be other, better ways to do this (see link below), but hey, I really am a novice and have never claimed to be an expert!

No we create the uimapper.service file in /lib/systemd/system/ and add the text below and change the event description in ‘BindsTo=’ and ‘After=’ lines to suit your device.
sudo nano /lib/systemd/system/uimapper.service