Friday, 17 June 2016

Hiding the mouse cursor in Kodi

I use Kodi with a touchscreen and wanted to hide the mouse cursor, but NOT disable mouse input. This can be done by using a 1x1 transparent PNG file as the mouse cursor. Find out what skin you're using and where your skin directory is. For me this is estouchy and its directory is /usr/share/kodi/addons/skin.estouchy. You need to download the PNG first:

wget https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png

Then move it to the skins media directory (or where ever the skin gets its cursors from):

sudo mv 1x1.png /usr/share/kodi/addons/skin.estouchy/media/pointer_1x1.png

Then edit the Pointer.xml file:

sudo nano /usr/share/kodi/addons/skin.estouchy/xml/Pointer.xml

And replace all "width" and "height" values with 1 and all "texture" values with the name of the 1x1 texture:

<width>1</width>
<height>1</height>
<texture>pointer_1x1.png</texture> 

Now your cursor should be invisible.

Thursday, 16 June 2016

Properly setting up a 5" HDMI touchscreen for Kodi on Raspbian

Wow. This was a royal pita. I wanted to build this wake-up-light-alarm-clock-media-thingy for my girlfriend. Well, I got my hands on a RPi Zero and needed an LCD display. I found this nice-looking "WaveShare 4" IPS display" on Ebay and spent days trying to get it to work properly. You want a hint? DONT DO IT! BUY A HDMI SCREEN! Refresh rate sucks, Kodi is not working with hardware-acceleration, Framebuffer copying never worked. Short: It sucked!

So. Next chapter: Bought a 5" HDMI screen here. It has an HDMI input and connects to the RPi GPIO pins for power and touch functionality... and works ALMOST out of the box on a fresh Raspbian install (from 5/2016). Nice!

Set up screen resolution and touchscreen

 

 Edit /boot/config.txt:

hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=87
hdmi_cvt 800 480 60 6 0 0 0


dtparam=spi=on
dtoverlay=ads7846,penirq=25,speed=10000,penirq_pull=2,xohms=150


This gets the display and the touchscreen up and running. You can test if the touchscreen does anything using:

cat /dev/input/mouse0
or
cat /dev/input/event0
 

Setting up the touchscreen for X

 

This should give you weird output whenever you touch the screen. Fine.
Now you want to calibrate it. To do that install the calibration utility:

sudo apt-get install xinput_calibrator

Then start the GUI. You should now have an entry "Calibrate touchscreen" in your menu. Start it and tap the points. When finished the screen should be calibrated.
Or, to do the calibration from the command line, run:

xinput-calibrator

You can also do this from SSH, if you don't have a keyboard attached:

DISPLAY=:0 xinput_calibrator

Tap the points and when finished, the tool will output some calibration information:

Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "114 3982 226 3883"
        Option  "SwapAxes"      "0"
EndSection


Paste the calibration info into "/usr/share/X11/xorg.conf.d/99-calibration.conf".

Setting up the touchscreen for Kodi

 

Now you have a calibrated X input device, so the Desktop works nicely, but Kodi still does not. When starting it from X, there is a cursor, but the calibration is way off. When starting it standalone, there is not even touch input...
Fix this by installing tslib and evtest:

sudo apt-get install evtest tslib libts-bin 
 
Additionally you will need Then calibrate the touchscreen using:

TSLIB_TSDEVICE=/dev/input/event0 ts_calibrate

Touch the 5 crosses and you're done. You can test the calibration using:

TSLIB_TSDEVICE=/dev/input/event0 ts_test

You might also need to add the "kodi" user to the "input" user group:

sudo usermod -a -G input kodi

If you have Kodi version >= 17 ("Krypton") you cannow set up scale factors for the display so the cursor is properly displayed.

Setting display scale factors in Kodi >= v17

 

Test if Kodi can use touch input now. Start Kodi and touch the upper left corner of the screen. You will see a cursor and can move it, but only in 1/5th of the screen. The problem is that the coordinates /dev/input/event0 sends are touch device coordinates (in the range you see in the calibration values above), but Kodi expects pixel screen coordinates (0-800,0-480). Kodi starts to support touchscreen calibration out-of-the-box as of v17 aka "Krypton".
Edit the file advancedsettings.xml in your users userdata folder, e.g. /home/kodi/.kodi/userdata or ~/.kodi/userdata. If it does not exist, create it and add the following entries:

