M:Robe 500: Rearrage TSC2100 reads to make touchscreen more reliable, add hack to...
[kugel-rb.git] / firmware / target / arm / tms320dm320 / mrobe-500 / button-mr500.c
blob09ff6cbd786fc4a1d70f14174fcf449008dc2332
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007, 2009 by Karl Kurbjun
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 /* this file also handles the touch screen driver interface */
24 #include "config.h"
25 #include "cpu.h"
26 #include "system.h"
27 #include "button.h"
28 #include "kernel.h"
29 #include "backlight.h"
30 #include "backlight-target.h"
31 #include "lcd-remote-target.h"
32 #include "uart-target.h"
33 #include "tsc2100.h"
34 #include "string.h"
35 #include "touchscreen.h"
37 static bool touch_available = false;
38 static bool hold_button = false;
40 static short touch_x, touch_y, touch_z1, touch_z2;
41 static long last_touch = 0;
43 static struct touch_calibration_point topleft, bottomright;
45 /* Jd's tests.. These will hopefully work for everyone so we dont have to
46 * create a calibration screen.
47 * Portait:
48 * (0,0) = 200, 3900
49 * (480,640) = 3880, 270
50 * Landscape:
51 * (0,0) = 200, 270
52 * (640,480) = 3880, 3900
55 static int touch_to_pixels(short *val_x, short *val_y)
57 short x,y;
59 #if CONFIG_ORIENTATION == SCREEN_PORTRAIT
60 x=*val_x;
61 y=*val_y;
62 #else
63 x=*val_y;
64 y=*val_x;
65 #endif
67 x = (x-topleft.val_x)*(bottomright.px_x - topleft.px_x) / (bottomright.val_x - topleft.val_x) + topleft.px_x;
68 y = (y-topleft.val_y)*(bottomright.px_y - topleft.px_y) / (bottomright.val_y - topleft.val_y) + topleft.px_y;
70 if (x < 0)
71 x = 0;
72 else if (x>=LCD_WIDTH)
73 x=LCD_WIDTH-1;
75 if (y < 0)
76 y = 0;
77 else if (y>=LCD_HEIGHT)
78 y=LCD_HEIGHT-1;
80 *val_x=x;
81 *val_y=y;
83 return (x<<16)|y;
86 void button_init_device(void)
88 touch_available = false;
89 /* GIO is the power button, set as input */
90 IO_GIO_DIR0 |= 0x01;
92 #if CONFIG_ORIENTATION == SCREEN_PORTRAIT
93 topleft.val_x = 200;
94 topleft.val_y = 3900;
96 bottomright.val_x = 3880;
97 bottomright.val_y = 270;
98 #else
99 topleft.val_x = 270;
100 topleft.val_y = 200;
102 bottomright.val_x = 3900;
103 bottomright.val_y = 3880;
104 #endif
106 topleft.px_x = 0;
107 topleft.px_y = 0;
109 bottomright.px_x = LCD_WIDTH;
110 bottomright.px_y = LCD_HEIGHT;
113 inline bool button_hold(void)
115 return hold_button;
118 /* This is called from the tsc2100 interupt handler in adc-mr500.c */
119 void touch_read_coord(void)
121 touch_available = true;
122 tsc2100_read_touch(&touch_x, &touch_y, &touch_z1, &touch_z2);
125 int button_read_device(int *data)
127 int button_read = BUTTON_NONE;
128 static bool hold_button_old = false;
130 *data = 0;
132 /* Handle touchscreen */
133 if (touch_available)
135 *data = touch_to_pixels(&touch_x, &touch_y);
136 button_read |= touchscreen_to_pixels(touch_x, touch_y, data);
138 touch_available = false;
139 last_touch=current_tick;
142 /* Handle power button */
143 if ((IO_GIO_BITSET0&0x01) == 0)
145 button_read |= BUTTON_POWER;
148 /* Read data from the remote */
149 button_read |= remote_read_device();
150 hold_button=remote_button_hold();
152 /* Take care of hold notifications */
153 #ifndef BOOTLOADER
154 /* give BL notice if HB state chaged */
155 if (hold_button != hold_button_old)
157 backlight_hold_changed(hold_button);
158 hold_button_old=hold_button;
160 #endif
162 if (hold_button)
164 button_read = BUTTON_NONE;
167 return button_read;