![](/media/cache/group/Display_Nokia_Insper_v01.jpg.50x50_q85.jpg)
Testando função Touch Display TFT
Dependencies: mbed TouchScreen_kbv_mbed MCUFRIEND_kbv_R299 GLUE_STUFF_kbv ADA_GFX_kbv_R1107
Diff: TFT_TouchShield.cpp
- Revision:
- 1:e55610b3999a
- Parent:
- 0:063c3eaefd81
- Child:
- 2:d8d564f86bc8
diff -r 063c3eaefd81 -r e55610b3999a TFT_TouchShield.cpp --- a/TFT_TouchShield.cpp Tue Apr 27 07:03:31 2021 +0000 +++ b/TFT_TouchShield.cpp Fri Apr 30 11:38:26 2021 +0000 @@ -1,39 +1,42 @@ -// the regular Adafruit "TouchScreen.h" library only works on AVRs +// ****** Henrique ****** 29/04/21 +// Teste com botões Touch +// +// ************** Display TFT- ILI9341 Versão V 1************** \\ -// different mcufriend shields have Touchscreen on different pins -// and rotation. -// Run the TouchScreen_Calibr_native sketch for calibration of your shield +#include "mbed.h" +#include "Arduino.h" #include <MCUFRIEND_kbv.h> -MCUFRIEND_kbv tft; // hard-wired for UNO shields anyway. +MCUFRIEND_kbv tft; #include "TouchScreen_kbv_mbed.h" -//#include <iostream> -//#include <string> -char *name = "Please Calibrate."; //edit name of shield -//const int XP=6,XM=A2,YP=A1,YM=7; //ID=0x9341 -//const int TS_LEFT=907,TS_RT=136,TS_TOP=942,TS_BOT=139; -const int TS_LEFT=183,TS_RT=903,TS_TOP=76,TS_BOT=899; + +const int TS_LEFT=150,TS_RT=120,TS_TOP=920,TS_BOT=940; const PinName XP = D7, YP = A2, XM = A1, YM = D6; //next common configuration DigitalInOut YPout(YP); + DigitalInOut XMout(XM); -#define Serial serial_kbv + + +Serial pc(USBTX, USBRX); + long map(long x, long in_min, long in_max, long out_min, long out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } + + TouchScreen_kbv ts = TouchScreen_kbv(XP, YP, XM, YM, 300); TSPoint_kbv tp; -#define MINPRESSURE 200 +#define MINPRESSURE 10 #define MAXPRESSURE 1000 -int16_t BOXSIZE; -int16_t PENRADIUS = 1; -uint16_t ID, oldcolor, currentcolor; -uint8_t Orientation = 0; //PORTRAIT + -// Assign human-readable names to some common 16-bit color values: +uint8_t Orientation = 1; // Orientção do Display + +// Tabela de valores de cores : #define BLACK 0x0000 #define BLUE 0x001F #define RED 0xF800 @@ -43,176 +46,94 @@ #define YELLOW 0xFFE0 #define WHITE 0xFFFF -void show_Serial(void) -{ - Serial.println(F("Most Touch Screens use pins 6, 7, A1, A2")); - Serial.println(F("But they can be in ANY order")); - Serial.println(F("e.g. right to left or bottom to top")); - Serial.println(F("or wrong direction")); - Serial.println(F("Edit name and calibration statements\n")); - Serial.println(name); - Serial.print(F("ID=0x")); - Serial.println(ID, HEX); - Serial.println("Screen is " + String(tft.width()) + "x" + String(tft.height())); - Serial.println("Calibration is: "); - Serial.println("LEFT = " + String(TS_LEFT) + " RT = " + String(TS_RT)); - Serial.println("TOP = " + String(TS_TOP) + " BOT = " + String(TS_BOT)); - Serial.println("Wiring is always PORTRAIT"); - Serial.println("YP=" + String(YP) + " XM=" + String(XM)); - Serial.println("YM=" + String(YM) + " XP=" + String(XP)); -} +bool botu_1 =0; void show_tft(void) { - tft.setCursor(0, 0); - tft.setTextSize(1); - tft.print(F("ID=0x")); - tft.println(ID, HEX); - //tft.println("Screen is " + String(tft.width()) + "x" + String(tft.height())); - tft.printf("Screen is %dx%d\n", tft.width(), tft.height()); - tft.println(""); + tft.setTextSize(2); - tft.println(name); - tft.setTextSize(1); - tft.println("PORTRAIT Values:"); - tft.println("LEFT = " + String(TS_LEFT) + " RT = " + String(TS_RT)); - tft.println("TOP = " + String(TS_TOP) + " BOT = " + String(TS_BOT)); - tft.println("\nWiring is: "); - tft.println("YP=" + String(YP) + " XM=" + String(XM)); - tft.println("YM=" + String(YM) + " XP=" + String(XP)); - tft.setTextSize(2); - tft.setTextColor(RED); - tft.setCursor((tft.width() - 48) / 2, (tft.height() * 2) / 4); - tft.print("EXIT"); - tft.setTextColor(YELLOW, BLACK); - tft.setCursor(0, (tft.height() * 6) / 8); - tft.print("Touch screen for loc"); + + + tft.setTextColor(MAGENTA,BLACK); + + while (1) { tp = ts.getPoint(); - //pinMode(XM, OUTPUT); - //pinMode(YP, OUTPUT); YPout.output(); XMout.output(); - if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) continue; - if (tp.x > 450 && tp.x < 570 && tp.y > 450 && tp.y < 570) break; + + if (tp.z < MINPRESSURE && tp.z > MAXPRESSURE) + + tp.x = tft.width() - (map(tp.x, TS_RT, TS_LEFT, tft.width(), 0)); + tp.y = tft.height() - (map(tp.y, TS_BOT, TS_TOP, tft.height(), 0)); + + + + if (tp.x>700&tp.x<800) { + + + tft.setCursor(15, 200); + tft.printf("Button 1"); + + } + + if (tp.x>776&tp.x<845) { + + + tft.setCursor(15, 200); + tft.printf("Button 2"); + + } + tft.setCursor(0, (tft.height() * 3) / 4); - //tft.print("tp.x=" + String(tp.x) + " tp.y=" + String(tp.y) + " "); - tft.printf("tp.x=%d tp.y=%d ", tp.x, tp.y); + + tft.printf("tp.x=%d ", tp.x); + } } + +void bot_1 () +{ + + + tft.setCursor(40, 50); + tft.drawRoundRect(5, 15, 200, 50, 5, WHITE); + tft.setTextColor(BLUE); + tft.setTextSize(3); + tft.setCursor(15, 30); + tft.println("Button 1"); + + + + + tft.drawRoundRect(5, 70, 200, 50, 5, WHITE); + tft.setTextColor(BLUE); + tft.setTextSize(3); + tft.setCursor(15,85); + tft.println("Button 2"); + + + +} + void setup(void) { - uint16_t tmp; tft.reset(); - ID = tft.readID(); - tft.begin(ID); - Serial.begin(9600); - show_Serial(); + tft.begin(); tft.setRotation(Orientation); tft.fillScreen(BLACK); + bot_1(); show_tft(); - BOXSIZE = tft.width() / 6; - tft.fillScreen(BLACK); - tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED); - tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW); - tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN); - tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN); - tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE); - tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA); - - tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE); - currentcolor = RED; delay(1000); } void loop() { - uint16_t xpos, ypos; //screen coordinates - tp = ts.getPoint(); //tp.x, tp.y are ADC values - // if sharing pins, you'll need to fix the directions of the touchscreen pins - //pinMode(XM, OUTPUT); - //pinMode(YP, OUTPUT); - YPout.output(); - XMout.output(); - // we have some minimum pressure we consider 'valid' - // pressure of 0 means no pressing! - - if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) { - // most mcufriend have touch (with icons) that extends below the TFT - // screens without icons need to reserve a space for "erase" - // scale the ADC values from ts.getPoint() to screen values e.g. 0-239 - // - // Calibration is true for PORTRAIT. tp.y is always long dimension - // map to your current pixel orientation - switch (Orientation) { - case 0: - xpos = map(tp.x, TS_LEFT, TS_RT, 0, tft.width()); - ypos = map(tp.y, TS_TOP, TS_BOT, 0, tft.height()); - break; - case 1: - xpos = map(tp.y, TS_TOP, TS_BOT, 0, tft.width()); - ypos = map(tp.x, TS_RT, TS_LEFT, 0, tft.height()); - break; - case 2: - xpos = map(tp.x, TS_RT, TS_LEFT, 0, tft.width()); - ypos = map(tp.y, TS_BOT, TS_TOP, 0, tft.height()); - break; - case 3: - xpos = map(tp.y, TS_BOT, TS_TOP, 0, tft.width()); - ypos = map(tp.x, TS_LEFT, TS_RT, 0, tft.height()); - break; - } - - // are we in top color box area ? - if (ypos < BOXSIZE) { //draw white border on selected color box - oldcolor = currentcolor; - - if (xpos < BOXSIZE) { - currentcolor = RED; - tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE); - } else if (xpos < BOXSIZE * 2) { - currentcolor = YELLOW; - tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE); - } else if (xpos < BOXSIZE * 3) { - currentcolor = GREEN; - tft.drawRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, WHITE); - } else if (xpos < BOXSIZE * 4) { - currentcolor = CYAN; - tft.drawRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, WHITE); - } else if (xpos < BOXSIZE * 5) { - currentcolor = BLUE; - tft.drawRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, WHITE); - } else if (xpos < BOXSIZE * 6) { - currentcolor = MAGENTA; - tft.drawRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, WHITE); - } - - if (oldcolor != currentcolor) { //rub out the previous white border - if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED); - if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW); - if (oldcolor == GREEN) tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN); - if (oldcolor == CYAN) tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN); - if (oldcolor == BLUE) tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE); - if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA); - } - } - // are we in drawing area ? - if (((ypos - PENRADIUS) > BOXSIZE) && ((ypos + PENRADIUS) < tft.height())) { - tft.fillCircle(xpos, ypos, PENRADIUS, currentcolor); - } - // are we in erase area ? - // Plain Touch panels use bottom 10 pixels e.g. > h - 10 - // Touch panels with icon area e.g. > h - 0 - if (ypos > tft.height() - 10) { - // press the bottom of the screen to erase - tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK); - } - } }