<advancedsettings>
    <touchscreen>
        <x_offset>-32</x_offset> <!-- cursor pixel x offset
        <y_offset>-35</y_offset> <!-- cursor pixel y offset
        <x_stretch_factor>0.209</x_stretch_factor> <!-- stretch factor
        <y_stretch_factor>0.131</y_stretch_factor> <!-- stretch factor
    </touchscreen>
</advancedsettings>


Good start values can be calculated by dividing the screen resolution by the X11 calibration values above (e.g. for x: 800/3982=~0.2 and 480/3883=~0.124). Note that you have to restart Kodi after editing the values to see any changes.

Installing a touchscreen calibration fix

 

When you have an older Kodi version and can not use advancedsettings.xml, there are also ways to properly calibrate your screen. Read here about using a tslib fork and ts_uinput_touch for this.

Fixing the double-click issue


A remaining problem is that Kodi wants a double-click to activate a function. This is not good as we want to activate functions with a single click or tap. We can change this by editing the file /usr/share/kodi/system/keymaps/mouse.xml. Change the entries in the mouse section to:

<mouse>
  <leftclick>leftclick</leftclick>
  <rightclick>rightclick</rightclick>
  <middleclick>middleclick</middleclick>
  <doubleclick id="0">noop</doubleclick>
  <longclick id="0">contextmenu</longclick>
  <wheeldown>wheeldown</wheeldown>
  <wheelup>wheelup</wheelup>
  <mousedrag>mousedrag</mousedrag>
  <mousedragend>leftclick</mousedragend>
  <mousemove>noop</mousemove>
</mouse>



P.S. Find out how to hide the cursor here.
P.P.S. You can also add backlight control to the screen...

Wednesday, 15 June 2016

Adding GPIO PWM backlight control to a 5" HDMI screen (and Kodi)

So the screen from here is working in Kodi now, but it draws a lot of power when the backlight is on (~200mA). We want to turn at least the backlight off, leaving the touchscreen active so we can interact and wake it up again. Buuut the LCD has no backlight control which is a pity (would have cost only cents). Good thing is: We can easily add it.
What you'll need:
  • IRF7104 P-Channel MOSFET
  • 10 kOhm resistor
The displays backlight power supply runs on 5V and already has an on/off switch. We cut the trace leading from the switch to the backlight power supply circuit:


Then solder the IRF7104 there (pin 3 (Source) right, pin 6 (Drain) left). Pin 1 of the MOSFET is the pin next to the little dent. The IRF will switch power to the backlight on and off and should handle currents of up to ~1.5A.
Now we need a circuit to connect the Gate (pin 4) of the MOSFET to the GPIO pin. I used GPIO #18 / PWM0 (pin 12) on my RPi Zero. NOTE: This pin may be connected to the audio out on other RPis, so you might need to choose a different pin...
We connect the Gate pin to a 10k Ohm resistor and connect that to our GPIO pin. The final circuit is in the image below:


Now when you start the RPi it should turn the screen on by default. You can use the commands:

gpio -g mode 18 out
gpio -g write 18 1

to turn the screen off. You need WiringPi for that, so if you don't have it, install it... To control backlight brightness by PWM you can do:

gpio -g mode pwm
gpio -g pwm 18 x (x takes values from 0 (full on), to 1023 (off))

To turn the screen back on do:

gpio -g mode 18 out
gpio -g mode 18 0

For the following instructions, use the home directory of the user you want to run Kodi from. If you make Kodi autostart by editing /etc/default/kodi, this is the user "kodi" (used here), else it is probably the user "pi".

Now to integrate support for the backlight into Kodi, we need to run those commands when the Kodi screensaver starts or ends. For that we need a service addon called "service.xbmc.callbacks". Download the script from the releases section and unzip it into /home/kodi/.kodi/addons. Then create two script files in the /home/kodi directory:

screen_off.sh:
#!/bin/sh
gpio -g mode 18 out
gpio -g write 18 1


screen_on.sh:
#!/bin/sh
gpio -g mode 18 out
gpio -g write 18 0

Make those scripts executable:

chmod +x screen_on.sh screen_off.sh

Now start Kodi and add those two scripts in the addon configuration for screensaver start / exit:


Your backlight should now turn off / on with the screen saver.