Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Karl Kurbjun | bebbac8 | 2007-09-22 15:43:38 +0000 | [diff] [blame] | 8 | * $Id$ |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 9 | * |
| 10 | * Copyright (C) 2007 by Karl Kurbjun |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
Jonathan Gordon | e108964 | 2007-09-30 11:15:14 +0000 | [diff] [blame] | 22 | /* this file also handles the touch screen driver interface */ |
| 23 | |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 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 "adc.h" |
| 31 | #include "system.h" |
| 32 | #include "backlight-target.h" |
Jonathan Gordon | 300f64c | 2007-09-20 09:08:40 +0000 | [diff] [blame] | 33 | #include "uart-target.h" |
Jonathan Gordon | e108964 | 2007-09-30 11:15:14 +0000 | [diff] [blame] | 34 | #include "tsc2100.h" |
Karl Kurbjun | b7a4e10 | 2007-10-13 14:53:34 +0000 | [diff] [blame] | 35 | #include "string.h" |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 36 | |
Jonathan Gordon | 376429d | 2007-09-20 08:42:06 +0000 | [diff] [blame] | 37 | #define BUTTON_TIMEOUT 50 |
Jonathan Gordon | c5d22bc | 2007-09-21 02:05:02 +0000 | [diff] [blame] | 38 | |
| 39 | #define BUTTON_START_BYTE 0xF0 |
| 40 | #define BUTTON_START_BYTE2 0xF4 /* not sure why, but sometimes you get F0 or F4, */ |
| 41 | /* but always the same one for the session? */ |
Jonathan Gordon | e108964 | 2007-09-30 11:15:14 +0000 | [diff] [blame] | 42 | static short last_x, last_y, last_z1, last_z2; /* for the touch screen */ |
Jonathan Gordon | 12d1ff9 | 2007-10-22 07:01:59 +0000 | [diff] [blame] | 43 | static bool touch_available = false; |
Jonathan Gordon | 274c2b8 | 2007-10-01 07:52:39 +0000 | [diff] [blame] | 44 | |
Jonathan Gordon | 5b5a626 | 2008-04-15 10:35:11 +0000 | [diff] [blame] | 45 | static enum touchpad_mode current_mode = TOUCHPAD_POINT; |
| 46 | static int touchpad_buttons[3][3] = { |
| 47 | {BUTTON_TOPLEFT, BUTTON_TOPMIDDLE, BUTTON_TOPRIGHT}, |
| 48 | {BUTTON_MIDLEFT, BUTTON_CENTER, BUTTON_MIDRIGHT}, |
| 49 | {BUTTON_BOTTOMLEFT, BUTTON_BOTTOMMIDDLE, BUTTON_BOTTOMRIGHT}, |
| 50 | }; |
| 51 | |
| 52 | void touchpad_set_mode(enum touchpad_mode mode) |
| 53 | { |
| 54 | current_mode = mode; |
| 55 | } |
| 56 | enum touchpad_mode touchpad_get_mode(void) |
| 57 | { |
| 58 | return current_mode; |
| 59 | } |
| 60 | |
Jonathan Gordon | 274c2b8 | 2007-10-01 07:52:39 +0000 | [diff] [blame] | 61 | static struct touch_calibration_point topleft, bottomright; |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 62 | |
Jonathan Gordon | d8667df | 2007-10-18 12:15:56 +0000 | [diff] [blame] | 63 | /* Jd's tests.. These will hopefully work for everyone so we dont have to |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 64 | * create a calibration screen. |
| 65 | * Portait: |
| 66 | * (0,0) = 200, 3900 |
| 67 | * (480,640) = 3880, 270 |
| 68 | * Landscape: |
| 69 | * (0,0) = 200, 270 |
| 70 | * (640,480) = 3880, 3900 |
Jonathan Gordon | d8667df | 2007-10-18 12:15:56 +0000 | [diff] [blame] | 71 | */ |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 72 | |
Jonathan Gordon | 274c2b8 | 2007-10-01 07:52:39 +0000 | [diff] [blame] | 73 | static int touch_to_pixels(short val_x, short val_y) |
| 74 | { |
| 75 | short x,y; |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 76 | |
Jonathan Gordon | c741ecc | 2008-04-29 01:09:39 +0000 | [diff] [blame] | 77 | #if CONFIG_ORIENTATION == SCREEN_PORTRAIT |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 78 | x=val_x; |
| 79 | y=val_y; |
| 80 | #else |
| 81 | x=val_y; |
| 82 | y=val_x; |
| 83 | #endif |
| 84 | |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 85 | x = (x-topleft.val_x)*(bottomright.px_x - topleft.px_x) / (bottomright.val_x - topleft.val_x) + topleft.px_x; |
| 86 | y = (y-topleft.val_y)*(bottomright.px_y - topleft.px_y) / (bottomright.val_y - topleft.val_y) + topleft.px_y; |
| 87 | |
Jonathan Gordon | 274c2b8 | 2007-10-01 07:52:39 +0000 | [diff] [blame] | 88 | if (x < 0) |
| 89 | x = 0; |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 90 | else if (x>=LCD_WIDTH) |
| 91 | x=LCD_WIDTH-1; |
| 92 | |
Jonathan Gordon | 274c2b8 | 2007-10-01 07:52:39 +0000 | [diff] [blame] | 93 | if (y < 0) |
| 94 | y = 0; |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 95 | else if (y>=LCD_HEIGHT) |
| 96 | y=LCD_HEIGHT-1; |
| 97 | |
| 98 | |
Jonathan Gordon | 274c2b8 | 2007-10-01 07:52:39 +0000 | [diff] [blame] | 99 | return (x<<16)|y; |
| 100 | } |
Karl Kurbjun | fa1a38e | 2007-10-18 05:14:10 +0000 | [diff] [blame] | 101 | |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 102 | void button_init_device(void) |
| 103 | { |
Jonathan Gordon | 12d1ff9 | 2007-10-22 07:01:59 +0000 | [diff] [blame] | 104 | touch_available = false; |
Jonathan Gordon | 376429d | 2007-09-20 08:42:06 +0000 | [diff] [blame] | 105 | /* GIO is the power button, set as input */ |
Jonathan Gordon | a5e788f | 2007-09-30 08:18:46 +0000 | [diff] [blame] | 106 | IO_GIO_DIR0 |= 0x01; |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 107 | |
Jonathan Gordon | c741ecc | 2008-04-29 01:09:39 +0000 | [diff] [blame] | 108 | #if CONFIG_ORIENTATION == SCREEN_PORTRAIT |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 109 | topleft.val_x = 200; |
| 110 | topleft.val_y = 3900; |
Jonathan Gordon | e108964 | 2007-09-30 11:15:14 +0000 | [diff] [blame] | 111 | |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 112 | bottomright.val_x = 3880; |
| 113 | bottomright.val_y = 270; |
Jonathan Gordon | 28dfb54 | 2007-11-11 04:48:33 +0000 | [diff] [blame] | 114 | #else |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 115 | topleft.val_x = 270; |
| 116 | topleft.val_y = 200; |
| 117 | |
| 118 | bottomright.val_x = 3900; |
| 119 | bottomright.val_y = 3880; |
| 120 | #endif |
| 121 | |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 122 | topleft.px_x = 0; |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 123 | topleft.px_y = 0; |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 124 | |
| 125 | bottomright.px_x = LCD_WIDTH; |
Karl Kurbjun | 59a2862 | 2007-11-11 03:31:24 +0000 | [diff] [blame] | 126 | bottomright.px_y = LCD_HEIGHT; |
| 127 | |
Jonathan Gordon | e108964 | 2007-09-30 11:15:14 +0000 | [diff] [blame] | 128 | /* Enable the touchscreen interrupt */ |
| 129 | IO_INTC_EINT2 |= (1<<3); /* IRQ_GIO14 */ |
| 130 | #if 0 |
| 131 | tsc2100_writereg(TSADC_PAGE, TSADC_ADDRESS, |
| 132 | TSADC_PSTCM| |
| 133 | (0x2<<TSADC_ADSCM_SHIFT)| /* scan x,y,z1,z2 */ |
| 134 | (0x1<<TSADC_RESOL_SHIFT) /* 8 bit resolution */ |
| 135 | ); |
| 136 | /* doesnt work for some reason... |
| 137 | setting to 8bit would probably be better than the 12bit currently */ |
| 138 | #endif |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | inline bool button_hold(void) |
| 142 | { |
| 143 | return false; |
| 144 | } |
Jonathan Gordon | d8667df | 2007-10-18 12:15:56 +0000 | [diff] [blame] | 145 | |
Karl Kurbjun | fa1a38e | 2007-10-18 05:14:10 +0000 | [diff] [blame] | 146 | static void remote_heartbeat(void) |
| 147 | { |
| 148 | char data[5] = {0x11, 0x30, 0x11^0x30, 0x11+0x30, '\0'}; |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 149 | uart1_puts(data, 5); |
Karl Kurbjun | fa1a38e | 2007-10-18 05:14:10 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Jonathan Gordon | 12d1ff9 | 2007-10-22 07:01:59 +0000 | [diff] [blame] | 152 | #define TOUCH_MARGIN 8 |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 153 | char r_buffer[5]; |
| 154 | int r_button = BUTTON_NONE; |
Jonathan Gordon | 12d1ff9 | 2007-10-22 07:01:59 +0000 | [diff] [blame] | 155 | int button_read_device(int *data) |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 156 | { |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 157 | int retval, calbuf; |
Karl Kurbjun | d3c0a7f | 2007-11-10 22:12:54 +0000 | [diff] [blame] | 158 | static int oldbutton = BUTTON_NONE; |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 159 | |
| 160 | r_button=BUTTON_NONE; |
Jonathan Gordon | 12d1ff9 | 2007-10-22 07:01:59 +0000 | [diff] [blame] | 161 | *data = 0; |
Karl Kurbjun | d3c0a7f | 2007-11-10 22:12:54 +0000 | [diff] [blame] | 162 | |
Jonathan Gordon | 12d1ff9 | 2007-10-22 07:01:59 +0000 | [diff] [blame] | 163 | if (touch_available) |
| 164 | { |
| 165 | short x,y; |
| 166 | static long last_touch = 0; |
| 167 | bool send_touch = false; |
| 168 | tsc2100_read_values(&x, &y, &last_z1, &last_z2); |
| 169 | if (TIME_BEFORE(last_touch + HZ/5, current_tick)) |
| 170 | { |
| 171 | if ((x > last_x + TOUCH_MARGIN) || |
| 172 | (x < last_x - TOUCH_MARGIN) || |
| 173 | (y > last_y + TOUCH_MARGIN) || |
| 174 | (y < last_y - TOUCH_MARGIN)) |
| 175 | { |
| 176 | send_touch = true; |
| 177 | } |
| 178 | } |
| 179 | else |
| 180 | send_touch = true; |
| 181 | if (send_touch) |
| 182 | { |
| 183 | last_x = x; |
| 184 | last_y = y; |
| 185 | *data = touch_to_pixels(x, y); |
Jonathan Gordon | 5b5a626 | 2008-04-15 10:35:11 +0000 | [diff] [blame] | 186 | switch (current_mode) |
| 187 | { |
| 188 | case TOUCHPAD_POINT: |
| 189 | r_button |= BUTTON_TOUCHPAD; |
| 190 | break; |
| 191 | case TOUCHPAD_BUTTON: |
| 192 | { |
| 193 | int px_x = ((*data&0xffff0000)>>16), px_y = ((*data&0x0000ffff)); |
| 194 | r_button |= touchpad_buttons[px_y/(LCD_HEIGHT/3)][px_x/(LCD_WIDTH/3)]; |
| 195 | oldbutton = r_button; |
| 196 | break; |
| 197 | } |
| 198 | } |
Jonathan Gordon | 12d1ff9 | 2007-10-22 07:01:59 +0000 | [diff] [blame] | 199 | } |
| 200 | last_touch = current_tick; |
| 201 | touch_available = false; |
| 202 | } |
Karl Kurbjun | fa1a38e | 2007-10-18 05:14:10 +0000 | [diff] [blame] | 203 | remote_heartbeat(); |
Karl Kurbjun | d3c0a7f | 2007-11-10 22:12:54 +0000 | [diff] [blame] | 204 | |
| 205 | if ((IO_GIO_BITSET0&0x01) == 0) |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 206 | { |
| 207 | r_button |= BUTTON_POWER; |
| 208 | oldbutton=r_button; |
| 209 | } |
Karl Kurbjun | d3c0a7f | 2007-11-10 22:12:54 +0000 | [diff] [blame] | 210 | |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 211 | retval=uart1_gets_queue(r_buffer, 5); |
Karl Kurbjun | d3c0a7f | 2007-11-10 22:12:54 +0000 | [diff] [blame] | 212 | do |
Jonathan Gordon | 376429d | 2007-09-20 08:42:06 +0000 | [diff] [blame] | 213 | { |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 214 | for(calbuf=0;calbuf<4;calbuf++) |
| 215 | { |
| 216 | if((r_buffer[calbuf]&0xF0)==0xF0 && (r_buffer[calbuf+1]&0xF0)!=0xF0) |
| 217 | break; |
| 218 | } |
| 219 | calbuf++; |
| 220 | if(calbuf==5) |
| 221 | calbuf=0; |
Karl Kurbjun | d3c0a7f | 2007-11-10 22:12:54 +0000 | [diff] [blame] | 222 | if(retval>=0) |
Jonathan Gordon | 376429d | 2007-09-20 08:42:06 +0000 | [diff] [blame] | 223 | { |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 224 | r_button |= r_buffer[calbuf]; |
| 225 | oldbutton=r_button; |
Karl Kurbjun | fa1a38e | 2007-10-18 05:14:10 +0000 | [diff] [blame] | 226 | } |
Karl Kurbjun | d3c0a7f | 2007-11-10 22:12:54 +0000 | [diff] [blame] | 227 | else |
Karl Kurbjun | fa1a38e | 2007-10-18 05:14:10 +0000 | [diff] [blame] | 228 | { |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 229 | r_button=oldbutton; |
Jonathan Gordon | 376429d | 2007-09-20 08:42:06 +0000 | [diff] [blame] | 230 | } |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 231 | } while((retval=uart1_gets_queue(r_buffer, 5))>=5); |
Karl Kurbjun | d3c0a7f | 2007-11-10 22:12:54 +0000 | [diff] [blame] | 232 | |
Karl Kurbjun | d3d0b26 | 2007-11-15 06:44:35 +0000 | [diff] [blame] | 233 | return r_button; |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 234 | } |
Jonathan Gordon | 12d1ff9 | 2007-10-22 07:01:59 +0000 | [diff] [blame] | 235 | |
| 236 | /* Touchpad data available interupt */ |
Jonathan Gordon | 4c06ad6 | 2007-10-22 15:28:40 +0000 | [diff] [blame] | 237 | void read_battery_inputs(void); |
Jonathan Gordon | e108964 | 2007-09-30 11:15:14 +0000 | [diff] [blame] | 238 | void GIO14(void) |
| 239 | { |
Jonathan Gordon | 4c06ad6 | 2007-10-22 15:28:40 +0000 | [diff] [blame] | 240 | short tsadc = tsc2100_readreg(TSADC_PAGE, TSADC_ADDRESS); |
| 241 | short adscm = (tsadc&TSADC_ADSCM_MASK)>>TSADC_ADSCM_SHIFT; |
| 242 | switch (adscm) |
| 243 | { |
| 244 | case 1: |
| 245 | case 2: |
| 246 | touch_available = true; |
| 247 | break; |
| 248 | case 0xb: |
| 249 | read_battery_inputs(); |
| 250 | break; |
| 251 | } |
Jonathan Gordon | 5b5a626 | 2008-04-15 10:35:11 +0000 | [diff] [blame] | 252 | //touch_available = true; |
Jonathan Gordon | 12d1ff9 | 2007-10-22 07:01:59 +0000 | [diff] [blame] | 253 | IO_INTC_IRQ2 = (1<<3); /* IRQ_GIO14 == 35 */ |
Jonathan Gordon | e108964 | 2007-09-30 11:15:14 +0000 | [diff] [blame] | 254 | } |