calibration: add new calibration module, to recalibrate input depending of a device name.

This commit is contained in:
Mathieu Virbel 2014-07-22 17:54:53 +02:00
parent dc2862959c
commit 0f495fcd20
2 changed files with 86 additions and 0 deletions

View File

@ -12,12 +12,14 @@ from kivy.input.postproc.tripletap import InputPostprocTripleTap
from kivy.input.postproc.ignorelist import InputPostprocIgnoreList
from kivy.input.postproc.retaintouch import InputPostprocRetainTouch
from kivy.input.postproc.dejitter import InputPostprocDejitter
from kivy.input.postproc.calibration import InputPostprocCalibration
# Mapping of ID to module
kivy_postproc_modules = {}
# Don't go further if we generate documentation
if 'KIVY_DOC' not in os.environ:
kivy_postproc_modules['calibration'] = InputPostprocCalibration()
kivy_postproc_modules['retaintouch'] = InputPostprocRetainTouch()
kivy_postproc_modules['ignorelist'] = InputPostprocIgnoreList()
kivy_postproc_modules['doubletap'] = InputPostprocDoubleTap()

View File

@ -0,0 +1,84 @@
'''
Calibration
===========
.. versionadded:: 1.8.1
Recalibrate input device to a specific range / offset.
Let's say you have 3 1080p displays, the 2 firsts are multitouch. By default,
both will have mixed touch, the range will conflict with each others: the 0-1
range will goes to 0-5760 px (remember, 3 * 1920 = 5760.)
To fix it, you need to manually reference them. For example::
[input]
left = mtdev,/dev/input/event17
middle = mtdev,/dev/input/event15
# the right screen is just a display.
Then, you can use the calibration postproc module::
[postproc:calibration]
left = xratio=0.3333
middle = xratio=0.3333,xoffset=0.3333
Now, the touches from the left screen will be within 0-0.3333 range, and the
touches from the middle screen will be within 0.3333-0.6666 range.
'''
__all__ = ('InputPostprocCalibration', )
from kivy.config import Config
from kivy.logger import Logger
class InputPostprocCalibration(object):
'''Recalibrate the inputs.
The configuration must go within a section named `postproc:calibration`.
Within the section, you must have line like::
devicename = param=value,param=value
:Parameters:
`xratio`: float
Value to multiply X
`yratio`: float
Value to multiply Y
`xoffset`: float
Value to add to X
`yoffset`: float
Value to add to Y
'''
def __init__(self):
super(InputPostprocCalibration, self).__init__()
self.devices = {}
if not Config.has_section('postproc:calibration'):
return
default_params = {'xoffset': 0, 'yoffset': 0, 'xratio': 1, 'yratio': 1}
for device_key, params_str in Config.items('postproc:calibration'):
params = default_params.copy()
for param in params_str.split(','):
param = param.strip()
if not param:
continue
key, value = param.split('=', 1)
if key not in ('xoffset', 'yoffset', 'xratio', 'yratio'):
Logger.error(
'Calibration: invalid key provided: {}'.format(key))
params[key] = float(value)
self.devices[device_key] = params
def process(self, events):
for etype, event in events:
if event.device not in self.devices:
continue
params = self.devices[event.device]
event.sx = event.sx * params['xratio'] + params['xoffset']
event.sy = event.sy * params['yratio'] + params['yoffset']
return events