2.4 inch tft lcd shield touch board
2.4 Inch TFT LCD Shield Touch Board¶
Objectif¶
Interfacer cet écran tactile sur un arduino Uno
Liste de matériel¶
- Arduino UNO
- 2.4 Inch TFT LCD Shield Touch Board
Schéma¶
Code¶
Télécharger et installer la bibliothèque depuis GitHub.
graphicTest¶
Affiche différents éléments en alternance (text, dessin...)
// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
// Modified for SPFD5408 Library by Joao Lopes
// Version 0.9.2 - Rotation for Mega and screen initial
// *** SPFD5408 change -- Begin
#include <SPFD5408_Adafruit_GFX.h> // Core graphics library
#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
#include <SPFD5408_TouchScreen.h>
// *** SPFD5408 change -- End
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
// -- Setup
void setup(void) {
Serial.begin(9600);
progmemPrintln(PSTR("TFT LCD test"));
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
progmemPrintln(PSTR("Using Adafruit 2.8\" TFT Arduino Shield Pinout"));
#else
progmemPrintln(PSTR("Using Adafruit 2.8\" TFT Breakout Board Pinout"));
#endif
tft.reset();
// *** SPFD5408 change -- Begin
// Original code commented
// uint16_t identifier = tft.readID();
//
// if(identifier == 0x9325) {
// Serial.println(F("Found ILI9325 LCD driver"));
// } else if(identifier == 0x9328) {
// Serial.println(F("Found ILI9328 LCD driver"));
// } else if(identifier == 0x7575) {
// Serial.println(F("Found HX8347G LCD driver"));
// } else if(identifier == 0x9341) {
// Serial.println(F("Found ILI9341 LCD driver"));
// } else if(identifier == 0x8357) {
// Serial.println(F("Found HX8357D LCD driver"));
// } else {
// Serial.print(F("Unknown LCD driver chip: "));
// Serial.println(identifier, HEX);
// Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
// Serial.println(F(" #define USE_ADAFRUIT_SHIELD_PINOUT"));
// Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
// Serial.println(F("If using the breakout board, it should NOT be #defined!"));
// Serial.println(F("Also if using the breakout, double-check that all wiring"));
// Serial.println(F("matches the tutorial."));
// return;
// }
//
// tft.begin(identifier);
// Code changed to works
tft.begin(0x9341); // SDFP5408
tft.setRotation(0); // Need for the Mega, please changed for your choice or rotation initial
// *** SPFD5408 change -- End
progmemPrintln(PSTR("Benchmark Time (microseconds)"));
progmemPrint(PSTR("Screen fill "));
Serial.println(testFillScreen());
delay(500);
progmemPrint(PSTR("Text "));
Serial.println(testText());
delay(3000);
progmemPrint(PSTR("Lines "));
Serial.println(testLines(CYAN));
delay(500);
progmemPrint(PSTR("Horiz/Vert Lines "));
Serial.println(testFastLines(RED, BLUE));
delay(500);
progmemPrint(PSTR("Rectangles (outline) "));
Serial.println(testRects(GREEN));
delay(500);
progmemPrint(PSTR("Rectangles (filled) "));
Serial.println(testFilledRects(YELLOW, MAGENTA));
delay(500);
progmemPrint(PSTR("Circles (filled) "));
Serial.println(testFilledCircles(10, MAGENTA));
progmemPrint(PSTR("Circles (outline) "));
Serial.println(testCircles(10, WHITE));
delay(500);
progmemPrint(PSTR("Triangles (outline) "));
Serial.println(testTriangles());
delay(500);
progmemPrint(PSTR("Triangles (filled) "));
Serial.println(testFilledTriangles());
delay(500);
progmemPrint(PSTR("Rounded rects (outline) "));
Serial.println(testRoundRects());
delay(500);
progmemPrint(PSTR("Rounded rects (filled) "));
Serial.println(testFilledRoundRects());
delay(500);
progmemPrintln(PSTR("Done!"));
}
void loop(void) {
for(uint8_t rotation=0; rotation<4; rotation++) {
tft.setRotation(rotation);
testText();
delay(2000);
}
}
unsigned long testFillScreen() {
unsigned long start = micros();
tft.fillScreen(BLACK);
tft.fillScreen(RED);
tft.fillScreen(GREEN);
tft.fillScreen(BLUE);
tft.fillScreen(BLACK);
return micros() - start;
}
unsigned long testText() {
tft.fillScreen(BLACK);
unsigned long start = micros();
tft.setCursor(0, 0);
tft.setTextColor(WHITE); tft.setTextSize(1);
tft.println("Hello World!");
tft.setTextColor(YELLOW); tft.setTextSize(2);
tft.println(1234.56);
tft.setTextColor(RED); tft.setTextSize(3);
tft.println(0xDEADBEEF, HEX);
tft.println();
tft.setTextColor(GREEN);
tft.setTextSize(5);
tft.println("Groop");
tft.setTextSize(2);
tft.println("I implore thee,");
tft.setTextSize(1);
tft.println("my foonting turlingdromes.");
tft.println("And hooptiously drangle me");
tft.println("with crinkly bindlewurdles,");
tft.println("Or I will rend thee");
tft.println("in the gobberwarts");
tft.println("with my blurglecruncheon,");
tft.println("see if I don't!");
return micros() - start;
}
unsigned long testLines(uint16_t color) {
unsigned long start, t;
int x1, y1, x2, y2,
w = tft.width(),
h = tft.height();
tft.fillScreen(BLACK);
x1 = y1 = 0;
y2 = h - 1;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = w - 1;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
t = micros() - start; // fillScreen doesn't count against timing
tft.fillScreen(BLACK);
x1 = w - 1;
y1 = 0;
y2 = h - 1;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = 0;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
t += micros() - start;
tft.fillScreen(BLACK);
x1 = 0;
y1 = h - 1;
y2 = 0;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = w - 1;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
t += micros() - start;
tft.fillScreen(BLACK);
x1 = w - 1;
y1 = h - 1;
y2 = 0;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = 0;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
return micros() - start;
}
unsigned long testFastLines(uint16_t color1, uint16_t color2) {
unsigned long start;
int x, y, w = tft.width(), h = tft.height();
tft.fillScreen(BLACK);
start = micros();
for(y=0; y<h; y+=5) tft.drawFastHLine(0, y, w, color1);
for(x=0; x<w; x+=5) tft.drawFastVLine(x, 0, h, color2);
return micros() - start;
}
unsigned long testRects(uint16_t color) {
unsigned long start;
int n, i, i2,
cx = tft.width() / 2,
cy = tft.height() / 2;
tft.fillScreen(BLACK);
n = min(tft.width(), tft.height());
start = micros();
for(i=2; i<n; i+=6) {
i2 = i / 2;
tft.drawRect(cx-i2, cy-i2, i, i, color);
}
return micros() - start;
}
unsigned long testFilledRects(uint16_t color1, uint16_t color2) {
unsigned long start, t = 0;
int n, i, i2,
cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;
tft.fillScreen(BLACK);
n = min(tft.width(), tft.height());
for(i=n; i>0; i-=6) {
i2 = i / 2;
start = micros();
tft.fillRect(cx-i2, cy-i2, i, i, color1);
t += micros() - start;
// Outlines are not included in timing results
tft.drawRect(cx-i2, cy-i2, i, i, color2);
}
return t;
}
unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
unsigned long start;
int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;
tft.fillScreen(BLACK);
start = micros();
for(x=radius; x<w; x+=r2) {
for(y=radius; y<h; y+=r2) {
tft.fillCircle(x, y, radius, color);
}
}
return micros() - start;
}
unsigned long testCircles(uint8_t radius, uint16_t color) {
unsigned long start;
int x, y, r2 = radius * 2,
w = tft.width() + radius,
h = tft.height() + radius;
// Screen is not cleared for this one -- this is
// intentional and does not affect the reported time.
start = micros();
for(x=0; x<w; x+=r2) {
for(y=0; y<h; y+=r2) {
tft.drawCircle(x, y, radius, color);
}
}
return micros() - start;
}
unsigned long testTriangles() {
unsigned long start;
int n, i, cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;
tft.fillScreen(BLACK);
n = min(cx, cy);
start = micros();
for(i=0; i<n; i+=5) {
tft.drawTriangle(
cx , cy - i, // peak
cx - i, cy + i, // bottom left
cx + i, cy + i, // bottom right
tft.color565(0, 0, i));
}
return micros() - start;
}
unsigned long testFilledTriangles() {
unsigned long start, t = 0;
int i, cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;
tft.fillScreen(BLACK);
start = micros();
for(i=min(cx,cy); i>10; i-=5) {
start = micros();
tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
tft.color565(0, i, i));
t += micros() - start;
tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
tft.color565(i, i, 0));
}
return t;
}
unsigned long testRoundRects() {
unsigned long start;
int w, i, i2,
cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;
tft.fillScreen(BLACK);
w = min(tft.width(), tft.height());
start = micros();
for(i=0; i<w; i+=6) {
i2 = i / 2;
tft.drawRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(i, 0, 0));
}
return micros() - start;
}
unsigned long testFilledRoundRects() {
unsigned long start;
int i, i2,
cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;
tft.fillScreen(BLACK);
start = micros();
for(i=min(tft.width(), tft.height()); i>20; i-=6) {
i2 = i / 2;
tft.fillRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(0, i, 0));
}
return micros() - start;
}
// Copy string from flash to serial port
// Source string MUST be inside a PSTR() declaration!
void progmemPrint(const char *str) {
char c;
while(c = pgm_read_byte(str++)) Serial.print(c);
}
// Same as above, with trailing newline
void progmemPrintln(const char *str) {
progmemPrint(str);
Serial.println();
}
Calibration¶
Ne fonctionne pas :(
///////////////
// Sketch: Calibrate - Calibrate TFT SPFD5408 Touch
// Author: Joao Lopes F. - joaolopesf@gmail.com
//
// Versions:
// - 0.9.0 First beta - July 2015
// - 0.9.1 Rotation for Mega
// Comments:
// Show the calibration parameters to put in your code
// Please use a small like the eraser on a pencil for best results
//
// Code for buttons, based on Adafruit arduin_o_phone example
///////////////
// library SPFD5408
#include <SPFD5408_Adafruit_GFX.h> // Core graphics library
#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
#include <SPFD5408_TouchScreen.h> // Touch library
// Calibrates value
#define SENSIBILITY 300
#define MINPRESSURE 10
#define MAXPRESSURE 1000
//These are the pins for the shield!
#define YP A1
#define XM A2
#define YM 7
#define XP 6
/*
//Macros replaced by variables
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
*/
short TS_MINX=150;
short TS_MINY=120;
short TS_MAXX=920;
short TS_MAXY=940;
// Init TouchScreen:
TouchScreen ts = TouchScreen(XP, YP, XM, YM, SENSIBILITY);
// LCD Pin
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4 // Optional : otherwise connect to Arduino's reset pin
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Init LCD
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// Dimensions
uint16_t width = 0;
uint16_t height = 0;
// Buttons
#define BUTTONS 3
#define BUTTON_CLEAR 0
#define BUTTON_SHOW 1
#define BUTTON_RESET 2
Adafruit_GFX_Button buttons[BUTTONS];
uint16_t buttons_y = 0;
//-- Setup
void setup(void) {
// Serial por for debug, not works if shield is plugged in arduino
// Serial.begin(9600);
// Inicialize the controller
tft.reset();
tft.begin(0x9341);
tft.setRotation(0); // Need for the Mega, please changed for your choice or rotation initial
width = tft.width() - 1;
height = tft.height() - 1;
// Debug
// Serial.println(F("TFT LCD test"));
// Serial.print("TFT size is ");
// Serial.print(tft.width());
// Serial.print("x");
// Serial.println(tft.height());
// UI
initializeButtons();
// Border
drawBorder();
// Initial screen
tft.setCursor (55, 50);
tft.setTextSize (3);
tft.setTextColor(RED);
tft.println("SPFD5408");
tft.setCursor (65, 85);
tft.println("Library");
tft.setCursor (55, 150);
tft.setTextSize (2);
tft.setTextColor(BLACK);
tft.println("Calibration");
tft.setCursor (80, 250);
tft.setTextSize (1);
tft.setTextColor(BLACK);
tft.println("Touch to proceed");
// Wait touch
waitOneTouch();
// Calibrate it
calibrate_TS();
// Wait touch
waitOneTouch();
// Calibration
showCalibration();
}
// -- Loop
void loop()
{
// Test of calibration
TSPoint p;
// Wait a touch
digitalWrite(13, HIGH);
p = waitOneTouch();
digitalWrite(13, LOW);
// Map of values
// p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
// p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
p.x = mapXValue(p);
p.y = mapYValue(p);
// Draw a point
tft.fillCircle(p.x, p.y, 3, BLUE);
// Show touch screen point (TSPOINT)
showTouched(p);
// Buttons
// Go thru all the buttons, checking if they were pressed
for (uint8_t b=0; b<BUTTONS; b++) {
if (buttons[b].contains(p.x, p.y)) {
// Action
switch (b) {
case BUTTON_CLEAR:
// Clear
showCalibration();
break;
case BUTTON_SHOW:
// Clear
showResults();
tft.println();
tft.println("Touch to proceed");
waitOneTouch();
showCalibration();
break;
case BUTTON_RESET:
// New calibration
calibrate_TS();
waitOneTouch();
showCalibration();
break;
}
}
}
}
// Calibration of Touch Screen (resistive)
void calibrate_TS(void) {
// Based in code posted in https://forum.arduino.cc/index.php?topic=223769.15
TSPoint p1, p2;
int16_t temp;
int32_t tempL;
tft.fillScreen(BLACK);
tft.fillCircle(4,4,4,WHITE); //show the first point
tft.setCursor(5, 30);
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.println("Please touch the dot");
uint16_t limit = 40;
do {
p1 = waitOneTouch();
} while (!(mapXValue(p1) < limit && mapYValue(p1) < limit));
tft.fillScreen(BLACK);
tft.fillCircle(234,314,4,WHITE); //show the 2nd point
tft.setCursor(50, 280);
tft.println("Please touch the other dot");
delay (500); // debunce
do {
p2 = waitOneTouch();
} while (!(mapXValue(p2) > (width - limit) && mapYValue(p2) > (height - limit)));
tft.fillScreen(BLACK);
delay (300);
temp=p2.x-p1.x; // Calculate the new coefficients, get X difference
tempL=((long)temp*1024)/(tft.width()-20);
TS_MINX=p1.x-( (tempL*10)>>10);// 10 pixels du bord
TS_MAXX=p1.x+( (tempL*tft.width())>>10);// 220 pixels entre points
temp=p2.y-p1.y; // ¨get Y difference
tempL=((long)temp*1024)/(tft.height()-20);
TS_MINY=p1.y-( (tempL*10)>>10);// 10 pixels du bord
TS_MAXY=TS_MINY+( (tempL*tft.height())>>10);
// Show results
showResults();
// p1.x = map(p1.x, TS_MAXX,TS_MINX, tft.width(), 0);
// p1.y = map(p1.y, TS_MAXY,TS_MINY, tft.height(), 0);
// p2.x = map(p2.x, TS_MAXX,TS_MINX, tft.width(), 0);
// p2.y = map(p2.y, TS_MAXY,TS_MINY, tft.height(), 0);
p1.x = mapXValue(p1);
p1.y = mapYValue(p1);
p2.x = mapXValue(p2);
p2.y = mapYValue(p2);
tft.println();
tft.println("Last touched points: ");
tft.print("Pt 1: ");tft.print(p1.x);tft.print(" : ");tft.println(p1.y);
tft.print("Pt 2: ");tft.print(p2.x);tft.print(" : ");tft.println(p2.y);
tft.println();
// Wait a touch
tft.println("Touch to proceed");
waitOneTouch();
}
// wait 1 touch to return the point
TSPoint waitOneTouch() {
TSPoint p;
do {
p= ts.getPoint();
pinMode(XM, OUTPUT); //Pins configures again for TFT control
pinMode(YP, OUTPUT);
} while((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));
return p;
}
// Draw a border
void drawBorder () {
uint16_t width = tft.width() - 1;
uint16_t height = tft.height() - 1;
uint8_t border = 10;
tft.fillScreen(RED);
tft.fillRect(border, border, (width - border * 2), (height - border * 2), WHITE);
}
// Show a screen of calibration
void showCalibration() {
// Clear
tft.fillScreen(BLACK);
tft.setTextSize (1);
// Header
tft.fillRect(0, 0, width, 10, RED);
tft.setCursor (40, 0);
tft.setTextColor(WHITE);
tft.println("*** Test of calibration ***");
// Footer
TSPoint p; // Only for show initial values
p.x=0;
p.y=0;
p.z=0;
showTouched(p);
// Buttons
for (uint8_t i=0; i<3; i++) {
buttons[i].drawButton();
}
}
// Show the coordinates
void showTouched(TSPoint p) {
uint8_t w = 40; // Width
uint8_t h = 10; // Heigth
uint8_t x = (width - (w*2)); // X
uint8_t y = 11; // Y
tft.fillRect(x, y, w*2, h, WHITE); // For cleanup
tft.drawRect(x, y, w, h, RED); // For X
tft.drawRect(x+w+2, y, w*2, h, RED); // For Y
tft.setTextColor(BLACK);
tft.setCursor(x+2, y + 1);
tft.print("X: ");
showValue(p.x);
tft.setCursor(x+2+w+2, y + 1);
tft.print("Y: ");
showValue(p.y);
}
// Show a value of TSPoint
void showValue (uint16_t value) {
if (value < 10)
tft.print("00");
if (value < 100)
tft.print("0");
tft.print(value);
}
// Show results of calibration
void showResults() {
tft.fillScreen(BLACK);
// Header
tft.fillRect(0, 0, width, 10, RED);
tft.setCursor (40, 0);
tft.setTextColor(WHITE);
tft.println("*** Results of calibration ***");
// Results
tft.setCursor(5, 30);
tft.setTextSize(2);
tft.println("After calibration: ");
tft.print("TS_MINX= ");tft.println(TS_MINX);
tft.print("TS_MINY= ");tft.println(TS_MINY);
tft.println();
tft.print("TS_MAXX= ");tft.println(TS_MAXX);
tft.print("TS_MAXY= ");tft.println(TS_MAXY);
}
// Initialize buttons
void initializeButtons() {
uint16_t x = 40;
uint16_t y = height - 20;
uint16_t w = 75;
uint16_t h = 20;
uint8_t spacing_x = 5;
uint8_t textSize = 1;
char buttonlabels[3][20] = {"Clear", "Show", "Recalib."};
uint16_t buttoncolors[15] = {RED, BLUE, RED};
for (uint8_t b=0; b<3; b++) {
buttons[b].initButton(&tft, // TFT object
x+b*(w+spacing_x), y, // x, y,
w, h, WHITE, buttoncolors[b], WHITE, // w, h, outline, fill,
buttonlabels[b], textSize); // text
}
// Save the y position to avoid draws
buttons_y = y;
}
// Map the coordinate X
uint16_t mapXValue(TSPoint p) {
uint16_t x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
//Correct offset of touch. Manual calibration
//x+=1;
return x;
}
// Map the coordinate Y
uint16_t mapYValue(TSPoint p) {
uint16_t y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
//Correct offset of touch. Manual calibration
//y-=2;
return y;
}