1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
24 #include "button-target.h"
25 #include "touchscreen.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, \
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
56 C(B
) = C(C
) = C(D
) = C(F
) = 0;
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));
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
));
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
);
120 /* TODO: add jitter (and others) filter */
121 int touchscreen_to_pixels(int x
, int y
, int *data
)
128 if (current_mode
== TOUCHSCREEN_BUTTON
)
130 int column
= 0, row
= 0;
132 if (x
< LCD_WIDTH
/3 - BUTTON_MARGIN_X
)
134 else if (x
> LCD_WIDTH
/3 + BUTTON_MARGIN_X
&&
135 x
< 2*LCD_WIDTH
/3 - BUTTON_MARGIN_X
)
137 else if (x
> 2*LCD_WIDTH
/3 + BUTTON_MARGIN_X
)
142 if (y
< LCD_HEIGHT
/3 - BUTTON_MARGIN_Y
)
144 else if (y
> LCD_HEIGHT
/3 + BUTTON_MARGIN_Y
&&
145 y
< 2*LCD_HEIGHT
/3 - BUTTON_MARGIN_Y
)
147 else if (y
> 2*LCD_HEIGHT
/3 + BUTTON_MARGIN_Y
)
152 return touchscreen_buttons
[row
][column
];
156 *data
= (x
<< 16 | y
);
157 return BUTTON_TOUCHSCREEN
;
161 void touchscreen_set_mode(enum touchscreen_mode mode
)
166 enum touchscreen_mode
touchscreen_get_mode(void)