tslib: add new lowpass filter
authorGrazvydas Ignotas <notasas@gmail.com>
Sun, 4 Dec 2011 00:52:12 +0000 (02:52 +0200)
committerGrazvydas Ignotas <notasas@gmail.com>
Sun, 4 Dec 2011 15:43:10 +0000 (17:43 +0200)
taken from http://docs.openmoko.org/trac/ticket/1194

recipes/tslib/tslib/add_lowpass_plugin.patch [new file with mode: 0644]
recipes/tslib/tslib/lowpass.c [new file with mode: 0644]
recipes/tslib/tslib_1.0.bb

diff --git a/recipes/tslib/tslib/add_lowpass_plugin.patch b/recipes/tslib/tslib/add_lowpass_plugin.patch
new file mode 100644 (file)
index 0000000..a3fd3a7
--- /dev/null
@@ -0,0 +1,23 @@
+diff -ur tslib-1.0_/plugins/Makefile.am tslib-1.0/plugins/Makefile.am
+--- tslib-1.0_/plugins/Makefile.am     2011-12-04 02:32:07.595566001 +0200
++++ tslib-1.0/plugins/Makefile.am      2011-12-04 02:36:20.755566002 +0200
+@@ -103,7 +103,8 @@
+       $(MK712_MODULE) \
+       $(ARCTIC2_MODULE) \
+       $(H2200_LINEAR_MODULE) \
+-      $(INPUT_MODULE)
++      $(INPUT_MODULE) \
++      lowpass.la
+   
+ variance_la_SOURCES   = variance.c
+ variance_la_LDFLAGS   = -module $(LTVSN)
+@@ -117,6 +118,9 @@
+ pthres_la_SOURCES     = pthres.c
+ pthres_la_LDFLAGS     = -module $(LTVSN)
++lowpass_la_SOURCES    = lowpass.c
++lowpass_la_LDFLAGS    = -module $(LTVSN)
++
+ # hw access
+ corgi_la_SOURCES      = corgi-raw.c
+ corgi_la_LDFLAGS      = -module $(LTVSN)
diff --git a/recipes/tslib/tslib/lowpass.c b/recipes/tslib/tslib/lowpass.c
new file mode 100644 (file)
index 0000000..8fc7960
--- /dev/null
@@ -0,0 +1,158 @@
+/*
+ * This file is placed under the LGPL.  Please see the file
+ * COPYING for more details.
+ * 
+ * Very stupid lowpass dejittering filter
+ * taken from http://docs.openmoko.org/trac/ticket/1194
+ */
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+#include "tslib.h"
+#include "tslib-filter.h"
+
+struct tslib_lowpass {
+       struct tslib_module_info module;
+        struct ts_sample last;
+        struct ts_sample ideal;
+       int factor;
+       unsigned int flags;
+       unsigned char threshold;
+#define VAR_PENUP              0x00000001
+};
+
+static int lowpass_read(struct tslib_module_info *info, struct ts_sample *samp, int nr)
+{
+       struct tslib_lowpass *var = (struct tslib_lowpass *)info;
+       struct ts_sample current;
+       int count = 0;
+       int delta;
+
+       while (count < nr) {
+               if (info->next->ops->read(info->next, &current, 1) < 1)
+                       return count;
+
+               if (current.pressure == 0) {
+                       var->flags |= VAR_PENUP;
+                       samp [count++] = current;
+               } else if (var->flags & VAR_PENUP) {
+                       var->flags &= ~VAR_PENUP;
+                       var->last = current;
+                       samp [count++] = current;
+               } else {
+                       var->ideal = current;
+
+                       var->ideal.x = var->last.x;
+                       delta = current.x - var->last.x;
+                       if (delta <= var->threshold && 
+                                       delta >= -var->threshold)
+                               delta = 0;
+                       delta = delta * var->factor >> 16;
+                       var->ideal.x += delta;
+                       
+                       var->ideal.y = var->last.y;
+                       delta = current.y - var->last.y;
+                       if (delta <= var->threshold && 
+                                       delta >= -var->threshold)
+                               delta = 0;
+                       delta = delta * var->factor >> 16;
+                       var->ideal.y += delta;
+
+                       var->last = var->ideal;
+                       samp [count++] = var->ideal;
+               }
+       }
+       return count;
+}
+
+static int lowpass_fini(struct tslib_module_info *info)
+{
+       free(info);
+        return 0;
+}
+
+static const struct tslib_ops lowpass_ops =
+{
+       .read   = lowpass_read,
+       .fini   = lowpass_fini,
+};
+
+static int lowpass_factor(struct tslib_module_info *inf, char *str, void *data)
+{
+       struct tslib_lowpass *var = (struct tslib_lowpass *)inf;
+       long double v;
+       int err = errno;
+
+       v = strtod(str, NULL);
+
+       if (v > 1 || v < 0)
+               return -1;
+
+       errno = err;
+       switch ((int)data) {
+       case 1:
+               var->factor = v * 65536;
+               break;
+
+       default:
+               return -1;
+       }
+       return 0;
+}
+
+static int lowpass_threshold(struct tslib_module_info *inf, char *str, 
+               void *data)
+{
+       struct tslib_lowpass *var = (struct tslib_lowpass *)inf;
+       long result;
+       int err = errno;
+
+       result = strtol(str,NULL,0);
+       if (errno == ERANGE)
+              return -1;
+
+       errno = err;
+
+       switch ((int)data) {
+       case 1:
+               var->threshold = result;
+               break;
+       default:
+               return -1;
+       }
+       return 0;
+}
+
+static const struct tslib_vars lowpass_vars[] =
+{
+       { "factor",     (void *)1, lowpass_factor },
+       { "threshold",  (void*) 1, lowpass_threshold },
+};
+
+#define NR_VARS (sizeof(lowpass_vars) / sizeof(lowpass_vars[0]))
+
+TSAPI struct tslib_module_info *mod_init(struct tsdev *dev, const char *params)
+{
+       struct tslib_lowpass *var;
+
+       var = malloc(sizeof(struct tslib_lowpass));
+       if (var == NULL)
+               return NULL;
+
+       memset(var, 0, sizeof *var);
+       var->module.ops = &lowpass_ops;
+
+       var->factor = 0.4 * 65536;
+       var->threshold = 2;
+       var->flags = VAR_PENUP;
+
+       if (tslib_parse_vars(&var->module, lowpass_vars, NR_VARS, params)) {
+               free(var);
+               return NULL;
+       }
+
+       return &var->module;
+}
index 277e0a7..fe13f2b 100644 (file)
@@ -2,7 +2,15 @@ SRC_URI = "http://download.berlios.de/tslib/${BP}.tar.bz2 \
            file://fix_version.patch;patch=1 \
            file://tslib-nopressure.patch;patch=1 \
            file://tslib-pluginsld.patch;patch=1 \
-           file://newer-libtool-fix.patch;patch=1 "
-PR = "${INC_PR}.5"
+           file://newer-libtool-fix.patch;patch=1 \
+           file://add_lowpass_plugin.patch;patch=1 \
+           file://lowpass.c \
+"
+
+PR = "${INC_PR}.6"
+
+do_configure_prepend() {
+       cp ${WORKDIR}/lowpass.c ${S}/plugins/
+}
 
 include tslib.inc