SeedStdudioTFT Library
Fork of SeeedStudioTFTv2 by
SeeedStudioTFTv2.cpp@4:4542d1ff81e4, 2013-10-19 (annotated)
- Committer:
- mazgch
- Date:
- Sat Oct 19 20:51:48 2013 +0000
- Revision:
- 4:4542d1ff81e4
- Child:
- 5:366bdc7a8315
SeeedStudioTFTv2 shield with Touch support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 4:4542d1ff81e4 | 1 | /* mbed library for resistive touch pads |
mazgch | 4:4542d1ff81e4 | 2 | * uses 4 pins - 2 IO and 2 Analog |
mazgch | 4:4542d1ff81e4 | 3 | |
mazgch | 4:4542d1ff81e4 | 4 | * c 2011 Peter Drescher - DC2PD |
mazgch | 4:4542d1ff81e4 | 5 | * |
mazgch | 4:4542d1ff81e4 | 6 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mazgch | 4:4542d1ff81e4 | 7 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mazgch | 4:4542d1ff81e4 | 8 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mazgch | 4:4542d1ff81e4 | 9 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mazgch | 4:4542d1ff81e4 | 10 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mazgch | 4:4542d1ff81e4 | 11 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mazgch | 4:4542d1ff81e4 | 12 | * THE SOFTWARE. |
mazgch | 4:4542d1ff81e4 | 13 | */ |
mazgch | 4:4542d1ff81e4 | 14 | |
mazgch | 4:4542d1ff81e4 | 15 | |
mazgch | 4:4542d1ff81e4 | 16 | #include "mbed.h" |
mazgch | 4:4542d1ff81e4 | 17 | #include "SeeedStudioTFTv2.h" |
mazgch | 4:4542d1ff81e4 | 18 | |
mazgch | 4:4542d1ff81e4 | 19 | SeeedStudioTFTv2::SeeedStudioTFTv2(PinName xp, PinName xm, PinName yp, PinName ym, |
mazgch | 4:4542d1ff81e4 | 20 | PinName mosi, PinName miso, PinName sclk, |
mazgch | 4:4542d1ff81e4 | 21 | PinName csTft, PinName rstTft, PinName dcTft, PinName blTft, |
mazgch | 4:4542d1ff81e4 | 22 | PinName csSd): |
mazgch | 4:4542d1ff81e4 | 23 | SPI_TFT_ILI9341(mosi,miso,sclk,csTft,rstTft,dcTft, "tft") |
mazgch | 4:4542d1ff81e4 | 24 | #ifdef USE_SDCARD |
mazgch | 4:4542d1ff81e4 | 25 | ,SDFileSystem(mosi,miso,sclk,csSd, "sdc") |
mazgch | 4:4542d1ff81e4 | 26 | #endif |
mazgch | 4:4542d1ff81e4 | 27 | { |
mazgch | 4:4542d1ff81e4 | 28 | #ifndef USE_SDCARD |
mazgch | 4:4542d1ff81e4 | 29 | // sd card |
mazgch | 4:4542d1ff81e4 | 30 | DigitalOut cs(csSd); |
mazgch | 4:4542d1ff81e4 | 31 | cs = 1; |
mazgch | 4:4542d1ff81e4 | 32 | #endif |
mazgch | 4:4542d1ff81e4 | 33 | // backlight |
mazgch | 4:4542d1ff81e4 | 34 | DigitalOut bl(blTft); |
mazgch | 4:4542d1ff81e4 | 35 | bl = 1; |
mazgch | 4:4542d1ff81e4 | 36 | // touch screen pins |
mazgch | 4:4542d1ff81e4 | 37 | _xp = xp; |
mazgch | 4:4542d1ff81e4 | 38 | _yp = yp; |
mazgch | 4:4542d1ff81e4 | 39 | _xm = xm; |
mazgch | 4:4542d1ff81e4 | 40 | _ym = ym; |
mazgch | 4:4542d1ff81e4 | 41 | // default touch calibration |
mazgch | 4:4542d1ff81e4 | 42 | // orientation // 0 1 2 3 |
mazgch | 4:4542d1ff81e4 | 43 | x_off = 108000; // 17252 16605 108755 108000 |
mazgch | 4:4542d1ff81e4 | 44 | y_off = 22000; // 22330 105819 97167 22000 |
mazgch | 4:4542d1ff81e4 | 45 | pp_tx = -291; // 378 289 -390 -291 |
mazgch | 4:4542d1ff81e4 | 46 | pp_ty = 356; // 261 -355 -239 356 |
mazgch | 4:4542d1ff81e4 | 47 | } |
mazgch | 4:4542d1ff81e4 | 48 | |
mazgch | 4:4542d1ff81e4 | 49 | int SeeedStudioTFTv2::readTouch(PinName p, PinName m, PinName a, PinName i) |
mazgch | 4:4542d1ff81e4 | 50 | { |
mazgch | 4:4542d1ff81e4 | 51 | DigitalOut _p(p); |
mazgch | 4:4542d1ff81e4 | 52 | _p = 1; |
mazgch | 4:4542d1ff81e4 | 53 | DigitalOut _m(m); |
mazgch | 4:4542d1ff81e4 | 54 | _m = 0; |
mazgch | 4:4542d1ff81e4 | 55 | AnalogIn _a(a); |
mazgch | 4:4542d1ff81e4 | 56 | DigitalIn _i(i); |
mazgch | 4:4542d1ff81e4 | 57 | wait_us(10); |
mazgch | 4:4542d1ff81e4 | 58 | return _a.read_u16(); |
mazgch | 4:4542d1ff81e4 | 59 | } |
mazgch | 4:4542d1ff81e4 | 60 | |
mazgch | 4:4542d1ff81e4 | 61 | SeeedStudioTFTv2::TOUCH SeeedStudioTFTv2::getTouch(point& p) |
mazgch | 4:4542d1ff81e4 | 62 | { |
mazgch | 4:4542d1ff81e4 | 63 | int y2 = readTouch(_xp,_xm,_yp,_ym); |
mazgch | 4:4542d1ff81e4 | 64 | int x2 = readTouch(_yp,_ym,_xp,_xm); |
mazgch | 4:4542d1ff81e4 | 65 | int y1 = readTouch(_xp,_xm,_yp,_ym); |
mazgch | 4:4542d1ff81e4 | 66 | int x1 = readTouch(_yp,_ym,_xp,_xm); |
mazgch | 4:4542d1ff81e4 | 67 | int xd = x1 - x2; |
mazgch | 4:4542d1ff81e4 | 68 | int yd = y1 - y2; |
mazgch | 4:4542d1ff81e4 | 69 | xd = (xd > 0) ? xd : -xd; |
mazgch | 4:4542d1ff81e4 | 70 | yd = (yd > 0) ? xd : -xd; |
mazgch | 4:4542d1ff81e4 | 71 | p.x = x1 + x2; |
mazgch | 4:4542d1ff81e4 | 72 | p.y = y1 + y2; |
mazgch | 4:4542d1ff81e4 | 73 | #if 0 |
mazgch | 4:4542d1ff81e4 | 74 | DigitalOut _p(_xp); |
mazgch | 4:4542d1ff81e4 | 75 | _p = 1; |
mazgch | 4:4542d1ff81e4 | 76 | DigitalOut _m(_ym); |
mazgch | 4:4542d1ff81e4 | 77 | _m = 0; |
mazgch | 4:4542d1ff81e4 | 78 | AnalogIn _ax(_xm); |
mazgch | 4:4542d1ff81e4 | 79 | AnalogIn _ay(_yp); |
mazgch | 4:4542d1ff81e4 | 80 | wait_us(20); |
mazgch | 4:4542d1ff81e4 | 81 | int ax = _ax.read_u16(); |
mazgch | 4:4542d1ff81e4 | 82 | int ay = _ay.read_u16(); |
mazgch | 4:4542d1ff81e4 | 83 | float z = 0; |
mazgch | 4:4542d1ff81e4 | 84 | z = (float)ay / ax / x / 2 * 0x10000; |
mazgch | 4:4542d1ff81e4 | 85 | #endif |
mazgch | 4:4542d1ff81e4 | 86 | const int th = 8000; |
mazgch | 4:4542d1ff81e4 | 87 | const int df = 100; |
mazgch | 4:4542d1ff81e4 | 88 | TOUCH touch; |
mazgch | 4:4542d1ff81e4 | 89 | if (x1 < th || x2 < th || |
mazgch | 4:4542d1ff81e4 | 90 | y1 < th || y2 < th) { |
mazgch | 4:4542d1ff81e4 | 91 | p.x = 0; |
mazgch | 4:4542d1ff81e4 | 92 | p.y = 0; |
mazgch | 4:4542d1ff81e4 | 93 | touch = NO; |
mazgch | 4:4542d1ff81e4 | 94 | } else if (xd > df || yd > df) { |
mazgch | 4:4542d1ff81e4 | 95 | touch = MAYBE; |
mazgch | 4:4542d1ff81e4 | 96 | } else { |
mazgch | 4:4542d1ff81e4 | 97 | touch = YES; |
mazgch | 4:4542d1ff81e4 | 98 | } |
mazgch | 4:4542d1ff81e4 | 99 | //locate(0,50); |
mazgch | 4:4542d1ff81e4 | 100 | //printf("x: %6i y: %6i",p.x,p.y); |
mazgch | 4:4542d1ff81e4 | 101 | return touch; |
mazgch | 4:4542d1ff81e4 | 102 | } |
mazgch | 4:4542d1ff81e4 | 103 | |
mazgch | 4:4542d1ff81e4 | 104 | void SeeedStudioTFTv2::calibrate(void) |
mazgch | 4:4542d1ff81e4 | 105 | { |
mazgch | 4:4542d1ff81e4 | 106 | int i; |
mazgch | 4:4542d1ff81e4 | 107 | int a = 0,b = 0,c = 0, d = 0; |
mazgch | 4:4542d1ff81e4 | 108 | int pos_x, pos_y; |
mazgch | 4:4542d1ff81e4 | 109 | point p; |
mazgch | 4:4542d1ff81e4 | 110 | |
mazgch | 4:4542d1ff81e4 | 111 | cls(); |
mazgch | 4:4542d1ff81e4 | 112 | foreground(White); // set chars to white |
mazgch | 4:4542d1ff81e4 | 113 | line(0,3,6,3,White); |
mazgch | 4:4542d1ff81e4 | 114 | line(3,0,3,6,White); |
mazgch | 4:4542d1ff81e4 | 115 | // get the center of the screen |
mazgch | 4:4542d1ff81e4 | 116 | pos_x = columns() / 2 - 3; |
mazgch | 4:4542d1ff81e4 | 117 | pos_x = pos_x * font[1]; |
mazgch | 4:4542d1ff81e4 | 118 | pos_y = (rows() / 2) - 1; |
mazgch | 4:4542d1ff81e4 | 119 | pos_y = pos_y * font[2]; |
mazgch | 4:4542d1ff81e4 | 120 | locate(pos_x,pos_y); |
mazgch | 4:4542d1ff81e4 | 121 | printf("press cross "); |
mazgch | 4:4542d1ff81e4 | 122 | locate(pos_x,pos_y + font[2]); |
mazgch | 4:4542d1ff81e4 | 123 | printf("to calibrate "); |
mazgch | 4:4542d1ff81e4 | 124 | for (i=0; i<5; i++) { |
mazgch | 4:4542d1ff81e4 | 125 | while (getTouch(p) != YES) |
mazgch | 4:4542d1ff81e4 | 126 | /*nothing*/; |
mazgch | 4:4542d1ff81e4 | 127 | a += p.x; |
mazgch | 4:4542d1ff81e4 | 128 | b += p.y; |
mazgch | 4:4542d1ff81e4 | 129 | } |
mazgch | 4:4542d1ff81e4 | 130 | a = a / 5; |
mazgch | 4:4542d1ff81e4 | 131 | b = b / 5; |
mazgch | 4:4542d1ff81e4 | 132 | locate(pos_x,pos_y); |
mazgch | 4:4542d1ff81e4 | 133 | printf("ok "); |
mazgch | 4:4542d1ff81e4 | 134 | locate(pos_x,pos_y + font[2]); |
mazgch | 4:4542d1ff81e4 | 135 | printf("release touch "); |
mazgch | 4:4542d1ff81e4 | 136 | while (getTouch(p) != NO) |
mazgch | 4:4542d1ff81e4 | 137 | /*nothing*/; |
mazgch | 4:4542d1ff81e4 | 138 | cls(); |
mazgch | 4:4542d1ff81e4 | 139 | line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross |
mazgch | 4:4542d1ff81e4 | 140 | line(width() - 8,height() - 5,width() - 1,height() - 5,White); |
mazgch | 4:4542d1ff81e4 | 141 | locate(pos_x,pos_y); |
mazgch | 4:4542d1ff81e4 | 142 | printf("press cross "); |
mazgch | 4:4542d1ff81e4 | 143 | locate(pos_x,pos_y + font[2]); |
mazgch | 4:4542d1ff81e4 | 144 | printf("to calibrate "); |
mazgch | 4:4542d1ff81e4 | 145 | for (i=0; i<5; i++) { |
mazgch | 4:4542d1ff81e4 | 146 | while (getTouch(p) != YES) |
mazgch | 4:4542d1ff81e4 | 147 | /*nothing*/; |
mazgch | 4:4542d1ff81e4 | 148 | c+= p.x; |
mazgch | 4:4542d1ff81e4 | 149 | d+= p.y; |
mazgch | 4:4542d1ff81e4 | 150 | } |
mazgch | 4:4542d1ff81e4 | 151 | c = c / 5; |
mazgch | 4:4542d1ff81e4 | 152 | d = d / 5; |
mazgch | 4:4542d1ff81e4 | 153 | locate(pos_x, pos_y); |
mazgch | 4:4542d1ff81e4 | 154 | |
mazgch | 4:4542d1ff81e4 | 155 | x_off = a; |
mazgch | 4:4542d1ff81e4 | 156 | y_off = b; |
mazgch | 4:4542d1ff81e4 | 157 | i = c-a; // delta x |
mazgch | 4:4542d1ff81e4 | 158 | pp_tx = i / (width() - 6); |
mazgch | 4:4542d1ff81e4 | 159 | i = d-b; // delta y |
mazgch | 4:4542d1ff81e4 | 160 | pp_ty = i / (height() - 6); |
mazgch | 4:4542d1ff81e4 | 161 | |
mazgch | 4:4542d1ff81e4 | 162 | locate(pos_x,pos_y); |
mazgch | 4:4542d1ff81e4 | 163 | printf("Calibrated "); |
mazgch | 4:4542d1ff81e4 | 164 | locate(pos_x,pos_y + font[2]); |
mazgch | 4:4542d1ff81e4 | 165 | printf("x %6i %4i", x_off, pp_tx); |
mazgch | 4:4542d1ff81e4 | 166 | locate(pos_x,pos_y + 2*font[2]); |
mazgch | 4:4542d1ff81e4 | 167 | printf("y %6i %4i", y_off, pp_ty); |
mazgch | 4:4542d1ff81e4 | 168 | while (getTouch(p) != NO) |
mazgch | 4:4542d1ff81e4 | 169 | /*nothing*/; |
mazgch | 4:4542d1ff81e4 | 170 | cls(); |
mazgch | 4:4542d1ff81e4 | 171 | } |
mazgch | 4:4542d1ff81e4 | 172 | |
mazgch | 4:4542d1ff81e4 | 173 | point SeeedStudioTFTv2::toPixel(point p) |
mazgch | 4:4542d1ff81e4 | 174 | { |
mazgch | 4:4542d1ff81e4 | 175 | p.x -= x_off; |
mazgch | 4:4542d1ff81e4 | 176 | p.x /= pp_tx; |
mazgch | 4:4542d1ff81e4 | 177 | int w = width(); |
mazgch | 4:4542d1ff81e4 | 178 | if (p.x > w) p.x = w; |
mazgch | 4:4542d1ff81e4 | 179 | if (p.x < 0) p.x = 0; |
mazgch | 4:4542d1ff81e4 | 180 | p.y -= y_off; |
mazgch | 4:4542d1ff81e4 | 181 | p.y /= pp_ty; |
mazgch | 4:4542d1ff81e4 | 182 | int h = height(); |
mazgch | 4:4542d1ff81e4 | 183 | if (p.y > h) p.y = h; |
mazgch | 4:4542d1ff81e4 | 184 | if (p.y < 0) p.y = 0; |
mazgch | 4:4542d1ff81e4 | 185 | return (p); |
mazgch | 4:4542d1ff81e4 | 186 | } |
mazgch | 4:4542d1ff81e4 | 187 | |
mazgch | 4:4542d1ff81e4 | 188 | bool SeeedStudioTFTv2::getPixel(point& p) |
mazgch | 4:4542d1ff81e4 | 189 | { |
mazgch | 4:4542d1ff81e4 | 190 | TOUCH touch = getTouch(p); |
mazgch | 4:4542d1ff81e4 | 191 | p = toPixel(p); |
mazgch | 4:4542d1ff81e4 | 192 | return touch == YES; |
mazgch | 4:4542d1ff81e4 | 193 | } |