LIB for resistiv touchscreen connected to 4 mbed pins Use SPI_TFT lib
Fork of Touch_tft by
touch_tft.cpp@3:1c2df48aa2eb, 2013-06-26 (annotated)
- Committer:
- leihen
- Date:
- Wed Jun 26 21:20:30 2013 +0000
- Revision:
- 3:1c2df48aa2eb
- Parent:
- 2:ef7972c29c0e
- Child:
- 4:330f2f16dabb
Preliminary Version used in HeizungsController with EthernetInterface. Before forking for the WiFly Version.
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 | |
dreschpe | 0:d78b00f167cb | 16 | #include "touch_tft.h" |
dreschpe | 0:d78b00f167cb | 17 | #include "mbed.h" |
dreschpe | 0:d78b00f167cb | 18 | |
dreschpe | 0:d78b00f167cb | 19 | #define threshold 0x2000 // threshold to detect pressed |
dreschpe | 0:d78b00f167cb | 20 | |
dreschpe | 0:d78b00f167cb | 21 | touch_tft::touch_tft(PinName xp, PinName xm, PinName yp, PinName ym, |
dreschpe | 0:d78b00f167cb | 22 | PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset,const char* name): |
dreschpe | 0:d78b00f167cb | 23 | _xp(xp),_xm(xm),_yp(yp),_ym(ym),_ax(xp),_ay(yp), |
dreschpe | 0:d78b00f167cb | 24 | SPI_TFT(mosi,miso,sclk,cs,reset,name) { |
dreschpe | 0:d78b00f167cb | 25 | xa = xp; |
leihen | 3:1c2df48aa2eb | 26 | xb = xm; |
dreschpe | 0:d78b00f167cb | 27 | ya = yp; |
leihen | 3:1c2df48aa2eb | 28 | yb = ym; |
dreschpe | 0:d78b00f167cb | 29 | |
dreschpe | 0:d78b00f167cb | 30 | } |
leihen | 3:1c2df48aa2eb | 31 | char Buffer[30]; |
dreschpe | 0:d78b00f167cb | 32 | |
dreschpe | 0:d78b00f167cb | 33 | point touch_tft::get_touch() { |
dreschpe | 0:d78b00f167cb | 34 | unsigned short x1 = 0,x2 = 0, y1 = 0, y2 = 0; |
leihen | 3:1c2df48aa2eb | 35 | int s1 = 0,s2 = 0,d1 , d2; |
dreschpe | 0:d78b00f167cb | 36 | point p; |
dreschpe | 0:d78b00f167cb | 37 | |
dreschpe | 0:d78b00f167cb | 38 | do { |
dreschpe | 0:d78b00f167cb | 39 | // read y voltage |
leihen | 3:1c2df48aa2eb | 40 | // DigitalOut __xp(xa); |
dreschpe | 0:d78b00f167cb | 41 | _xp.output(); |
leihen | 3:1c2df48aa2eb | 42 | // DigitalOut __xm(xb); |
dreschpe | 0:d78b00f167cb | 43 | _xm.output(); |
dreschpe | 0:d78b00f167cb | 44 | switch (orientation) { |
dreschpe | 0:d78b00f167cb | 45 | case(0): |
dreschpe | 0:d78b00f167cb | 46 | case(3): |
dreschpe | 0:d78b00f167cb | 47 | _xp = 1; |
dreschpe | 0:d78b00f167cb | 48 | _xm = 0; |
leihen | 3:1c2df48aa2eb | 49 | // __xp = 1; |
leihen | 3:1c2df48aa2eb | 50 | // __xm = 0; |
dreschpe | 0:d78b00f167cb | 51 | break; |
dreschpe | 0:d78b00f167cb | 52 | case(1): |
dreschpe | 0:d78b00f167cb | 53 | case(2): |
dreschpe | 0:d78b00f167cb | 54 | _xp = 0; |
dreschpe | 0:d78b00f167cb | 55 | _xm = 1; |
leihen | 3:1c2df48aa2eb | 56 | // __xp = 0; |
leihen | 3:1c2df48aa2eb | 57 | // __xm = 1; |
dreschpe | 0:d78b00f167cb | 58 | break; |
dreschpe | 0:d78b00f167cb | 59 | } |
leihen | 3:1c2df48aa2eb | 60 | // DigitalIn __ym(yb); |
leihen | 3:1c2df48aa2eb | 61 | // __ym.mode(PullNone); |
dreschpe | 0:d78b00f167cb | 62 | _ym.input(); // y- have to be passive |
leihen | 3:1c2df48aa2eb | 63 | _ym.mode(PullNone); |
dreschpe | 0:d78b00f167cb | 64 | AnalogIn Ay(ya); // we have to call the constructor to switch to analog mode |
dreschpe | 0:d78b00f167cb | 65 | wait_us(10); |
dreschpe | 0:d78b00f167cb | 66 | y1 = Ay.read_u16(); // get y voltage |
leihen | 3:1c2df48aa2eb | 67 | y1 = Ay.read_u16(); |
dreschpe | 0:d78b00f167cb | 68 | d1 = (y1 > y2)? (y1-y2) : (y2-y1); |
leihen | 3:1c2df48aa2eb | 69 | if (((y1 < 8000) && (d1 < 2000)) || ((y1 > 8000) && (d1 < 250))) s1 ++; |
dreschpe | 0:d78b00f167cb | 70 | else { |
dreschpe | 0:d78b00f167cb | 71 | if (s1 > 0) s1 --; |
dreschpe | 0:d78b00f167cb | 72 | } |
leihen | 3:1c2df48aa2eb | 73 | // debug |
leihen | 3:1c2df48aa2eb | 74 | locate(1,7); |
leihen | 3:1c2df48aa2eb | 75 | // sprintf(Buffer, "d: %4d y: %5d s1: %4d",d1,y1,s1); |
leihen | 3:1c2df48aa2eb | 76 | sprintf(Buffer, "d: %5d y1: %5d y2: %5d s1: %5d",d1,y1,y2,s1); |
leihen | 3:1c2df48aa2eb | 77 | printf(Buffer); |
dreschpe | 0:d78b00f167cb | 78 | y2 = y1; |
leihen | 3:1c2df48aa2eb | 79 | |
dreschpe | 0:d78b00f167cb | 80 | // read x voltage |
leihen | 3:1c2df48aa2eb | 81 | // DigitalOut __yp(ya); |
leihen | 3:1c2df48aa2eb | 82 | // DigitalOut __ym1(yb); |
dreschpe | 0:d78b00f167cb | 83 | _yp.output(); |
dreschpe | 0:d78b00f167cb | 84 | _ym.output(); |
dreschpe | 0:d78b00f167cb | 85 | switch (orientation) { |
dreschpe | 0:d78b00f167cb | 86 | case(0): |
dreschpe | 0:d78b00f167cb | 87 | case(1): |
dreschpe | 0:d78b00f167cb | 88 | _yp = 1; |
dreschpe | 0:d78b00f167cb | 89 | _ym = 0; |
leihen | 3:1c2df48aa2eb | 90 | // __yp = 1; |
leihen | 3:1c2df48aa2eb | 91 | // __ym1 = 0; |
dreschpe | 0:d78b00f167cb | 92 | break; |
dreschpe | 0:d78b00f167cb | 93 | case(2): |
dreschpe | 0:d78b00f167cb | 94 | case(3): |
dreschpe | 0:d78b00f167cb | 95 | _yp = 0; |
dreschpe | 0:d78b00f167cb | 96 | _ym = 1; |
leihen | 3:1c2df48aa2eb | 97 | // __yp = 0; |
leihen | 3:1c2df48aa2eb | 98 | // __ym1 = 1; |
dreschpe | 0:d78b00f167cb | 99 | break; |
dreschpe | 0:d78b00f167cb | 100 | } |
leihen | 3:1c2df48aa2eb | 101 | // DigitalIn __ixm(xb); |
dreschpe | 0:d78b00f167cb | 102 | _xm.input(); // x- have to be passive |
leihen | 3:1c2df48aa2eb | 103 | // _xm.mode(PullNone); |
dreschpe | 0:d78b00f167cb | 104 | AnalogIn Ax(xa); // we have to call the constructor to switch to analog mode |
dreschpe | 0:d78b00f167cb | 105 | wait_us(10); |
dreschpe | 0:d78b00f167cb | 106 | x1 = Ax.read_u16(); // get x voltage |
leihen | 3:1c2df48aa2eb | 107 | x1 = Ax.read_u16(); |
dreschpe | 0:d78b00f167cb | 108 | d2 = (x1 > x2)? (x1-x2) : (x2-x1); |
leihen | 3:1c2df48aa2eb | 109 | if (((x1 < 8000) && (d2 < 2000)) || ((x1 > 8000) && (d2 < 250))) s2 ++; |
dreschpe | 0:d78b00f167cb | 110 | else { |
dreschpe | 0:d78b00f167cb | 111 | if (s2 > 0) s2 --; |
dreschpe | 0:d78b00f167cb | 112 | } |
dreschpe | 0:d78b00f167cb | 113 | x2 = x1; |
dreschpe | 0:d78b00f167cb | 114 | // debug |
leihen | 3:1c2df48aa2eb | 115 | locate(1,20); |
leihen | 3:1c2df48aa2eb | 116 | sprintf(Buffer, "d: %5d x1: %5d x2: %5d s1: %4d",d2,x1,x2, s2); |
leihen | 3:1c2df48aa2eb | 117 | printf(Buffer); |
leihen | 3:1c2df48aa2eb | 118 | //printf("d: %4d x: %5d s2: %4d",d2,x1,s2); |
dreschpe | 0:d78b00f167cb | 119 | |
dreschpe | 0:d78b00f167cb | 120 | } while (s1 < 3 || s2 < 3); // read until we have three samples close together |
dreschpe | 0:d78b00f167cb | 121 | switch (orientation) { |
dreschpe | 0:d78b00f167cb | 122 | case(0): |
dreschpe | 0:d78b00f167cb | 123 | case(2): |
dreschpe | 0:d78b00f167cb | 124 | p.y = (x1+x2) / 2; // average of two sample |
dreschpe | 0:d78b00f167cb | 125 | p.x = (y1+y2) / 2; |
dreschpe | 0:d78b00f167cb | 126 | break; |
dreschpe | 0:d78b00f167cb | 127 | case(1): |
dreschpe | 0:d78b00f167cb | 128 | case(3): |
dreschpe | 0:d78b00f167cb | 129 | p.x = (x1+x2) / 2; // average of two sample |
dreschpe | 0:d78b00f167cb | 130 | p.y = (y1+y2) / 2; |
dreschpe | 0:d78b00f167cb | 131 | break; |
dreschpe | 0:d78b00f167cb | 132 | } |
dreschpe | 0:d78b00f167cb | 133 | return(p); |
dreschpe | 0:d78b00f167cb | 134 | } |
dreschpe | 0:d78b00f167cb | 135 | |
dreschpe | 0:d78b00f167cb | 136 | void touch_tft::calibrate(void) { |
dreschpe | 0:d78b00f167cb | 137 | int i; |
dreschpe | 0:d78b00f167cb | 138 | int a = 0,b = 0,c = 0, d = 0; |
dreschpe | 2:ef7972c29c0e | 139 | int pos_x, pos_y; |
dreschpe | 0:d78b00f167cb | 140 | point p; |
dreschpe | 0:d78b00f167cb | 141 | |
dreschpe | 0:d78b00f167cb | 142 | cls(); |
dreschpe | 0:d78b00f167cb | 143 | line(0,3,6,3,White); |
dreschpe | 0:d78b00f167cb | 144 | line(3,0,3,6,White); |
dreschpe | 2:ef7972c29c0e | 145 | |
dreschpe | 2:ef7972c29c0e | 146 | // get the center of the screen |
dreschpe | 2:ef7972c29c0e | 147 | pos_x = columns() / 2 - 3; |
dreschpe | 2:ef7972c29c0e | 148 | pos_x = pos_x * font[1]; |
dreschpe | 2:ef7972c29c0e | 149 | pos_y = (rows() / 2) - 1; |
dreschpe | 2:ef7972c29c0e | 150 | pos_y = pos_y * font[2]; |
dreschpe | 2:ef7972c29c0e | 151 | |
dreschpe | 2:ef7972c29c0e | 152 | locate(pos_x,pos_y); |
dreschpe | 0:d78b00f167cb | 153 | printf("press cross"); |
dreschpe | 2:ef7972c29c0e | 154 | locate(pos_x,pos_y + font[2]); |
dreschpe | 0:d78b00f167cb | 155 | printf("to calibrate"); |
dreschpe | 0:d78b00f167cb | 156 | for (i=0; i<5; i++) { |
dreschpe | 0:d78b00f167cb | 157 | do { |
dreschpe | 0:d78b00f167cb | 158 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 159 | } while (p.x < 0x2000 | p.y < 0x2000); // wait for touch |
dreschpe | 0:d78b00f167cb | 160 | a += p.x; |
dreschpe | 0:d78b00f167cb | 161 | b += p.y; |
dreschpe | 0:d78b00f167cb | 162 | } |
dreschpe | 0:d78b00f167cb | 163 | a = a / 5; |
dreschpe | 0:d78b00f167cb | 164 | b = b / 5; |
dreschpe | 2:ef7972c29c0e | 165 | locate(pos_x,pos_y); |
dreschpe | 0:d78b00f167cb | 166 | printf("OK "); |
dreschpe | 0:d78b00f167cb | 167 | do { |
dreschpe | 0:d78b00f167cb | 168 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 169 | } while (p.y > 0x2000 | p.x > 0x2000); // wait for no touch |
dreschpe | 0:d78b00f167cb | 170 | |
dreschpe | 0:d78b00f167cb | 171 | cls(); |
dreschpe | 0:d78b00f167cb | 172 | line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross |
dreschpe | 0:d78b00f167cb | 173 | line(width() - 8,height() - 5,width() - 1,height() - 5,White); |
dreschpe | 2:ef7972c29c0e | 174 | locate(pos_x,pos_y); |
dreschpe | 0:d78b00f167cb | 175 | printf("press cross"); |
dreschpe | 2:ef7972c29c0e | 176 | locate(pos_x,pos_y + font[2]); |
dreschpe | 0:d78b00f167cb | 177 | printf("to calibrate"); |
dreschpe | 0:d78b00f167cb | 178 | for (i=0; i<5; i++) { |
dreschpe | 0:d78b00f167cb | 179 | do { |
dreschpe | 0:d78b00f167cb | 180 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 181 | } while (p.y < 0x2000 | p.x < 0x2000); // wait for touch |
dreschpe | 0:d78b00f167cb | 182 | c+= p.x; |
dreschpe | 0:d78b00f167cb | 183 | d+= p.y; |
dreschpe | 0:d78b00f167cb | 184 | } |
dreschpe | 0:d78b00f167cb | 185 | c = c / 5; |
dreschpe | 0:d78b00f167cb | 186 | d = d / 5; |
dreschpe | 0:d78b00f167cb | 187 | |
dreschpe | 2:ef7972c29c0e | 188 | locate(pos_x, pos_y); |
dreschpe | 0:d78b00f167cb | 189 | printf("OK "); |
dreschpe | 0:d78b00f167cb | 190 | do { |
dreschpe | 0:d78b00f167cb | 191 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 192 | } while (p.y > 0x2000 | p.x > 0x2000); // wait for no touch |
dreschpe | 0:d78b00f167cb | 193 | |
dreschpe | 0:d78b00f167cb | 194 | cls(); |
dreschpe | 0:d78b00f167cb | 195 | |
dreschpe | 0:d78b00f167cb | 196 | x_off = a; |
dreschpe | 0:d78b00f167cb | 197 | y_off = b; |
dreschpe | 0:d78b00f167cb | 198 | |
dreschpe | 0:d78b00f167cb | 199 | i = c-a; // delta x |
dreschpe | 0:d78b00f167cb | 200 | pp_tx = i / (width() - 6); |
dreschpe | 0:d78b00f167cb | 201 | |
dreschpe | 0:d78b00f167cb | 202 | i = d-b; // delta y |
dreschpe | 0:d78b00f167cb | 203 | pp_ty = i / (height() - 6); |
dreschpe | 0:d78b00f167cb | 204 | } |
dreschpe | 0:d78b00f167cb | 205 | |
dreschpe | 0:d78b00f167cb | 206 | |
dreschpe | 0:d78b00f167cb | 207 | point touch_tft::to_pixel(point a_point) { |
dreschpe | 0:d78b00f167cb | 208 | point p; |
dreschpe | 0:d78b00f167cb | 209 | |
dreschpe | 0:d78b00f167cb | 210 | p.x = (a_point.x - x_off) / pp_tx; |
dreschpe | 2:ef7972c29c0e | 211 | if (p.x > width()) p.x = width(); |
dreschpe | 0:d78b00f167cb | 212 | p.y = (a_point.y - y_off) / pp_ty; |
dreschpe | 2:ef7972c29c0e | 213 | if (p.y > height()) p.y = height(); |
dreschpe | 0:d78b00f167cb | 214 | return (p); |
dreschpe | 0:d78b00f167cb | 215 | } |
dreschpe | 0:d78b00f167cb | 216 | |
dreschpe | 2:ef7972c29c0e | 217 | bool touch_tft::is_touched(point a) { |
dreschpe | 2:ef7972c29c0e | 218 | if (a.x > threshold & a.y > threshold) return(true); |
dreschpe | 2:ef7972c29c0e | 219 | else return(false); |
dreschpe | 0:d78b00f167cb | 220 | } |