驴友花雕 发表于 2021-7-7 10:37:58

实验场景图


驴友花雕 发表于 2021-7-7 10:41:56


修改后的项目18,实验程序

/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目十八:测试并使用 TFT 触摸屏驱动程序 0x9341
模块直插,引脚用法如下:
LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
Arduino Uno 8 9 2 3 4 5 6 7
*/

#include <Adafruit_GFX.h>
#include <TouchScreen.h>
#include <Adafruit_TFTLCD.h>

#define YP A2// must be an analog pin, use "An" notation!
#define XM A1// must be an analog pin, use "An" notation!
#define YM 6   // can be a digital pin
#define XP 7   // can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 850
#define TS_MAXY 891

//SPI Communication
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4

//Color Definitons
#define BLACK   0x0000
#define WHITE   0xFFFF
#define RED   0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF

#define MINPRESSURE 1
#define MAXPRESSURE 1000

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
// Pins A2-A6
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);

//2.4 = 240 x 320
//Height 319 to fit on screen

//Size of key containers 70px
#define BOXSIZE 70

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

//Container variables for touch coordinates
int X, Y, Z;

//Screen height without hidden pixel
double tHeight = tft.height() - 1;
//Centering the mid square
double center = (tft.width() / 2) - (BOXSIZE / 2);
//Space between squares
double padding = 10;
//Position of squares to the left and right of center
double fromCenter = BOXSIZE + padding;
//Second row Y-Axis position
double secondRow = BOXSIZE + padding;
//Third row Y-Axis position
double thirdRow = secondRow + BOXSIZE + padding;
//Fourth row Y-Axis position
double fourthRow = thirdRow + BOXSIZE + padding;
//Y-Axis align for all squares
double verticalAlign = (tHeight - ((BOXSIZE * 4) + (padding * 3))) / 2;
//Left column starting x posision
double leftColPositionX = center - fromCenter;
//Mid column starting x posision
double midColPositionX = center;
//Right column starting x posision
double rightColPositionX = center + fromCenter;

void setup() {
Serial.begin(9600);
tft.reset();
tft.begin(0x9341);
//Background color
tft.fillScreen(BLACK);
Serial.println(F("准备就绪OK!"));
createButtons();
Serial.println(F("请按 TFT 屏幕上的任意按钮:"));
}

void loop() {
retrieveTouch();
int boxHeightRow1 = verticalAlign + BOXSIZE;
int boxHeightRow2 = secondRow + BOXSIZE;
int boxHeightRow3 = thirdRow + BOXSIZE;
int boxHeightRow4 = fourthRow + BOXSIZE;

if (Z > MINPRESSURE && Z < MAXPRESSURE) {
    //Check if element clicked is in left column
    if (X > leftColPositionX && X < (leftColPositionX + BOXSIZE)) {
      //Check if element clicked is in row 1
      if (Y > verticalAlign) {
      if (Y < boxHeightRow1) {
          Serial.println("1");
          delay(150);
      }
      //Check if element clicked is in row 2
      else if (Y < boxHeightRow2) {
          Serial.println("4");
          delay(150);
      }
      //Check if element clicked is in row 3
      else if (Y < boxHeightRow3) {
          Serial.println("7");
          delay(150);
      }
      //Check if element clicked is in row 4
      else if (Y < boxHeightRow4) {
          Serial.println("0");
          delay(150);
      }
      }
      //Check if element clicked is in mid column
    } else if (X > midColPositionX && X < (midColPositionX + BOXSIZE)) {
      //Check if element clicked is in row 1
      if (Y > verticalAlign) {
      if (Y < boxHeightRow1) {
          Serial.println("2");
          delay(150);
      }
      //Check if element clicked is in row 2
      else if (Y < boxHeightRow2) {
          Serial.println("5");
          delay(150);
      }
      //Check if element clicked is in row 3
      else if (Y < boxHeightRow3) {
          Serial.println("8");
          delay(150);
      }
      //Check if element clicked is in row 4
      else if (Y < boxHeightRow4) {
          Serial.println("0");
          delay(150);
      }
      }
      //Check if element clicked is in third column
    } else if (X > rightColPositionX && X < (rightColPositionX + BOXSIZE)) {
      if (Y > verticalAlign) {
      //Check if element clicked is in row 1
      if (Y < boxHeightRow1) {
          Serial.println("3");
          delay(150);
      }
      //Check if element clicked is in row 2
      else if (Y < boxHeightRow2) {
          Serial.println("6");
          delay(150);
      }
      //Check if element clicked is in row 3
      else if (Y < boxHeightRow3) {
          Serial.println("9");
          delay(150);
      }
      //Check if element clicked is in row 3
      else if (Y < boxHeightRow4) {
          Serial.println(".");
          delay(150);
      }
      }
    }
}
}

void retrieveTouch()
{
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);

//If sharing pins, you'll need to fix the directions of the touchscreen pins
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);

//Scale from 0->1023 to tft.width
X = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
Y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
Z = p.z;
}

void createButtons() {
//(initial x,initial y,width,height,color)
double secondRowVertialAlign = secondRow + verticalAlign;
double thirdRowVertialAlign = thirdRow + verticalAlign;
double fourthRowVertialAlign = fourthRow + verticalAlign;

/***Draw squares with specified dimensions and position (No Fill)***/
//(initial x,initial y,width,height,color)
//First Row
tft.drawRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);
tft.drawRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);
tft.drawRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);

