LIB for resistiv touchscreen connected to 4 mbed pins Use SPI_TFT lib
Dependents: touch LCD_Grapher Mandelbrot Tactile ... more
touch_tft.cpp@0:d78b00f167cb, 2011-07-14 (annotated)
- Committer:
- dreschpe
- Date:
- Thu Jul 14 21:02:43 2011 +0000
- Revision:
- 0:d78b00f167cb
- Child:
- 1:1745fdf054b5
0.5
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; |
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 | 0:d78b00f167cb | 113 | int pos; |
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 | 0:d78b00f167cb | 119 | pos = columns() / 2 - 5; |
dreschpe | 0:d78b00f167cb | 120 | locate(pos,5); |
dreschpe | 0:d78b00f167cb | 121 | printf("press cross"); |
dreschpe | 0:d78b00f167cb | 122 | locate(pos,6); |
dreschpe | 0:d78b00f167cb | 123 | printf("to calibrate"); |
dreschpe | 0:d78b00f167cb | 124 | for (i=0; i<5; i++) { |
dreschpe | 0:d78b00f167cb | 125 | do { |
dreschpe | 0:d78b00f167cb | 126 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 127 | } while (p.x < 0x2000 | p.y < 0x2000); // wait for touch |
dreschpe | 0:d78b00f167cb | 128 | a += p.x; |
dreschpe | 0:d78b00f167cb | 129 | b += p.y; |
dreschpe | 0:d78b00f167cb | 130 | } |
dreschpe | 0:d78b00f167cb | 131 | a = a / 5; |
dreschpe | 0:d78b00f167cb | 132 | b = b / 5; |
dreschpe | 0:d78b00f167cb | 133 | locate(pos,5); |
dreschpe | 0:d78b00f167cb | 134 | printf("OK "); |
dreschpe | 0:d78b00f167cb | 135 | do { |
dreschpe | 0:d78b00f167cb | 136 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 137 | } while (p.y > 0x2000 | p.x > 0x2000); // wait for no touch |
dreschpe | 0:d78b00f167cb | 138 | |
dreschpe | 0:d78b00f167cb | 139 | cls(); |
dreschpe | 0:d78b00f167cb | 140 | line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross |
dreschpe | 0:d78b00f167cb | 141 | line(width() - 8,height() - 5,width() - 1,height() - 5,White); |
dreschpe | 0:d78b00f167cb | 142 | locate(pos,5); |
dreschpe | 0:d78b00f167cb | 143 | printf("press cross"); |
dreschpe | 0:d78b00f167cb | 144 | locate(pos,6); |
dreschpe | 0:d78b00f167cb | 145 | printf("to calibrate"); |
dreschpe | 0:d78b00f167cb | 146 | for (i=0; i<5; i++) { |
dreschpe | 0:d78b00f167cb | 147 | do { |
dreschpe | 0:d78b00f167cb | 148 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 149 | } while (p.y < 0x2000 | p.x < 0x2000); // wait for touch |
dreschpe | 0:d78b00f167cb | 150 | c+= p.x; |
dreschpe | 0:d78b00f167cb | 151 | d+= p.y; |
dreschpe | 0:d78b00f167cb | 152 | } |
dreschpe | 0:d78b00f167cb | 153 | c = c / 5; |
dreschpe | 0:d78b00f167cb | 154 | d = d / 5; |
dreschpe | 0:d78b00f167cb | 155 | |
dreschpe | 0:d78b00f167cb | 156 | locate(pos,5); |
dreschpe | 0:d78b00f167cb | 157 | printf("OK "); |
dreschpe | 0:d78b00f167cb | 158 | do { |
dreschpe | 0:d78b00f167cb | 159 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 160 | } while (p.y > 0x2000 | p.x > 0x2000); // wait for no touch |
dreschpe | 0:d78b00f167cb | 161 | |
dreschpe | 0:d78b00f167cb | 162 | cls(); |
dreschpe | 0:d78b00f167cb | 163 | |
dreschpe | 0:d78b00f167cb | 164 | x_off = a; |
dreschpe | 0:d78b00f167cb | 165 | y_off = b; |
dreschpe | 0:d78b00f167cb | 166 | |
dreschpe | 0:d78b00f167cb | 167 | i = c-a; // delta x |
dreschpe | 0:d78b00f167cb | 168 | pp_tx = i / (width() - 6); |
dreschpe | 0:d78b00f167cb | 169 | |
dreschpe | 0:d78b00f167cb | 170 | i = d-b; // delta y |
dreschpe | 0:d78b00f167cb | 171 | pp_ty = i / (height() - 6); |
dreschpe | 0:d78b00f167cb | 172 | } |
dreschpe | 0:d78b00f167cb | 173 | |
dreschpe | 0:d78b00f167cb | 174 | |
dreschpe | 0:d78b00f167cb | 175 | point touch_tft::to_pixel(point a_point) { |
dreschpe | 0:d78b00f167cb | 176 | point p; |
dreschpe | 0:d78b00f167cb | 177 | |
dreschpe | 0:d78b00f167cb | 178 | p.x = (a_point.x - x_off) / pp_tx; |
dreschpe | 0:d78b00f167cb | 179 | if(p.x > width()) p.x = 0; |
dreschpe | 0:d78b00f167cb | 180 | p.y = (a_point.y - y_off) / pp_ty; |
dreschpe | 0:d78b00f167cb | 181 | if(p.y > height()) p.y = 0; |
dreschpe | 0:d78b00f167cb | 182 | return (p); |
dreschpe | 0:d78b00f167cb | 183 | } |
dreschpe | 0:d78b00f167cb | 184 | |
dreschpe | 0:d78b00f167cb | 185 | bool is_touched(point a){ |
dreschpe | 0:d78b00f167cb | 186 | if (a.x > threshold & a.y > threshold) return(true); |
dreschpe | 0:d78b00f167cb | 187 | else return(false); |
dreschpe | 0:d78b00f167cb | 188 | } |