Do some #ifdef'ing to make the Player happy.
[kugel-rb.git] / firmware / drivers / touchscreen.c
blob002acf12362397ab1e8c505e2b28f304d2ea5fa1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Maurus Cuelenaere
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include "button.h"
24 #include "button-target.h"
25 #include "touchscreen.h"
26 #include "string.h"
27 #include "logf.h"
29 static enum touchscreen_mode current_mode = TOUCHSCREEN_POINT;
30 static const int touchscreen_buttons[3][3] =
32 {BUTTON_TOPLEFT, BUTTON_TOPMIDDLE, BUTTON_TOPRIGHT},
33 {BUTTON_MIDLEFT, BUTTON_CENTER, BUTTON_MIDRIGHT},
34 {BUTTON_BOTTOMLEFT, BUTTON_BOTTOMMIDDLE, BUTTON_BOTTOMRIGHT}
37 #ifndef DEFAULT_TOUCHSCREEN_CALIBRATION
38 #define DEFAULT_TOUCHSCREEN_CALIBRATION { .A=1, .B=0, .C=0, \
39 .D=0, .E=1, .F=0, \
40 .divider=1 }
41 #endif
43 struct touchscreen_parameter calibration_parameters
44 = DEFAULT_TOUCHSCREEN_CALIBRATION;
45 const struct touchscreen_parameter default_calibration_parameters
46 = DEFAULT_TOUCHSCREEN_CALIBRATION;
48 void touchscreen_disable_mapping(void)
50 #define C(x) calibration_parameters.x
51 C(A) = C(E) = 1;
52 C(B) = C(C) = C(D) = C(F) = 0;
53 C(divider) = 1;
54 #undef C
57 void touchscreen_reset_mapping(void)
59 memcpy(&calibration_parameters, &default_calibration_parameters,
60 sizeof(struct touchscreen_parameter));
63 int touchscreen_calibrate(struct touchscreen_calibration *cal)
65 #define C(x) calibration_parameters.x /* Calibration */
66 #define S(i,j) cal->i[j][0] /* Screen */
67 #define D(i,j) cal->i[j][1] /* Display */
68 long divider = (S(x,0) - S(x,2)) * (S(y,1) - S(y,2)) -
69 (S(x,1) - S(x,2)) * (S(y,0) - S(y,2));
71 if(divider == 0)
72 return -1;
73 else
74 C(divider) = divider;
76 C(A) = (D(x,0) - D(x,2)) * (S(y,1) - S(y,2)) -
77 (D(x,1) - D(x,2)) * (S(y,0) - S(y,2));
79 C(B) = (S(x,0) - S(x,2)) * (D(x,1) - D(x,2)) -
80 (D(x,0) - D(x,2)) * (S(x,1) - S(x,2));
82 C(C) = S(y,0) * (S(x,2) * D(x,1) - S(x,1) * D(x,2)) +
83 S(y,1) * (S(x,0) * D(x,2) - S(x,2) * D(x,0)) +
84 S(y,2) * (S(x,1) * D(x,0) - S(x,0) * D(x,1));
86 C(D) = (D(y,0) - D(y,2)) * (S(y,1) - S(y,2)) -
87 (D(y,1) - D(y,2)) * (S(y,0) - S(y,2));
89 C(E) = (S(x,0) - S(x,2)) * (D(y,1) - D(y,2)) -
90 (D(y,0) - D(y,2)) * (S(x,1) - S(x,2));
92 C(F) = S(y,0) * (S(x,2) * D(y,1) - S(x,1) * D(y,2)) +
93 S(y,1) * (S(x,0) * D(y,2) - S(x,2) * D(y,0)) +
94 S(y,2) * (S(x,1) * D(y,0) - S(x,0) * D(y,1));
96 logf("A: %lX B: %lX C: %lX", C(A), C(B), C(C));
97 logf("D: %lX E: %lX F: %lX", C(D), C(E), C(F));
98 logf("divider: %lX", C(divider));
100 return 0;
101 #undef C
102 #undef S
103 #undef D
106 static void map_pixels(int *x, int *y)
108 #define C(x) calibration_parameters.x
109 int _x = *x, _y = *y;
111 *x = (C(A) * _x + C(B) * _y + C(C)) / C(divider);
112 *y = (C(D) * _x + C(E) * _y + C(F)) / C(divider);
113 #undef C
116 /* TODO: add jitter (and others) filter */
117 int touchscreen_to_pixels(int x, int y, int *data)
119 x &= 0xFFFF;
120 y &= 0xFFFF;
122 map_pixels(&x, &y);
124 if(current_mode == TOUCHSCREEN_BUTTON)
125 return touchscreen_buttons[y / (LCD_HEIGHT/3)]
126 [x / (LCD_WIDTH/3) ];
127 else
129 *data = (x << 16 | y);
130 return BUTTON_TOUCHSCREEN;
134 void touchscreen_set_mode(enum touchscreen_mode mode)
136 current_mode = mode;
139 enum touchscreen_mode touchscreen_get_mode(void)
141 return current_mode;