//Second Row
tft.drawRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);
tft.drawRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);
tft.drawRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);

//Third Row
tft.drawRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);
tft.drawRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);
tft.drawRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);

//Fourth Row
tft.drawRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, RED);
tft.drawRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, RED);
}

驴友花雕 发表于 2021-7-7 10:45:49

触摸屏幕方格后,实验串口返回情况









驴友花雕 发表于 2021-7-7 11:24:54

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏项目十九:Arduino Uno 数字键盘(浅灰色)
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目十九:Arduino Uno 数字键盘(浅灰色)
模块直插,引脚用法如下:
LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
Arduino Uno 8 9 2 3 4 5 6 7
*/

#include <Adafruit_GFX.h>
#include <TouchScreen.h>
#include <Adafruit_TFTLCD.h>

#define YP A2// must be an analog pin, use "An" notation!
#define XM A1// must be an analog pin, use "An" notation!
#define YM 6   // can be a digital pin
#define XP 7// can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 850
#define TS_MAXY 891

//SPI Communication
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4

//Color Definitons
#define BLACK   0x0000
#define BLUE      0x001F
#define GREY      0xCE79
#define LIGHTGREY 0xDEDB
#define MINPRESSURE 1
#define MAXPRESSURE 1000

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
// Pins A2-A6
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);

//Size of key containers 70px
#define BOXSIZE 70

//2.4 = 240 x 320
//Height 319 to fit on screen

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

//Container variables for touch coordinates
int X, Y, Z;

//Screen height without hidden pixel
double tHeight = tft.height() - 1;
//Centering the mid square
double center = (tft.width() / 2) - (BOXSIZE / 2);
//Space between squares
double padding = 10;
//Position of squares to the left and right of center
double fromCenter = BOXSIZE + padding;
//Second row Y-Axis position
double secondRow = BOXSIZE + padding;
//Third row Y-Axis position
double thirdRow = secondRow + BOXSIZE + padding;
//Fourth row Y-Axis position
double fourthRow = thirdRow + BOXSIZE + padding;
//Y-Axis align for all squares
double verticalAlign = (tHeight - ((BOXSIZE * 4) + (padding * 3))) / 2;
//Left column starting x posision
double leftColPositionX = center - fromCenter;
//Mid column starting x posision
double midColPositionX = center;
//Right column starting x posision
double rightColPositionX = center + fromCenter;

void setup() {
Serial.begin(9600);
tft.reset();
tft.begin(0x9341);

//Background color
tft.fillScreen(LIGHTGREY);

createButtons();
insertNumbers();
Serial.println(F("Press any button on the TFT screen: "));
}

void loop() {
retrieveTouch();
int boxHeightRow1 = verticalAlign + BOXSIZE;
int boxHeightRow2 = secondRow + BOXSIZE;
int boxHeightRow3 = thirdRow + BOXSIZE;
int boxHeightRow4 = fourthRow + BOXSIZE;

if (Z > MINPRESSURE && Z < MAXPRESSURE) {
    //Check if element clicked is in left column
    if (X > leftColPositionX && X < (leftColPositionX + BOXSIZE)) {
      //Check if element clicked is in row 1
      if (Y > verticalAlign) {
      if (Y < boxHeightRow1) {
          Serial.println("1");
          delay(150);
      }
      //Check if element clicked is in row 2
      else if (Y < boxHeightRow2) {
          Serial.println("4");
          delay(150);
      }
      //Check if element clicked is in row 3
      else if (Y < boxHeightRow3) {
          Serial.println("7");
          delay(150);
      }
      //Check if element clicked is in row 4
      else if (Y < boxHeightRow4) {
          Serial.println("0");
          delay(150);
      }
      }
      //Check if element clicked is in mid column
    } else if (X > midColPositionX && X < (midColPositionX + BOXSIZE)) {
      //Check if element clicked is in row 1
      if (Y > verticalAlign) {
      if (Y < boxHeightRow1) {
          Serial.println("2");
          delay(150);
      }
      //Check if element clicked is in row 2
      else if (Y < boxHeightRow2) {
          Serial.println("5");
          delay(150);
      }
      //Check if element clicked is in row 3
      else if (Y < boxHeightRow3) {
          Serial.println("8");
          delay(150);
      }
      //Check if element clicked is in row 4
      else if (Y < boxHeightRow4) {
          Serial.println("0");
          delay(150);
      }
      }
      //Check if element clicked is in third column
    } else if (X > rightColPositionX && X < (rightColPositionX + BOXSIZE)) {
      if (Y > verticalAlign) {
      //Check if element clicked is in row 1
      if (Y < boxHeightRow1) {
          Serial.println("3");
          delay(150);
      }
      //Check if element clicked is in row 2
      else if (Y < boxHeightRow2) {
          Serial.println("6");
          delay(150);
      }
      //Check if element clicked is in row 3
      else if (Y < boxHeightRow3) {
          Serial.println("9");
          delay(150);
      }
      //Check if element clicked is in row 3
      else if (Y < boxHeightRow4) {
          Serial.println(".");
          delay(150);
      }
      }
    }
}
}

