seed

Dependencies:   SPI_TFT_ILI9341

Fork of SeeedStudioTFTv2 by Components

Committer:
Ganstrich
Date:
Mon Apr 18 06:59:46 2016 +0000
Revision:
14:304b55ac904b
Parent:
10:b55a7f8d1145
Child:
15:23288c15e11d
Test des menus pr?sents mais non affich?s. Tentative de debug et r?solution

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