slightly better touchpad driver. Still not brilliant, but the bootloader/debugger...
[Rockbox.git] / firmware / target / arm / tms320dm320 / mrobe-500 / button-mr500.c
blobea97f6d0dbd47a7a43dc41b5f719290fcd1170ae
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Karl Kurbjun
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 /* this file also handles the touch screen driver interface */
22 #include "config.h"
23 #include "cpu.h"
24 #include "system.h"
25 #include "button.h"
26 #include "kernel.h"
27 #include "backlight.h"
28 #include "adc.h"
29 #include "system.h"
30 #include "backlight-target.h"
31 #include "uart-target.h"
32 #include "tsc2100.h"
33 #include "string.h"
35 #define BUTTON_TIMEOUT 50
37 #define BUTTON_START_BYTE 0xF0
38 #define BUTTON_START_BYTE2 0xF4 /* not sure why, but sometimes you get F0 or F4, */
39 /* but always the same one for the session? */
40 static short last_x, last_y, last_z1, last_z2; /* for the touch screen */
41 static int last_touch;
43 static struct touch_calibration_point topleft, bottomright;
44 static bool using_calibration = false;
45 void use_calibration(bool enable)
47 using_calibration = enable;
49 /* Jd's tests.. These will hopefully work for everyone so we dont have to
50 create a calibration screen. and
51 (0,0) = 0x00c0, 0xf40
52 (480,320) = 0x0f19, 0x00fc
54 void set_calibration_points(struct touch_calibration_point *tl,
55 struct touch_calibration_point *br)
57 memcpy(&topleft, tl, sizeof(struct touch_calibration_point));
58 memcpy(&bottomright, br, sizeof(struct touch_calibration_point));
60 static int touch_to_pixels(short val_x, short val_y)
62 short x,y;
63 int x1,x2;
64 if (!using_calibration)
65 return (val_x<<16)|val_y;
66 x1 = topleft.val_x; x2 = bottomright.val_x;
67 x = (val_x-x1)*(bottomright.px_x - topleft.px_x) / (x2 - x1) + topleft.px_x;
68 x1 = topleft.val_y; x2 = bottomright.val_y;
69 y = (val_y-x1)*(bottomright.px_y - topleft.px_y) / (x2 - x1) + topleft.px_y;
70 if (x < 0)
71 x = 0;
72 if (y < 0)
73 y = 0;
74 return (x<<16)|y;
77 void button_init_device(void)
79 last_touch = 0;
80 /* GIO is the power button, set as input */
81 IO_GIO_DIR0 |= 0x01;
82 topleft.px_x = 0; topleft.px_y = 0;
83 topleft.val_x = 0x00c0; topleft.val_y = 0xf40;
85 bottomright.px_x = LCD_WIDTH; bottomright.px_y = LCD_HEIGHT;
86 bottomright.val_x = 0x0f19; bottomright.val_y = 0x00fc;
87 using_calibration = true;
89 /* Enable the touchscreen interrupt */
90 IO_INTC_EINT2 |= (1<<3); /* IRQ_GIO14 */
91 #if 0
92 tsc2100_writereg(TSADC_PAGE, TSADC_ADDRESS,
93 TSADC_PSTCM|
94 (0x2<<TSADC_ADSCM_SHIFT)| /* scan x,y,z1,z2 */
95 (0x1<<TSADC_RESOL_SHIFT) /* 8 bit resolution */
97 /* doesnt work for some reason...
98 setting to 8bit would probably be better than the 12bit currently */
99 #endif
102 inline bool button_hold(void)
104 return false;
107 int button_get_last_touch(void)
109 int ret_val = last_touch;
110 last_touch = 0;
111 return ret_val;
114 static void remote_heartbeat(void)
116 char data[5] = {0x11, 0x30, 0x11^0x30, 0x11+0x30, '\0'};
117 uart1_puts(data);
120 int button_read_device(void)
122 char c;
123 int i = 0;
124 int btn = BUTTON_NONE;
126 if ((IO_GIO_BITSET0&0x01) == 0)
127 btn |= BUTTON_POWER;
129 remote_heartbeat();
130 while (uart1_getch(&c))
132 if (i==0 && (c == BUTTON_START_BYTE || c == BUTTON_START_BYTE2) )
134 i++;
136 else if (i)
138 i++;
139 if(i==2)
141 if (c& (1<<7))
142 btn |= BUTTON_RC_HEART;
143 if (c& (1<<6))
144 btn |= BUTTON_RC_MODE;
145 if (c& (1<<5))
146 btn |= BUTTON_RC_VOL_DOWN;
147 if (c& (1<<4))
148 btn |= BUTTON_RC_VOL_UP;
149 if (c& (1<<3))
150 btn |= BUTTON_RC_REW;
151 if (c& (1<<2))
152 btn |= BUTTON_RC_FF;
153 if (c& (1<<1))
154 btn |= BUTTON_RC_DOWN;
155 if (c& (1<<0))
156 btn |= BUTTON_RC_PLAY;
158 else if(i==5)
160 i=0;
164 return btn;
166 #define TOUCH_MARGIN 8
167 void GIO14(void)
169 short x,y;
170 static int last_tick = 0;
171 tsc2100_read_values(&x, &y,
172 &last_z1, &last_z2);
173 if (TIME_BEFORE(last_tick+HZ/5, current_tick))
175 if ((x > last_x + TOUCH_MARGIN) ||
176 (x < last_x - TOUCH_MARGIN) ||
177 (y > last_y + TOUCH_MARGIN) ||
178 (y < last_y - TOUCH_MARGIN))
180 last_x = x;
181 last_y = y;
182 queue_clear(&button_queue);
183 queue_post(&button_queue, BUTTON_TOUCHPAD,
184 touch_to_pixels(x, y));
186 last_tick = current_tick;
188 IO_INTC_IRQ2 = (1<<3);