Fork with support for ILI9341 controller
Dependents: CANary_9341 CANary
Fork of TOUCH_TFTx2 by
TOUCH_TFTx2.cpp@4:a3cd26c97b76, 2013-02-11 (annotated)
- Committer:
- TickTock
- Date:
- Mon Feb 11 02:14:15 2013 +0000
- Revision:
- 4:a3cd26c97b76
- Parent:
- 3:3db7309b6146
- Child:
- 5:a9890c586a64
Added support for dual display with X&Y connected in series
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 0:d78b00f167cb | 1 | /* mbed library for resistive touch pads |
dreschpe | 0:d78b00f167cb | 2 | * uses 4 pins - 2 IO and 2 Analog |
dreschpe | 0:d78b00f167cb | 3 | |
dreschpe | 0:d78b00f167cb | 4 | * c 2011 Peter Drescher - DC2PD |
dreschpe | 0:d78b00f167cb | 5 | * |
dreschpe | 0:d78b00f167cb | 6 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
dreschpe | 0:d78b00f167cb | 7 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
dreschpe | 0:d78b00f167cb | 8 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
dreschpe | 0:d78b00f167cb | 9 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
dreschpe | 0:d78b00f167cb | 10 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dreschpe | 0:d78b00f167cb | 11 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
dreschpe | 0:d78b00f167cb | 12 | * THE SOFTWARE. |
dreschpe | 0:d78b00f167cb | 13 | */ |
dreschpe | 0:d78b00f167cb | 14 | |
dreschpe | 0:d78b00f167cb | 15 | |
TickTock | 3:3db7309b6146 | 16 | #include "TOUCH_TFTx2.h" |
dreschpe | 0:d78b00f167cb | 17 | #include "mbed.h" |
dreschpe | 0:d78b00f167cb | 18 | |
TickTock | 4:a3cd26c97b76 | 19 | #define threshold 0x1000 // threshold to detect pressed |
dreschpe | 0:d78b00f167cb | 20 | |
TickTock | 3:3db7309b6146 | 21 | TOUCH_TFTx2::TOUCH_TFTx2(PinName xp, PinName xm, PinName yp, PinName ym, |
TickTock | 3:3db7309b6146 | 22 | PinName mosi, PinName miso, PinName sclk, PinName cs0, PinName cs1, PinName reset,const char* name): |
dreschpe | 0:d78b00f167cb | 23 | _xp(xp),_xm(xm),_yp(yp),_ym(ym),_ax(xp),_ay(yp), |
TickTock | 3:3db7309b6146 | 24 | SPI_TFTx2(mosi,miso,sclk,cs0,cs1,reset,name) { |
dreschpe | 0:d78b00f167cb | 25 | xa = xp; |
dreschpe | 0:d78b00f167cb | 26 | ya = yp; |
dreschpe | 0:d78b00f167cb | 27 | |
dreschpe | 0:d78b00f167cb | 28 | } |
dreschpe | 0:d78b00f167cb | 29 | |
TickTock | 3:3db7309b6146 | 30 | point TOUCH_TFTx2::get_touch() { |
TickTock | 4:a3cd26c97b76 | 31 | unsigned short x1, x2, y1, y2; |
dreschpe | 0:d78b00f167cb | 32 | unsigned int s1 = 0,s2 = 0,d1 , d2; |
dreschpe | 0:d78b00f167cb | 33 | point p; |
dreschpe | 0:d78b00f167cb | 34 | |
dreschpe | 0:d78b00f167cb | 35 | do { |
dreschpe | 0:d78b00f167cb | 36 | // read y voltage |
TickTock | 4:a3cd26c97b76 | 37 | _ym.input(); //LAJ y- has to be passive |
TickTock | 4:a3cd26c97b76 | 38 | _yp.input(); //LAJ y+ has to be passive |
dreschpe | 0:d78b00f167cb | 39 | _xp.output(); |
dreschpe | 0:d78b00f167cb | 40 | _xm.output(); |
dreschpe | 0:d78b00f167cb | 41 | switch (orientation) { |
dreschpe | 0:d78b00f167cb | 42 | case(0): |
dreschpe | 0:d78b00f167cb | 43 | case(3): |
dreschpe | 0:d78b00f167cb | 44 | _xp = 1; |
dreschpe | 0:d78b00f167cb | 45 | _xm = 0; |
dreschpe | 0:d78b00f167cb | 46 | break; |
dreschpe | 0:d78b00f167cb | 47 | case(1): |
dreschpe | 0:d78b00f167cb | 48 | case(2): |
dreschpe | 0:d78b00f167cb | 49 | _xp = 0; |
dreschpe | 0:d78b00f167cb | 50 | _xm = 1; |
dreschpe | 0:d78b00f167cb | 51 | break; |
dreschpe | 0:d78b00f167cb | 52 | } |
dreschpe | 0:d78b00f167cb | 53 | AnalogIn Ay(ya); // we have to call the constructor to switch to analog mode |
dreschpe | 0:d78b00f167cb | 54 | wait_us(10); |
dreschpe | 0:d78b00f167cb | 55 | y1 = Ay.read_u16(); // get y voltage |
dreschpe | 0:d78b00f167cb | 56 | d1 = (y1 > y2)? (y1-y2) : (y2-y1); |
TickTock | 4:a3cd26c97b76 | 57 | if (d1 < 3000) s1 ++; |
dreschpe | 0:d78b00f167cb | 58 | else { |
TickTock | 4:a3cd26c97b76 | 59 | s1 = 0; |
dreschpe | 0:d78b00f167cb | 60 | } |
dreschpe | 0:d78b00f167cb | 61 | y2 = y1; |
dreschpe | 0:d78b00f167cb | 62 | // debug |
TickTock | 4:a3cd26c97b76 | 63 | //locate(1,80); |
dreschpe | 0:d78b00f167cb | 64 | //printf("d: %4d y: %5d s1: %4d",d1,y1,s1); |
dreschpe | 0:d78b00f167cb | 65 | |
dreschpe | 0:d78b00f167cb | 66 | // read x voltage |
TickTock | 4:a3cd26c97b76 | 67 | _xm.input(); //LAJ x- has to be passive |
TickTock | 4:a3cd26c97b76 | 68 | _xp.input(); //LAJ x+ has to be passive |
dreschpe | 0:d78b00f167cb | 69 | _yp.output(); |
dreschpe | 0:d78b00f167cb | 70 | _ym.output(); |
dreschpe | 0:d78b00f167cb | 71 | switch (orientation) { |
dreschpe | 0:d78b00f167cb | 72 | case(0): |
dreschpe | 0:d78b00f167cb | 73 | case(1): |
dreschpe | 0:d78b00f167cb | 74 | _yp = 1; |
dreschpe | 0:d78b00f167cb | 75 | _ym = 0; |
dreschpe | 0:d78b00f167cb | 76 | break; |
dreschpe | 0:d78b00f167cb | 77 | case(2): |
dreschpe | 0:d78b00f167cb | 78 | case(3): |
dreschpe | 0:d78b00f167cb | 79 | _yp = 0; |
dreschpe | 0:d78b00f167cb | 80 | _ym = 1; |
dreschpe | 0:d78b00f167cb | 81 | break; |
dreschpe | 0:d78b00f167cb | 82 | } |
dreschpe | 0:d78b00f167cb | 83 | AnalogIn Ax(xa); // we have to call the constructor to switch to analog mode |
dreschpe | 0:d78b00f167cb | 84 | wait_us(10); |
dreschpe | 0:d78b00f167cb | 85 | x1 = Ax.read_u16(); // get x voltage |
dreschpe | 0:d78b00f167cb | 86 | d2 = (x1 > x2)? (x1-x2) : (x2-x1); |
TickTock | 4:a3cd26c97b76 | 87 | if (d2 < 3000) s2 ++; |
dreschpe | 0:d78b00f167cb | 88 | else { |
TickTock | 4:a3cd26c97b76 | 89 | s2 = 0; |
dreschpe | 0:d78b00f167cb | 90 | } |
dreschpe | 0:d78b00f167cb | 91 | x2 = x1; |
dreschpe | 0:d78b00f167cb | 92 | // debug |
TickTock | 4:a3cd26c97b76 | 93 | //locate(1,100); |
dreschpe | 0:d78b00f167cb | 94 | //printf("d: %4d x: %5d s2: %4d",d2,x1,s2); |
TickTock | 4:a3cd26c97b76 | 95 | //wait(0.25); |
dreschpe | 0:d78b00f167cb | 96 | } while (s1 < 3 || s2 < 3); // read until we have three samples close together |
dreschpe | 0:d78b00f167cb | 97 | switch (orientation) { |
dreschpe | 0:d78b00f167cb | 98 | case(0): |
dreschpe | 0:d78b00f167cb | 99 | case(2): |
dreschpe | 0:d78b00f167cb | 100 | p.y = (x1+x2) / 2; // average of two sample |
dreschpe | 0:d78b00f167cb | 101 | p.x = (y1+y2) / 2; |
dreschpe | 0:d78b00f167cb | 102 | break; |
dreschpe | 0:d78b00f167cb | 103 | case(1): |
dreschpe | 0:d78b00f167cb | 104 | case(3): |
dreschpe | 0:d78b00f167cb | 105 | p.x = (x1+x2) / 2; // average of two sample |
dreschpe | 0:d78b00f167cb | 106 | p.y = (y1+y2) / 2; |
dreschpe | 0:d78b00f167cb | 107 | break; |
dreschpe | 0:d78b00f167cb | 108 | } |
dreschpe | 0:d78b00f167cb | 109 | return(p); |
dreschpe | 0:d78b00f167cb | 110 | } |
dreschpe | 0:d78b00f167cb | 111 | |
TickTock | 3:3db7309b6146 | 112 | void TOUCH_TFTx2::calibrate(void) { |
dreschpe | 0:d78b00f167cb | 113 | int i; |
dreschpe | 0:d78b00f167cb | 114 | int a = 0,b = 0,c = 0, d = 0; |
dreschpe | 2:ef7972c29c0e | 115 | int pos_x, pos_y; |
dreschpe | 0:d78b00f167cb | 116 | point p; |
dreschpe | 0:d78b00f167cb | 117 | |
TickTock | 4:a3cd26c97b76 | 118 | seldisp=1; // select right display |
dreschpe | 0:d78b00f167cb | 119 | cls(); |
dreschpe | 0:d78b00f167cb | 120 | line(0,3,6,3,White); |
dreschpe | 0:d78b00f167cb | 121 | line(3,0,3,6,White); |
dreschpe | 2:ef7972c29c0e | 122 | |
dreschpe | 2:ef7972c29c0e | 123 | // get the center of the screen |
dreschpe | 2:ef7972c29c0e | 124 | pos_x = columns() / 2 - 3; |
dreschpe | 2:ef7972c29c0e | 125 | pos_x = pos_x * font[1]; |
dreschpe | 2:ef7972c29c0e | 126 | pos_y = (rows() / 2) - 1; |
dreschpe | 2:ef7972c29c0e | 127 | pos_y = pos_y * font[2]; |
dreschpe | 2:ef7972c29c0e | 128 | locate(pos_x,pos_y); |
dreschpe | 0:d78b00f167cb | 129 | printf("press cross"); |
dreschpe | 2:ef7972c29c0e | 130 | locate(pos_x,pos_y + font[2]); |
dreschpe | 0:d78b00f167cb | 131 | printf("to calibrate"); |
TickTock | 4:a3cd26c97b76 | 132 | do { |
TickTock | 4:a3cd26c97b76 | 133 | wait(0.1); |
TickTock | 4:a3cd26c97b76 | 134 | } while (!is_touched()); //Wait for touch |
dreschpe | 0:d78b00f167cb | 135 | for (i=0; i<5; i++) { |
TickTock | 4:a3cd26c97b76 | 136 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 137 | b += p.y; |
dreschpe | 0:d78b00f167cb | 138 | } |
dreschpe | 0:d78b00f167cb | 139 | b = b / 5; |
dreschpe | 2:ef7972c29c0e | 140 | locate(pos_x,pos_y); |
dreschpe | 0:d78b00f167cb | 141 | printf("OK "); |
dreschpe | 0:d78b00f167cb | 142 | do { |
TickTock | 4:a3cd26c97b76 | 143 | wait(0.1); |
TickTock | 4:a3cd26c97b76 | 144 | } while (is_touched()); //Wait for no touch |
dreschpe | 0:d78b00f167cb | 145 | |
dreschpe | 0:d78b00f167cb | 146 | cls(); |
dreschpe | 0:d78b00f167cb | 147 | line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross |
dreschpe | 0:d78b00f167cb | 148 | line(width() - 8,height() - 5,width() - 1,height() - 5,White); |
dreschpe | 2:ef7972c29c0e | 149 | locate(pos_x,pos_y); |
dreschpe | 0:d78b00f167cb | 150 | printf("press cross"); |
dreschpe | 2:ef7972c29c0e | 151 | locate(pos_x,pos_y + font[2]); |
dreschpe | 0:d78b00f167cb | 152 | printf("to calibrate"); |
TickTock | 4:a3cd26c97b76 | 153 | do { |
TickTock | 4:a3cd26c97b76 | 154 | wait(0.1); |
TickTock | 4:a3cd26c97b76 | 155 | } while (!is_touched()); //Wait for touch |
dreschpe | 0:d78b00f167cb | 156 | for (i=0; i<5; i++) { |
TickTock | 4:a3cd26c97b76 | 157 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 158 | c+= p.x; |
TickTock | 4:a3cd26c97b76 | 159 | } |
TickTock | 4:a3cd26c97b76 | 160 | c = c / 5; |
TickTock | 4:a3cd26c97b76 | 161 | |
TickTock | 4:a3cd26c97b76 | 162 | locate(pos_x, pos_y); |
TickTock | 4:a3cd26c97b76 | 163 | printf("OK "); |
TickTock | 4:a3cd26c97b76 | 164 | do { |
TickTock | 4:a3cd26c97b76 | 165 | wait(0.1); |
TickTock | 4:a3cd26c97b76 | 166 | } while (is_touched()); //Wait for no touch |
TickTock | 4:a3cd26c97b76 | 167 | |
TickTock | 4:a3cd26c97b76 | 168 | cls(); |
TickTock | 4:a3cd26c97b76 | 169 | seldisp=0; // select left display |
TickTock | 4:a3cd26c97b76 | 170 | cls(); |
TickTock | 4:a3cd26c97b76 | 171 | line(0,3,6,3,White); |
TickTock | 4:a3cd26c97b76 | 172 | line(3,0,3,6,White); |
TickTock | 4:a3cd26c97b76 | 173 | |
TickTock | 4:a3cd26c97b76 | 174 | // get the center of the screen |
TickTock | 4:a3cd26c97b76 | 175 | pos_x = columns() / 2 - 3; |
TickTock | 4:a3cd26c97b76 | 176 | pos_x = pos_x * font[1]; |
TickTock | 4:a3cd26c97b76 | 177 | pos_y = (rows() / 2) - 1; |
TickTock | 4:a3cd26c97b76 | 178 | pos_y = pos_y * font[2]; |
TickTock | 4:a3cd26c97b76 | 179 | locate(pos_x,pos_y); |
TickTock | 4:a3cd26c97b76 | 180 | printf("press cross"); |
TickTock | 4:a3cd26c97b76 | 181 | locate(pos_x,pos_y + font[2]); |
TickTock | 4:a3cd26c97b76 | 182 | printf("to calibrate"); |
TickTock | 4:a3cd26c97b76 | 183 | do { |
TickTock | 4:a3cd26c97b76 | 184 | wait(0.1); |
TickTock | 4:a3cd26c97b76 | 185 | } while (!is_touched()); //Wait for touch |
TickTock | 4:a3cd26c97b76 | 186 | for (i=0; i<5; i++) { |
TickTock | 4:a3cd26c97b76 | 187 | p = get_touch(); |
TickTock | 4:a3cd26c97b76 | 188 | a += p.x; |
TickTock | 4:a3cd26c97b76 | 189 | } |
TickTock | 4:a3cd26c97b76 | 190 | a = a / 5; |
TickTock | 4:a3cd26c97b76 | 191 | locate(pos_x,pos_y); |
TickTock | 4:a3cd26c97b76 | 192 | printf("OK "); |
TickTock | 4:a3cd26c97b76 | 193 | do { |
TickTock | 4:a3cd26c97b76 | 194 | wait(0.1); |
TickTock | 4:a3cd26c97b76 | 195 | } while (is_touched()); //Wait for no touch |
TickTock | 4:a3cd26c97b76 | 196 | cls(); |
TickTock | 4:a3cd26c97b76 | 197 | line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross |
TickTock | 4:a3cd26c97b76 | 198 | line(width() - 8,height() - 5,width() - 1,height() - 5,White); |
TickTock | 4:a3cd26c97b76 | 199 | locate(pos_x,pos_y); |
TickTock | 4:a3cd26c97b76 | 200 | printf("press cross"); |
TickTock | 4:a3cd26c97b76 | 201 | locate(pos_x,pos_y + font[2]); |
TickTock | 4:a3cd26c97b76 | 202 | printf("to calibrate"); |
TickTock | 4:a3cd26c97b76 | 203 | do { |
TickTock | 4:a3cd26c97b76 | 204 | wait(0.1); |
TickTock | 4:a3cd26c97b76 | 205 | } while (!is_touched()); //Wait for touch |
TickTock | 4:a3cd26c97b76 | 206 | for (i=0; i<5; i++) { |
TickTock | 4:a3cd26c97b76 | 207 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 208 | d+= p.y; |
dreschpe | 0:d78b00f167cb | 209 | } |
dreschpe | 0:d78b00f167cb | 210 | d = d / 5; |
dreschpe | 0:d78b00f167cb | 211 | |
dreschpe | 2:ef7972c29c0e | 212 | locate(pos_x, pos_y); |
dreschpe | 0:d78b00f167cb | 213 | printf("OK "); |
dreschpe | 0:d78b00f167cb | 214 | do { |
TickTock | 4:a3cd26c97b76 | 215 | wait(0.1); |
TickTock | 4:a3cd26c97b76 | 216 | } while (is_touched()); //Wait for no touch |
dreschpe | 0:d78b00f167cb | 217 | |
dreschpe | 0:d78b00f167cb | 218 | cls(); |
dreschpe | 0:d78b00f167cb | 219 | |
dreschpe | 0:d78b00f167cb | 220 | x_off = a; |
dreschpe | 0:d78b00f167cb | 221 | y_off = b; |
dreschpe | 0:d78b00f167cb | 222 | |
dreschpe | 0:d78b00f167cb | 223 | i = c-a; // delta x |
dreschpe | 0:d78b00f167cb | 224 | pp_tx = i / (width() - 6); |
dreschpe | 0:d78b00f167cb | 225 | |
dreschpe | 0:d78b00f167cb | 226 | i = d-b; // delta y |
dreschpe | 0:d78b00f167cb | 227 | pp_ty = i / (height() - 6); |
dreschpe | 0:d78b00f167cb | 228 | } |
dreschpe | 0:d78b00f167cb | 229 | |
dreschpe | 0:d78b00f167cb | 230 | |
TickTock | 3:3db7309b6146 | 231 | point TOUCH_TFTx2::to_pixel(point a_point) { |
dreschpe | 0:d78b00f167cb | 232 | point p; |
dreschpe | 0:d78b00f167cb | 233 | |
dreschpe | 0:d78b00f167cb | 234 | p.x = (a_point.x - x_off) / pp_tx; |
dreschpe | 2:ef7972c29c0e | 235 | if (p.x > width()) p.x = width(); |
dreschpe | 0:d78b00f167cb | 236 | p.y = (a_point.y - y_off) / pp_ty; |
dreschpe | 2:ef7972c29c0e | 237 | if (p.y > height()) p.y = height(); |
TickTock | 4:a3cd26c97b76 | 238 | //locate(1,60); //debug |
TickTock | 4:a3cd26c97b76 | 239 | //printf("x: %d\ty: %d",p.x,p.y); |
dreschpe | 0:d78b00f167cb | 240 | return (p); |
dreschpe | 0:d78b00f167cb | 241 | } |
dreschpe | 0:d78b00f167cb | 242 | |
TickTock | 4:a3cd26c97b76 | 243 | bool TOUCH_TFTx2::is_touched(void) { |
TickTock | 4:a3cd26c97b76 | 244 | unsigned short y1; |
TickTock | 4:a3cd26c97b76 | 245 | // read y voltage |
TickTock | 4:a3cd26c97b76 | 246 | _ym.input(); //y- has to be passive |
TickTock | 4:a3cd26c97b76 | 247 | _yp.input(); //y+ has to be passive |
TickTock | 4:a3cd26c97b76 | 248 | _ym.mode(PullUp); |
TickTock | 4:a3cd26c97b76 | 249 | _yp.mode(PullUp); |
TickTock | 4:a3cd26c97b76 | 250 | _xp.output(); |
TickTock | 4:a3cd26c97b76 | 251 | _xm.output(); |
TickTock | 4:a3cd26c97b76 | 252 | _xp = 0; //drive x+ low |
TickTock | 4:a3cd26c97b76 | 253 | _xm = 0; //drive x- low |
TickTock | 4:a3cd26c97b76 | 254 | AnalogIn Ay(ya); // we have to call the constructor to switch to analog mode |
TickTock | 4:a3cd26c97b76 | 255 | wait_us(10); |
TickTock | 4:a3cd26c97b76 | 256 | y1 = Ay.read_u16(); // get y voltage |
TickTock | 4:a3cd26c97b76 | 257 | return (y1<threshold); |
dreschpe | 0:d78b00f167cb | 258 | } |