void retrieveTouch()
{
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);

//If sharing pins, you'll need to fix the directions of the touchscreen pins
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);

//Scale from 0->1023 to tft.width
X = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
Y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
Z = p.z;
}

void createButtons() {
//(initial x,initial y,width,height,color)
double secondRowVertialAlign = secondRow + verticalAlign;
double thirdRowVertialAlign = thirdRow + verticalAlign;
double fourthRowVertialAlign = fourthRow + verticalAlign;

/***Draw filled squares with specified dimensions and position***/
//First Row
tft.fillRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, GREY);
tft.fillRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, GREY);
tft.fillRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, GREY);

//Second Row
tft.fillRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
tft.fillRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
tft.fillRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, GREY);

//Third Row
tft.fillRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
tft.fillRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
tft.fillRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREY);

//Fourth Row
tft.fillRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, GREY);
tft.fillRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, GREY);

/***Draw Borders around squares***/
//First Row
tft.drawRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK);
tft.drawRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK);
tft.drawRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK);

//Second Row
tft.drawRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
tft.drawRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
tft.drawRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);

//Third Row
tft.drawRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
tft.drawRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
tft.drawRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);

//Fourth Row
tft.drawRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, BLACK);
tft.drawRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
}

void insertNumbers() {
//Centers text horizontally on all three columns
double leftColCursorX   = leftColPositionX + (BOXSIZE / 3);
double midColCursorX    = midColPositionX+ (BOXSIZE / 3);
double rightColCursorX= rightColPositionX + (BOXSIZE / 3);
//Centers text horizontally on all four rows
double firstRowCursorY= verticalAlign + (BOXSIZE / 3);
double secondRowCursorY = secondRow + firstRowCursorY;
double thirdRowCursorY= thirdRow+ firstRowCursorY;
double fourthRowCursorY = fourthRow + firstRowCursorY;

tft.setTextSize(4);
tft.setTextColor(BLACK);

//Insert Number 1
tft.setCursor(leftColCursorX, firstRowCursorY);
tft.println("1");

//Insert Number 2
tft.setCursor(midColCursorX, firstRowCursorY);
tft.println("2");

//Insert Number 3
tft.setCursor(rightColCursorX, firstRowCursorY);
tft.println("3");

//Insert Number 4
tft.setCursor(leftColCursorX, secondRowCursorY);
tft.println("4");

//Insert Number 5
tft.setCursor(midColCursorX, secondRowCursorY);
tft.println("5");

//Insert Number 6
tft.setCursor(rightColCursorX, secondRowCursorY);
tft.println("6");

//Insert Number 7
tft.setCursor(leftColCursorX, thirdRowCursorY);
tft.println("7");

//Insert Number 8
tft.setCursor(midColCursorX, thirdRowCursorY);
tft.println("8");

//Insert Number 9
tft.setCursor(rightColCursorX, thirdRowCursorY);
tft.println("9");

//Insert Number 0
tft.setCursor(leftColPositionX + BOXSIZE, fourthRowCursorY);
tft.println("0");

//Insert Period Character
tft.setCursor(rightColCursorX, fourthRowCursorY);
tft.println(".");
}

驴友花雕 发表于 2021-7-7 11:28:17

实验场景图


驴友花雕 发表于 2021-7-7 18:05:46

本帖最后由 驴友花雕 于 2021-7-7 18:28 编辑

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十:串口打印触摸屏检测的数据

/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十:串口打印触摸屏检测的数据
模块直插,引脚用法如下:
LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
Arduino Uno 8 9 2 3 4 5 6 7
*/

#include <stdint.h>
#include "TouchScreen.h" //导入触摸驱动库

#define YP A2
#define XM A1
#define YM 6
#define XP 7

// 为了更好的压力精度,我们需要知道阻力
// 在 X+ 和 X- 之间使用任何万用表读取它
// 对于我们使用的那个,它在 X 板上的 300 欧姆
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

void setup(void) {
Serial.begin(9600);
}

void loop(void) {
TSPoint p = ts.getPoint();
if (p.z > ts.pressureThreshhold) {
    Serial.print("X = "); Serial.print(p.x);
    Serial.print("\tY = "); Serial.print(p.y);
    Serial.print("\tPressure = "); Serial.println(p.z);
}
delay(100);
}
TSPoint p = ts.getPoint(); 将长度(x)、宽度(y)和压力(z)存储到p对象。

驴友花雕 发表于 2021-7-7 18:07:10



驴友花雕 发表于 2021-7-7 19:17:43

Adafruit_TFTLCD库相关函数

class Adafruit_TFTLCD : public Adafruit_GFX {

public:
Adafruit_TFTLCD(uint8_t cs, uint8_t cd, uint8_t wr, uint8_t rd, uint8_t rst);
Adafruit_TFTLCD(void);

void begin(uint16_t id = 0x9325);
void drawPixel(int16_t x, int16_t y, uint16_t color);
void drawFastHLine(int16_t x0, int16_t y0, int16_t w, uint16_t color);
void drawFastVLine(int16_t x0, int16_t y0, int16_t h, uint16_t color);
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t c);
void fillScreen(uint16_t color);
void reset(void);
void setRegisters8(uint8_t *ptr, uint8_t n);
void setRegisters16(uint16_t *ptr, uint8_t n);
void setRotation(uint8_t x);

