Platform 开发ESP32驱动ST7735芯片的1.8寸 LCD屏幕

目录

一、添加TFT_eSPI库

二、打开设置文件

[二、设置驱动 (47行)](#二、设置驱动 (47行))

[三、设定屏幕宽高 分辨率 (84行)](#三、设定屏幕宽高 分辨率 (84行))

四、设置SPI引脚

五、TFT_eSPI的基本使用方法

六、常用函数

1.设置打字起始坐标位置和字号

[2. 设置字体颜色](#2. 设置字体颜色)

[3. 设置字体倍数](#3. 设置字体倍数)

[4. print打印显示字体](#4. print打印显示字体)

[5. 绘制字符串(居左)](#5. 绘制字符串(居左))

[6. 绘制字符串(居中)](#6. 绘制字符串(居中))

[7. 绘制字符串(居左)](#7. 绘制字符串(居左))

[8. 绘制字符](#8. 绘制字符)

[9. 绘制浮点数](#9. 绘制浮点数)

[10. 绘制数字](#10. 绘制数字)

[11. 画点](#11. 画点)

[12. 画线](#12. 画线)

[13. 画横线](#13. 画横线)

[14. 画竖线](#14. 画竖线)

[15. 画空心圆](#15. 画空心圆)

[16. 画实心圆](#16. 画实心圆)

[17. 画空心椭圆](#17. 画空心椭圆)

[18. 画实心椭圆](#18. 画实心椭圆)

[19. 画空心矩形](#19. 画空心矩形)

[20. 画实心矩形](#20. 画实心矩形)

[21. 画空心圆角矩形](#21. 画空心圆角矩形)

[22. 画实心圆角矩形](#22. 画实心圆角矩形)

[23. 画空心三角形](#23. 画空心三角形)

[24. 画实心三角形](#24. 画实心三角形)

七、官方例程

例程1

例程2

例程3

例程4

例程5


一、添加TFT_eSPI库

二、打开设置文件

打开TFT_eSPI的库文件设置,位置如下 pio--> libdeps--> TFT_eSPI-->User_setup.h

二、设置驱动 (47行)

注意:除了自己对应屏幕的驱动取消注释,其它的要注释上。

三、设定屏幕宽高 分辨率 (84行)

**注意:**这里只有ST7789, ST7735, ILI9163 and GC9A01这几个驱动的屏幕需要设置

四、设置SPI引脚

注意:TFT_eSPI库自己有默认的引脚设置大约在170行,我们需要注释掉这里的配置,选择自己要的配置。

五、TFT_eSPI的基本使用方法

注意:完成上面的配置后才能进行下面的操作

cpp 复制代码
#include <Arduino.h>
#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();  // 创建对象控制屏幕

void setup()
{
    tft.init();                // 初始屏幕
    tft.fillScreen(TFT_BLACK); // 清屏 把屏幕设置为黑色

    tft.setTextColor(TFT_YELLOW);        // 设置文字颜色
    tft.setTextSize(1);                  // 设置文字大小
    tft.drawString("Hello world", 5, 5); // 字符串 x, y

    tft.setCursor(5, 20, 2);     // 将"光标"设置(20, 20) 并设置字体为2 号
    tft.setTextColor(TFT_WHITE); // 将字体颜色设置为白色,背景为黑色,将文本大小倍增设置为1
    tft.setTextSize(1);          // 将字体增大1倍
    tft.println("Hello World!");
    
    tft.setTextColor(TFT_OLIVE);
    tft.setCursor(100, 40);
    tft.setTextFont(4); 
    tft.setTextSize(1);
    tft.println(12);

    tft.setTextColor(TFT_BLUE);
    tft.setCursor(5, 50);
    tft.setTextFont(7); 
    tft.setTextSize(1);
    tft.println(34);

    tft.drawLine(0, 80, 128, 80, TFT_BLACK);                // 绘制直线
    tft.drawRoundRect(5, 100, 50, 50, 5, TFT_SKYBLUE);      // 绘制圆角矩形
    tft.fillTriangle(60, 100, 60, 150, 128, 150, TFT_GOLD); // 绘制填充三角形

}

void loop()
{
    // put your main code here, to run repeatedly:
}

六、常用函数

1.设置打字起始坐标位置和字号

cpp 复制代码
// 设置文本显示坐标,默认以文本左上角为参考点,可以改变参考点
void setCursor(int16_t x, int16_t y);
// 设置文本显示坐标,和文本的字体
void setCursor(int16_t x, int16_t y, uint8_t font); 

2. 设置字体颜色

cpp 复制代码
// 设置文本颜色
void setTextColor(uint16_t color);
 
// 设置文本颜色与背景色
void setTextColor(uint16_t fgcolor, uint16_t bgcolor);

3. 设置字体倍数

cpp 复制代码
// 设置文本大小,文本大小倍数范围为 1~7 的整数
void setTextSize(uint8_t size);

4. print打印显示字体

cpp 复制代码
tft.print("Hello World!");
tft.println("Hello World!");

注意: print 函数打印不会在文本末尾添加换行,而println会在末尾添加换行

5. 绘制字符串(居左)

cpp 复制代码
int16_t drawString(const String &string, int32_t x, int32_t y)
int16_t drawString(const char *string, int32_t x, int32_t y)
int16_t drawString(const String &string, int32_t x, int32_t y, uint8_t font)
int16_t drawString(const char *string, int32_t x, int32_t y, uint8_t font)

6. 绘制字符串(居中)

cpp 复制代码
int16_t drawCentreString(const char *string, int32_t x, int32_t y, uint8_t font)
int16_t drawCentreString(const String &string, int32_t x, int32_t y, uint8_t font)

7. 绘制字符串(居左)

cpp 复制代码
int16_t drawRightString(const char *string, int32_t x, int32_t y, uint8_t font)
int16_t drawRightString(const String &string, int32_t x, int32_t y, uint8_t font)

8. 绘制字符

cpp 复制代码
int16_t drawChar(uint16_t uniCode, int32_t x, int32_t y)
int16_t drawChar(uint16_t uniCode, int32_t x, int32_t y, uint8_t font)
void drawChar(int32_t x, int32_t y, uint16_t c, uint32_t color, uint32_t bg, uint8_t size)

9. 绘制浮点数

cpp 复制代码
int16_t TFT_eSPI::drawFloat(float floatNumber, uint8_t decimal, int32_t x, int32_t y)
int16_t TFT_eSPI::drawFloat(float floatNumber, uint8_t decimal, int32_t x, int32_t y, uint8_t font)

10. 绘制数字

cpp 复制代码
int16_t drawNumber(long intNumber, int32_t x, int32_t y)
int16_t drawNumber(long intNumber, int32_t x, int32_t y, uint8_t font)

11. 画点

cpp 复制代码
void drawPixel(int32_t x, int32_t y, uint32_t color)

12. 画线

cpp 复制代码
void drawLine(int32_t xs, int32_t ys, int32_t xe, int32_t ye, uint32_t color)

13. 画横线

cpp 复制代码
void drawFastHLine(int32_t x, int32_t y, int32_t w, uint32_t color)

14. 画竖线

cpp 复制代码
void drawFastVLine(int32_t x, int32_t y, int32_t h, uint32_t color)

15. 画空心圆

cpp 复制代码
tft.drawCircle(100,100,50,TFT_RED);

16. 画实心圆

cpp 复制代码
void fillCircle(int32_t x, int32_t y, int32_t r, uint32_t color)
voidfillRect(int32_t x,int32_t y,int32_t w,int32_t h,uint32_t color)

17. 画空心椭圆

cpp 复制代码
tft.drawEllipse(100,100,100,60,TFT_GREENYELLOW);

18. 画实心椭圆

cpp 复制代码
void drawRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color)

19. 画空心矩形

cpp 复制代码
void drawRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color)

20. 画实心矩形

cpp 复制代码
void fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color)

21. 画空心圆角矩形

cpp 复制代码
void drawRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t radius, uint32_t color)

22. 画实心圆角矩形

cpp 复制代码
void fillRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t radius, uint32_t color)

23. 画空心三角形

cpp 复制代码
void drawTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, uint32_t color)

24. 画实心三角形

cpp 复制代码
void fillTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, uint32_t color)

七、官方例程

例程1

cpp 复制代码
/*
一个使用TFT LCD屏幕显示时间的示例模拟时钟
使用ST7735库的一些绘图命令。

为了更准确的时钟,最好使用RTClib库。
但这只是一个演示。

使用编译时间来设置时间,因此重置后会再次从编译时间开始计时

Gilchrist 6/2/2014 1.0
由Bodmer更新
*/

#include <TFT_eSPI.h> 
#include <SPI.h>

TFT_eSPI tft = TFT_eSPI();  

#define TFT_GREY 0xBDF7

float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;    // Saved H, M, S x & y multipliers
float sdeg=0, mdeg=0, hdeg=0;
uint16_t osx=64, osy=64, omx=64, omy=64, ohx=64, ohy=64;  // Saved H, M, S x & y coords
uint16_t x0=0, x1=0, yy0=0, yy1=0;
uint32_t targetTime = 0;                    // for next 1 second timeout

static uint8_t conv2d(const char* p) {
  uint8_t v = 0;
  if ('0' <= *p && *p <= '9')
    v = *p - '0';
  return 10 * v + *++p - '0';
}

uint8_t hh=conv2d(__TIME__), mm=conv2d(__TIME__+3), ss=conv2d(__TIME__+6);  // Get H, M, S from compile time

bool initial = 1;

void setup(void) {
  tft.init();
  tft.setRotation(0);
  tft.fillScreen(TFT_GREY);
  tft.setTextColor(TFT_GREEN, TFT_GREY);  // Adding a black background colour erases previous text automatically
  
  // Draw clock face
  tft.fillCircle(64, 64, 61, TFT_BLUE);
  tft.fillCircle(64, 64, 57, TFT_BLACK);

  // Draw 12 lines
  for(int i = 0; i<360; i+= 30) {
    sx = cos((i-90)*0.0174532925);
    sy = sin((i-90)*0.0174532925);
    x0 = sx*57+64;
    yy0 = sy*57+64;
    x1 = sx*50+64;
    yy1 = sy*50+64;

    tft.drawLine(x0, yy0, x1, yy1, TFT_BLUE);
  }

  // Draw 60 dots
  for(int i = 0; i<360; i+= 6) {
    sx = cos((i-90)*0.0174532925);
    sy = sin((i-90)*0.0174532925);
    x0 = sx*53+64;
    yy0 = sy*53+64;
    
    tft.drawPixel(x0, yy0, TFT_BLUE);
    if(i==0 || i==180) tft.fillCircle(x0, yy0, 1, TFT_CYAN);
    if(i==0 || i==180) tft.fillCircle(x0+1, yy0, 1, TFT_CYAN);
    if(i==90 || i==270) tft.fillCircle(x0, yy0, 1, TFT_CYAN);
    if(i==90 || i==270) tft.fillCircle(x0+1, yy0, 1, TFT_CYAN);
  }

  tft.fillCircle(65, 65, 3, TFT_RED);

  // Draw text at position 64,125 using fonts 4
  // Only font numbers 2,4,6,7 are valid. Font 6 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : . a p m
  // Font 7 is a 7 segment font and only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : .
  tft.drawCentreString("Time flies",64,130,4);

  targetTime = millis() + 1000; 
}

void loop() {
  if (targetTime < millis()) {
    targetTime = millis()+1000;
    ss++;              // Advance second
    if (ss==60) {
      ss=0;
      mm++;            // Advance minute
      if(mm>59) {
        mm=0;
        hh++;          // Advance hour
        if (hh>23) {
          hh=0;
        }
      }
    }

    // Pre-compute hand degrees, x & y coords for a fast screen update
    sdeg = ss*6;                  // 0-59 -> 0-354
    mdeg = mm*6+sdeg*0.01666667;  // 0-59 -> 0-360 - includes seconds
    hdeg = hh*30+mdeg*0.0833333;  // 0-11 -> 0-360 - includes minutes and seconds
    hx = cos((hdeg-90)*0.0174532925);    
    hy = sin((hdeg-90)*0.0174532925);
    mx = cos((mdeg-90)*0.0174532925);    
    my = sin((mdeg-90)*0.0174532925);
    sx = cos((sdeg-90)*0.0174532925);    
    sy = sin((sdeg-90)*0.0174532925);

    if (ss==0 || initial) {
      initial = 0;
      // Erase hour and minute hand positions every minute
      tft.drawLine(ohx, ohy, 65, 65, TFT_BLACK);
      ohx = hx*33+65;    
      ohy = hy*33+65;
      tft.drawLine(omx, omy, 65, 65, TFT_BLACK);
      omx = mx*44+65;    
      omy = my*44+65;
    }

      // Redraw new hand positions, hour and minute hands not erased here to avoid flicker
      tft.drawLine(osx, osy, 65, 65, TFT_BLACK);
      tft.drawLine(ohx, ohy, 65, 65, TFT_WHITE);
      tft.drawLine(omx, omy, 65, 65, TFT_WHITE);
      osx = sx*47+65;    
      osy = sy*47+65;
      tft.drawLine(osx, osy, 65, 65, TFT_RED);

    tft.fillCircle(65, 65, 3, TFT_RED);
  }
}

例程2

cpp 复制代码
/*
测试tft.print()函数,其中包含嵌入的tft.write()函数

请确保所有显示驱动程序和引脚连接正确,通过编辑TFT_eSPI库文件夹中的User_Setup.h文件进行设置。

注意,在长时间的for/while循环中必须调用yield()或delay(0),以防止ESP8266看门狗触发。

#########################################################################

不要忘记更新库中的User_Setup.h文件
#########################################################################
*/

#include <Arduino.h>
#include <TFT_eSPI.h> // ST7735驱动芯片的图形和字体库
#include <SPI.h>

TFT_eSPI tft = TFT_eSPI(); // 调用库,引脚在User_Setup.h中定义

#define TFT_GREY 0x5AEB // 新颜色

void setup(void)
{
    tft.init();
    tft.setRotation(1);
}

void loop()
{

    // 用灰色填充屏幕,以便我们可以看到带有和不带有背景颜色定义的打印效果
    tft.fillScreen(TFT_GREY);

    // 将"光标"设置在显示器的左上角(0,0),并选择字体2
    // (在使用'tft.println'打印时,光标将自动移动到下一行
    // 或者如果有足够的空间,光标将停留在同一行上)
    tft.setCursor(0, 0, 2);

    // 将字体颜色设置为白色,背景为黑色,将文本大小乘数设置为1
    tft.setTextColor(TFT_WHITE, TFT_BLACK);
    tft.setTextSize(1);
    // 现在我们可以使用"print"类在屏幕上绘制文本
    tft.println("Hello World!");

    // 将字体颜色设置为黄色,没有背景,设置为字体7
    tft.setTextColor(TFT_YELLOW);
    tft.setTextFont(2);
    tft.println(1234.56);

    // 将字体颜色设置为红色,背景为黑色,设置为字体4
    tft.setTextColor(TFT_RED, TFT_BLACK);
    tft.setTextFont(4);
    tft.println((long)3735928559, HEX); // 应该打印DEADBEEF

    // 将字体颜色设置为绿色,背景为黑色,设置为字体2
    tft.setTextColor(TFT_GREEN, TFT_BLACK);
    tft.setTextFont(2);
    tft.println("Groop");

    // 测试一些打印格式化函数
    float fnumber = 123.45;
    // 将字体颜色设置为蓝色,没有背景,设置为字体2
    tft.setTextColor(TFT_BLUE);
    tft.setTextFont(2);
    tft.print("Float = ");
    tft.println(fnumber); // 打印浮点数
    tft.print("Binary = ");
    tft.println((int)fnumber, BIN); // 以二进制形式打印为整数值
    tft.print("Hexadecimal = ");
    tft.println((int)fnumber, HEX); // 以十六进制形式打印为整数值

    while (1)
        yield(); // 我们必须调用yield()以防止看门狗超时。
}

例程3

cpp 复制代码
/*
使用TFT LCD屏幕显示时间的示例数字时钟。
演示了字体打印例程的使用。(时间更新,但日期不更新。)

为了更准确的时钟,最好使用RTClib库。
但这只是一个演示。

此示例仅使用硬件SPI。非硬件SPI速度太慢(慢大约8倍!)

基于Gilchrist的时钟示例 6/2/2014 1.0
由Bodmer更新
一些颜色代码:

代码 颜色
0x0000 黑色
0xFFFF 白色
0xBDF7 浅灰色
0x7BEF 深灰色
0xF800 红色
0xFFE0 黄色
0xFBE0 橙色
0x79E0 棕色
0x7E0 绿色
0x7FF 青色
0x1F 蓝色
0xF81F 粉色

*/
#include <Arduino.h>
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>

TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h

uint32_t targetTime = 0; // for next 1 second timeout

byte omm = 99;
bool initial = 1;
byte xcolon = 0;
unsigned int colour = 0;

static uint8_t conv2d(const char *p)
{
    uint8_t v = 0;
    if ('0' <= *p && *p <= '9')
        v = *p - '0';
    return 10 * v + *++p - '0';
}

uint8_t hh = conv2d(__TIME__), mm = conv2d(__TIME__ + 3), ss = conv2d(__TIME__ + 6); // 从编译时间获取小时、分钟、秒
void setup(void)
{
    tft.init();
    tft.setRotation(1);
    tft.fillScreen(TFT_BLACK);

    tft.setTextColor(TFT_YELLOW, TFT_BLACK); // 注意:新的字体不绘制背景颜色

    targetTime = millis() + 1000;
}

void loop()
{
    if (targetTime < millis())
    {
        targetTime = millis() + 1000;
        ss++; // Advance second
        if (ss == 60)
        {
            ss = 0;
            omm = mm;
            mm++; // Advance minute
            if (mm > 59)
            {
                mm = 0;
                hh++; // Advance hour
                if (hh > 23)
                {
                    hh = 0;
                }
            }
        }

        if (ss == 0 || initial)
        {
            initial = 0;
            tft.setTextColor(TFT_GREEN, TFT_BLACK);
            tft.setCursor(8, 52);

            tft.print(__DATE__); // This uses the standard ADAFruit small font

            tft.setTextColor(TFT_BLUE, TFT_BLACK);
            tft.drawCentreString("It is windy", 120, 48, 2); // Next size up font 2

            // tft.setTextColor(0xF81F, TFT_BLACK); // Pink
            // tft.drawCentreString("12.34",80,100,6); // Large font 6 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 . : a p m
        }

        // Update digital time
        byte xpos = 6;
        byte ypos = 0;
        if (omm != mm)
        {
            // 为了最小化闪烁,只在每分钟重新绘制一次
            // 取消下面两行中的一个注释,使用幽灵图像演示文本覆盖,因为时间会在其上绘制
            tft.setTextColor(0x39C4, TFT_BLACK);    // 留下一个7段幽灵图像,注释掉下一行!
                                                    // tft.setTextColor(TFT_BLACK, TFT_BLACK); // 将字体颜色设置为黑色以清除图像
                                                    // 字体7用于显示伪7段显示。
                                                    // 字体7只包含字符[空格] 0 1 2 3 4 5 6 7 8 9 0 : .
            tft.drawString("88:88", xpos, ypos, 7); // 重写文本以清除它
            tft.setTextColor(0xFBE0);               // Orange
            omm = mm;

            if (hh < 10)
                xpos += tft.drawChar('0', xpos, ypos, 7);
            xpos += tft.drawNumber(hh, xpos, ypos, 7);
            xcolon = xpos;
            xpos += tft.drawChar(':', xpos, ypos, 7);
            if (mm < 10)
                xpos += tft.drawChar('0', xpos, ypos, 7);
            tft.drawNumber(mm, xpos, ypos, 7);
        }

        if (ss % 2)
        { // Flash the colon
            tft.setTextColor(0x39C4, TFT_BLACK);
            xpos += tft.drawChar(':', xcolon, ypos, 7);
            tft.setTextColor(0xFBE0, TFT_BLACK);
        }
        else
        {
            tft.drawChar(':', xcolon, ypos, 7);
            colour = random(0xFFFF);
            // 使用矩形擦除旧文本,这种方法的缺点是增加了显示闪烁
            tft.fillRect(0, 64, 160, 20, TFT_BLACK);
            tft.setTextColor(colour);
            tft.drawRightString("Colour", 75, 64, 4); // 将字符串右对齐绘制到 x 位置 75
            String scolour = String(colour, HEX);
            scolour.toUpperCase();
            char buffer[20];
            scolour.toCharArray(buffer, 20);
            tft.drawString(buffer, 82, 64, 4);
        }
    }
}

例程4

cpp 复制代码
/*
 Adapted from the Adafruit and Xark's PDQ graphicstest sketch.

 This sketch uses the GLCD font only.

 Make sure all the display driver and pin connections are correct by
 editing the User_Setup.h file in the TFT_eSPI library folder.

 Note that yield() or delay(0) must be called in long duration for/while
 loops to stop the ESP8266 watchdog triggering.

 #########################################################################
 ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
 #########################################################################
 */

#include <Arduino.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
uint32_t testHaD();
uint32_t testFillScreen();
uint32_t testText();
uint32_t testPixels();
uint32_t testLines(uint16_t color);
uint32_t testRects(uint16_t color);
uint32_t testFilledRects(uint16_t color1, uint16_t color2);
uint32_t testFilledCircles(uint8_t radius, uint16_t color);
uint32_t testCircles(uint8_t radius, uint16_t color);
uint32_t testFastLines(uint16_t color1, uint16_t color2);
uint32_t testTriangles();
uint32_t testFilledTriangles();
uint32_t testRoundRects();
uint32_t testFilledRoundRects();
void printnice(int32_t v);

TFT_eSPI tft = TFT_eSPI(); // Invoke custom library

// These are used to get information about static SRAM and flash memory sizes
extern "C" char __data_start[];    // start of SRAM data
extern "C" char _end[];            // end of SRAM data (used to check amount of SRAM this program's variables use)
extern "C" char __data_load_end[]; // end of FLASH (used to check amount of Flash this program's code and data uses)

void setup()
{
    Serial.begin(115200);

    Serial.println(F("TFT 1.8\" SPI TFT Test!     "));

    tft.init(); // initialize LCD
}

// NOTE: This demo should run, but may look odd on 128x128 LCD (vs 128x160)

void loop(void)
{

    Serial.print(F(__DATE__ " " __TIME__ " - Flash=0x"));
    // These are not compatible with the ESP8266 core library
    // Serial.print((uint16_t)__data_load_end, HEX);
    // Serial.print(F(" RAM=0x"));
    // Serial.println((uint16_t)_end - (uint16_t)__data_start, HEX);
    Serial.println(F("Benchmark                Time (microseconds)"));

    uint32_t usecHaD = testHaD();
    Serial.print(F("HaD pushColor             "));
    Serial.println(usecHaD);
    delay(100);

    uint32_t usecFillScreen = testFillScreen();
    Serial.print(F("Screen fill              "));
    Serial.println(usecFillScreen);
    delay(100);

    tft.setRotation(1);
    uint32_t usecText = testText();
    Serial.print(F("Text                     "));
    Serial.println(usecText);
    delay(100);
    tft.setRotation(0);

    uint32_t usecPixels = testPixels();
    Serial.print(F("Pixels                   "));
    Serial.println(usecPixels);
    delay(100);

    uint32_t usecLines = testLines(TFT_BLUE);
    Serial.print(F("Lines                    "));
    Serial.println(usecLines);
    delay(100);

    uint32_t usecFastLines = testFastLines(TFT_RED, TFT_BLUE);
    Serial.print(F("Horiz/Vert Lines         "));
    Serial.println(usecFastLines);
    delay(100);

    uint32_t usecRects = testRects(TFT_GREEN);
    Serial.print(F("Rectangles (outline)     "));
    Serial.println(usecRects);
    delay(100);

    uint32_t usecFilledRects = testFilledRects(TFT_YELLOW, TFT_MAGENTA);
    Serial.print(F("Rectangles (filled)      "));
    Serial.println(usecFilledRects);
    delay(100);

    uint32_t usecFilledCircles = testFilledCircles(10, TFT_MAGENTA);
    Serial.print(F("Circles (filled)         "));
    Serial.println(usecFilledCircles);
    delay(100);

    uint32_t usecCircles = testCircles(10, TFT_WHITE);
    Serial.print(F("Circles (outline)        "));
    Serial.println(usecCircles);
    delay(100);

    uint32_t usecTriangles = testTriangles();
    Serial.print(F("Triangles (outline)      "));
    Serial.println(usecTriangles);
    delay(100);

    uint32_t usecFilledTrangles = testFilledTriangles();
    Serial.print(F("Triangles (filled)       "));
    Serial.println(usecFilledTrangles);
    delay(100);

    uint32_t usecRoundRects = testRoundRects();
    Serial.print(F("Rounded rects (outline)  "));
    Serial.println(usecRoundRects);
    delay(100);

    uint32_t usedFilledRoundRects = testFilledRoundRects();
    Serial.print(F("Rounded rects (filled)   "));
    Serial.println(usedFilledRoundRects);
    delay(100);

    Serial.println(F("Done!"));

    uint16_t c = 4;
    int8_t d = 1;
    for (int32_t i = 0; i < tft.height(); i++)
    {
        tft.drawFastHLine(0, i, tft.width(), c);
        c += d;
        if (c <= 4 || c >= 11)
            d = -d;
    }

    tft.setCursor(0, 0);
    tft.setTextColor(TFT_MAGENTA);

    tft.println(F("Bodmer's TFT_eSPI"));

    tft.setTextSize(1);
    tft.setTextColor(TFT_WHITE);
    tft.println(F("SPI TFT on ESP8266"));
    tft.println(F(""));
    tft.setTextSize(1);
    tft.setTextColor(tft.color565(0x80, 0x80, 0x80));

    // These are not compatible with the ESP8266 core library
    // tft.print(F("Flash=0x"));
    // tft.print((uint16_t)__data_load_end, HEX);
    // tft.print(F(" RAM=0x"));
    // tft.print((uint16_t)_end - (uint16_t)__data_start, HEX);

    tft.setTextColor(TFT_GREEN);
    tft.print(F("Benchmark        usec"));

    tft.setTextColor(TFT_CYAN);
    tft.print(F("HaD logo   "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecHaD);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("Clear      "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecFillScreen);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("Text       "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecText);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("Pixels     "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecPixels);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("Lines      "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecLines);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("H/V Lines  "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecFastLines);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("Rectangles "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecRects);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("Rects-Fill "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecFilledRects);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("Circles    "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecCircles);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("CircleFill "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecFilledCircles);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("Triangles  "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecTriangles);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("Tris-Fill  "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecFilledTrangles);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("Rnd-Rects  "));
    tft.setTextColor(TFT_YELLOW);
    printnice(usecRoundRects);

    tft.setTextColor(TFT_CYAN);
    tft.print(F("RRects-Fill"));
    tft.setTextColor(TFT_YELLOW);
    printnice(usedFilledRoundRects);

    tft.setTextSize(1);
    tft.println(F(""));
    tft.setTextColor(TFT_GREEN);
    tft.print(F("Benchmark Completed!"));

    delay(15 * 1000L);
}

void printnice(int32_t v)
{
    char str[32] = {0};
    sprintf(str, "%d", v);
    for (char *p = (str + strlen(str)) - 3; p > str; p -= 3)
    {
        memmove(p + 1, p, strlen(p) + 1);
        *p = ',';
    }
    while (strlen(str) < 10)
    {
        memmove(str + 1, str, strlen(str) + 1);
        *str = ' ';
    }
    tft.print(str);
}

static inline uint32_t micros_start() __attribute__((always_inline));
static inline uint32_t micros_start()
{
    uint8_t oms = millis();
    while ((uint8_t)millis() == oms)
        ;
    return micros();
}

uint32_t testHaD()
{
    // pseudo-code for cheesy RLE
    // start with color1
    // while more input data remaining
    //  count =  0nnnnnnn = 1 byte or 1nnnnnnn nnnnnnnn 2 bytes (0 - 32767)
    //  repeat color count times
    //  toggle color1/color2
    static const uint8_t HaD_128x160[] PROGMEM =
        {
            0x85, 0x91, 0x09, 0x4b, 0x09, 0x24, 0x0a, 0x47, 0x09, 0x27, 0x0a, 0x44, 0x0a, 0x29, 0x0a, 0x42,
            0x0a, 0x2b, 0x0a, 0x41, 0x0a, 0x2c, 0x0a, 0x3e, 0x0b, 0x2f, 0x09, 0x3d, 0x09, 0x32, 0x08, 0x3c,
            0x09, 0x33, 0x09, 0x3b, 0x08, 0x33, 0x0a, 0x3a, 0x0a, 0x31, 0x0b, 0x3a, 0x0c, 0x1d, 0x01, 0x10,
            0x0d, 0x39, 0x0c, 0x1d, 0x01, 0x10, 0x0d, 0x39, 0x0d, 0x0f, 0x01, 0x0c, 0x03, 0x0d, 0x0e, 0x39,
            0x0e, 0x0c, 0x03, 0x0c, 0x04, 0x0b, 0x0f, 0x39, 0x0f, 0x0a, 0x04, 0x0c, 0x05, 0x09, 0x10, 0x39,
            0x10, 0x08, 0x05, 0x0c, 0x06, 0x07, 0x11, 0x39, 0x11, 0x06, 0x06, 0x0d, 0x07, 0x04, 0x13, 0x37,
            0x12, 0x05, 0x07, 0x0d, 0x08, 0x02, 0x15, 0x34, 0x15, 0x03, 0x08, 0x0d, 0x20, 0x32, 0x20, 0x0e,
            0x21, 0x31, 0x20, 0x0f, 0x21, 0x2e, 0x22, 0x10, 0x22, 0x2b, 0x22, 0x12, 0x22, 0x12, 0x05, 0x12,
            0x22, 0x14, 0x22, 0x0c, 0x0f, 0x0c, 0x22, 0x16, 0x22, 0x08, 0x15, 0x08, 0x22, 0x18, 0x22, 0x05,
            0x19, 0x05, 0x21, 0x1c, 0x1f, 0x04, 0x1c, 0x05, 0x1f, 0x1f, 0x1c, 0x04, 0x1e, 0x04, 0x1d, 0x2b,
            0x11, 0x04, 0x21, 0x03, 0x12, 0x36, 0x0f, 0x03, 0x24, 0x03, 0x10, 0x38, 0x0d, 0x03, 0x26, 0x03,
            0x0d, 0x3b, 0x0b, 0x03, 0x28, 0x03, 0x0b, 0x3d, 0x0a, 0x03, 0x29, 0x03, 0x09, 0x40, 0x07, 0x03,
            0x2b, 0x03, 0x07, 0x42, 0x05, 0x03, 0x2c, 0x04, 0x04, 0x45, 0x04, 0x03, 0x2d, 0x03, 0x04, 0x46,
            0x02, 0x03, 0x2e, 0x03, 0x03, 0x48, 0x01, 0x03, 0x2f, 0x03, 0x01, 0x4c, 0x31, 0x4e, 0x32, 0x4e,
            0x33, 0x4c, 0x34, 0x4c, 0x34, 0x4c, 0x35, 0x4b, 0x35, 0x4a, 0x0e, 0x03, 0x14, 0x04, 0x0d, 0x4a,
            0x0b, 0x09, 0x0e, 0x0a, 0x0a, 0x4a, 0x0a, 0x0b, 0x0c, 0x0c, 0x09, 0x4a, 0x09, 0x0d, 0x0a, 0x0e,
            0x09, 0x49, 0x08, 0x0f, 0x09, 0x0e, 0x09, 0x49, 0x08, 0x0f, 0x09, 0x0f, 0x08, 0x49, 0x08, 0x0f,
            0x09, 0x0f, 0x08, 0x49, 0x07, 0x0f, 0x0a, 0x0f, 0x08, 0x49, 0x07, 0x0f, 0x0b, 0x0e, 0x08, 0x49,
            0x07, 0x0d, 0x0e, 0x0d, 0x08, 0x49, 0x07, 0x0b, 0x13, 0x0a, 0x08, 0x49, 0x08, 0x07, 0x18, 0x08,
            0x08, 0x49, 0x08, 0x06, 0x1b, 0x06, 0x08, 0x49, 0x09, 0x04, 0x1c, 0x05, 0x08, 0x4a, 0x09, 0x04,
            0x1d, 0x04, 0x08, 0x4a, 0x0a, 0x03, 0x1d, 0x03, 0x09, 0x4b, 0x19, 0x02, 0x1a, 0x4b, 0x19, 0x03,
            0x19, 0x4b, 0x18, 0x04, 0x18, 0x4d, 0x17, 0x05, 0x17, 0x4a, 0x01, 0x02, 0x17, 0x05, 0x16, 0x4a,
            0x02, 0x02, 0x17, 0x05, 0x16, 0x02, 0x03, 0x44, 0x03, 0x03, 0x16, 0x02, 0x01, 0x02, 0x16, 0x02,
            0x03, 0x43, 0x05, 0x03, 0x15, 0x01, 0x03, 0x01, 0x15, 0x03, 0x04, 0x41, 0x06, 0x03, 0x15, 0x01,
            0x03, 0x01, 0x14, 0x03, 0x07, 0x3d, 0x09, 0x03, 0x2d, 0x03, 0x08, 0x3b, 0x0a, 0x04, 0x2b, 0x03,
            0x0a, 0x39, 0x0c, 0x03, 0x2a, 0x04, 0x0b, 0x37, 0x0e, 0x03, 0x28, 0x03, 0x0e, 0x2e, 0x04, 0x03,
            0x10, 0x03, 0x27, 0x03, 0x10, 0x03, 0x03, 0x24, 0x19, 0x03, 0x26, 0x03, 0x1a, 0x1e, 0x1d, 0x03,
            0x24, 0x03, 0x1e, 0x19, 0x20, 0x04, 0x21, 0x03, 0x20, 0x17, 0x22, 0x04, 0x1f, 0x03, 0x22, 0x15,
            0x22, 0x04, 0x21, 0x04, 0x21, 0x13, 0x22, 0x05, 0x15, 0x01, 0x0b, 0x05, 0x21, 0x12, 0x21, 0x06,
            0x15, 0x01, 0x0b, 0x06, 0x21, 0x10, 0x21, 0x07, 0x0a, 0x01, 0x0a, 0x01, 0x0b, 0x07, 0x21, 0x0e,
            0x20, 0x0a, 0x09, 0x02, 0x09, 0x02, 0x09, 0x09, 0x20, 0x0e, 0x08, 0x02, 0x15, 0x0b, 0x08, 0x03,
            0x08, 0x03, 0x08, 0x0b, 0x15, 0x03, 0x08, 0x0d, 0x07, 0x04, 0x13, 0x0d, 0x06, 0x05, 0x06, 0x06,
            0x05, 0x0d, 0x14, 0x04, 0x07, 0x0c, 0x07, 0x06, 0x11, 0x38, 0x12, 0x06, 0x06, 0x0c, 0x06, 0x08,
            0x10, 0x39, 0x10, 0x08, 0x05, 0x0c, 0x04, 0x0b, 0x0f, 0x39, 0x0f, 0x0a, 0x04, 0x0c, 0x03, 0x0d,
            0x0e, 0x39, 0x0e, 0x0c, 0x03, 0x0c, 0x02, 0x0e, 0x0e, 0x39, 0x0d, 0x0f, 0x01, 0x0c, 0x01, 0x10,
            0x0d, 0x39, 0x0d, 0x0f, 0x01, 0x1e, 0x0c, 0x39, 0x0c, 0x30, 0x0a, 0x3a, 0x0a, 0x33, 0x09, 0x3b,
            0x08, 0x34, 0x09, 0x3b, 0x09, 0x32, 0x09, 0x3c, 0x0a, 0x2f, 0x0a, 0x3e, 0x0a, 0x2d, 0x0b, 0x3f,
            0x0a, 0x2b, 0x0b, 0x41, 0x0a, 0x29, 0x0b, 0x43, 0x0a, 0x27, 0x0a, 0x46, 0x0a, 0x25, 0x0a, 0x49,
            0x09, 0x23, 0x08, 0x4e, 0x08, 0x96, 0x12};

    tft.fillScreen(TFT_BLACK);

    uint32_t start = micros_start();

    tft.startWrite();

    for (int i = 0; i < 0x10; i++)
    {
        tft.setAddrWindow(0, 0, tft.width(), tft.height());

        uint16_t cnt = 0;
        uint16_t color = tft.color565((i << 4) | i, (i << 4) | i, (i << 4) | i);
        uint16_t curcolor = 0;

        const uint8_t *cmp = &HaD_128x160[0];
        tft.startWrite();
        while (cmp < &HaD_128x160[sizeof(HaD_128x160)])
        {
            cnt = pgm_read_byte(cmp++);
            if (cnt & 0x80)
                cnt = ((cnt & 0x7f) << 8) | pgm_read_byte(cmp++);

            tft.pushColor(curcolor, cnt); // PDQ_GFX has count

            curcolor ^= color;
        }
        tft.endWrite();
    }

    tft.endWrite();

    uint32_t t = micros() - start;

    tft.setTextColor(TFT_YELLOW);
    tft.setCursor(0, 145);
    tft.print(F(" http://hackaday.io/"));
    tft.print(F("         Xark"));

    delay(3 * 1000L);

    return t;
}

uint32_t testFillScreen()
{
    uint32_t start = micros_start();

    for (uint8_t i = 0; i < 12; i++)
    {
        tft.fillScreen(TFT_WHITE);
        tft.fillScreen(TFT_RED);
        tft.fillScreen(TFT_GREEN);
        tft.fillScreen(TFT_BLUE);
        tft.fillScreen(TFT_BLACK);
    }

    return micros() - start;
}

uint32_t testText()
{
    tft.fillScreen(TFT_BLACK);
    uint32_t start = micros_start();
    tft.setCursor(0, 0);
    tft.setTextColor(TFT_WHITE);
    tft.setTextSize(1);
    tft.println(F("Hello World!"));
    tft.setTextColor(tft.color565(0xff, 0x00, 0x00));
    tft.print(F("RED "));
    tft.setTextColor(tft.color565(0x00, 0xff, 0x00));
    tft.print(F("GREEN "));
    tft.setTextColor(tft.color565(0x00, 0x00, 0xff));
    tft.println(F("BLUE"));
    tft.setTextColor(TFT_YELLOW);
    tft.println(1234.56);
    tft.setTextColor(TFT_RED);
    tft.println(0xDEADBEEF, HEX);
    tft.setTextColor(TFT_GREEN);
    tft.setTextSize(2);
    tft.println(F("Groop"));
    tft.println(F("I implore thee,"));
    tft.setTextSize(1);
    tft.println(F("my foonting turlingdromes."));
    tft.println(F("And hooptiously drangle me"));
    tft.println(F("with crinkly bindlewurdles,"));
    tft.println(F("Or I will rend thee"));
    tft.println(F("in the gobberwarts"));
    tft.println(F("with my blurglecruncheon,"));
    tft.println(F("see if I don't!"));
    tft.println(F(""));
    tft.setTextColor(TFT_MAGENTA);
    tft.println(F("Woot!"));
    uint32_t t = micros() - start;
    delay(1000);
    return t;
}

uint32_t testPixels()
{
    int32_t w = tft.width();
    int32_t h = tft.height();

    uint32_t start = micros_start();
    tft.startWrite();
    for (uint16_t y = 0; y < h; y++)
    {
        for (uint16_t x = 0; x < w; x++)
        {
            tft.drawPixel(x, y, tft.color565(x << 3, y << 3, x * y));
        }
    }
    tft.endWrite();
    return micros() - start;
}

uint32_t testLines(uint16_t color)
{
    uint32_t start, t;
    int32_t x1, y1, x2, y2;
    int32_t w = tft.width();
    int32_t h = tft.height();

    tft.fillScreen(TFT_BLACK);

    x1 = y1 = 0;
    y2 = h - 1;

    start = micros_start();

    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(TFT_BLACK);

    x1 = w - 1;
    y1 = 0;
    y2 = h - 1;

    start = micros_start();

    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(TFT_BLACK);

    x1 = 0;
    y1 = h - 1;
    y2 = 0;

    start = micros_start();

    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(TFT_BLACK);

    x1 = w - 1;
    y1 = h - 1;
    y2 = 0;

    start = micros_start();

    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;

    return t;
}

uint32_t testFastLines(uint16_t color1, uint16_t color2)
{
    uint32_t start;
    int32_t x, y;
    int32_t w = tft.width();
    int32_t h = tft.height();

    tft.fillScreen(TFT_BLACK);

    start = micros_start();

    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;
}

uint32_t testRects(uint16_t color)
{
    uint32_t start;
    int32_t n, i, i2;
    int32_t cx = tft.width() / 2;
    int32_t cy = tft.height() / 2;

    tft.fillScreen(TFT_BLACK);
    n = min(tft.width(), tft.height());
    start = micros_start();
    for (i = 2; i < n; i += 6)
    {
        i2 = i / 2;
        tft.drawRect(cx - i2, cy - i2, i, i, color);
    }

    return micros() - start;
}

uint32_t testFilledRects(uint16_t color1, uint16_t color2)
{
    uint32_t start, t = 0;
    int32_t n, i, i2;
    int32_t cx = tft.width() / 2 - 1;
    int32_t cy = tft.height() / 2 - 1;

    tft.fillScreen(TFT_BLACK);
    n = min(tft.width(), tft.height());
    for (i = n; i > 0; i -= 6)
    {
        i2 = i / 2;

        start = micros_start();

        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;
}

uint32_t testFilledCircles(uint8_t radius, uint16_t color)
{
    uint32_t start;
    int32_t x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;

    tft.fillScreen(TFT_BLACK);

    start = micros_start();

    for (x = radius; x < w; x += r2)
    {
        for (y = radius; y < h; y += r2)
        {
            tft.fillCircle(x, y, radius, color);
        }
    }

    return micros() - start;
}

uint32_t testCircles(uint8_t radius, uint16_t color)
{
    uint32_t start;
    int32_t x, y, r2 = radius * 2;
    int32_t w = tft.width() + radius;
    int32_t h = tft.height() + radius;

    // Screen is not cleared for this one -- this is
    // intentional and does not affect the reported time.
    start = micros_start();

    for (x = 0; x < w; x += r2)
    {
        for (y = 0; y < h; y += r2)
        {
            tft.drawCircle(x, y, radius, color);
        }
    }

    return micros() - start;
}

uint32_t testTriangles()
{
    uint32_t start;
    int32_t n, i;
    int32_t cx = tft.width() / 2 - 1;
    int32_t cy = tft.height() / 2 - 1;

    tft.fillScreen(TFT_BLACK);
    n = min(cx, cy);

    start = micros_start();

    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;
}

uint32_t testFilledTriangles()
{
    uint32_t start, t = 0;
    int32_t i;
    int32_t cx = tft.width() / 2 - 1;
    int32_t cy = tft.height() / 2 - 1;

    tft.fillScreen(TFT_BLACK);

    start = micros_start();

    for (i = min(cx, cy); i > 10; i -= 5)
    {
        start = micros_start();
        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;
}

uint32_t testRoundRects()
{
    uint32_t start;
    int32_t w, i, i2;
    int32_t cx = tft.width() / 2 - 1;
    int32_t cy = tft.height() / 2 - 1;

    tft.fillScreen(TFT_BLACK);

    w = min(tft.width(), tft.height());

    start = micros_start();

    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;
}

uint32_t testFilledRoundRects()
{
    uint32_t start;
    int32_t i, i2;
    int32_t cx = tft.width() / 2 - 1;
    int32_t cy = tft.height() / 2 - 1;

    tft.fillScreen(TFT_BLACK);

    start = micros_start();

    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;
}

例程5

比上面的例4要好点,大小太大放不出来,你们自己下载编译看。

cpp 复制代码
/*
 Adapted from the Adafruit graphicstest sketch.

 This sketch uses the GLCD font (font 1) only. Disable other fonts to make
 the sketch fit in an UNO!

 Make sure all the display driver and pin connections are correct by
 editing the User_Setup.h file in the TFT_eSPI library folder.

 Note that yield() or delay(0) must be called in long duration for/while
 loops to stop the ESP8266 watchdog triggering.

 #########################################################################
 ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
 #########################################################################
 */

#include <Arduino.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>

void testlines(uint16_t color);
void testdrawtext(char *text, uint16_t color);
void testfastlines(uint16_t color1, uint16_t color2);

void testdrawrects(uint16_t color);
void testfillrects(uint16_t color1, uint16_t color2);
void testfillcircles(uint8_t radius, uint16_t color);

void testdrawcircles(uint8_t radius, uint16_t color);

void testtriangles();
void testroundrects();
void mediabuttons();
void tftPrintTest();

TFT_eSPI tft = TFT_eSPI(); // Invoke custom library

float p = 3.1415926;

void setup(void)
{
    Serial.begin(9600);
    Serial.print("Hello! ST7735 TFT Test");

    // Use this initializer if you're using a 1.8" TFT
    tft.init(); // initialize a ST7735S chip

    Serial.println("Initialized");

    uint16_t time = millis();
    tft.fillScreen(TFT_BLACK);
    time = millis() - time;

    Serial.println(time, DEC);
    delay(500);

    // large block of text
    tft.fillScreen(TFT_BLACK);
    testdrawtext("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", TFT_WHITE);
    delay(1000);

    // tft print function!
    tftPrintTest();
    delay(4000);

    // a single pixel
    tft.drawPixel(tft.width() / 2, tft.height() / 2, TFT_GREEN);
    delay(500);

    // line draw test
    testlines(TFT_YELLOW);
    delay(500);

    // optimized lines
    testfastlines(TFT_RED, TFT_BLUE);
    delay(500);

    testdrawrects(TFT_GREEN);
    delay(500);

    testfillrects(TFT_YELLOW, TFT_MAGENTA);
    delay(500);

    tft.fillScreen(TFT_BLACK);
    testfillcircles(10, TFT_BLUE);
    testdrawcircles(10, TFT_WHITE);
    delay(500);

    testroundrects();
    delay(500);

    testtriangles();
    delay(500);

    mediabuttons();
    delay(500);

    Serial.println("done");
    delay(1000);
}

void loop()
{
    tft.invertDisplay(true);
    delay(500);
    tft.invertDisplay(false);
    delay(500);
}

void testlines(uint16_t color)
{
    tft.fillScreen(TFT_BLACK);
    for (int16_t x = 0; x < tft.width(); x += 6)
    {
        tft.drawLine(0, 0, x, tft.height() - 1, color);
    }
    for (int16_t y = 0; y < tft.height(); y += 6)
    {
        tft.drawLine(0, 0, tft.width() - 1, y, color);
    }

    tft.fillScreen(TFT_BLACK);
    for (int16_t x = 0; x < tft.width(); x += 6)
    {
        tft.drawLine(tft.width() - 1, 0, x, tft.height() - 1, color);
    }
    for (int16_t y = 0; y < tft.height(); y += 6)
    {
        tft.drawLine(tft.width() - 1, 0, 0, y, color);
    }

    tft.fillScreen(TFT_BLACK);
    for (int16_t x = 0; x < tft.width(); x += 6)
    {
        tft.drawLine(0, tft.height() - 1, x, 0, color);
    }
    for (int16_t y = 0; y < tft.height(); y += 6)
    {
        tft.drawLine(0, tft.height() - 1, tft.width() - 1, y, color);
    }

    tft.fillScreen(TFT_BLACK);
    for (int16_t x = 0; x < tft.width(); x += 6)
    {
        tft.drawLine(tft.width() - 1, tft.height() - 1, x, 0, color);
    }
    for (int16_t y = 0; y < tft.height(); y += 6)
    {
        tft.drawLine(tft.width() - 1, tft.height() - 1, 0, y, color);
    }
}

void testdrawtext(char *text, uint16_t color)
{
    tft.setCursor(0, 0);
    tft.setTextColor(color);
    tft.setTextWrap(true);
    tft.print(text);
}

void testfastlines(uint16_t color1, uint16_t color2)
{
    tft.fillScreen(TFT_BLACK);
    for (int16_t y = 0; y < tft.height(); y += 5)
    {
        tft.drawFastHLine(0, y, tft.width(), color1);
    }
    for (int16_t x = 0; x < tft.width(); x += 5)
    {
        tft.drawFastVLine(x, 0, tft.height(), color2);
    }
}

void testdrawrects(uint16_t color)
{
    tft.fillScreen(TFT_BLACK);
    for (int16_t x = 0; x < tft.width(); x += 6)
    {
        tft.drawRect(tft.width() / 2 - x / 2, tft.height() / 2 - x / 2, x, x, color);
    }
}

void testfillrects(uint16_t color1, uint16_t color2)
{
    tft.fillScreen(TFT_BLACK);
    for (int16_t x = tft.width() - 1; x > 6; x -= 6)
    {
        tft.fillRect(tft.width() / 2 - x / 2, tft.height() / 2 - x / 2, x, x, color1);
        tft.drawRect(tft.width() / 2 - x / 2, tft.height() / 2 - x / 2, x, x, color2);
    }
}

void testfillcircles(uint8_t radius, uint16_t color)
{
    for (int16_t x = radius; x < tft.width(); x += radius * 2)
    {
        for (int16_t y = radius; y < tft.height(); y += radius * 2)
        {
            tft.fillCircle(x, y, radius, color);
        }
    }
}

void testdrawcircles(uint8_t radius, uint16_t color)
{
    for (int16_t x = 0; x < tft.width() + radius; x += radius * 2)
    {
        for (int16_t y = 0; y < tft.height() + radius; y += radius * 2)
        {
            tft.drawCircle(x, y, radius, color);
        }
    }
}

void testtriangles()
{
    tft.fillScreen(TFT_BLACK);
    int color = 0xF800;
    int t;
    int w = tft.width() / 2;
    int x = tft.height() - 1;
    int y = 0;
    int z = tft.width();
    for (t = 0; t <= 15; t += 1)
    {
        tft.drawTriangle(w, y, y, x, z, x, color);
        x -= 4;
        y += 4;
        z -= 4;
        color += 100;
    }
}

void testroundrects()
{
    tft.fillScreen(TFT_BLACK);
    int color = 100;
    int i;
    int t;
    for (t = 0; t <= 4; t += 1)
    {
        int x = 0;
        int y = 0;
        int w = tft.width() - 2;
        int h = tft.height() - 2;
        for (i = 0; i <= 16; i += 1)
        {
            tft.drawRoundRect(x, y, w, h, 5, color);
            x += 2;
            y += 3;
            w -= 4;
            h -= 6;
            color += 1100;
        }
        color += 100;
    }
}

void tftPrintTest()
{
    tft.setTextWrap(false);
    tft.fillScreen(TFT_BLACK);
    tft.setCursor(0, 30);
    tft.setTextColor(TFT_RED);
    tft.setTextSize(1);
    tft.println("Hello World!");
    tft.setTextColor(TFT_YELLOW);
    tft.setTextSize(2);
    tft.println("Hello World!");
    tft.setTextColor(TFT_GREEN);
    tft.setTextSize(3);
    tft.println("Hello World!");
    tft.setTextColor(TFT_BLUE);
    tft.setTextSize(4);
    tft.print(1234.567);
    delay(1500);
    tft.setCursor(0, 0);
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_WHITE);
    tft.setTextSize(0);
    tft.println("Hello World!");
    tft.setTextSize(1);
    tft.setTextColor(TFT_GREEN);
    tft.print(p, 6);
    tft.println(" Want pi?");
    tft.println(" ");
    tft.print(8675309, HEX); // print 8,675,309 out in HEX!
    tft.println(" Print HEX!");
    tft.println(" ");
    tft.setTextColor(TFT_WHITE);
    tft.println("Sketch has been");
    tft.println("running for: ");
    tft.setTextColor(TFT_MAGENTA);
    tft.print(millis() / 1000);
    tft.setTextColor(TFT_WHITE);
    tft.print(" seconds.");
}

void mediabuttons()
{
    // play
    tft.fillScreen(TFT_BLACK);
    tft.fillRoundRect(25, 10, 78, 60, 8, TFT_WHITE);
    tft.fillTriangle(42, 20, 42, 60, 90, 40, TFT_RED);
    delay(500);
    // pause
    tft.fillRoundRect(25, 90, 78, 60, 8, TFT_WHITE);
    tft.fillRoundRect(39, 98, 20, 45, 5, TFT_GREEN);
    tft.fillRoundRect(69, 98, 20, 45, 5, TFT_GREEN);
    delay(500);
    // play color
    tft.fillTriangle(42, 20, 42, 60, 90, 40, TFT_BLUE);
    delay(50);
    // pause color
    tft.fillRoundRect(39, 98, 20, 45, 5, TFT_RED);
    tft.fillRoundRect(69, 98, 20, 45, 5, TFT_RED);
    // play color
    tft.fillTriangle(42, 20, 42, 60, 90, 40, TFT_GREEN);
}
相关推荐
SUN_Gyq5 分钟前
什么是 C++ 中的模板特化和偏特化? 如何进行模板特化和偏特化?
开发语言·c++·算法
愿天垂怜15 分钟前
【C++】C++11引入的新特性(1)
java·c语言·数据结构·c++·算法·rust·哈希算法
文弱书生65617 分钟前
TIM输入捕获
stm32·单片机·嵌入式硬件
大帅哥_22 分钟前
访问限定符
c语言·c++
小林熬夜学编程1 小时前
【Linux系统编程】第五十弹---构建高效单例模式线程池、详解线程安全与可重入性、解析死锁与避免策略,以及STL与智能指针的线程安全性探究
linux·运维·服务器·c语言·c++·安全·单例模式
我qq不是451516521 小时前
C语言指针作业
c语言
苏言の狗1 小时前
小R的二叉树探险 | 模拟
c语言·数据结构·算法·宽度优先
加载中loading...1 小时前
C/C++实现tcp客户端和服务端的实现(从零开始写自己的高性能服务器)
linux·运维·服务器·c语言·网络
凯子坚持 c1 小时前
C++之二叉搜索树:高效与美的极致平衡
开发语言·c++
埋头编程~1 小时前
【C++】踏上C++学习之旅(十):深入“类和对象“世界,掌握编程黄金法则(五)(最终篇,内含初始化列表、静态成员、友元以及内部类等等)
java·c++·学习