Fix include problem
[kugel-rb.git] / firmware / drivers / touchscreen.c
blob9660e0cb9dd86f8ebdeddebd2881cbdb42940c19
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 /* Size of the 'dead zone' around each 3x3 button */
30 #define BUTTON_MARGIN_X (int)(LCD_WIDTH * 0.03)
31 #define BUTTON_MARGIN_Y (int)(LCD_HEIGHT * 0.03)
33 static enum touchscreen_mode current_mode = TOUCHSCREEN_POINT;
34 static const int touchscreen_buttons[3][3] =
36 {BUTTON_TOPLEFT, BUTTON_TOPMIDDLE, BUTTON_TOPRIGHT},
37 {BUTTON_MIDLEFT, BUTTON_CENTER, BUTTON_MIDRIGHT},
38 {BUTTON_BOTTOMLEFT, BUTTON_BOTTOMMIDDLE, BUTTON_BOTTOMRIGHT}
41 #ifndef DEFAULT_TOUCHSCREEN_CALIBRATION
42 #define DEFAULT_TOUCHSCREEN_CALIBRATION { .A=1, .B=0, .C=0, \
43 .D=0, .E=1, .F=0, \
44 .divider=1 }
45 #endif
47 struct touchscreen_parameter calibration_parameters
48 = DEFAULT_TOUCHSCREEN_CALIBRATION;
49 const struct touchscreen_parameter default_calibration_parameters
50 = DEFAULT_TOUCHSCREEN_CALIBRATION;
52 void touchscreen_disable_mapping(void)
54 #define C(x) calibration_parameters.x
55 C(A) = C(E) = 1;
56 C(B) = C(C) = C(D) = C(F) = 0;
57 C(divider) = 1;
58 #undef C
61 void touchscreen_reset_mapping(void)
63 memcpy(&calibration_parameters, &default_calibration_parameters,
64 sizeof(struct touchscreen_parameter));
67 int touchscreen_calibrate(struct touchscreen_calibration *cal)
69 #define C(x) calibration_parameters.x /* Calibration */
70 #define S(i,j) cal->i[j][0] /* Screen */
71 #define D(i,j) cal->i[j][1] /* Display */
72 long divider = (S(x,0) - S(x,2)) * (S(y,1) - S(y,2)) -
73 (S(x,1) - S(x,2)) * (S(y,0) - S(y,2));
75 if(divider == 0)
76 return -1;
77 else
78 C(divider) = divider;
80 C(A) = (D(x,0) - D(x,2)) * (S(y,1) - S(y,2)) -
81 (D(x,1) - D(x,2)) * (S(y,0) - S(y,2));
83 C(B) = (S(x,0) - S(x,2)) * (D(x,1) - D(x,2)) -
84 (D(x,0) - D(x,2)) * (S(x,1) - S(x,2));
86 C(C) = S(y,0) * (S(x,2) * D(x,1) - S(x,1) * D(x,2)) +
87 S(y,1) * (S(x,0) * D(x,2) - S(x,2) * D(x,0)) +
88 S(y,2) * (S(x,1) * D(x,0) - S(x,0) * D(x,1));
90 C(D) = (D(y,0) - D(y,2)) * (S(y,1) - S(y,2)) -
91 (D(y,1) - D(y,2)) * (S(y,0) - S(y,2));
93 C(E) = (S(x,0) - S(x,2)) * (D(y,1) - D(y,2)) -
94 (D(y,0) - D(y,2)) * (S(x,1) - S(x,2));
96 C(F) = S(y,0) * (S(x,2) * D(y,1) - S(x,1) * D(y,2)) +
97 S(y,1) * (S(x,0) * D(y,2) - S(x,2) * D(y,0)) +
98 S(y,2) * (S(x,1) * D(y,0) - S(x,0) * D(y,1));
100 logf("A: %lX B: %lX C: %lX", C(A), C(B), C(C));
101 logf("D: %lX E: %lX F: %lX", C(D), C(E), C(F));
102 logf("divider: %lX", C(divider));
104 return 0;
105 #undef C
106 #undef S
107 #undef D
110 static void map_pixels(int *x, int *y)
112 #define C(x) calibration_parameters.x
113 int _x = *x, _y = *y;
115 *x = (C(A) * _x + C(B) * _y + C(C)) / C(divider);
116 *y = (C(D) * _x + C(E) * _y + C(F)) / C(divider);
117 #undef C
120 /* TODO: add jitter (and others) filter */
121 int touchscreen_to_pixels(int x, int y, int *data)
123 x &= 0xFFFF;
124 y &= 0xFFFF;
126 map_pixels(&x, &y);
128 if (current_mode == TOUCHSCREEN_BUTTON)
130 int column = 0, row = 0;
132 if (x < LCD_WIDTH/3 - BUTTON_MARGIN_X)
133 column = 0;
134 else if (x > LCD_WIDTH/3 + BUTTON_MARGIN_X &&
135 x < 2*LCD_WIDTH/3 - BUTTON_MARGIN_X)
136 column = 1;
137 else if (x > 2*LCD_WIDTH/3 + BUTTON_MARGIN_X)
138 column = 2;
139 else
140 return BUTTON_NONE;
142 if (y < LCD_HEIGHT/3 - BUTTON_MARGIN_Y)
143 row = 0;
144 else if (y > LCD_HEIGHT/3 + BUTTON_MARGIN_Y &&
145 y < 2*LCD_HEIGHT/3 - BUTTON_MARGIN_Y)
146 row = 1;
147 else if (y > 2*LCD_HEIGHT/3 + BUTTON_MARGIN_Y)
148 row = 2;
149 else
150 return BUTTON_NONE;
152 return touchscreen_buttons[row][column];
154 else
156 *data = (x << 16 | y);
157 return BUTTON_TOUCHSCREEN;
161 void touchscreen_set_mode(enum touchscreen_mode mode)
163 current_mode = mode;
166 enum touchscreen_mode touchscreen_get_mode(void)
168 return current_mode;