void setAddrWindow(int x1, int y1, int x2, int y2);
void pushColors(uint16_t *data, uint8_t len, boolean first);

uint16_t color565(uint8_t r, uint8_t g, uint8_t b),
      readPixel(int16_t x, int16_t y), readID(void);
uint32_t readReg(uint8_t r);

private:
void init(),

驴友花雕 发表于 2021-7-7 19:23:55

1、创建对象
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

void setup(void) {
tft.reset();
tft.begin(0x9341);
}
首先创建了一个Adafruit_TFTLCD对象,名为tft,管脚定义这里省去了。begin方法中的0x9341表示改TFT LCD的驱动为ILI9341,其它的这里不做介绍
2、屏幕
void fillScreen(uint16_t color);

uint16_t width();//屏幕的宽度
uint16_t height();//屏幕的高度
全屏填充颜色color,再次之前显示的内容会被挡住
案例
tft.fillScreen(BLACK);
delay(1000);
tft.fillScreen(RED);
delay(1000);
tft.fillScreen(BLUE);
   delay(1000);
3、点
void drawPixel(int16_t x, int16_t y, uint16_t color);
在点(x,y)上画一个颜色为color的像素点。
案例
tft.drawPixel(1,1,RED);
tft.drawPixel(10,10,RED);
tft.drawPixel(20,20,RED);
tft.drawPixel(40,40,RED);
tft.drawPixel(60,60,RED);

驴友花雕 发表于 2021-7-7 19:35:16

4、线

void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
void drawFastHLine(int16_t x0, int16_t y0, int16_t w, uint16_t color);
void drawFastVLine(int16_t x0, int16_t y0, int16_t h, uint16_t color);

最简单的是两点确定一条直线,当然也可以确定一个点、方向、长度,后两个是画水平线或者铅锤线。

案例

tft.drawFastHLine(10,10,170,RED);
tft.drawFastVLine(10,10,170,RED);
tft.drawLine(10,10,100,180,RED);
5、矩形&&圆角矩形

void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t c);
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t c);

void drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t radius, uint16_t color);
void fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t radius, uint16_t color);
drawRect的矩形直画出边框,内部不填充,如果需要内部填充颜色则需要使用fillRect

圆角矩形则是多了一个设置圆角半径的参数raduis,是否填充与上面一样。

案例

   tft.drawRect(10,10,150,100,RED);
   tft.fillRect(10,120,150,100,RED);

tft.drawRoundRect(10,10,150,100,10,RED);
tft.fillRoundRect(10,120,150,100,10,RED);

6、圆形

void drawCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color);
void fillCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color);

这个就比较简单,圆心坐标(x0,y0),半径r,颜色color

案例

tft.drawCircle(100,100,50,WHITE);
tft.fillCircle(100,260,50,BLUE);

驴友花雕 发表于 2021-7-7 19:45:30

7、三角形

void drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
void fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);

三角形需要确定三个顶点以及颜色

案例

tft.drawTriangle(10,10,100,15,180,100,GREEN);
tft.fillTriangle(10,110,100,115,180,200,GREEN);

8、字符&&英文文本

void drawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint16_t bg, uint8_t size);

void setCursor(uint16_t x0, uint16_t y0);//字体左上角顶点坐标
void setTextColor(uint16_t color); //字体前景色
void setTextColor(uint16_t color, uint16_t backgroundcolor);//字体前景色与背景色
void setTextSize(uint8_t size);//字体大小放法因子
void setTextWrap(boolean w);        //是否自动换行,默认为true,滚动显示设置为false

drawChar只能显示单个字符,需要确定左上角顶点坐标(x,y),字符c,颜色color,前景色color,背景色bg,大小size,大小为1时表示5 * 8像素,为2就表示10 * 16

自定义字体不支持背景色,可在此之前绘制填充颜色的形状,如圆角矩形

案例

tft.fillScreen(GREEN);
tft.drawChar(150,10,'A',RED,WHITE,5);

tft.setCursor(10,50);
tft.print("AB 3.14");    //默认前景色white、无背景色、大小为1

tft.setCursor(10,80);
tft.setTextSize( 4);
tft.print("AB 3.14");

tft.setCursor(10,115);
tft.setTextColor(RED); //背景色不做设置
tft.setTextSize( 4);
tft.print("AB你好3.141516");

tft.setCursor(10,180);
tft.setTextColor(RED, WHITE);
tft.setTextSize( 4);
tft.setTextWrap(false);
tft.print("AB你好3.141516");

可以看到默认的字体不支持中文,需要使用中文的改日再补上

9、旋转

void setRotation(uint8_t rotation);

旋转参数可以是0、1、2或3,分别对应0,90,180或270度。

对于属于Arduino屏蔽的显示,旋转值0将显示设置为竖屏(高)模式,旋转值2也是纵向模式,。旋转1是横屏模式,,而旋转3也是横屏模式。

案例

//tft.setRotation(1); //注释和未注释的情况下做对比

tft.fillScreen(GREEN);
tft.drawChar(150,10,'A',RED,WHITE,5);

tft.setCursor(10,50);
tft.print("AB 3.14");    //默认前景色white、无背景色、大小为1

