Testando função Touch Display TFT

Dependencies:   mbed TouchScreen_kbv_mbed MCUFRIEND_kbv_R299 GLUE_STUFF_kbv ADA_GFX_kbv_R1107

Committer:
davidprentice
Date:
Tue Apr 27 07:03:31 2021 +0000
Revision:
0:063c3eaefd81
Child:
1:e55610b3999a
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davidprentice 0:063c3eaefd81 1 // the regular Adafruit "TouchScreen.h" library only works on AVRs
davidprentice 0:063c3eaefd81 2
davidprentice 0:063c3eaefd81 3 // different mcufriend shields have Touchscreen on different pins
davidprentice 0:063c3eaefd81 4 // and rotation.
davidprentice 0:063c3eaefd81 5 // Run the TouchScreen_Calibr_native sketch for calibration of your shield
davidprentice 0:063c3eaefd81 6
davidprentice 0:063c3eaefd81 7 #include <MCUFRIEND_kbv.h>
davidprentice 0:063c3eaefd81 8 MCUFRIEND_kbv tft; // hard-wired for UNO shields anyway.
davidprentice 0:063c3eaefd81 9 #include "TouchScreen_kbv_mbed.h"
davidprentice 0:063c3eaefd81 10 //#include <iostream>
davidprentice 0:063c3eaefd81 11 //#include <string>
davidprentice 0:063c3eaefd81 12
davidprentice 0:063c3eaefd81 13 char *name = "Please Calibrate."; //edit name of shield
davidprentice 0:063c3eaefd81 14 //const int XP=6,XM=A2,YP=A1,YM=7; //ID=0x9341
davidprentice 0:063c3eaefd81 15 //const int TS_LEFT=907,TS_RT=136,TS_TOP=942,TS_BOT=139;
davidprentice 0:063c3eaefd81 16 const int TS_LEFT=183,TS_RT=903,TS_TOP=76,TS_BOT=899;
davidprentice 0:063c3eaefd81 17 const PinName XP = D7, YP = A2, XM = A1, YM = D6; //next common configuration
davidprentice 0:063c3eaefd81 18 DigitalInOut YPout(YP);
davidprentice 0:063c3eaefd81 19 DigitalInOut XMout(XM);
davidprentice 0:063c3eaefd81 20 #define Serial serial_kbv
davidprentice 0:063c3eaefd81 21 long map(long x, long in_min, long in_max, long out_min, long out_max)
davidprentice 0:063c3eaefd81 22 {
davidprentice 0:063c3eaefd81 23 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
davidprentice 0:063c3eaefd81 24 }
davidprentice 0:063c3eaefd81 25 TouchScreen_kbv ts = TouchScreen_kbv(XP, YP, XM, YM, 300);
davidprentice 0:063c3eaefd81 26 TSPoint_kbv tp;
davidprentice 0:063c3eaefd81 27
davidprentice 0:063c3eaefd81 28 #define MINPRESSURE 200
davidprentice 0:063c3eaefd81 29 #define MAXPRESSURE 1000
davidprentice 0:063c3eaefd81 30
davidprentice 0:063c3eaefd81 31 int16_t BOXSIZE;
davidprentice 0:063c3eaefd81 32 int16_t PENRADIUS = 1;
davidprentice 0:063c3eaefd81 33 uint16_t ID, oldcolor, currentcolor;
davidprentice 0:063c3eaefd81 34 uint8_t Orientation = 0; //PORTRAIT
davidprentice 0:063c3eaefd81 35
davidprentice 0:063c3eaefd81 36 // Assign human-readable names to some common 16-bit color values:
davidprentice 0:063c3eaefd81 37 #define BLACK 0x0000
davidprentice 0:063c3eaefd81 38 #define BLUE 0x001F
davidprentice 0:063c3eaefd81 39 #define RED 0xF800
davidprentice 0:063c3eaefd81 40 #define GREEN 0x07E0
davidprentice 0:063c3eaefd81 41 #define CYAN 0x07FF
davidprentice 0:063c3eaefd81 42 #define MAGENTA 0xF81F
davidprentice 0:063c3eaefd81 43 #define YELLOW 0xFFE0
davidprentice 0:063c3eaefd81 44 #define WHITE 0xFFFF
davidprentice 0:063c3eaefd81 45
davidprentice 0:063c3eaefd81 46 void show_Serial(void)
davidprentice 0:063c3eaefd81 47 {
davidprentice 0:063c3eaefd81 48 Serial.println(F("Most Touch Screens use pins 6, 7, A1, A2"));
davidprentice 0:063c3eaefd81 49 Serial.println(F("But they can be in ANY order"));
davidprentice 0:063c3eaefd81 50 Serial.println(F("e.g. right to left or bottom to top"));
davidprentice 0:063c3eaefd81 51 Serial.println(F("or wrong direction"));
davidprentice 0:063c3eaefd81 52 Serial.println(F("Edit name and calibration statements\n"));
davidprentice 0:063c3eaefd81 53 Serial.println(name);
davidprentice 0:063c3eaefd81 54 Serial.print(F("ID=0x"));
davidprentice 0:063c3eaefd81 55 Serial.println(ID, HEX);
davidprentice 0:063c3eaefd81 56 Serial.println("Screen is " + String(tft.width()) + "x" + String(tft.height()));
davidprentice 0:063c3eaefd81 57 Serial.println("Calibration is: ");
davidprentice 0:063c3eaefd81 58 Serial.println("LEFT = " + String(TS_LEFT) + " RT = " + String(TS_RT));
davidprentice 0:063c3eaefd81 59 Serial.println("TOP = " + String(TS_TOP) + " BOT = " + String(TS_BOT));
davidprentice 0:063c3eaefd81 60 Serial.println("Wiring is always PORTRAIT");
davidprentice 0:063c3eaefd81 61 Serial.println("YP=" + String(YP) + " XM=" + String(XM));
davidprentice 0:063c3eaefd81 62 Serial.println("YM=" + String(YM) + " XP=" + String(XP));
davidprentice 0:063c3eaefd81 63 }
davidprentice 0:063c3eaefd81 64
davidprentice 0:063c3eaefd81 65 void show_tft(void)
davidprentice 0:063c3eaefd81 66 {
davidprentice 0:063c3eaefd81 67 tft.setCursor(0, 0);
davidprentice 0:063c3eaefd81 68 tft.setTextSize(1);
davidprentice 0:063c3eaefd81 69 tft.print(F("ID=0x"));
davidprentice 0:063c3eaefd81 70 tft.println(ID, HEX);
davidprentice 0:063c3eaefd81 71 //tft.println("Screen is " + String(tft.width()) + "x" + String(tft.height()));
davidprentice 0:063c3eaefd81 72 tft.printf("Screen is %dx%d\n", tft.width(), tft.height());
davidprentice 0:063c3eaefd81 73 tft.println("");
davidprentice 0:063c3eaefd81 74 tft.setTextSize(2);
davidprentice 0:063c3eaefd81 75 tft.println(name);
davidprentice 0:063c3eaefd81 76 tft.setTextSize(1);
davidprentice 0:063c3eaefd81 77 tft.println("PORTRAIT Values:");
davidprentice 0:063c3eaefd81 78 tft.println("LEFT = " + String(TS_LEFT) + " RT = " + String(TS_RT));
davidprentice 0:063c3eaefd81 79 tft.println("TOP = " + String(TS_TOP) + " BOT = " + String(TS_BOT));
davidprentice 0:063c3eaefd81 80 tft.println("\nWiring is: ");
davidprentice 0:063c3eaefd81 81 tft.println("YP=" + String(YP) + " XM=" + String(XM));
davidprentice 0:063c3eaefd81 82 tft.println("YM=" + String(YM) + " XP=" + String(XP));
davidprentice 0:063c3eaefd81 83 tft.setTextSize(2);
davidprentice 0:063c3eaefd81 84 tft.setTextColor(RED);
davidprentice 0:063c3eaefd81 85 tft.setCursor((tft.width() - 48) / 2, (tft.height() * 2) / 4);
davidprentice 0:063c3eaefd81 86 tft.print("EXIT");
davidprentice 0:063c3eaefd81 87 tft.setTextColor(YELLOW, BLACK);
davidprentice 0:063c3eaefd81 88 tft.setCursor(0, (tft.height() * 6) / 8);
davidprentice 0:063c3eaefd81 89 tft.print("Touch screen for loc");
davidprentice 0:063c3eaefd81 90 while (1) {
davidprentice 0:063c3eaefd81 91 tp = ts.getPoint();
davidprentice 0:063c3eaefd81 92 //pinMode(XM, OUTPUT);
davidprentice 0:063c3eaefd81 93 //pinMode(YP, OUTPUT);
davidprentice 0:063c3eaefd81 94 YPout.output();
davidprentice 0:063c3eaefd81 95 XMout.output();
davidprentice 0:063c3eaefd81 96 if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) continue;
davidprentice 0:063c3eaefd81 97 if (tp.x > 450 && tp.x < 570 && tp.y > 450 && tp.y < 570) break;
davidprentice 0:063c3eaefd81 98 tft.setCursor(0, (tft.height() * 3) / 4);
davidprentice 0:063c3eaefd81 99 //tft.print("tp.x=" + String(tp.x) + " tp.y=" + String(tp.y) + " ");
davidprentice 0:063c3eaefd81 100 tft.printf("tp.x=%d tp.y=%d ", tp.x, tp.y);
davidprentice 0:063c3eaefd81 101 }
davidprentice 0:063c3eaefd81 102 }
davidprentice 0:063c3eaefd81 103
davidprentice 0:063c3eaefd81 104
davidprentice 0:063c3eaefd81 105 void setup(void)
davidprentice 0:063c3eaefd81 106 {
davidprentice 0:063c3eaefd81 107 uint16_t tmp;
davidprentice 0:063c3eaefd81 108
davidprentice 0:063c3eaefd81 109 tft.reset();
davidprentice 0:063c3eaefd81 110 ID = tft.readID();
davidprentice 0:063c3eaefd81 111 tft.begin(ID);
davidprentice 0:063c3eaefd81 112 Serial.begin(9600);
davidprentice 0:063c3eaefd81 113 show_Serial();
davidprentice 0:063c3eaefd81 114 tft.setRotation(Orientation);
davidprentice 0:063c3eaefd81 115 tft.fillScreen(BLACK);
davidprentice 0:063c3eaefd81 116 show_tft();
davidprentice 0:063c3eaefd81 117
davidprentice 0:063c3eaefd81 118 BOXSIZE = tft.width() / 6;
davidprentice 0:063c3eaefd81 119 tft.fillScreen(BLACK);
davidprentice 0:063c3eaefd81 120
davidprentice 0:063c3eaefd81 121 tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
davidprentice 0:063c3eaefd81 122 tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
davidprentice 0:063c3eaefd81 123 tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
davidprentice 0:063c3eaefd81 124 tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
davidprentice 0:063c3eaefd81 125 tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
davidprentice 0:063c3eaefd81 126 tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
davidprentice 0:063c3eaefd81 127
davidprentice 0:063c3eaefd81 128 tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
davidprentice 0:063c3eaefd81 129 currentcolor = RED;
davidprentice 0:063c3eaefd81 130 delay(1000);
davidprentice 0:063c3eaefd81 131 }
davidprentice 0:063c3eaefd81 132
davidprentice 0:063c3eaefd81 133 void loop()
davidprentice 0:063c3eaefd81 134 {
davidprentice 0:063c3eaefd81 135 uint16_t xpos, ypos; //screen coordinates
davidprentice 0:063c3eaefd81 136 tp = ts.getPoint(); //tp.x, tp.y are ADC values
davidprentice 0:063c3eaefd81 137
davidprentice 0:063c3eaefd81 138 // if sharing pins, you'll need to fix the directions of the touchscreen pins
davidprentice 0:063c3eaefd81 139 //pinMode(XM, OUTPUT);
davidprentice 0:063c3eaefd81 140 //pinMode(YP, OUTPUT);
davidprentice 0:063c3eaefd81 141 YPout.output();
davidprentice 0:063c3eaefd81 142 XMout.output();
davidprentice 0:063c3eaefd81 143 // we have some minimum pressure we consider 'valid'
davidprentice 0:063c3eaefd81 144 // pressure of 0 means no pressing!
davidprentice 0:063c3eaefd81 145
davidprentice 0:063c3eaefd81 146 if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {
davidprentice 0:063c3eaefd81 147 // most mcufriend have touch (with icons) that extends below the TFT
davidprentice 0:063c3eaefd81 148 // screens without icons need to reserve a space for "erase"
davidprentice 0:063c3eaefd81 149 // scale the ADC values from ts.getPoint() to screen values e.g. 0-239
davidprentice 0:063c3eaefd81 150 //
davidprentice 0:063c3eaefd81 151 // Calibration is true for PORTRAIT. tp.y is always long dimension
davidprentice 0:063c3eaefd81 152 // map to your current pixel orientation
davidprentice 0:063c3eaefd81 153 switch (Orientation) {
davidprentice 0:063c3eaefd81 154 case 0:
davidprentice 0:063c3eaefd81 155 xpos = map(tp.x, TS_LEFT, TS_RT, 0, tft.width());
davidprentice 0:063c3eaefd81 156 ypos = map(tp.y, TS_TOP, TS_BOT, 0, tft.height());
davidprentice 0:063c3eaefd81 157 break;
davidprentice 0:063c3eaefd81 158 case 1:
davidprentice 0:063c3eaefd81 159 xpos = map(tp.y, TS_TOP, TS_BOT, 0, tft.width());
davidprentice 0:063c3eaefd81 160 ypos = map(tp.x, TS_RT, TS_LEFT, 0, tft.height());
davidprentice 0:063c3eaefd81 161 break;
davidprentice 0:063c3eaefd81 162 case 2:
davidprentice 0:063c3eaefd81 163 xpos = map(tp.x, TS_RT, TS_LEFT, 0, tft.width());
davidprentice 0:063c3eaefd81 164 ypos = map(tp.y, TS_BOT, TS_TOP, 0, tft.height());
davidprentice 0:063c3eaefd81 165 break;
davidprentice 0:063c3eaefd81 166 case 3:
davidprentice 0:063c3eaefd81 167 xpos = map(tp.y, TS_BOT, TS_TOP, 0, tft.width());
davidprentice 0:063c3eaefd81 168 ypos = map(tp.x, TS_LEFT, TS_RT, 0, tft.height());
davidprentice 0:063c3eaefd81 169 break;
davidprentice 0:063c3eaefd81 170 }
davidprentice 0:063c3eaefd81 171
davidprentice 0:063c3eaefd81 172 // are we in top color box area ?
davidprentice 0:063c3eaefd81 173 if (ypos < BOXSIZE) { //draw white border on selected color box
davidprentice 0:063c3eaefd81 174 oldcolor = currentcolor;
davidprentice 0:063c3eaefd81 175
davidprentice 0:063c3eaefd81 176 if (xpos < BOXSIZE) {
davidprentice 0:063c3eaefd81 177 currentcolor = RED;
davidprentice 0:063c3eaefd81 178 tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
davidprentice 0:063c3eaefd81 179 } else if (xpos < BOXSIZE * 2) {
davidprentice 0:063c3eaefd81 180 currentcolor = YELLOW;
davidprentice 0:063c3eaefd81 181 tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
davidprentice 0:063c3eaefd81 182 } else if (xpos < BOXSIZE * 3) {
davidprentice 0:063c3eaefd81 183 currentcolor = GREEN;
davidprentice 0:063c3eaefd81 184 tft.drawRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, WHITE);
davidprentice 0:063c3eaefd81 185 } else if (xpos < BOXSIZE * 4) {
davidprentice 0:063c3eaefd81 186 currentcolor = CYAN;
davidprentice 0:063c3eaefd81 187 tft.drawRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, WHITE);
davidprentice 0:063c3eaefd81 188 } else if (xpos < BOXSIZE * 5) {
davidprentice 0:063c3eaefd81 189 currentcolor = BLUE;
davidprentice 0:063c3eaefd81 190 tft.drawRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, WHITE);
davidprentice 0:063c3eaefd81 191 } else if (xpos < BOXSIZE * 6) {
davidprentice 0:063c3eaefd81 192 currentcolor = MAGENTA;
davidprentice 0:063c3eaefd81 193 tft.drawRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, WHITE);
davidprentice 0:063c3eaefd81 194 }
davidprentice 0:063c3eaefd81 195
davidprentice 0:063c3eaefd81 196 if (oldcolor != currentcolor) { //rub out the previous white border
davidprentice 0:063c3eaefd81 197 if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
davidprentice 0:063c3eaefd81 198 if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
davidprentice 0:063c3eaefd81 199 if (oldcolor == GREEN) tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
davidprentice 0:063c3eaefd81 200 if (oldcolor == CYAN) tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
davidprentice 0:063c3eaefd81 201 if (oldcolor == BLUE) tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
davidprentice 0:063c3eaefd81 202 if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
davidprentice 0:063c3eaefd81 203 }
davidprentice 0:063c3eaefd81 204 }
davidprentice 0:063c3eaefd81 205 // are we in drawing area ?
davidprentice 0:063c3eaefd81 206 if (((ypos - PENRADIUS) > BOXSIZE) && ((ypos + PENRADIUS) < tft.height())) {
davidprentice 0:063c3eaefd81 207 tft.fillCircle(xpos, ypos, PENRADIUS, currentcolor);
davidprentice 0:063c3eaefd81 208 }
davidprentice 0:063c3eaefd81 209 // are we in erase area ?
davidprentice 0:063c3eaefd81 210 // Plain Touch panels use bottom 10 pixels e.g. > h - 10
davidprentice 0:063c3eaefd81 211 // Touch panels with icon area e.g. > h - 0
davidprentice 0:063c3eaefd81 212 if (ypos > tft.height() - 10) {
davidprentice 0:063c3eaefd81 213 // press the bottom of the screen to erase
davidprentice 0:063c3eaefd81 214 tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
davidprentice 0:063c3eaefd81 215 }
davidprentice 0:063c3eaefd81 216 }
davidprentice 0:063c3eaefd81 217 }
davidprentice 0:063c3eaefd81 218