pbms.c revision 1.1 1 /* $Id: pbms.c,v 1.1 2006/02/05 18:06:50 christos Exp $ */
2
3 /*
4 * Copyright (c) 2005, Johan Walln
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * 3. The name of the copyright holder may not be used to endorse or
20 * promote products derived from this software without specific
21 * prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * The pbms driver provides support for the trackpad on new (post
38 * February 2005) Apple PowerBooks (and iBooks?) that are not standard
39 * USB HID mice.
40 */
41
42 /*
43 * The protocol (that is, the interpretation of the data generated by
44 * the trackpad) is taken from the Linux appletouch driver version
45 * 0.08 by Johannes Berg, Stelian Pop and Frank Arnold. The method
46 * used to detect fingers on the trackpad is also taken from that
47 * driver.
48 */
49
50 /*
51 * XXX This should be extended and converted to a man page, or
52 * removed.
53 *
54 * NAME
55 * pbms -- PowerBook (and iBook) trackpad mouse support
56 *
57 * SYNOPSIS
58 * pbms* at uhidev? reportid ?
59 * wsmouse* at pbms*
60 *
61 * DESCRIPTION
62 *
63 * The pbms driver provides support for the trackpads on new (post
64 * February 2005) Apple PowerBooks and iBooks that are not standard
65 * USB HID mice. Access to the trackpad is through the wscons(4)
66 * driver.
67 *
68 * SEE ALSO
69 *
70 * uhidev(4), usb(4), wsmouse(4)
71 */
72
73 /*
74 * To add the driver to the kernel, copy this file to (say)
75 * sys/arch/macppc/dev/pbms.c and add
76 *
77 * device pbms: wsmousedev
78 * attach pbms at uhidbus
79 * file arch/macppc/dev/pbms.c pbms
80 *
81 * to sys/arch/macppc/conf/macppc.files. Then add the corresponding
82 * specification to your kernel configuration file (see the synopsis
83 * for the driver).
84 *
85 * To add support for other devices using the same protocol, add an
86 * entry to the pbms_devices table below. See the comments for
87 * pbms_devices and struct pbms_devs.
88 */
89
90 /*
91 * PROTOCOL:
92 *
93 * The driver transfers continuously 81 byte events. The last byte is
94 * 1 if the button is pressed, and is 0 otherwise. Of the remaining
95 * bytes, 26 + 16 = 42 are sensors detecting pressure in the X or
96 * horizontal, and Y or vertical directions, respectively. On 12 and
97 * 15 inch PowerBooks, only the 16 first sensors in the X-direction
98 * are used. In the X-direction, the sensors correspond to byte
99 * positions
100 *
101 * 2, 7, 12, 17, 22, 27, 32, 37, 4, 9, 14, 19, 24, 29, 34, 39, 42,
102 * 47, 52, 57, 62, 67, 72, 77, 44 and 49;
103 *
104 * in the Y direction, the sensors correspond to byte positions
105 *
106 * 1, 6, 11, 16, 21, 26, 31, 36, 3, 8, 13, 18, 23, 28, 33 and 38.
107 *
108 * The change in the sensor values over time is more interesting than
109 * their absolute values: if the pressure increases, we know that the
110 * finger has just moved there.
111 *
112 * We keep track of the previous sample (of sensor values in the X and
113 * Y directions) and the accumulated change for each sensor. When we
114 * receive a new sample, we add the difference of the new sensor value
115 * and the old value to the accumulated change. If the accumulator
116 * becomes negative, we set it to zero. The effect is that the
117 * accumulator is large for sensors whose pressure has recently
118 * increased. If there is little change in pressure (or if the
119 * pressure decreases), the accumulator drifts back to zero.
120 *
121 * Since there is some fluctuations, we ignore accumulator values
122 * below a threshold. The raw finger position is computed as a
123 * weighted average of the other sensors (the weights are the
124 * accumulated changes).
125 *
126 * For smoothing, we keep track of the previous raw finger position,
127 * and the virtual position reported to wsmouse. The new raw position
128 * is computed as a weighted average of the old raw position and the
129 * computed raw position. Since this still generates some noise, we
130 * compute a new virtual position as a weighted average of the previous
131 * virtual position and the new raw position. The weights are
132 * controlled by the raw change and a noise parameter. The position
133 * is reported as a relative position.
134 */
135
136 /*
137 * TODO:
138 *
139 * Add support for other drivers of the same type.
140 *
141 * Add support for tapping and two-finger scrolling? The
142 * implementation already detects two fingers, so this should be
143 * relatively easy.
144 *
145 * Write a man page?
146 *
147 * Implement some of the mouse ioctls?
148 *
149 * Take care of the XXXs.
150 *
151 */
152
153 #include <sys/cdefs.h>
154
155 #include <sys/param.h>
156 #include <sys/device.h>
157 #include <sys/errno.h>
158
159 #include <sys/ioctl.h>
160 #include <sys/systm.h>
161 #include <sys/tty.h>
162
163 #include <dev/usb/usb.h>
164 #include <dev/usb/usbdi.h>
165 #include <dev/usb/usbdevs.h>
166 #include <dev/usb/uhidev.h>
167
168 #include <dev/wscons/wsconsio.h>
169 #include <dev/wscons/wsmousevar.h>
170
171
172 /*
173 * Debugging output.
174 */
175
176
177 /* XXX Should be redone, and its use should be added back. */
178
179 #ifdef PBMS_DEBUG
180
181 /*
182 * Print the error message (preceded by the driver and function)
183 * specified by the string literal fmt (followed by newline) if
184 * pbmsdebug is greater than n. The macro may only be used in the
185 * scope of sc, which must be castable to struct device *. There must
186 * be at least one vararg. Do not define PBMS_DEBUG on non-C99
187 * compilers.
188 */
189
190 #define DPRINTFN(n, fmt, ...) \
191 do { \
192 if (pbmsdebug > (n)) \
193 logprintf("%s: %s: " fmt "\n", \
194 ((struct device *) sc)->dv_xname, \
195 __func__, __VA_ARGS__); \
196 } while ( /* CONSTCOND */ 0)
197
198 int pbmsdebug = 0;
199
200 #endif /* PBMS_DEBUG */
201
202
203 /*
204 * Magic numbers.
205 */
206
207
208 /* The amount of data transfered by the USB device. */
209 #define PBMS_DATA_LEN 81
210
211 /* The maximum number of sensors. */
212 #define PBMS_X_SENSORS 26
213 #define PBMS_Y_SENSORS 16
214 #define PBMS_SENSORS (PBMS_X_SENSORS + PBMS_Y_SENSORS)
215
216 /*
217 * Parameters for supported devices. For generality, these parameters
218 * can be different for each device. The meanings of the parameters
219 * are as follows.
220 *
221 * desc: A printable description used for dmesg output.
222 *
223 * noise: Amount of noise in the computed position. This controls
224 * how large a change must be to get reported, and how
225 * large enough changes are smoothed. A good value can
226 * probably only be found experimentally, but something around
227 * 16 seems suitable.
228 *
229 * product: The product ID of the trackpad.
230 *
231 *
232 * threshold: Accumulated changes less than this are ignored. A good
233 * value could be determined experimentally, but 5 is a
234 * reasonable guess.
235 *
236 * vendor: The vendor ID. Currently USB_VENDOR_APPLE for all devices.
237 *
238 * x_factor: Factor used in computations with X-coordinates. If the
239 * x-resolution of the display is x, this should be
240 * (x + 1) / (x_sensors - 1). Other values work fine, but
241 * then the aspect ratio is not necessarily kept.
242 *
243 * x_sensors: The number of sensors in the X-direction.
244 *
245 * y_factor: As x_factors, but for Y-coordinates.
246 *
247 * y_sensors: The number of sensors in the Y-direction.
248 */
249
250 struct pbms_dev {
251 const char *descr; /* Description of the driver (for dmesg). */
252 int noise; /* Amount of noise in the computed position. */
253 int threshold; /* Changes less than this are ignored. */
254 int x_factor; /* Factor used in computation with X-coordinates. */
255 int x_sensors; /* The number of X-sensors. */
256 int y_factor; /* Factor used in computation with Y-coordinates. */
257 int y_sensors; /* The number of Y-sensors. */
258 uint16_t product; /* Product ID. */
259 uint16_t vendor; /* The vendor ID. */
260 };
261
262 /* Devices supported by this driver. */
263 static struct pbms_dev pbms_devices[] =
264 {
265 #define POWERBOOK_TOUCHPAD(inches, prod, x_fact, x_sens, y_fact) \
266 { \
267 .descr = #inches " inch PowerBook Trackpad", \
268 .vendor = USB_VENDOR_APPLE, \
269 .product = (prod), \
270 .noise = 16, \
271 .threshold = 5, \
272 .x_factor = (x_fact), \
273 .x_sensors = (x_sens), \
274 .y_factor = (y_fact), \
275 .y_sensors = 16 \
276 }
277 /* 12 inch PowerBooks */
278 POWERBOOK_TOUCHPAD(12, 0x030a, 69, 16, 52), /* XXX Not tested. */
279 /* 15 inch PowerBooks */
280 POWERBOOK_TOUCHPAD(15, 0x020e, 85, 16, 57), /* XXX Not tested. */
281 POWERBOOK_TOUCHPAD(15, 0x020f, 85, 16, 57),
282 /* 17 inch PowerBooks */
283 POWERBOOK_TOUCHPAD(17, 0x020d, 71, 26, 68) /* XXX Not tested. */
284 #undef POWERBOOK_TOUCHPAD
285 };
286
287 /* The number of supported devices. */
288 #define PBMS_NUM_DEVICES (sizeof(pbms_devices) / sizeof(pbms_devices[0]))
289
290
291 /*
292 * Types and prototypes.
293 */
294
295
296 /* Device data. */
297 struct pbms_softc {
298 struct uhidev sc_hdev; /* USB parent (got the struct device). */
299 int sc_acc[PBMS_SENSORS]; /* Accumulated sensor values. */
300 signed char sc_prev[PBMS_SENSORS]; /* Previous sample. */
301 signed char sc_sample[PBMS_SENSORS]; /* Current sample. */
302 struct device *sc_wsmousedev; /* WSMouse device. */
303 int sc_noise; /* Amount of noise. */
304 int sc_theshold; /* Threshold value. */
305 int sc_x; /* Virtual position in horizontal
306 * direction (wsmouse position). */
307 int sc_x_factor; /* X-coordinate factor. */
308 int sc_x_raw; /* X-position of finger on trackpad. */
309 int sc_x_sensors; /* Number of X-sensors. */
310 int sc_y; /* Virtual position in vertical direction
311 * (wsmouse position). */
312 int sc_y_factor; /* Y-coordinate factor. */
313 int sc_y_raw; /* Y-position of finger on trackpad. */
314 int sc_y_sensors; /* Number of Y-sensors. */
315 uint32_t sc_buttons; /* Button state. */
316 uint32_t sc_status; /* Status flags. */
317 #define PBMS_ENABLED 1 /* Is the device enabled? */
318 #define PBMS_DYING 2 /* Is the device dying? */
319 #define PBMS_VALID 4 /* Is the previous sample valid? */
320 };
321
322
323 /* Static function prototypes. */
324 static void pbms_intr(struct uhidev *, void *, unsigned int);
325 static int pbms_enable(void *);
326 static void pbms_disable(void *);
327 static int pbms_ioctl(void *, unsigned long, caddr_t, int, usb_proc_ptr);
328 static void reorder_sample(signed char *, signed char *);
329 static int compute_delta(struct pbms_softc *, int *, int *, int *, uint32_t *);
330 static int detect_pos(int *, int, int, int, int *, int *);
331 static int smooth_pos(int, int, int);
332
333 /* Access methods for wsmouse. */
334 const struct wsmouse_accessops pbms_accessops = {
335 pbms_enable,
336 pbms_ioctl,
337 pbms_disable,
338 };
339
340 /* This take cares also of the basic device registration. */
341 USB_DECLARE_DRIVER(pbms);
342
343
344 /*
345 * Basic driver.
346 */
347
348
349 /* Try to match the device at some uhidev. */
350
351 int
352 pbms_match(struct device *parent, struct cfdata *match, void *aux)
353 {
354 struct uhidev_attach_arg *uha = aux;
355 usb_device_descriptor_t *udd;
356 int i;
357 uint16_t vendor, product;
358
359 /*
360 * We just check if the vendor and product IDs have the magic numbers
361 * we expect.
362 */
363 if ((udd = usbd_get_device_descriptor(uha->parent->sc_udev)) != NULL) {
364 vendor = UGETW(udd->idVendor);
365 product = UGETW(udd->idProduct);
366 for (i = 0; i < PBMS_NUM_DEVICES; i++) {
367 if (vendor == pbms_devices[i].vendor &&
368 product == pbms_devices[i].product)
369 return UMATCH_IFACECLASS;
370 }
371 }
372 return UMATCH_NONE;
373 }
374
375
376 /* Attach the device. */
377
378 void
379 pbms_attach(struct device *parent, struct device *self, void *aux)
380 {
381 struct wsmousedev_attach_args a;
382 struct uhidev_attach_arg *uha = aux;
383 struct pbms_dev *pd;
384 struct pbms_softc *sc = (struct pbms_softc *)self;
385 usb_device_descriptor_t *udd;
386 int i;
387 uint16_t vendor, product;
388
389 sc->sc_hdev.sc_intr = pbms_intr;
390 sc->sc_hdev.sc_parent = uha->parent;
391 sc->sc_hdev.sc_report_id = uha->reportid;
392
393 /* Fill in device-specific parameters. */
394 if ((udd = usbd_get_device_descriptor(uha->parent->sc_udev)) != NULL) {
395 product = UGETW(udd->idProduct);
396 vendor = UGETW(udd->idVendor);
397 for (i = 0; i < PBMS_NUM_DEVICES; i++) {
398 pd = &pbms_devices[i];
399 if (product == pd->product && vendor == pd->vendor) {
400 printf(": %s\n", pd->descr);
401 sc->sc_noise = pd->noise;
402 sc->sc_theshold = pd->threshold;
403 sc->sc_x_factor = pd->x_factor;
404 sc->sc_x_sensors = pd->x_sensors;
405 sc->sc_y_factor = pd->y_factor;
406 sc->sc_y_sensors = pd->y_sensors;
407 break;
408 }
409 }
410 }
411 KASSERT(0 <= sc->sc_x_sensors && sc->sc_x_sensors <= PBMS_X_SENSORS);
412 KASSERT(0 <= sc->sc_y_sensors && sc->sc_y_sensors <= PBMS_Y_SENSORS);
413
414 sc->sc_status = 0;
415
416 a.accessops = &pbms_accessops;
417 a.accesscookie = sc;
418
419 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
420
421 USB_ATTACH_SUCCESS_RETURN;
422 }
423
424
425 /* Detach the device. */
426
427 int
428 pbms_detach(struct device *self, int flags)
429 {
430 struct pbms_softc *sc = (struct pbms_softc *)self;
431 int ret;
432
433 /* The wsmouse driver does all the work. */
434 ret = 0;
435 if (sc->sc_wsmousedev != NULL)
436 ret = config_detach(sc->sc_wsmousedev, flags);
437
438 return ret;
439 }
440
441
442 /* Activate the device. */
443
444 int
445 pbms_activate(device_ptr_t self, enum devact act)
446 {
447 struct pbms_softc *sc = (struct pbms_softc *)self;
448 int ret;
449
450 if (act == DVACT_DEACTIVATE) {
451 ret = 0;
452 if (sc->sc_wsmousedev != NULL)
453 ret = config_deactivate(sc->sc_wsmousedev);
454 sc->sc_status |= PBMS_DYING;
455 return ret;
456 }
457 return EOPNOTSUPP;
458 }
459
460
461 /* Enable the device. */
462
463 static int
464 pbms_enable(void *v)
465 {
466 struct pbms_softc *sc = v;
467
468 /* Check that we are not detaching or already enabled. */
469 if (sc->sc_status & PBMS_DYING)
470 return EIO;
471 if (sc->sc_status & PBMS_ENABLED)
472 return EBUSY;
473
474 sc->sc_status |= PBMS_ENABLED;
475 sc->sc_status &= ~PBMS_VALID;
476 sc->sc_buttons = 0;
477 memset(sc->sc_sample, 0, sizeof(sc->sc_sample));
478
479 return uhidev_open(&sc->sc_hdev);
480 }
481
482
483 /* Disable the device. */
484
485 static void
486 pbms_disable(void *v)
487 {
488 struct pbms_softc *sc = v;
489
490 if (!(sc->sc_status & PBMS_ENABLED))
491 return;
492
493 sc->sc_status &= ~PBMS_ENABLED;
494 uhidev_close(&sc->sc_hdev);
495 }
496
497
498 /* XXX ioctl not implemented. */
499
500 static int
501 pbms_ioctl(void *v, unsigned long cmd, caddr_t data, int flag, usb_proc_ptr p)
502 {
503 return EPASSTHROUGH;
504 }
505
506
507 /*
508 * Interrupts & pointer movement.
509 */
510
511
512 /* Handle interrupts. */
513
514 void
515 pbms_intr(struct uhidev *addr, void *ibuf, unsigned int len)
516 {
517 struct pbms_softc *sc = (struct pbms_softc *)addr;
518 signed char *data;
519 int dx, dy, dz, i, s;
520 uint32_t buttons;
521
522 /* Ignore incomplete data packets. */
523 if (len != PBMS_DATA_LEN)
524 return;
525 data = ibuf;
526
527 /* The last byte is 1 if the button is pressed and 0 otherwise. */
528 buttons = !!data[PBMS_DATA_LEN - 1];
529
530 /* Everything below assumes that the sample is reordered. */
531 reorder_sample(sc->sc_sample, data);
532
533 /* Is this the first sample? */
534 if (!(sc->sc_status & PBMS_VALID)) {
535 sc->sc_status |= PBMS_VALID;
536 sc->sc_x = sc->sc_y = -1;
537 sc->sc_x_raw = sc->sc_y_raw = -1;
538 memcpy(sc->sc_prev, sc->sc_sample, sizeof(sc->sc_prev));
539 memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
540 return;
541 }
542 /* Accumulate the sensor change while keeping it nonnegative. */
543 for (i = 0; i < PBMS_SENSORS; i++) {
544 sc->sc_acc[i] += sc->sc_sample[i] - sc->sc_prev[i];
545 if (sc->sc_acc[i] < 0)
546 sc->sc_acc[i] = 0;
547 }
548 memcpy(sc->sc_prev, sc->sc_sample, sizeof(sc->sc_prev));
549
550 /* Compute change. */
551 dx = dy = dz = 0;
552 if (!compute_delta(sc, &dx, &dy, &dz, &buttons))
553 return;
554
555 /* Report to wsmouse. */
556 if ((dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) &&
557 sc->sc_wsmousedev != NULL) {
558 s = spltty();
559 wsmouse_input(sc->sc_wsmousedev, buttons, dx, -dy, dz,
560 WSMOUSE_INPUT_DELTA);
561 splx(s);
562 }
563 sc->sc_buttons = buttons;
564 }
565
566
567 /*
568 * Reorder the sensor values so that all the X-sensors are before the
569 * Y-sensors in the natural order. Note that this might have to be
570 * rewritten if PBMS_X_SENSORS or PBMS_Y_SENSORS change.
571 */
572
573 static void
574 reorder_sample(signed char *to, signed char *from)
575 {
576 int i;
577
578 for (i = 0; i < 8; i++) {
579 /* X-sensors. */
580 to[i] = from[5 * i + 2];
581 to[i + 8] = from[5 * i + 4];
582 to[i + 16] = from[5 * i + 42];
583 #if 0
584 /*
585 * XXX This seems to introduce random ventical jumps, so
586 * we ignore these sensors until we figure out their meaning.
587 */
588 if (i < 2)
589 to[i + 24] = from[5 * i + 44];
590 #endif /* 0 */
591 /* Y-sensors. */
592 to[i + 26] = from[5 * i + 1];
593 to[i + 34] = from[5 * i + 3];
594 }
595 }
596
597
598 /*
599 * Compute the change in x, y and z direction, update the button state
600 * (to simulate more than one button, scrolling etc.), and update the
601 * history. Note that dx, dy, dz and buttons are modified only if
602 * corresponding pressure is detected and should thus be initialised
603 * before the call. Return 0 on error.
604 */
605
606 /* XXX Could we report something useful in dz? */
607
608 static int
609 compute_delta(struct pbms_softc *sc, int *dx, int *dy, int *dz,
610 uint32_t * buttons)
611 {
612 int x_det, y_det, x_raw, y_raw, x_fingers, y_fingers, fingers, x, y;
613
614 x_det = detect_pos(sc->sc_acc, sc->sc_x_sensors, sc->sc_theshold,
615 sc->sc_x_factor, &x_raw, &x_fingers);
616 y_det = detect_pos(sc->sc_acc + PBMS_X_SENSORS, sc->sc_y_sensors,
617 sc->sc_theshold, sc->sc_y_factor,
618 &y_raw, &y_fingers);
619 fingers = max(x_fingers, y_fingers);
620
621 /* Check the number of fingers and if we have detected a position. */
622 if (fingers > 1) {
623 /* More than one finger detected, resetting. */
624 memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
625 sc->sc_x_raw = sc->sc_y_raw = sc->sc_x = sc->sc_y = -1;
626 return 0;
627 } else if (x_det == 0 && y_det == 0) {
628 /* No position detected, resetting. */
629 memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
630 sc->sc_x_raw = sc->sc_y_raw = sc->sc_x = sc->sc_y = -1;
631 } else if (x_det > 0 && y_det > 0) {
632 /* Smooth position. */
633 if (sc->sc_x_raw >= 0) {
634 sc->sc_x_raw = (3 * sc->sc_x_raw + x_raw) / 4;
635 sc->sc_y_raw = (3 * sc->sc_y_raw + y_raw) / 4;
636 /*
637 * Compute virtual position and change if we already
638 * have a decent position.
639 */
640 if (sc->sc_x >= 0) {
641 x = smooth_pos(sc->sc_x, sc->sc_x_raw,
642 sc->sc_noise);
643 y = smooth_pos(sc->sc_y, sc->sc_y_raw,
644 sc->sc_noise);
645 *dx = x - sc->sc_x;
646 *dy = y - sc->sc_y;
647 sc->sc_x = x;
648 sc->sc_y = y;
649 } else {
650 /* Initialise virtual position. */
651 sc->sc_x = sc->sc_x_raw;
652 sc->sc_y = sc->sc_y_raw;
653 }
654 } else {
655 /* Initialise raw position. */
656 sc->sc_x_raw = x_raw;
657 sc->sc_y_raw = y_raw;
658 }
659 }
660 return 1;
661 }
662
663
664 /*
665 * Compute the new smoothed position from the previous smoothed position
666 * and the raw position.
667 */
668
669 static int
670 smooth_pos(int pos_old, int pos_raw, int noise)
671 {
672 int ad, delta;
673
674 delta = pos_raw - pos_old;
675 ad = abs(delta);
676
677 /* Too small changes are ignored. */
678 if (ad < noise / 2)
679 delta = 0;
680 /* A bit larger changes are smoothed. */
681 else if (ad < noise)
682 delta /= 4;
683 else if (ad < 2 * noise)
684 delta /= 2;
685
686 return pos_old + delta;
687 }
688
689
690 /*
691 * Detect the position of the finger. Returns the total pressure.
692 * The position is returned in pos_ret and the number of fingers
693 * is returned in fingers_ret. The position returned in pos_ret
694 * is in [0, (n_sensors - 1) * factor - 1].
695 */
696
697 static int
698 detect_pos(int *sensors, int n_sensors, int threshold, int fact,
699 int *pos_ret, int *fingers_ret)
700 {
701 int i, w, s;
702
703 /*
704 * Compute the number of fingers, total pressure, and weighted
705 * position of the fingers.
706 */
707 *fingers_ret = 0;
708 w = s = 0;
709 for (i = 0; i < n_sensors; i++) {
710 if (sensors[i] >= threshold) {
711 if (i == 0 || sensors[i - 1] < threshold)
712 *fingers_ret += 1;
713 s += sensors[i];
714 w += sensors[i] * i;
715 }
716 }
717
718 if (s > 0)
719 *pos_ret = w * fact / s;
720
721 return s;
722 }
723