Login |  Register



Welcome
Welcome to engineeringdiy

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Calibration Add-On didn't work for me so...
PostPosted: Mon Jan 25, 2016 9:23 pm 
Joined: Mon Jan 25, 2016 9:01 pm
Posts: 4
.. I tried to fix it.

Here is what I came up with.
Changes:
- added screen with results
- Result screen and log is more detailed to be able to manually calculate the values
- Calculation changed so that it worked for me under different resolution settings. (Only tested on one screen so far)
- Added a border compensation (On my screen I'm unable to hit the last 10pixels. With the border compensation the cursor is a bit more left right top bottom as your finger and you are able to reach the corners. 0 = default/off. I use 10)
- Uses all values by building an average of the two X and Y values instead of just using the first one

How To:
- make a backup of the original /home/pi/.kodi/addons/plugin.program.touchCalibration/addon.py
- copy the code into addon.py (replace everything)
- change resolution in /boot/config.txt
- change resolution in Kodi
- start calibration
- tap the targets.
- restart Kodi
- enjoy

Feel free to test it and report bugs.

Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import calendar
import sys
import os
import xbmc
import xbmcgui
import xbmcaddon

SWAP_AXES_THRESHOLD = 10
ACTION_PREVIOUS_MENU = 10
ACTION_SELECT_ITEM = 7
ACTION_BACKSPACE = 92

TEXT_ALIGN_LEFT = 0
TEXT_ALIGN_RIGHT = 1
TEXT_ALIGN_CENTER_X = 2
TEXT_ALIGN_CENTER_Y = 4
TEXT_ALIGN_RIGHT_CENTER_Y = 5
TEXT_ALIGN_LEFT_CENTER_X_CENTER_Y = 6


class touchCalibration(xbmcgui.WindowDialog):

    def __init__(self):

        # Go to a new window to see animations from Home
        # TODO: - Create a new empty window instead of using Programs
        #       - Add text with "You don't have the radio module or the radio server was not
        #         started"

        xbmc.executebuiltin('XBMC.ActivateWindow(Programs)')
        self.retval = 0
        self.clientScript = os.path.join(addon.getAddonInfo('path'), 'resources', 'radio_client.py')
        self.stations = os.path.join(addon.getAddonInfo('path'), 'resources', 'stations')
        self.mediaPath = os.path.join(addon.getAddonInfo('path'), 'resources', 'media') + '/'
       
        # Background
        self.w = self.getWidth()
        self.h = self.getHeight()

        # ############################################
        # BUG/Feature??
        # Background size and Target position is still in 1280x720 wich doesn't make sense. Kodi keeps a fiexed grid size.
        # Therefore there is grid_fact_w and grid_fact_h
        # ############################################

        self.grid_fact_w = float(1280.0)/self.w
        self.grid_fact_h = float(720.0)/self.h
       
        self.background = xbmcgui.ControlImage(
            0,
            0,
            int(self.w * self.grid_fact_w),
            int(self.h * self.grid_fact_h),
            self.mediaPath + 'background.jpg',
            colorDiffuse='0xffffffff',
            )
        self.addControl(self.background)

        self.calibrationFile = '/home/pi/touchscreen_calibration_log'
        self.touchAxesCalib = '/home/pi/touchscreen_axes_calib'

        # Target
        self.targetW = 50
        self.targetH = 50
        self.offsetX = self.targetW / 2
        self.offsetY = self.targetH / 2
        self.x1 = 0
        self.y1 = 0
        self.x2 = self.w - self.targetW
        self.y2 = 0
        self.x3 = self.w - self.targetW
        self.y3 = self.h - self.targetH
        self.x4 = 0
        self.y4 = self.h - self.targetH
        self.targetImagePath = self.mediaPath + 'target.png'
        self.currentTarget = 1  # initial target index

        # Values used for calibration
        self.p1x = 0
        self.p1y = 0
        self.p2x = 0
        self.p2y = 0
        self.p3x = 0
        self.p3y = 0
        self.p4x = 0
        self.p4y = 0

        # First target
        self.targetImage = xbmcgui.ControlImage(
            int(self.x1 * self.grid_fact_w),
            int(self.y1 * self.grid_fact_h),
            int(self.targetW * self.grid_fact_w),
            int(self.targetH * self.grid_fact_h),
            self.targetImagePath,
            colorDiffuse='0xffffffff',
            )
        self.addControl(self.targetImage)

        # Informational text label
        self.info = xbmcgui.ControlLabel(
            int(self.targetW * self.grid_fact_w),
            10,
            int(self.w * self.grid_fact_w) - int(self.targetW * self.grid_fact_w),
            int(self.h * self.grid_fact_w) - 10,
            '',
            textColor='0xffffffff',
            font='font30',
            alignment=TEXT_ALIGN_LEFT,
            )
        self.addControl(self.info)
        self.info.setLabel(str(self.w) + 'x' + str(self.h) + '\n Touch the top left point and then press ENTER')

    def onAction(self, action):
        if action == ACTION_SELECT_ITEM:
            self.currentTarget = self.currentTarget + 1
            try:
                pos = xbmcgui.getMouseRawPosition()
                pos_string = 'x:%i y:%i' % (pos / 10000, pos % 10000)
                self.removeControl(self.targetImage)
            except:
                self.info.setLabel('- error -')

            if self.currentTarget == 2:
                self.info.setLabel('\n Touch the top right point and then press ENTER'
                                   )
                self.p1x = xbmcgui.getMouseRawPosition() / 10000
                self.p1y = xbmcgui.getMouseRawPosition() % 10000

            # Second target
                self.targetImage = xbmcgui.ControlImage(
                    int(self.x2 * self.grid_fact_w),
                    int(self.y2 * self.grid_fact_h),
                    int(self.targetW * self.grid_fact_w),
                    int(self.targetH * self.grid_fact_h),
                    self.targetImagePath,
                    colorDiffuse='0xffffffff',
                    )
                self.addControl(self.targetImage)

            if self.currentTarget == 3:
                self.info.setLabel('\n Touch the bottom right point and then press ENTER'
                                   )
                self.p2x = xbmcgui.getMouseRawPosition() / 10000
                self.p2y = xbmcgui.getMouseRawPosition() % 10000

            # Third target
                self.targetImage = xbmcgui.ControlImage(
                    int(self.x3 * self.grid_fact_w),
                    int(self.y3 * self.grid_fact_h),
                    int(self.targetW * self.grid_fact_w),
                    int(self.targetH * self.grid_fact_h),
                    self.targetImagePath,
                    colorDiffuse='0xffffffff',
                    )
                self.addControl(self.targetImage)

            if self.currentTarget == 4:
                self.info.setLabel('\n Touch the bottom left point and then press ENTER'
                                   )
                self.p3x = xbmcgui.getMouseRawPosition() / 10000
                self.p3y = xbmcgui.getMouseRawPosition() % 10000

            # Fourth target
                self.targetImage = xbmcgui.ControlImage(
                    int(self.x4 * self.grid_fact_w),
                    int(self.y4 * self.grid_fact_h),
                    int(self.targetW * self.grid_fact_w),
                    int(self.targetH * self.grid_fact_h),
                    self.targetImagePath,
                    colorDiffuse='0xffffffff',
                    )
                self.addControl(self.targetImage)

            if self.currentTarget == 5:
                self.p4x = xbmcgui.getMouseRawPosition() / 10000
                self.p4y = xbmcgui.getMouseRawPosition() % 10000

            # Compute values and write them in the file
                doCalibration(self)

            if self.currentTarget > 5:

            # exit
                self.retval = 0
                self.close()

        if action == ACTION_PREVIOUS_MENU or action == ACTION_BACKSPACE:
            self.retval = 0
            self.close()


DELTA = 10  # adds Hysteresis


def doCalibration(self):

    # Predefined values
    x1 = self.x1 + self.offsetX
    x2 = self.x2 + self.offsetX
    x3 = self.x3 + self.offsetX
    x4 = self.x4 + self.offsetX
    y1 = self.y1 + self.offsetY
    y2 = self.y2 + self.offsetY
    y3 = self.y3 + self.offsetY
    y4 = self.y4 + self.offsetY

    border_compensation = 0  # unusable border in pixel

    # Read values
    px1 = self.p1x
    px2 = self.p2x
    px3 = self.p3x
    px4 = self.p4x
    py1 = self.p1y
    py2 = self.p2y
    py3 = self.p3y
    py4 = self.p4y

    fd_log = open(self.calibrationFile, 'w')

    fd_log.write(str(self.w) + 'x' + str(self.h) + '\n')
    fd_log.write('Touch screen calibration plugin\n')
    fd_log.write('Predefined points:\n')
    fd_log.write(str(x1) + ',' + str(y1) + '\n')
    fd_log.write(str(x2) + ',' + str(y2) + '\n')
    fd_log.write(str(x3) + ',' + str(y3) + '\n')
    fd_log.write(str(x4) + ',' + str(y4) + '\n')
    fd_log.write('Obtained points:')
    fd_log.write(str(px1) + ',' + str(py1) + '\n')
    fd_log.write(str(px2) + ',' + str(py2) + '\n')
    fd_log.write(str(px3) + ',' + str(py3) + '\n')
    fd_log.write(str(px4) + ',' + str(py4) + '\n')

    # Final values
    calib_x_fact = 1.0
    calib_y_fact = 1.0
    calib_x_d = 0
    calib_y_d = 0
    swap_axes = 0
    click_confines = 8
    touch_mouse = 1

    case1 = 0
    case2 = 0
    startx = 0
    starty = 0
    rev = 0

    if px2 > px1 + DELTA or px1 > px2 + DELTA:

        # Compute the ratio between the big screen(touch) and the visible screen(the ratio can
        # be negative)
        px_avg = (px2 - px1 + px3 - px4) / float(2.0)
        py_avg = (py4 - py1 + py3 - py2) / float(2.0)
        calib_x_fact = (x2 - x1 + border_compensation * 2) / px_avg
        calib_y_fact = (y4 - y1 + border_compensation * 2) / py_avg

        # whichever point is the left one decides the x offset of the touch frame
        if px2 > px1 + DELTA:

        # cases 1 & 4
            case1 = 14
            calib_x_d = self.offsetX - int((px1 + px4) / 2 * calib_x_fact) - border_compensation

        if px1 > px2 + DELTA:

        # cases 5 & 8
            case1 = 58

        # take the left margin as reference
            calib_x_d = self.w - self.offsetX - int((px2 + px3) / 2 * calib_x_fact) + border_compensation

        if py4 > py1 + DELTA or py1 > py4 + DELTA:

        # whichever point is the top one decides the y offset of the touch frame
            if py4 > py1 + DELTA:

            # cases 1 & 8
                case2 = 18
                calib_py = (py1 + py2) / 2 - y1
                calib_y_d = self.offsetY - int((py1 + py2) / 2 * calib_y_fact) - border_compensation

            if py1 > py4 + DELTA:

            # cases 4 & 5
                case2 = 45
                calib_y_d = self.h - self.offsetY - int((py3 + py4) / 2 * calib_y_fact) + border_compensation
    else:

        rev = 1

    if rev == 1:
        xbmcgui.Dialog().ok('Please reverse touch screen cable', 'Please reverse the touch screen cable comming from the screen and going into the usb controller and then try again')
        fd_log.write('Please reverse the touch screen cable comming from the screen and going into the usb controller'
                     )
        fd_log.close()

    # Restart with the first target
        self.currentTarget = 1  # initial target index
        self.targetImage = xbmcgui.ControlImage(
            self.x1,
            self.y1,
            self.targetW,
            self.targetH,
            self.targetImagePath,
            colorDiffuse='0xffffffff',
            )
        self.addControl(self.targetImage)
    else:

    # Compute the main case
        case = 0

        if case1 / 10 == case2 / 10:
            case = case1 / 10
        if case1 % 10 == case2 % 10:
            case = case1 % 10
        if case1 / 10 == case2 % 10:
            case = case1 / 10
        if case1 % 10 == case2 / 10:
            case = case1 % 10
        if case == 2 or case == 3 or case == 6 or case == 7:
            swap_axes = 1

        tmp_text = 'Results saved. Press enter to exit.\n'
        tmp_text += 'Input:\n'
        tmp_text += '1: ' + str(px1) + '/' + str(py1) + '(' + str(x1) + '/' + str(y1) + ')-------------- 2: ' + str(px2) + '/' + str(py2) + '(' + str(x2) + '/' + str(y2) + ')\n'
        tmp_text += '     |                                                                    |\n'
        tmp_text += '     |                                                                    |\n'
        tmp_text += '4: ' + str(px4) + '/' + str(py4) + '(' + str(x4) + '/' + str(y4) + ')-------------- 3: ' + str(px3) + '/' + str(py3) + '(' + str(x3) + '/' + str(y3) + ')\n'
        tmp_text += 'Output:\n'
        tmp_text += 'calib_x_d=' + str(calib_x_d) + ';\n'
        tmp_text += 'calib_x_fact=' + str(calib_x_fact) + ';\n'
        tmp_text += 'calib_y_d=' + str(calib_y_d) + ';\n'
        tmp_text += 'calib_y_fact=' + str(calib_y_fact) + ';\n'
        tmp_text += 'swap_axes=' + str(swap_axes) + ';\n'
        tmp_text += 'click_confines=' + str(click_confines) + ';\n'
        tmp_text += 'touch_mouse=' + str(touch_mouse) + ';\n'
        tmp_text += 'border_compensation=' + str(border_compensation) + ';\n'

    # print on screen
        self.info.setLabel(tmp_text)

    # Debug values
        fd_log.write(tmp_text)
        fd_log.close()

    # Calibration values
        fd_calib = open(self.touchAxesCalib, 'w')
        fd_calib.write('calib_x_d=' + str(calib_x_d) + ';')
        fd_calib.write('calib_x_fact=' + str(calib_x_fact) + ';')
        fd_calib.write('calib_y_d=' + str(calib_y_d) + ';')
        fd_calib.write('calib_y_fact=' + str(calib_y_fact) + ';')
        fd_calib.write('swap_axes=' + str(swap_axes) + ';')
        fd_calib.write('click_confines=' + str(click_confines) + ';')
        fd_calib.write('touch_mouse=' + str(touch_mouse))
        fd_calib.close()

        self.retval = 0


addon = xbmcaddon.Addon(id='plugin.program.touchCalibration')
finished = 0
counter = 0

while finished == 0:
    dialog = touchCalibration()
    dialog.doModal()

    if dialog.retval == 0:
        finished = 1
    del dialog
del addon



Last edited by PiBevoreCake on Tue Feb 02, 2016 5:21 am, edited 2 times in total.

Report this post
Profile  Offline
 
 Post subject: Re: Calibration Add-On didn't work for me so...
PostPosted: Sun Jan 31, 2016 5:24 am 
Joined: Sun Jan 31, 2016 2:42 am
Posts: 9
You sir are amazing. This works flawlessly on 4.2 Helix.

Thank you.


Report this post
Profile  Offline
 
 Post subject: Re: Calibration Add-On didn't work for me so...
PostPosted: Sun Jan 31, 2016 4:17 pm 
Joined: Mon Jan 25, 2016 9:01 pm
Posts: 4
Thanks, I'm glad you like it. I added a workaround for the Target position.


Report this post
Profile  Offline
 
 Post subject: Re: Calibration Add-On didn't work for me so...
PostPosted: Mon Feb 01, 2016 4:36 pm 
Joined: Fri May 22, 2015 8:46 pm
Posts: 315
It works like a charm !! Awesome


Report this post
Profile  Offline
 
 Post subject: Re: Calibration Add-On didn't work for me so...
PostPosted: Tue Feb 02, 2016 5:22 am 
Joined: Mon Jan 25, 2016 9:01 pm
Posts: 4
I found the bug :)
I recommend you copy the latest code and do the calibration again. It should be more precise now with targets.


Report this post
Profile  Offline
 
 Post subject: Re: Calibration Add-On didn't work for me so...
PostPosted: Wed Dec 07, 2016 8:44 pm 
Joined: Wed Dec 07, 2016 8:41 pm
Posts: 1
@PiBevoreCake
very very very very thanks
I've been dealing for a year now with touch calibrations and I finished tonight :D :D :D


Report this post
Profile  Offline
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest

Panel

Top You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Search for:
Jump to:  


suspicion-preferred