tft.setCursor(10,80);
tft.setTextSize( 4);
tft.print("AB 3.14");

tft.setCursor(10,115);
tft.setTextColor(RED); //背景色不做设置
tft.setTextSize( 4);
tft.print("AB你好3.141516");

tft.setCursor(10,180);
tft.setTextColor(RED, WHITE);
tft.setTextSize( 4);
tft.setTextWrap(false);
tft.print("AB你好3.141516");



驴友花雕 发表于 2021-7-7 20:30:44

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十一:几何图形的点线面循环

/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十一:几何图形的点线面循环
模块直插,引脚用法如下:
LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
Arduino Uno 8 9 2 3 4 5 6 7
*/

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

#define BLACK   0x0000
#define BLUE    0x001F
#define RED   0xF800
#define GREEN   0x07E0
#define WHITE   0xFFFF

void setup(void) {
tft.reset();
tft.begin(0x9341);
tft.fillScreen(BLACK);
}

void loop() {
// 点
tft.drawPixel(1, 1, RED);
tft.drawPixel(10, 10, RED);
tft.drawPixel(20, 20, RED);
tft.drawPixel(40, 40, RED);
tft.drawPixel(60, 60, RED);
delay(500);
// 线
tft.drawFastHLine(10, 10, 170, RED);
tft.drawFastVLine(10, 10, 170, RED);
tft.drawLine(10, 10, 100, 180, RED);
delay(500);
// 矩形
tft.drawRect(10, 10, 150, 100, RED);
tft.fillRect(10, 120, 150, 100, RED);
delay(500);
// 圆角矩形
tft.drawRoundRect(10, 10, 150, 100, 10, RED);
tft.fillRoundRect(10, 120, 150, 100, 10, RED);
delay(500);
//圆形
tft.drawCircle(100, 100, 50, WHITE);
tft.fillCircle(100, 260, 50, BLUE);
delay(500);
//三角形
tft.drawTriangle(10, 10, 100, 15, 180, 100, GREEN);
tft.fillTriangle(10, 110, 100, 115, 180, 200, GREEN);
delay(500);
//旋转
tft.setRotation(0);
delay(500);
//字符与文本
tft.fillScreen(GREEN);
tft.drawChar(150, 10, 'A', RED, WHITE, 5);
tft.setCursor(10, 50);
tft.print("AB 3.14");    //默认前景色white、无背景色、大小为1
tft.setCursor(10, 80);
tft.setTextSize( 4);
tft.print("AB 3.14");
tft.setCursor(10, 115);
tft.setTextColor(RED); //背景色不做设置
tft.setTextSize( 4);
tft.print("AB 3.141516");
tft.setCursor(10, 180);
tft.setTextColor(RED, WHITE);
tft.setTextSize( 4);
tft.setTextWrap(false);
tft.print("AB 3.141516");
delay(500);
//屏幕
tft.fillScreen(BLACK);
delay(1000);
tft.fillScreen(GREEN);
delay(1000);
//旋转
tft.setRotation(1);
delay(500);
tft.fillScreen(RED);
delay(1000);
tft.fillScreen(BLUE);
delay(1000);
//旋转
tft.setRotation(2);
delay(500);
tft.fillScreen(BLACK);
delay(1000);
//旋转
tft.setRotation(0);
delay(500);
}

驴友花雕 发表于 2021-7-8 06:55:42

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十一:几何图形的点线面循环

实验动态图

驴友花雕 发表于 2021-7-8 07:02:57

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十一:几何图形的点线面循环

实验动态图

驴友花雕 发表于 2021-7-8 07:55:10

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十二:使用 Arduino 2.4 触摸屏创建简易绘画应用程序
实验开源代码

/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十二:使用 Arduino 2.4 触摸屏创建简易绘画应用程序
模块直插,引脚用法如下:
LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
Arduino Uno 8 9 2 3 4 5 6 7
*/

#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>

#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
#define YP A2
#define XM A1
#define YM 6
#define XP 7

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define BLACK   0x0000
#define BLUE    0x001F
#define RED   0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
#define BOXSIZE 40
#define PENRADIUS 3
int oldcolor, currentcolor;

void setup(void) {
Serial.begin(9600);
tft.reset();
tft.begin(0x9341);
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;
pinMode(13, OUTPUT);
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop() {
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    if (p.y < (TS_MINY - 5)) {
      tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
    }
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
    if (p.y < BOXSIZE) {
      oldcolor = currentcolor;
      if (p.x < BOXSIZE) {
      currentcolor = RED;
      tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
      } else if (p.x < BOXSIZE * 2) {
      currentcolor = YELLOW;
      tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
      } else if (p.x < BOXSIZE * 3) {
      currentcolor = GREEN;
      tft.drawRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, WHITE);
      } else if (p.x < BOXSIZE * 4) {
      currentcolor = CYAN;
      tft.drawRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, WHITE);
      } else if (p.x < BOXSIZE * 5) {
      currentcolor = BLUE;
      tft.drawRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, WHITE);
      } else if (p.x < BOXSIZE * 6) {
      currentcolor = MAGENTA;
      tft.drawRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, WHITE);
      } if (oldcolor != currentcolor) {
      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);
      }
    } if (((p.y - PENRADIUS) > BOXSIZE) && ((p.y + PENRADIUS) < tft.height())) {
      tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
    }
}
tft.setRotation(3);
}

