Indice
Descrizione
In questo episodio del tutorial di Arduino, impareremo a usare il display TFT touchscreen. Vedremo come installare le librerie necessarie per farlo funzionare. Realizzeremo, inoltre, alcuni semplici esempi di sketch e circuiti per disegnare sullo schermo e utilizzare il touchscreen.
#include <MCUFRIEND_kbv.h>
#define MARGIN 10
#define INPUT_PIN A15
#define THRESHOLD 3
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
MCUFRIEND_kbv tft;
uint16_t colors[] = {RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA};
int colorsLength = sizeof(colors) / sizeof(uint16_t);
int sliderWidth;
int colorWidth;
int lastValue = -1;
void setup() {
tft.begin(tft.readID());
tft.setRotation(1);
sliderWidth = tft.width() - 2 * MARGIN;
colorWidth = sliderWidth / colorsLength;
initScreen();
}
void loop() {
int value = analogRead(INPUT_PIN);
if (value < lastValue - THRESHOLD || value > lastValue + THRESHOLD) {
int cursor = map(value, 0, 1023, 0, sliderWidth);
int colorIndex = selectColor(cursor);
if (colorIndex != -1) {
drawCursor(colors[colorIndex], cursor);
printValue(colors[colorIndex], "INPUT", String(value));
}
lastValue = value;
}
delay(1);
}
void initScreen() {
tft.fillScreen(BLACK);
tft.setCursor(MARGIN, MARGIN);
tft.print("Tutorial di Arduino");
tft.setCursor(MARGIN, MARGIN+15);
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.print("Display TFT Touch");
tft.drawLine(MARGIN, MARGIN + 40, tft.width() - MARGIN, MARGIN + 40, WHITE);
tft.fillRect(MARGIN, 122, sliderWidth, 2, WHITE);
for (int i = 0; i < colorsLength; i++) {
tft.fillRect(MARGIN + i * colorWidth, 117, 2, 5, WHITE);
tft.fillRect(MARGIN + i * colorWidth, 130, colorWidth, 30, colors[i]);
}
tft.fillRect(MARGIN + colorsLength * colorWidth - 2, 117, 2, 5, WHITE);
}
int selectColor(int cursor) {
for (int i = 0; i < colorsLength; i++) {
if (cursor >= i * colorWidth && cursor < (i + 1) * colorWidth) {
return i;
}
}
return -1;
}
void drawCursor(uint16_t color, int cursor) {
int pos = MARGIN + cursor;
tft.fillRect(0, 85, tft.width(), 26, BLACK);
tft.fillRect(pos - 2, 85, 5, 15, color);
tft.fillTriangle(pos - 9, 100, pos, 110, pos + 9, 100, color);
}
void printValue(uint16_t color, String label, String value) {
tft.setTextColor(color);
tft.setTextSize(3);
tft.setCursor(MARGIN, tft.height() - MARGIN - 22);
tft.print(label + ": ");
tft.fillRect(MARGIN + 125, tft.height() - MARGIN - 23, tft.width(), 25, BLACK);
tft.setCursor(MARGIN + 130, tft.height() - MARGIN - 22);
tft.print(value);
}
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
#define MARGIN 10
#define RED_PIN 46
#define GREEN_PIN 45
#define BLUE_PIN 44
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9595
const int TS_LEFT=920,TS_RT=139,TS_TOP=93,TS_BOT=907;
MCUFRIEND_kbv tft;
TouchScreen touch = TouchScreen(XP, YP, XM, YM, 300);
uint16_t colors[] = {RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA};
int colorsLength = sizeof(colors) / sizeof(uint16_t);
String colorNames[] {"rosso", "giallo", "verde", "ciano", "blu", "magenta"};
int sliderWidth;
int colorWidth;
int lastValue = -1;
void setup() {
tft.begin(tft.readID());
tft.setRotation(1);
sliderWidth = tft.width() - 2 * MARGIN;
colorWidth = sliderWidth / colorsLength;
initScreen();
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
int x, y;
bool isTouch = readTouch(x, y);
if (isTouch && (x > MARGIN && x < tft.width() - MARGIN && y > 130 && y < 160)) {
int colorIndex = selectColor(x - MARGIN);
if (colorIndex != -1 && colorIndex != lastValue) {
int cursor = colorWidth * (colorIndex + 0.5);
drawCursor(colors[colorIndex], cursor);
printValue(colors[colorIndex], "OUTPUT", colorNames[colorIndex]);
showColor(colors[colorIndex]);
}
lastValue = colorIndex;
}
delay(1);
}
void initScreen() {
tft.fillScreen(BLACK);
tft.setCursor(MARGIN, MARGIN);
tft.print("Tutorial di Arduino");
tft.setCursor(MARGIN, MARGIN+15);
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.print("Display TFT Touch");
tft.drawLine(MARGIN, MARGIN + 40, tft.width() - MARGIN, MARGIN + 40, WHITE);
tft.fillRect(MARGIN, 122, sliderWidth, 2, WHITE);
for (int i = 0; i < colorsLength; i++) {
tft.fillRect(MARGIN + i * colorWidth, 117, 2, 5, WHITE);
tft.fillRect(MARGIN + i * colorWidth, 130, colorWidth, 30, colors[i]);
}
tft.fillRect(MARGIN + colorsLength * colorWidth - 2, 117, 2, 5, WHITE);
}
int selectColor(int cursor) {
for (int i = 0; i < colorsLength; i++) {
if (cursor >= i * colorWidth && cursor < (i + 1) * colorWidth) {
return i;
}
}
return -1;
}
void drawCursor(uint16_t color, int cursor) {
int pos = MARGIN + cursor;
tft.fillRect(0, 85, tft.width(), 26, BLACK);
tft.fillRect(pos - 2, 85, 5, 15, color);
tft.fillTriangle(pos - 9, 100, pos, 110, pos + 9, 100, color);
}
void printValue(uint16_t color, String label, String value) {
tft.setTextColor(color);
tft.setTextSize(3);
tft.setCursor(MARGIN, tft.height() - MARGIN - 22);
tft.print(label + ": ");
tft.fillRect(MARGIN + 125, tft.height() - MARGIN - 23, tft.width(), 25, BLACK);
tft.setCursor(MARGIN + 130, tft.height() - MARGIN - 22);
tft.print(value);
}
bool readTouch(int &x, int &y) {
TSPoint point = touch.getPoint();
pinMode(YP, OUTPUT);
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH);
digitalWrite(XM, HIGH);
if (point.z > 10 && point.z < 1000) {
x = map(point.y, TS_TOP, TS_BOT, 0, tft.width()-1);
y = map(point.x, TS_RT, TS_LEFT, 0, tft.height()-1);
return true;
}
return false;
}
void showColor(uint16_t color) {
int r = map((color & 0xF800) >> 11, 0, 0x1F, 0, 255);
int g = map((color & 0x7E0) >> 5, 0, 0x3F, 0, 255);
int b = map(color & 0x1F, 0, 0x1F, 0, 255);
analogWrite(RED_PIN, r);
analogWrite(GREEN_PIN, g);
analogWrite(BLUE_PIN, b);
}