Rockbox supports not only 1bpp BMPs
[kugel-rb.git] / firmware / drivers / touchscreen.c
blobf7b1b09b92d43b985ba187bd042ab922fd00bf21
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 /* Based on ftp://ftp.embedded.com/pub/2002/06vidales/calibrate.c
39 * Copyright (c) 2001, Carlos E. Vidales. All rights reserved.
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.
57 struct touchscreen_parameter
59 long A;
60 long B;
61 long C;
62 long D;
63 long E;
64 long F;
65 long divider;
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
74 static struct touchscreen_parameter calibration_parameters
75 = DEFAULT_TOUCHSCREEN_CALIBRATION;
76 static const struct touchscreen_parameter default_parameters
77 = DEFAULT_TOUCHSCREEN_CALIBRATION;
79 void touchscreen_disable_mapping(void)
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;
90 void touchscreen_reset_mapping(void)
92 memcpy(&calibration_parameters, &default_parameters,
93 sizeof(struct touchscreen_parameter));
96 int touchscreen_calibrate(struct touchscreen_calibration *cal)
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])) ;
101 if(calibration_parameters.divider == 0)
102 return -1;
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])) ;
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])) ;
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] ;
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])) ;
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])) ;
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] ;
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);
130 return 0;
133 static void map_pixels(int *x, int *y)
135 int _x = *x, _y = *y;
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;
143 int touchscreen_to_pixels(int x, int y, int *data)
145 x &= 0xFFFF;
146 y &= 0xFFFF;
148 map_pixels(&x, &y);
150 if(current_mode == TOUCHSCREEN_BUTTON)
151 return touchscreen_buttons[y / (LCD_HEIGHT/3)]
152 [x / (LCD_WIDTH/3) ];
153 else
155 *data = (x << 16 | y);
156 return BUTTON_TOUCHSCREEN;
160 void touchscreen_set_mode(enum touchscreen_mode mode)
162 current_mode = mode;
165 enum touchscreen_mode touchscreen_get_mode(void)
167 return current_mode;