SeedStdudioTFT Library

Dependencies:   SPI_TFT_ILI9341

Fork of SeeedStudioTFTv2 by Components

Committer:
mazgch
Date:
Wed Oct 23 01:28:17 2013 +0000
Revision:
10:b55a7f8d1145
Parent:
8:36ea6be5182e
fix for freescale

Who changed what in which revision?

UserRevisionLine numberNew 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 8:36ea6be5182e 21 PinName csTft, PinName dcTft, PinName blTft,
mazgch 4:4542d1ff81e4 22 PinName csSd):
mazgch 4:4542d1ff81e4 23 #ifdef USE_SDCARD
mazgch 5:366bdc7a8315 24 SDFileSystem(mosi,miso,sclk,csSd, "sdc"),
mazgch 4:4542d1ff81e4 25 #endif
mazgch 8:36ea6be5182e 26 SPI_TFT_ILI9341(mosi,miso,sclk,csTft,NC,dcTft, "tft"),
mazgch 5:366bdc7a8315 27 bl(blTft)
mazgch 4:4542d1ff81e4 28 {
mazgch 4:4542d1ff81e4 29 #ifndef USE_SDCARD
mazgch 4:4542d1ff81e4 30 // sd card
mazgch 4:4542d1ff81e4 31 DigitalOut cs(csSd);
mazgch 4:4542d1ff81e4 32 cs = 1;
mazgch 4:4542d1ff81e4 33 #endif
mazgch 4:4542d1ff81e4 34 // backlight
mazgch 4:4542d1ff81e4 35 bl = 1;
mazgch 6:2612205220c5 36 font = NULL;
mazgch 4:4542d1ff81e4 37 // touch screen pins
mazgch 4:4542d1ff81e4 38 _xp = xp;
mazgch 4:4542d1ff81e4 39 _yp = yp;
mazgch 4:4542d1ff81e4 40 _xm = xm;
mazgch 4:4542d1ff81e4 41 _ym = ym;
mazgch 4:4542d1ff81e4 42 // default touch calibration
mazgch 4:4542d1ff81e4 43 // orientation // 0 1 2 3
mazgch 4:4542d1ff81e4 44 x_off = 108000; // 17252 16605 108755 108000
mazgch 4:4542d1ff81e4 45 y_off = 22000; // 22330 105819 97167 22000
mazgch 4:4542d1ff81e4 46 pp_tx = -291; // 378 289 -390 -291
mazgch 4:4542d1ff81e4 47 pp_ty = 356; // 261 -355 -239 356
mazgch 4:4542d1ff81e4 48 }
mazgch 4:4542d1ff81e4 49
mazgch 5:366bdc7a8315 50 void SeeedStudioTFTv2::setBacklight(bool enabled)
mazgch 5:366bdc7a8315 51 {
mazgch 5:366bdc7a8315 52 bl = enabled;
mazgch 5:366bdc7a8315 53 }
mazgch 5:366bdc7a8315 54
mazgch 4:4542d1ff81e4 55 int SeeedStudioTFTv2::readTouch(PinName p, PinName m, PinName a, PinName i)
mazgch 4:4542d1ff81e4 56 {
mazgch 4:4542d1ff81e4 57 DigitalOut _p(p);
mazgch 4:4542d1ff81e4 58 _p = 1;
mazgch 4:4542d1ff81e4 59 DigitalOut _m(m);
mazgch 4:4542d1ff81e4 60 _m = 0;
mazgch 4:4542d1ff81e4 61 AnalogIn _a(a);
mazgch 10:b55a7f8d1145 62 AnalogIn _i(i); // this pin has to be high Z (DigitalIn may also work)
mazgch 4:4542d1ff81e4 63 wait_us(10);
mazgch 4:4542d1ff81e4 64 return _a.read_u16();
mazgch 4:4542d1ff81e4 65 }
mazgch 4:4542d1ff81e4 66
mazgch 4:4542d1ff81e4 67 SeeedStudioTFTv2::TOUCH SeeedStudioTFTv2::getTouch(point& p)
mazgch 4:4542d1ff81e4 68 {
mazgch 4:4542d1ff81e4 69 int y2 = readTouch(_xp,_xm,_yp,_ym);
mazgch 4:4542d1ff81e4 70 int x2 = readTouch(_yp,_ym,_xp,_xm);
mazgch 4:4542d1ff81e4 71 int y1 = readTouch(_xp,_xm,_yp,_ym);
mazgch 4:4542d1ff81e4 72 int x1 = readTouch(_yp,_ym,_xp,_xm);
mazgch 4:4542d1ff81e4 73 int xd = x1 - x2;
mazgch 4:4542d1ff81e4 74 int yd = y1 - y2;
mazgch 4:4542d1ff81e4 75 xd = (xd > 0) ? xd : -xd;
mazgch 4:4542d1ff81e4 76 yd = (yd > 0) ? xd : -xd;
mazgch 4:4542d1ff81e4 77 p.x = x1 + x2;
mazgch 4:4542d1ff81e4 78 p.y = y1 + y2;
mazgch 4:4542d1ff81e4 79 #if 0
mazgch 4:4542d1ff81e4 80 DigitalOut _p(_xp);
mazgch 4:4542d1ff81e4 81 _p = 1;
mazgch 4:4542d1ff81e4 82 DigitalOut _m(_ym);
mazgch 4:4542d1ff81e4 83 _m = 0;
mazgch 4:4542d1ff81e4 84 AnalogIn _ax(_xm);
mazgch 4:4542d1ff81e4 85 AnalogIn _ay(_yp);
mazgch 4:4542d1ff81e4 86 wait_us(20);
mazgch 4:4542d1ff81e4 87 int ax = _ax.read_u16();
mazgch 4:4542d1ff81e4 88 int ay = _ay.read_u16();
mazgch 4:4542d1ff81e4 89 float z = 0;
mazgch 4:4542d1ff81e4 90 z = (float)ay / ax / x / 2 * 0x10000;
mazgch 4:4542d1ff81e4 91 #endif
mazgch 4:4542d1ff81e4 92 const int th = 8000;
mazgch 4:4542d1ff81e4 93 const int df = 100;
mazgch 4:4542d1ff81e4 94 TOUCH touch;
mazgch 4:4542d1ff81e4 95 if (x1 < th || x2 < th ||
mazgch 4:4542d1ff81e4 96 y1 < th || y2 < th) {
mazgch 4:4542d1ff81e4 97 p.x = 0;
mazgch 4:4542d1ff81e4 98 p.y = 0;
mazgch 4:4542d1ff81e4 99 touch = NO;
mazgch 4:4542d1ff81e4 100 } else if (xd > df || yd > df) {
mazgch 4:4542d1ff81e4 101 touch = MAYBE;
mazgch 4:4542d1ff81e4 102 } else {
mazgch 4:4542d1ff81e4 103 touch = YES;
mazgch 4:4542d1ff81e4 104 }
mazgch 4:4542d1ff81e4 105 //locate(0,50);
mazgch 4:4542d1ff81e4 106 //printf("x: %6i y: %6i",p.x,p.y);
mazgch 4:4542d1ff81e4 107 return touch;
mazgch 4:4542d1ff81e4 108 }
mazgch 4:4542d1ff81e4 109
mazgch 4:4542d1ff81e4 110 void SeeedStudioTFTv2::calibrate(void)
mazgch 4:4542d1ff81e4 111 {
mazgch 4:4542d1ff81e4 112 int i;
mazgch 4:4542d1ff81e4 113 int a = 0,b = 0,c = 0, d = 0;
mazgch 6:2612205220c5 114 int pos_x = 0, pos_y = 0;
mazgch 4:4542d1ff81e4 115 point p;
mazgch 4:4542d1ff81e4 116
mazgch 4:4542d1ff81e4 117 cls();
mazgch 4:4542d1ff81e4 118 foreground(White); // set chars to white
mazgch 4:4542d1ff81e4 119 line(0,3,6,3,White);
mazgch 4:4542d1ff81e4 120 line(3,0,3,6,White);
mazgch 6:2612205220c5 121 if (font)
mazgch 6:2612205220c5 122 {
mazgch 6:2612205220c5 123 // get the center of the screen
mazgch 6:2612205220c5 124 pos_x = columns() / 2 - 3;
mazgch 6:2612205220c5 125 pos_x = pos_x * font[1];
mazgch 6:2612205220c5 126 pos_y = (rows() / 2) - 1;
mazgch 6:2612205220c5 127 pos_y = pos_y * font[2];
mazgch 6:2612205220c5 128 locate(pos_x,pos_y);
mazgch 6:2612205220c5 129 printf("press cross ");
mazgch 6:2612205220c5 130 locate(pos_x,pos_y + font[2]);
mazgch 6:2612205220c5 131 printf("to calibrate ");
mazgch 6:2612205220c5 132 }
mazgch 4:4542d1ff81e4 133 for (i=0; i<5; i++) {
mazgch 4:4542d1ff81e4 134 while (getTouch(p) != YES)
mazgch 4:4542d1ff81e4 135 /*nothing*/;
mazgch 4:4542d1ff81e4 136 a += p.x;
mazgch 4:4542d1ff81e4 137 b += p.y;
mazgch 4:4542d1ff81e4 138 }
mazgch 4:4542d1ff81e4 139 a = a / 5;
mazgch 4:4542d1ff81e4 140 b = b / 5;
mazgch 6:2612205220c5 141 if (font)
mazgch 6:2612205220c5 142 {
mazgch 6:2612205220c5 143 locate(pos_x,pos_y);
mazgch 6:2612205220c5 144 printf("ok ");
mazgch 6:2612205220c5 145 locate(pos_x,pos_y + font[2]);
mazgch 6:2612205220c5 146 printf("release touch ");
mazgch 6:2612205220c5 147 }
mazgch 4:4542d1ff81e4 148 while (getTouch(p) != NO)
mazgch 4:4542d1ff81e4 149 /*nothing*/;
mazgch 4:4542d1ff81e4 150 cls();
mazgch 4:4542d1ff81e4 151 line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross
mazgch 4:4542d1ff81e4 152 line(width() - 8,height() - 5,width() - 1,height() - 5,White);
mazgch 6:2612205220c5 153 if (font)
mazgch 6:2612205220c5 154 {
mazgch 6:2612205220c5 155 locate(pos_x,pos_y);
mazgch 6:2612205220c5 156 printf("press cross ");
mazgch 6:2612205220c5 157 locate(pos_x,pos_y + font[2]);
mazgch 6:2612205220c5 158 printf("to calibrate ");
mazgch 6:2612205220c5 159 }
mazgch 4:4542d1ff81e4 160 for (i=0; i<5; i++) {
mazgch 4:4542d1ff81e4 161 while (getTouch(p) != YES)
mazgch 4:4542d1ff81e4 162 /*nothing*/;
mazgch 4:4542d1ff81e4 163 c+= p.x;
mazgch 4:4542d1ff81e4 164 d+= p.y;
mazgch 4:4542d1ff81e4 165 }
mazgch 4:4542d1ff81e4 166 c = c / 5;
mazgch 4:4542d1ff81e4 167 d = d / 5;
mazgch 4:4542d1ff81e4 168 x_off = a;
mazgch 4:4542d1ff81e4 169 y_off = b;
mazgch 4:4542d1ff81e4 170 i = c-a; // delta x
mazgch 4:4542d1ff81e4 171 pp_tx = i / (width() - 6);
mazgch 4:4542d1ff81e4 172 i = d-b; // delta y
mazgch 4:4542d1ff81e4 173 pp_ty = i / (height() - 6);
mazgch 6:2612205220c5 174 if (font)
mazgch 6:2612205220c5 175 {
mazgch 6:2612205220c5 176 locate(pos_x,pos_y);
mazgch 6:2612205220c5 177 printf("Calibrated ");
mazgch 6:2612205220c5 178 locate(pos_x,pos_y + font[2]);
mazgch 6:2612205220c5 179 printf("x %6i %4i", x_off, pp_tx);
mazgch 6:2612205220c5 180 locate(pos_x,pos_y + 2*font[2]);
mazgch 6:2612205220c5 181 printf("y %6i %4i", y_off, pp_ty);
mazgch 6:2612205220c5 182 }
mazgch 4:4542d1ff81e4 183 while (getTouch(p) != NO)
mazgch 4:4542d1ff81e4 184 /*nothing*/;
mazgch 4:4542d1ff81e4 185 cls();
mazgch 4:4542d1ff81e4 186 }
mazgch 4:4542d1ff81e4 187
mazgch 4:4542d1ff81e4 188 point SeeedStudioTFTv2::toPixel(point p)
mazgch 4:4542d1ff81e4 189 {
mazgch 4:4542d1ff81e4 190 p.x -= x_off;
mazgch 4:4542d1ff81e4 191 p.x /= pp_tx;
mazgch 4:4542d1ff81e4 192 int w = width();
mazgch 4:4542d1ff81e4 193 if (p.x > w) p.x = w;
mazgch 4:4542d1ff81e4 194 if (p.x < 0) p.x = 0;
mazgch 4:4542d1ff81e4 195 p.y -= y_off;
mazgch 4:4542d1ff81e4 196 p.y /= pp_ty;
mazgch 4:4542d1ff81e4 197 int h = height();
mazgch 4:4542d1ff81e4 198 if (p.y > h) p.y = h;
mazgch 4:4542d1ff81e4 199 if (p.y < 0) p.y = 0;
mazgch 4:4542d1ff81e4 200 return (p);
mazgch 4:4542d1ff81e4 201 }
mazgch 4:4542d1ff81e4 202
mazgch 4:4542d1ff81e4 203 bool SeeedStudioTFTv2::getPixel(point& p)
mazgch 4:4542d1ff81e4 204 {
mazgch 4:4542d1ff81e4 205 TOUCH touch = getTouch(p);
mazgch 4:4542d1ff81e4 206 p = toPixel(p);
mazgch 4:4542d1ff81e4 207 return touch == YES;
mazgch 4:4542d1ff81e4 208 }