From 0f495fcd20fb92065518137d7259f63b34f3fd98 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Tue, 22 Jul 2014 17:54:53 +0200 Subject: [PATCH] calibration: add new calibration module, to recalibrate input depending of a device name. --- kivy/input/postproc/__init__.py | 2 + kivy/input/postproc/calibration.py | 84 ++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 kivy/input/postproc/calibration.py diff --git a/kivy/input/postproc/__init__.py b/kivy/input/postproc/__init__.py index aaeca4184..8207454b5 100644 --- a/kivy/input/postproc/__init__.py +++ b/kivy/input/postproc/__init__.py @@ -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() diff --git a/kivy/input/postproc/calibration.py b/kivy/input/postproc/calibration.py new file mode 100644 index 000000000..9f90426d5 --- /dev/null +++ b/kivy/input/postproc/calibration.py @@ -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 +