驴友花雕 发表于 2021-7-8 07:59:32

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十二:使用 Arduino 2.4 触摸屏创建简易绘画应用程序

实验场景图



驴友花雕 发表于 2021-7-8 08:37:47

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十三:两个按钮的极简开关板
实验开源代码

/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十三:两个按钮的极简开关板
模块直插,引脚用法如下:
LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
Arduino Uno 8 9 2 3 4 5 6 7
*/

#if 1
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>

#define MINPRESSURE 200
#define MAXPRESSURE 1000

// 所有触摸屏和接线都是不同的
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;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

Adafruit_GFX_Button on_btn, off_btn;

int pixel_x, pixel_y;   //更新全局变量

bool Touch_getXY(void) {
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT);      //恢复共享引脚
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH);   //因为 TFT 控制引脚
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
    pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //kbv 对我有意义
    pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}

#define BLACK   0x0000
#define BLUE    0x001F
#define RED   0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW0xFFE0
#define WHITE   0xFFFF

void setup(void) {
Serial.begin(9600);
tft.reset();
tft.begin(0x9341);
tft.fillScreen(BLACK);
tft.setRotation(2); // 翻转180度,对应硬件
delay(500);
on_btn.initButton(&tft,60, 200, 100, 40, WHITE, CYAN, BLACK, "ON", 2);
off_btn.initButton(&tft, 180, 200, 100, 40, WHITE, CYAN, BLACK, "OFF", 2);
on_btn.drawButton(false);
off_btn.drawButton(false);
tft.fillRect(40, 80, 160, 80, RED);
}

// 两个按钮很简单
void loop(void) {
bool down = Touch_getXY();
on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
if (on_btn.justReleased())
    on_btn.drawButton();
if (off_btn.justReleased())
    off_btn.drawButton();
if (on_btn.justPressed()) {
    on_btn.drawButton(true);
    tft.fillRect(40, 80, 160, 80, GREEN);
}
if (off_btn.justPressed()) {
    off_btn.drawButton(true);
    tft.fillRect(40, 80, 160, 80, RED);
}
}
#endif

驴友花雕 发表于 2021-7-8 08:48:02

本帖最后由 驴友花雕 于 2021-7-8 08:52 编辑

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏项目二十三:两个按钮的极简开关板
实验场景图

驴友花雕 发表于 2021-7-8 09:30:02

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十四:动态环形百分比图表
实验开源代码

/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十四:动态环形百分比图表
模块直插,引脚用法如下:
LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
Arduino Uno 8 9 2 3 4 5 6 7
*/

#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "Temperature.h"

MCUFRIEND_kbv tft;

#define PI 3.1415926535897932384626433832795

int n, f;
int j, j2;
int i, i2;
int pct = 0;
int d = {10, 60, 16, 9, 10};
uint16_t col = {0x7006, 0xF986, 0x6905, 0x7FF7, 0x024D};

void setup() {
tft.reset();
Serial.begin(9600);
tft.begin(0x9341);
tft.invertDisplay(true);
tft.setTextSize(2);
}

void loop() {
tft.fillScreen(0x0042);
tft.setRotation(1);

for (int p = 0; p < 4000; p++) {
    j = 120 * (sin(PI * p / 2000));
    i = 120 * (cos(PI * p / 2000));
    j2 = 60 * (sin(PI * p / 2000));
    i2 = 60 * (cos(PI * p / 2000));
    tft.drawLine(i2 + 160, j2 + 160, i + 160, j + 160, col);
}
n = 0;
for (int a = 0; a < 5; a++) {
    pct += d * 40;
    f = 4000 - pct;

    for (int b = 0; b < f; b++) {
      j = 120 * (sin(PI * b / 2000));
      i = 120 * (cos(PI * b / 2000));
      j2 = 60 * (sin(PI * b / 2000));
      i2 = 60 * (cos(PI * b / 2000));
      tft.drawLine(i2 + 160, j2 + 160, i + 160, j + 160, col);
    }
    tft.fillCircle(380, 100 + (30 * n), 10,col);
    tft.setTextColor(0xffff);
    tft.setCursor(400, 94 + (30 * n));
    tft.print(d); tft.print("%");
    n++;
}
while (1);
}

驴友花雕 发表于 2021-7-8 09:32:08

本帖最后由 驴友花雕 于 2021-7-8 10:13 编辑

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏项目二十四:动态环形百分比图表
实验场景图

驴友花雕 发表于 2021-7-8 10:00:47

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十五:显示触摸屏电话面板
实验开源代码

/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
项目二十五:显示触摸屏电话面板
模块直插,引脚用法如下:
LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
Arduino Uno 8 9 2 3 4 5 6 7
*/

#include <TouchScreen.h> //导入触摸库
#include <LCDWIKI_GUI.h> //Core graphics library
#include <LCDWIKI_KBV.h> //Hardware-specific library

//if the IC model is known or the modules is unreadable,you can use this constructed function
LCDWIKI_KBV my_lcd(ILI9341, A3, A2, A1, A0, A4); //model,cs,cd,wr,rd,reset
//if the IC model is not known and the modules is readable,you can use this constructed function
//LCDWIKI_KBV my_lcd(240,320,A3,A2,A1,A0,A4);//width,height,cs,cd,wr,rd,reset

