diff options
author | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-06-16 17:04:47 +0000 |
---|---|---|
committer | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-06-16 17:04:47 +0000 |
commit | b8435f54464fd50c145d321b118d773ea05ef6a2 (patch) | |
tree | a514bbe44e41c454dfca56184376209787c40268 /firmware/drivers/touchscreen.c | |
parent | b1a60934c5d178842ec3bec83a3e5286410cab7d (diff) | |
download | rockbox-b8435f54464fd50c145d321b118d773ea05ef6a2.tar.gz rockbox-b8435f54464fd50c145d321b118d773ea05ef6a2.zip |
Touchscreen targets: add calibration screen + rewrite calibration driver (FS#10295)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21312 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/drivers/touchscreen.c')
-rwxr-xr-x | firmware/drivers/touchscreen.c | 124 |
1 files changed, 49 insertions, 75 deletions
diff --git a/firmware/drivers/touchscreen.c b/firmware/drivers/touchscreen.c index f7b1b09b92..002acf1236 100755 --- a/firmware/drivers/touchscreen.c +++ b/firmware/drivers/touchscreen.c | |||
@@ -34,119 +34,93 @@ static const int touchscreen_buttons[3][3] = | |||
34 | {BUTTON_BOTTOMLEFT, BUTTON_BOTTOMMIDDLE, BUTTON_BOTTOMRIGHT} | 34 | {BUTTON_BOTTOMLEFT, BUTTON_BOTTOMMIDDLE, BUTTON_BOTTOMRIGHT} |
35 | }; | 35 | }; |
36 | 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 | 37 | #ifndef DEFAULT_TOUCHSCREEN_CALIBRATION |
69 | #define DEFAULT_TOUCHSCREEN_CALIBRATION {.A=1, .B=0, .C=0, \ | 38 | #define DEFAULT_TOUCHSCREEN_CALIBRATION { .A=1, .B=0, .C=0, \ |
70 | .D=0, .E=1, .F=0, \ | 39 | .D=0, .E=1, .F=0, \ |
71 | .divider=1} | 40 | .divider=1 } |
72 | #endif | 41 | #endif |
73 | 42 | ||
74 | static struct touchscreen_parameter calibration_parameters | 43 | struct touchscreen_parameter calibration_parameters |
75 | = DEFAULT_TOUCHSCREEN_CALIBRATION; | 44 | = DEFAULT_TOUCHSCREEN_CALIBRATION; |
76 | static const struct touchscreen_parameter default_parameters | 45 | const struct touchscreen_parameter default_calibration_parameters |
77 | = DEFAULT_TOUCHSCREEN_CALIBRATION; | 46 | = DEFAULT_TOUCHSCREEN_CALIBRATION; |
78 | 47 | ||
79 | void touchscreen_disable_mapping(void) | 48 | void touchscreen_disable_mapping(void) |
80 | { | 49 | { |
81 | calibration_parameters.A = 1; | 50 | #define C(x) calibration_parameters.x |
82 | calibration_parameters.B = 0; | 51 | C(A) = C(E) = 1; |
83 | calibration_parameters.C = 0; | 52 | C(B) = C(C) = C(D) = C(F) = 0; |
84 | calibration_parameters.D = 0; | 53 | C(divider) = 1; |
85 | calibration_parameters.E = 1; | 54 | #undef C |
86 | calibration_parameters.F = 0; | ||
87 | calibration_parameters.divider = 1; | ||
88 | } | 55 | } |
89 | 56 | ||
90 | void touchscreen_reset_mapping(void) | 57 | void touchscreen_reset_mapping(void) |
91 | { | 58 | { |
92 | memcpy(&calibration_parameters, &default_parameters, | 59 | memcpy(&calibration_parameters, &default_calibration_parameters, |
93 | sizeof(struct touchscreen_parameter)); | 60 | sizeof(struct touchscreen_parameter)); |
94 | } | 61 | } |
95 | 62 | ||
96 | int touchscreen_calibrate(struct touchscreen_calibration *cal) | 63 | int touchscreen_calibrate(struct touchscreen_calibration *cal) |
97 | { | 64 | { |
98 | calibration_parameters.divider = ((cal->x[0] - cal->x[2]) * (cal->y[1] - cal->y[2])) - | 65 | #define C(x) calibration_parameters.x /* Calibration */ |
99 | ((cal->x[1] - cal->x[2]) * (cal->y[0] - cal->y[2])) ; | 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)); | ||
100 | 70 | ||
101 | if(calibration_parameters.divider == 0) | 71 | if(divider == 0) |
102 | return -1; | 72 | return -1; |
103 | 73 | else | |
104 | calibration_parameters.A = ((cal->xfb[0] - cal->xfb[2]) * (cal->y[1] - cal->y[2])) - | 74 | C(divider) = divider; |
105 | ((cal->xfb[1] - cal->xfb[2]) * (cal->y[0] - cal->y[2])) ; | ||
106 | 75 | ||
107 | calibration_parameters.B = ((cal->x[0] - cal->x[2]) * (cal->xfb[1] - cal->xfb[2])) - | 76 | C(A) = (D(x,0) - D(x,2)) * (S(y,1) - S(y,2)) - |
108 | ((cal->xfb[0] - cal->xfb[2]) * (cal->x[1] - cal->x[2])) ; | 77 | (D(x,1) - D(x,2)) * (S(y,0) - S(y,2)); |
109 | 78 | ||
110 | calibration_parameters.C = (cal->x[2] * cal->xfb[1] - cal->x[1] * cal->xfb[2]) * cal->y[0] + | 79 | C(B) = (S(x,0) - S(x,2)) * (D(x,1) - D(x,2)) - |
111 | (cal->x[0] * cal->xfb[2] - cal->x[2] * cal->xfb[0]) * cal->y[1] + | 80 | (D(x,0) - D(x,2)) * (S(x,1) - S(x,2)); |
112 | (cal->x[1] * cal->xfb[0] - cal->x[0] * cal->xfb[1]) * cal->y[2] ; | ||
113 | 81 | ||
114 | calibration_parameters.D = ((cal->yfb[0] - cal->yfb[2]) * (cal->y[1] - cal->y[2])) - | 82 | C(C) = S(y,0) * (S(x,2) * D(x,1) - S(x,1) * D(x,2)) + |
115 | ((cal->yfb[1] - cal->yfb[2]) * (cal->y[0] - cal->y[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)); | ||
116 | 85 | ||
117 | calibration_parameters.E = ((cal->x[0] - cal->x[2]) * (cal->yfb[1] - cal->yfb[2])) - | 86 | C(D) = (D(y,0) - D(y,2)) * (S(y,1) - S(y,2)) - |
118 | ((cal->yfb[0] - cal->yfb[2]) * (cal->x[1] - cal->x[2])) ; | 87 | (D(y,1) - D(y,2)) * (S(y,0) - S(y,2)); |
119 | 88 | ||
120 | calibration_parameters.F = (cal->x[2] * cal->yfb[1] - cal->x[1] * cal->yfb[2]) * cal->y[0] + | 89 | C(E) = (S(x,0) - S(x,2)) * (D(y,1) - D(y,2)) - |
121 | (cal->x[0] * cal->yfb[2] - cal->x[2] * cal->yfb[0]) * cal->y[1] + | 90 | (D(y,0) - D(y,2)) * (S(x,1) - S(x,2)); |
122 | (cal->x[1] * cal->yfb[0] - cal->x[0] * cal->yfb[1]) * cal->y[2] ; | 91 | |
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)); | ||
95 | |||
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)); | ||
123 | 99 | ||
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; | 100 | return 0; |
101 | #undef C | ||
102 | #undef S | ||
103 | #undef D | ||
131 | } | 104 | } |
132 | 105 | ||
133 | static void map_pixels(int *x, int *y) | 106 | static void map_pixels(int *x, int *y) |
134 | { | 107 | { |
108 | #define C(x) calibration_parameters.x | ||
135 | int _x = *x, _y = *y; | 109 | int _x = *x, _y = *y; |
136 | 110 | ||
137 | *x = (calibration_parameters.A*_x + calibration_parameters.B*_y + | 111 | *x = (C(A) * _x + C(B) * _y + C(C)) / C(divider); |
138 | calibration_parameters.C) / calibration_parameters.divider; | 112 | *y = (C(D) * _x + C(E) * _y + C(F)) / C(divider); |
139 | *y = (calibration_parameters.D*_x + calibration_parameters.E*_y + | 113 | #undef C |
140 | calibration_parameters.F) / calibration_parameters.divider; | ||
141 | } | 114 | } |
142 | 115 | ||
116 | /* TODO: add jitter (and others) filter */ | ||
143 | int touchscreen_to_pixels(int x, int y, int *data) | 117 | int touchscreen_to_pixels(int x, int y, int *data) |
144 | { | 118 | { |
145 | x &= 0xFFFF; | 119 | x &= 0xFFFF; |
146 | y &= 0xFFFF; | 120 | y &= 0xFFFF; |
147 | 121 | ||
148 | map_pixels(&x, &y); | 122 | map_pixels(&x, &y); |
149 | 123 | ||
150 | if(current_mode == TOUCHSCREEN_BUTTON) | 124 | if(current_mode == TOUCHSCREEN_BUTTON) |
151 | return touchscreen_buttons[y / (LCD_HEIGHT/3)] | 125 | return touchscreen_buttons[y / (LCD_HEIGHT/3)] |
152 | [x / (LCD_WIDTH/3) ]; | 126 | [x / (LCD_WIDTH/3) ]; |