diff options
author | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-02-19 22:15:02 +0000 |
---|---|---|
committer | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-02-19 22:15:02 +0000 |
commit | 44eba075165f932f6b960e5fc069be22b0c9b929 (patch) | |
tree | 674afb2eda2b4ea3f62163232297a9cd898a90d5 /firmware/drivers/touchscreen.c | |
parent | 1cb3ff0ab06cc71eb82b82e36eb22ca1d5eaa61f (diff) | |
download | rockbox-44eba075165f932f6b960e5fc069be22b0c9b929.tar.gz rockbox-44eba075165f932f6b960e5fc069be22b0c9b929.zip |
Add generic touchscreen driver which allows calibration (apps/ layer will follow later).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@20055 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/drivers/touchscreen.c')
-rwxr-xr-x | firmware/drivers/touchscreen.c | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/firmware/drivers/touchscreen.c b/firmware/drivers/touchscreen.c new file mode 100755 index 0000000000..f7b1b09b92 --- /dev/null +++ b/firmware/drivers/touchscreen.c | |||
@@ -0,0 +1,168 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2008 by Maurus Cuelenaere | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
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" | ||
28 | |||
29 | static enum touchscreen_mode current_mode = TOUCHSCREEN_POINT; | ||
30 | static const int touchscreen_buttons[3][3] = | ||
31 | { | ||
32 | {BUTTON_TOPLEFT, BUTTON_TOPMIDDLE, BUTTON_TOPRIGHT}, | ||
33 | {BUTTON_MIDLEFT, BUTTON_CENTER, BUTTON_MIDRIGHT}, | ||
34 | {BUTTON_BOTTOMLEFT, BUTTON_BOTTOMMIDDLE, BUTTON_BOTTOMRIGHT} | ||
35 | }; | ||
36 | |||
37 | /* Based on ftp://ftp.embedded.com/pub/2002/06vidales/calibrate.c | ||
38 | * | ||
39 | * Copyright (c) 2001, Carlos E. Vidales. All rights reserved. | ||
40 | * | ||
41 | * This sample program was written and put in the public domain | ||
42 | * by Carlos E. Vidales. The program is provided "as is" | ||
43 | * without warranty of any kind, either expressed or implied. | ||
44 | * If you choose to use the program within your own products | ||
45 | * you do so at your own risk, and assume the responsibility | ||
46 | * for servicing, repairing or correcting the program should | ||
47 | * it prove defective in any manner. | ||
48 | * You may copy and distribute the program's source code in any | ||
49 | * medium, provided that you also include in each copy an | ||
50 | * appropriate copyright notice and disclaimer of warranty. | ||
51 | * You may also modify this program and distribute copies of | ||
52 | * it provided that you include prominent notices stating | ||
53 | * that you changed the file(s) and the date of any change, | ||
54 | * and that you do not charge any royalties or licenses for | ||
55 | * its use. | ||
56 | */ | ||
57 | struct touchscreen_parameter | ||
58 | { | ||
59 | long A; | ||
60 | long B; | ||
61 | long C; | ||
62 | long D; | ||
63 | long E; | ||
64 | long F; | ||
65 | long divider; | ||
66 | }; | ||
67 | |||
68 | #ifndef DEFAULT_TOUCHSCREEN_CALIBRATION | ||
69 | #define DEFAULT_TOUCHSCREEN_CALIBRATION {.A=1, .B=0, .C=0, \ | ||
70 | .D=0, .E=1, .F=0, \ | ||
71 | .divider=1} | ||
72 | #endif | ||
73 | |||
74 | static struct touchscreen_parameter calibration_parameters | ||
75 | = DEFAULT_TOUCHSCREEN_CALIBRATION; | ||
76 | static const struct touchscreen_parameter default_parameters | ||
77 | = DEFAULT_TOUCHSCREEN_CALIBRATION; | ||
78 | |||
79 | void touchscreen_disable_mapping(void) | ||
80 | { | ||
81 | calibration_parameters.A = 1; | ||
82 | calibration_parameters.B = 0; | ||
83 | calibration_parameters.C = 0; | ||
84 | calibration_parameters.D = 0; | ||
85 | calibration_parameters.E = 1; | ||
86 | calibration_parameters.F = 0; | ||
87 | calibration_parameters.divider = 1; | ||
88 | } | ||
89 | |||
90 | void touchscreen_reset_mapping(void) | ||
91 | { | ||
92 | memcpy(&calibration_parameters, &default_parameters, | ||
93 | sizeof(struct touchscreen_parameter)); | ||
94 | } | ||
95 | |||
96 | int touchscreen_calibrate(struct touchscreen_calibration *cal) | ||
97 | { | ||
98 | calibration_parameters.divider = ((cal->x[0] - cal->x[2]) * (cal->y[1] - cal->y[2])) - | ||
99 | ((cal->x[1] - cal->x[2]) * (cal->y[0] - cal->y[2])) ; | ||
100 | |||
101 | if(calibration_parameters.divider == 0) | ||
102 | return -1; | ||
103 | |||
104 | calibration_parameters.A = ((cal->xfb[0] - cal->xfb[2]) * (cal->y[1] - cal->y[2])) - | ||
105 | ((cal->xfb[1] - cal->xfb[2]) * (cal->y[0] - cal->y[2])) ; | ||
106 | |||
107 | calibration_parameters.B = ((cal->x[0] - cal->x[2]) * (cal->xfb[1] - cal->xfb[2])) - | ||
108 | ((cal->xfb[0] - cal->xfb[2]) * (cal->x[1] - cal->x[2])) ; | ||
109 | |||
110 | calibration_parameters.C = (cal->x[2] * cal->xfb[1] - cal->x[1] * cal->xfb[2]) * cal->y[0] + | ||
111 | (cal->x[0] * cal->xfb[2] - cal->x[2] * cal->xfb[0]) * cal->y[1] + | ||
112 | (cal->x[1] * cal->xfb[0] - cal->x[0] * cal->xfb[1]) * cal->y[2] ; | ||
113 | |||
114 | calibration_parameters.D = ((cal->yfb[0] - cal->yfb[2]) * (cal->y[1] - cal->y[2])) - | ||
115 | ((cal->yfb[1] - cal->yfb[2]) * (cal->y[0] - cal->y[2])) ; | ||
116 | |||
117 | calibration_parameters.E = ((cal->x[0] - cal->x[2]) * (cal->yfb[1] - cal->yfb[2])) - | ||
118 | ((cal->yfb[0] - cal->yfb[2]) * (cal->x[1] - cal->x[2])) ; | ||
119 | |||
120 | calibration_parameters.F = (cal->x[2] * cal->yfb[1] - cal->x[1] * cal->yfb[2]) * cal->y[0] + | ||
121 | (cal->x[0] * cal->yfb[2] - cal->x[2] * cal->yfb[0]) * cal->y[1] + | ||
122 | (cal->x[1] * cal->yfb[0] - cal->x[0] * cal->yfb[1]) * cal->y[2] ; | ||
123 | |||
124 | logf("A: %lX B: %lX C: %lX", calibration_parameters.A, | ||
125 | calibration_parameters.B, calibration_parameters.C); | ||
126 | logf("D: %lX E: %lX F: %lX", calibration_parameters.D, | ||
127 | calibration_parameters.E, calibration_parameters.F); | ||
128 | logf("divider: %lX", calibration_parameters.divider); | ||
129 | |||
130 | return 0; | ||
131 | } | ||
132 | |||
133 | static void map_pixels(int *x, int *y) | ||
134 | { | ||
135 | int _x = *x, _y = *y; | ||
136 | |||
137 | *x = (calibration_parameters.A*_x + calibration_parameters.B*_y + | ||
138 | calibration_parameters.C) / calibration_parameters.divider; | ||
139 | *y = (calibration_parameters.D*_x + calibration_parameters.E*_y + | ||
140 | calibration_parameters.F) / calibration_parameters.divider; | ||
141 | } | ||
142 | |||
143 | int touchscreen_to_pixels(int x, int y, int *data) | ||
144 | { | ||
145 | x &= 0xFFFF; | ||
146 | y &= 0xFFFF; | ||
147 | |||
148 | map_pixels(&x, &y); | ||
149 | |||
150 | if(current_mode == TOUCHSCREEN_BUTTON) | ||
151 | return touchscreen_buttons[y / (LCD_HEIGHT/3)] | ||
152 | [x / (LCD_WIDTH/3) ]; | ||
153 | else | ||
154 | { | ||
155 | *data = (x << 16 | y); | ||
156 | return BUTTON_TOUCHSCREEN; | ||
157 | } | ||
158 | } | ||
159 | |||
160 | void touchscreen_set_mode(enum touchscreen_mode mode) | ||
161 | { | ||
162 | current_mode = mode; | ||
163 | } | ||
164 | |||
165 | enum touchscreen_mode touchscreen_get_mode(void) | ||
166 | { | ||
167 | return current_mode; | ||
168 | } | ||