/*r   g    b */
#define BLACK      0x0000/*   0,   0,   0 */
#define BLUE         0x001F/*   0,   0, 255 */
#define RED          0xF800/* 255,   0,   0 */
#define GREEN      0x07E0/*   0, 255,   0 */
#define CYAN         0x07FF/*   0, 255, 255 */
#define MAGENTA      0xF81F/* 255,   0, 255 */
#define YELLOW       0xFFE0/* 255, 255,   0 */
#define WHITE      0xFFFF/* 255, 255, 255 */
#define NAVY         0x000F/*   0,   0, 128 */
#define DARKGREEN    0x03E0/*   0, 128,   0 */
#define DARKCYAN   0x03EF/*   0, 128, 128 */
#define MAROON       0x7800/* 128,   0,   0 */
#define PURPLE       0x780F/* 128,   0, 128 */
#define OLIVE      0x7BE0/* 128, 128,   0 */
#define LIGHTGREY    0xC618/* 192, 192, 192 */
#define DARKGREY   0x7BEF/* 128, 128, 128 */
#define ORANGE       0xFD20/* 255, 165,   0 */
#define GREENYELLOW0xAFE5/* 173, 255,47 */
#define PINK         0xF81F/* 255,   0, 255 */

/******************* UI details */
#define BUTTON_R 25 //the radius of button
#define BUTTON_SPACING_X 25 //the horizontal distance between button
#define BUTTON_SPACING_Y 5//the vertical distance between button
#define EDG_Y 5 //lower edge distance
#define EDG_X 20 //left and right distance

#define YP A2// must be an analog pin, use "An" notation!
#define XM A1// must be an analog pin, use "An" notation!
#define YM 6   // can be a digital pin
#define XP 7   // can be a digital pin

//X 的触摸灵敏度
#define TS_MINX 124
#define TS_MAXX 906

//Y 的触摸灵敏度
#define TS_MINY 83
#define TS_MAXY 893

// 有一个状态行,例如 FONA 是否在工作
#define STATUS_X 10
#define STATUS_Y 65

//按下时的触摸灵敏度
#define MINPRESSURE 10
#define MAXPRESSURE 1000

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

typedef struct _button_info {
uint8_t button_name;
uint8_t button_name_size;
uint16_t button_name_colour;
uint16_t button_colour;
uint16_t button_x;
uint16_t button_y;
} button_info;

//按钮的定义
button_info phone_button = {
"1", 3, BLACK, CYAN, EDG_X + BUTTON_R - 1, my_lcd.Get_Display_Height() - EDG_Y - 4 * BUTTON_SPACING_Y - 9 * BUTTON_R - 1,
"2", 3, BLACK, CYAN, EDG_X + 3 * BUTTON_R + BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 4 * BUTTON_SPACING_Y - 9 * BUTTON_R - 1,
"3", 3, BLACK, CYAN, EDG_X + 5 * BUTTON_R + 2 * BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 4 * BUTTON_SPACING_Y - 9 * BUTTON_R - 1,
"4", 3, BLACK, CYAN, EDG_X + BUTTON_R - 1, my_lcd.Get_Display_Height() - EDG_Y - 3 * BUTTON_SPACING_Y - 7 * BUTTON_R - 1,
"5", 3, BLACK, CYAN, EDG_X + 3 * BUTTON_R + BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 3 * BUTTON_SPACING_Y - 7 * BUTTON_R - 1,
"6", 3, BLACK, CYAN, EDG_X + 5 * BUTTON_R + 2 * BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 3 * BUTTON_SPACING_Y - 7 * BUTTON_R - 1,
"7", 3, BLACK, CYAN, EDG_X + BUTTON_R - 1, my_lcd.Get_Display_Height() - EDG_Y - 2 * BUTTON_SPACING_Y - 5 * BUTTON_R - 1,
"8", 3, BLACK, CYAN, EDG_X + 3 * BUTTON_R + BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 2 * BUTTON_SPACING_Y - 5 * BUTTON_R - 1,
"9", 3, BLACK, CYAN, EDG_X + 5 * BUTTON_R + 2 * BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 2 * BUTTON_SPACING_Y - 5 * BUTTON_R - 1,
"*", 3, BLACK, PINK, EDG_X + BUTTON_R - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_SPACING_Y - 3 * BUTTON_R - 1,
"0", 3, BLACK, CYAN, EDG_X + 3 * BUTTON_R + BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_SPACING_Y - 3 * BUTTON_R - 1,
"#", 3, BLACK, PINK, EDG_X + 5 * BUTTON_R + 2 * BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_SPACING_Y - 3 * BUTTON_R - 1,
"end", 2, BLACK, RED, EDG_X + BUTTON_R - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_R - 1,
"call", 2, BLACK, GREEN, EDG_X + 3 * BUTTON_R + BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_R - 1,
"dele", 2, BLACK, LIGHTGREY, EDG_X + 5 * BUTTON_R + 2 * BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_R - 1,
};

