bla

Dependencies:   SPI_TFT_ILI9341

Fork of SeeedStudioTFTv2 by Mbed Game

Committer:
alpali
Date:
Tue Apr 17 11:19:18 2018 +0000
Revision:
17:7108f1e0f033
Parent:
15:6b5421dcadae
blah

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