4 * Copyright (C) 2001 Russell King.
6 * This file is placed under the LGPL. Please see the file
7 * COPYING for more details.
10 * Problem: some touchscreens read the X/Y values from ADC with a
11 * great level of noise in their lowest bits. This produces "jitter"
12 * in touchscreen output, e.g. even if we hold the stylus still,
13 * we get a great deal of X/Y coordinate pairs that are close enough
14 * but not equal. Also if we try to draw a straight line in a painter
15 * program, we'll get a line full of spikes.
17 * Solution: we apply a smoothing filter on the last several values
18 * thus excluding spikes from output. If we detect a substantial change
19 * in coordinates, we reset the backlog of pen positions, thus avoiding
20 * smoothing coordinates that are not supposed to be smoothed. This
21 * supposes all noise has been filtered by the lower-level filter,
22 * e.g. by the "variance" module.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/fcntl.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 //#include <asm/typedef.h>
33 #include <mach/iomux.h>
34 #include <asm/uaccess.h>
35 #include <asm/types.h>
37 #include <asm/delay.h>
42 * This filter works as follows: we keep track of latest N samples,
43 * and average them with certain weights. The oldest samples have the
44 * least weight and the most recent samples have the most weight.
45 * This helps remove the jitter and at the same time doesn't influence
46 * responsivity because for each input sample we generate one output
47 * sample; pen movement becomes just somehow more smooth.
50 /* To keep things simple (avoiding division) we ensure that
51 * SUM(weight) = power-of-two. Also we must know how to approximate
52 * measurements when we have less than NR_SAMPHISTLEN samples.
54 static const unsigned char weight [NR_SAMPHISTLEN - 1][NR_SAMPHISTLEN + 1] =
56 /* The last element is pow2(SUM(0..3)) */
57 { 5, 3, 0, 0, 3 }, /* When we have 2 samples ... */
58 { 8, 5, 3, 0, 4 }, /* When we have 3 samples ... */
59 { 6, 4, 3, 3, 4 }, /* When we have 4 samples ... */
62 static void average (struct tslib_dejitter *djt, struct ts_sample *samp)
64 const unsigned char *w;
69 w = weight [djt->nr - 2];
71 for (i = 0; i < djt->nr; i++) {
72 x += djt->hist [sn].x * w [i];
73 y += djt->hist [sn].y * w [i];
74 //p += djt->hist [sn].p * w [i];
75 sn = (sn - 1) & (NR_SAMPHISTLEN - 1);
78 samp->x = x >> w [NR_SAMPHISTLEN];
79 samp->y = y >> w [NR_SAMPHISTLEN];
80 //samp->pressure = p >> w [NR_SAMPHISTLEN];
82 printk("DEJITTER----------------> %d %d %d\n",
83 samp->x, samp->y, samp->pressure);
87 int dejitter_read(struct tslib_info *info, struct ts_sample *samp, int nr)
89 struct tslib_dejitter *djt = info->djt;
93 ret = variance_read(info, samp, nr);
94 for (s = samp; ret > 0; s++, ret--) {
95 if (s->pressure == 0) {
97 * Pen was released. Reset the state and
98 * forget all history events.
105 /* If the pen moves too fast, reset the backlog. */
107 int prev = (djt->head - 1) & (NR_SAMPHISTLEN - 1);
108 if (sqr(s->x - djt->hist [prev].x) +
109 sqr(s->y - djt->hist [prev].y) > djt->delta) {
111 printk("DEJITTER: pen movement exceeds threshold\n");
117 djt->hist[djt->head].x = s->x;
118 djt->hist[djt->head].y = s->y;
119 djt->hist[djt->head].p = s->pressure;
120 if (djt->nr < NR_SAMPHISTLEN)
123 /* We'll pass through the very first sample since
124 * we can't average it (no history yet).
129 average (djt, samp + count);
133 djt->head = (djt->head + 1) & (NR_SAMPHISTLEN - 1);