LIB for resistiv touchscreen connected to 4 mbed pins Use SPI_TFT lib

Dependents:   touch LCD_Grapher Mandelbrot Tactile ... more

Committer:
dreschpe
Date:
Sun Sep 04 21:23:31 2011 +0000
Revision:
2:ef7972c29c0e
Parent:
1:1745fdf054b5
Fix bug from SPI_TFT lib changes

Who changed what in which revision?

UserRevisionLine numberNew 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;
dreschpe 0:d78b00f167cb 26 ya = yp;
dreschpe 0:d78b00f167cb 27
dreschpe 0:d78b00f167cb 28 }
dreschpe 0:d78b00f167cb 29
dreschpe 0:d78b00f167cb 30 point touch_tft::get_touch() {
dreschpe 0:d78b00f167cb 31 unsigned short x1 = 0,x2 = 0, y1 = 0, y2 = 0;
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
dreschpe 0:d78b00f167cb 37 _xp.output();
dreschpe 0:d78b00f167cb 38 _xm.output();
dreschpe 0:d78b00f167cb 39 switch (orientation) {
dreschpe 0:d78b00f167cb 40 case(0):
dreschpe 0:d78b00f167cb 41 case(3):
dreschpe 0:d78b00f167cb 42 _xp = 1;
dreschpe 0:d78b00f167cb 43 _xm = 0;
dreschpe 0:d78b00f167cb 44 break;
dreschpe 0:d78b00f167cb 45 case(1):
dreschpe 0:d78b00f167cb 46 case(2):
dreschpe 0:d78b00f167cb 47 _xp = 0;
dreschpe 0:d78b00f167cb 48 _xm = 1;
dreschpe 0:d78b00f167cb 49 break;
dreschpe 0:d78b00f167cb 50 }
dreschpe 0:d78b00f167cb 51 _ym.input(); // y- have to be passive
dreschpe 0:d78b00f167cb 52 AnalogIn Ay(ya); // we have to call the constructor to switch to analog mode
dreschpe 0:d78b00f167cb 53 wait_us(10);
dreschpe 0:d78b00f167cb 54 y1 = Ay.read_u16(); // get y voltage
dreschpe 0:d78b00f167cb 55 d1 = (y1 > y2)? (y1-y2) : (y2-y1);
dreschpe 0:d78b00f167cb 56 if (((y1 < 8000) && (d1 < 2000)) || ((y1 > 8000) && (d1 < 150))) s1 ++;
dreschpe 0:d78b00f167cb 57 else {
dreschpe 0:d78b00f167cb 58 if (s1 > 0) s1 --;
dreschpe 0:d78b00f167cb 59 }
dreschpe 0:d78b00f167cb 60 y2 = y1;
dreschpe 0:d78b00f167cb 61 // debug
dreschpe 0:d78b00f167cb 62 //locate(1,7);
dreschpe 0:d78b00f167cb 63 //printf("d: %4d y: %5d s1: %4d",d1,y1,s1);
dreschpe 0:d78b00f167cb 64
dreschpe 0:d78b00f167cb 65 // read x voltage
dreschpe 0:d78b00f167cb 66 _yp.output();
dreschpe 0:d78b00f167cb 67 _ym.output();
dreschpe 0:d78b00f167cb 68 switch (orientation) {
dreschpe 0:d78b00f167cb 69 case(0):
dreschpe 0:d78b00f167cb 70 case(1):
dreschpe 0:d78b00f167cb 71 _yp = 1;
dreschpe 0:d78b00f167cb 72 _ym = 0;
dreschpe 0:d78b00f167cb 73 break;
dreschpe 0:d78b00f167cb 74 case(2):
dreschpe 0:d78b00f167cb 75 case(3):
dreschpe 0:d78b00f167cb 76 _yp = 0;
dreschpe 0:d78b00f167cb 77 _ym = 1;
dreschpe 0:d78b00f167cb 78 break;
dreschpe 0:d78b00f167cb 79 }
dreschpe 0:d78b00f167cb 80 _xm.input(); // x- have to be passive
dreschpe 0:d78b00f167cb 81 AnalogIn Ax(xa); // we have to call the constructor to switch to analog mode
dreschpe 0:d78b00f167cb 82 wait_us(10);
dreschpe 0:d78b00f167cb 83 x1 = Ax.read_u16(); // get x voltage
dreschpe 0:d78b00f167cb 84 d2 = (x1 > x2)? (x1-x2) : (x2-x1);
dreschpe 0:d78b00f167cb 85 if (((x1 < 8000) && (d2 < 2000)) || ((x1 > 8000) && (d2 < 150))) s2 ++;
dreschpe 0:d78b00f167cb 86 else {
dreschpe 0:d78b00f167cb 87 if (s2 > 0) s2 --;
dreschpe 0:d78b00f167cb 88 }
dreschpe 0:d78b00f167cb 89 x2 = x1;
dreschpe 0:d78b00f167cb 90 // debug
dreschpe 0:d78b00f167cb 91 //locate(1,8);
dreschpe 0:d78b00f167cb 92 //printf("d: %4d x: %5d s2: %4d",d2,x1,s2);
dreschpe 0:d78b00f167cb 93
dreschpe 0:d78b00f167cb 94 } while (s1 < 3 || s2 < 3); // read until we have three samples close together
dreschpe 0:d78b00f167cb 95 switch (orientation) {
dreschpe 0:d78b00f167cb 96 case(0):
dreschpe 0:d78b00f167cb 97 case(2):
dreschpe 0:d78b00f167cb 98 p.y = (x1+x2) / 2; // average of two sample
dreschpe 0:d78b00f167cb 99 p.x = (y1+y2) / 2;
dreschpe 0:d78b00f167cb 100 break;
dreschpe 0:d78b00f167cb 101 case(1):
dreschpe 0:d78b00f167cb 102 case(3):
dreschpe 0:d78b00f167cb 103 p.x = (x1+x2) / 2; // average of two sample
dreschpe 0:d78b00f167cb 104 p.y = (y1+y2) / 2;
dreschpe 0:d78b00f167cb 105 break;
dreschpe 0:d78b00f167cb 106 }
dreschpe 0:d78b00f167cb 107 return(p);
dreschpe 0:d78b00f167cb 108 }
dreschpe 0:d78b00f167cb 109
dreschpe 0:d78b00f167cb 110 void touch_tft::calibrate(void) {
dreschpe 0:d78b00f167cb 111 int i;
dreschpe 0:d78b00f167cb 112 int a = 0,b = 0,c = 0, d = 0;
dreschpe 2:ef7972c29c0e 113 int pos_x, pos_y;
dreschpe 0:d78b00f167cb 114 point p;
dreschpe 0:d78b00f167cb 115
dreschpe 0:d78b00f167cb 116 cls();
dreschpe 0:d78b00f167cb 117 line(0,3,6,3,White);
dreschpe 0:d78b00f167cb 118 line(3,0,3,6,White);
dreschpe 2:ef7972c29c0e 119
dreschpe 2:ef7972c29c0e 120 // get the center of the screen
dreschpe 2:ef7972c29c0e 121 pos_x = columns() / 2 - 3;
dreschpe 2:ef7972c29c0e 122 pos_x = pos_x * font[1];
dreschpe 2:ef7972c29c0e 123 pos_y = (rows() / 2) - 1;
dreschpe 2:ef7972c29c0e 124 pos_y = pos_y * font[2];
dreschpe 2:ef7972c29c0e 125
dreschpe 2:ef7972c29c0e 126 locate(pos_x,pos_y);
dreschpe 0:d78b00f167cb 127 printf("press cross");
dreschpe 2:ef7972c29c0e 128 locate(pos_x,pos_y + font[2]);
dreschpe 0:d78b00f167cb 129 printf("to calibrate");
dreschpe 0:d78b00f167cb 130 for (i=0; i<5; i++) {
dreschpe 0:d78b00f167cb 131 do {
dreschpe 0:d78b00f167cb 132 p = get_touch();
dreschpe 0:d78b00f167cb 133 } while (p.x < 0x2000 | p.y < 0x2000); // wait for touch
dreschpe 0:d78b00f167cb 134 a += p.x;
dreschpe 0:d78b00f167cb 135 b += p.y;
dreschpe 0:d78b00f167cb 136 }
dreschpe 0:d78b00f167cb 137 a = a / 5;
dreschpe 0:d78b00f167cb 138 b = b / 5;
dreschpe 2:ef7972c29c0e 139 locate(pos_x,pos_y);
dreschpe 0:d78b00f167cb 140 printf("OK ");
dreschpe 0:d78b00f167cb 141 do {
dreschpe 0:d78b00f167cb 142 p = get_touch();
dreschpe 0:d78b00f167cb 143 } while (p.y > 0x2000 | p.x > 0x2000); // wait for no touch
dreschpe 0:d78b00f167cb 144
dreschpe 0:d78b00f167cb 145 cls();
dreschpe 0:d78b00f167cb 146 line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross
dreschpe 0:d78b00f167cb 147 line(width() - 8,height() - 5,width() - 1,height() - 5,White);
dreschpe 2:ef7972c29c0e 148 locate(pos_x,pos_y);
dreschpe 0:d78b00f167cb 149 printf("press cross");
dreschpe 2:ef7972c29c0e 150 locate(pos_x,pos_y + font[2]);
dreschpe 0:d78b00f167cb 151 printf("to calibrate");
dreschpe 0:d78b00f167cb 152 for (i=0; i<5; i++) {
dreschpe 0:d78b00f167cb 153 do {
dreschpe 0:d78b00f167cb 154 p = get_touch();
dreschpe 0:d78b00f167cb 155 } while (p.y < 0x2000 | p.x < 0x2000); // wait for touch
dreschpe 0:d78b00f167cb 156 c+= p.x;
dreschpe 0:d78b00f167cb 157 d+= p.y;
dreschpe 0:d78b00f167cb 158 }
dreschpe 0:d78b00f167cb 159 c = c / 5;
dreschpe 0:d78b00f167cb 160 d = d / 5;
dreschpe 0:d78b00f167cb 161
dreschpe 2:ef7972c29c0e 162 locate(pos_x, pos_y);
dreschpe 0:d78b00f167cb 163 printf("OK ");
dreschpe 0:d78b00f167cb 164 do {
dreschpe 0:d78b00f167cb 165 p = get_touch();
dreschpe 0:d78b00f167cb 166 } while (p.y > 0x2000 | p.x > 0x2000); // wait for no touch
dreschpe 0:d78b00f167cb 167
dreschpe 0:d78b00f167cb 168 cls();
dreschpe 0:d78b00f167cb 169
dreschpe 0:d78b00f167cb 170 x_off = a;
dreschpe 0:d78b00f167cb 171 y_off = b;
dreschpe 0:d78b00f167cb 172
dreschpe 0:d78b00f167cb 173 i = c-a; // delta x
dreschpe 0:d78b00f167cb 174 pp_tx = i / (width() - 6);
dreschpe 0:d78b00f167cb 175
dreschpe 0:d78b00f167cb 176 i = d-b; // delta y
dreschpe 0:d78b00f167cb 177 pp_ty = i / (height() - 6);
dreschpe 0:d78b00f167cb 178 }
dreschpe 0:d78b00f167cb 179
dreschpe 0:d78b00f167cb 180
dreschpe 0:d78b00f167cb 181 point touch_tft::to_pixel(point a_point) {
dreschpe 0:d78b00f167cb 182 point p;
dreschpe 0:d78b00f167cb 183
dreschpe 0:d78b00f167cb 184 p.x = (a_point.x - x_off) / pp_tx;
dreschpe 2:ef7972c29c0e 185 if (p.x > width()) p.x = width();
dreschpe 0:d78b00f167cb 186 p.y = (a_point.y - y_off) / pp_ty;
dreschpe 2:ef7972c29c0e 187 if (p.y > height()) p.y = height();
dreschpe 0:d78b00f167cb 188 return (p);
dreschpe 0:d78b00f167cb 189 }
dreschpe 0:d78b00f167cb 190
dreschpe 2:ef7972c29c0e 191 bool touch_tft::is_touched(point a) {
dreschpe 2:ef7972c29c0e 192 if (a.x > threshold & a.y > threshold) return(true);
dreschpe 2:ef7972c29c0e 193 else return(false);
dreschpe 0:d78b00f167cb 194 }