//显示字符串
void show_string(uint8_t *str, int16_t x, int16_t y, uint8_t csize, uint16_t fc, uint16_t bc, boolean mode) {
my_lcd.Set_Text_Mode(mode);
my_lcd.Set_Text_Size(csize);
my_lcd.Set_Text_colour(fc);
my_lcd.Set_Text_Back_colour(bc);
my_lcd.Print_String(str, x, y);
}

//检查是否按下
boolean is_pressed(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t px, int16_t py) {
if ((px > x1 && px < x2) && (py > y1 && py < y2))
{
    return true;
}
else
{
    return false;
}
}

//显示主菜单
void show_menu(void)
{
uint16_t i;
for (i = 0; i < sizeof(phone_button) / sizeof(button_info); i++)
{
    my_lcd.Set_Draw_color(phone_button.button_colour);
    my_lcd.Fill_Circle(phone_button.button_x, phone_button.button_y, BUTTON_R);
    show_string(phone_button.button_name, phone_button.button_x - strlen(phone_button.button_name)*phone_button.button_name_size * 6 / 2 + 1, phone_button.button_y - phone_button.button_name_size * 8 / 2 + 1, phone_button.button_name_size, phone_button.button_name_colour, BLACK, 1);
}
my_lcd.Set_Draw_color(BLACK);
my_lcd.Fill_Rectangle(1, 1, my_lcd.Get_Display_Width() - 2, 3);
my_lcd.Fill_Rectangle(1, 29, my_lcd.Get_Display_Width() - 2, 31);
my_lcd.Fill_Rectangle(1, 1, 3, 31);
my_lcd.Fill_Rectangle(my_lcd.Get_Display_Width() - 4, 1, my_lcd.Get_Display_Width() - 2, 31);
}

void setup(void) {
Serial.begin(9600);
my_lcd.Init_LCD();
Serial.println(my_lcd.Read_ID(), HEX);
my_lcd.Fill_Screen(BLUE);
show_menu();
}

uint16_t text_x = 10, text_y = 6, text_x_add = 6 * phone_button.button_name_size, text_y_add = 8 * phone_button.button_name_size;
uint16_t n = 0;

void loop(void) {
uint16_t i;
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);

pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
    //p.x = my_lcd.Get_Display_Width()-map(p.x, TS_MINX, TS_MAXX, my_lcd.Get_Display_Width(), 0);
    //p.y = my_lcd.Get_Display_Height()-map(p.y, TS_MINY, TS_MAXY, my_lcd.Get_Display_Height(), 0);
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, my_lcd.Get_Display_Width());
    p.y = map(p.y, TS_MINY, TS_MAXY, 0, my_lcd.Get_Display_Height());
    // }
    for (i = 0; i < sizeof(phone_button) / sizeof(button_info); i++) {
      //按下按钮
      if (is_pressed(phone_button.button_x - BUTTON_R, phone_button.button_y - BUTTON_R, phone_button.button_x + BUTTON_R, phone_button.button_y + BUTTON_R, p.x, p.y))
      {
      my_lcd.Set_Draw_color(DARKGREY);
      my_lcd.Fill_Circle(phone_button.button_x, phone_button.button_y, BUTTON_R);
      show_string(phone_button.button_name, phone_button.button_x - strlen(phone_button.button_name)*phone_button.button_name_size * 6 / 2 + 1, phone_button.button_y - phone_button.button_name_size * 8 / 2 + 1, phone_button.button_name_size, WHITE, BLACK, 1);
      delay(100);
      my_lcd.Set_Draw_color(phone_button.button_colour);
      my_lcd.Fill_Circle(phone_button.button_x, phone_button.button_y, BUTTON_R);
      show_string(phone_button.button_name, phone_button.button_x - strlen(phone_button.button_name)*phone_button.button_name_size * 6 / 2 + 1, phone_button.button_y - phone_button.button_name_size * 8 / 2 + 1, phone_button.button_name_size, phone_button.button_name_colour, BLACK, 1);
      if (i < 12)
      {
          if (n < 13)
          {
            show_string(phone_button.button_name, text_x, text_y, phone_button.button_name_size, GREENYELLOW, BLACK, 1);
            text_x += text_x_add - 1;
            n++;
          }
      }
      else if (12 == i) //节目通话结束
      {
          my_lcd.Set_Draw_color(BLUE);
          my_lcd.Fill_Rectangle(0, 33, my_lcd.Get_Display_Width() - 1, 42);
          show_string("Calling ended", CENTER, 33, 1, OLIVE, BLACK, 1);
      }
      else if (13 == i) //显示呼叫
      {
          my_lcd.Set_Draw_color(BLUE);
          my_lcd.Fill_Rectangle(0, 33, my_lcd.Get_Display_Width() - 1, 42);
          show_string("Calling...", CENTER, 33, 1, OLIVE, BLACK, 1);
      }
      else if (14 == i) //删除按钮
      {
          if (n > 0)
          {
            my_lcd.Set_Draw_color(BLUE);
            text_x -= (text_x_add - 1);
            my_lcd.Fill_Rectangle(text_x, text_y, text_x + text_x_add - 1, text_y + text_y_add - 2);
            n--;
          }
      }
      }
    }
}
}

页: 1 2 3 4 5 [6] 7
查看完整版本: 【Arduino】168种传感器模块系列实验(165)---2.4寸TFT液晶触...