|
预先设计的模板 载入中 在这个模板中,我们只使用了一个字符串和8个填充圆圈,按顺序改变颜色。 要在静态点周围绘制圆圈,可以使用sin(); 和cos();函数。您需要定义PI数字。 要更改颜色,可以使用color565(); 函数并替换您的RGB代码。 - #include "Adafruit_GFX.h"
- #include "MCUFRIEND_kbv.h"
- MCUFRIEND_kbv tft;
- #include "Fonts/FreeSans9pt7b.h"
- #include "Fonts/FreeSans12pt7b.h"
- #include "Fonts/FreeSerif12pt7b.h"
- #include "FreeDefaultFonts.h"
- #define PI 3.1415926535897932384626433832795
- int col[8];
- void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg)
- {
- int16_t x1, y1;
- uint16_t wid, ht;
- tft.setFont(f);
- tft.setCursor(x, y);
- tft.setTextColor(0x0000);
- tft.setTextSize(sz);
- tft.print(msg);
- }
- void setup() {
- tft.reset();
- Serial.begin(9600);
- uint16_t ID = tft.readID();
- tft.begin(ID);
- tft.setRotation(1);
- tft.invertDisplay(true);
- tft.fillScreen(0xffff);
- showmsgXY(170, 250, 2, &FreeSans9pt7b, "Loading...");
- col[0] = tft.color565(155, 0, 50);
- col[1] = tft.color565(170, 30, 80);
- col[2] = tft.color565(195, 60, 110);
- col[3] = tft.color565(215, 90, 140);
- col[4] = tft.color565(230, 120, 170);
- col[5] = tft.color565(250, 150, 200);
- col[6] = tft.color565(255, 180, 220);
- col[7] = tft.color565(255, 210, 240);
- }
- void loop() {
- for (int i = 8; i > 0; i--) {
- tft.fillCircle(240 + 40 * (cos(-i * PI / 4)), 120 + 40 * (sin(-i * PI / 4)), 10, col[0]); delay(15);
- tft.fillCircle(240 + 40 * (cos(-(i + 1)*PI / 4)), 120 + 40 * (sin(-(i + 1)*PI / 4)), 10, col[1]); delay(15);
- tft.fillCircle(240 + 40 * (cos(-(i + 2)*PI / 4)), 120 + 40 * (sin(-(i + 2)*PI / 4)), 10, col[2]); delay(15);
- tft.fillCircle(240 + 40 * (cos(-(i + 3)*PI / 4)), 120 + 40 * (sin(-(i + 3)*PI / 4)), 10, col[3]); delay(15);
- tft.fillCircle(240 + 40 * (cos(-(i + 4)*PI / 4)), 120 + 40 * (sin(-(i + 4)*PI / 4)), 10, col[4]); delay(15);
- tft.fillCircle(240 + 40 * (cos(-(i + 5)*PI / 4)), 120 + 40 * (sin(-(i + 5)*PI / 4)), 10, col[5]); delay(15);
- tft.fillCircle(240 + 40 * (cos(-(i + 6)*PI / 4)), 120 + 40 * (sin(-(i + 6)*PI / 4)), 10, col[6]); delay(15);
- tft.fillCircle(240 + 40 * (cos(-(i + 7)*PI / 4)), 120 + 40 * (sin(-(i + 7)*PI / 4)), 10, col[7]); delay(15);
- }
- }
复制代码
徽标演示 在这个模板中,我们将a.jpg图像转换为.c文件并添加到代码中,写入一个字符串并使用淡入淡出代码进行显示。 然后我们使用滚动代码向左移动屏幕。 下载.h文件并将其添加到Arduino sketch的文件夹中。 - #include "Adafruit_GFX.h" // Core graphics library
- #include "MCUFRIEND_kbv.h" // Hardware-specific library
- MCUFRIEND_kbv tft;
- #include "Ard_Logo.h"
- #define BLACK 0x0000
- #define RED 0xF800
- #define GREEN 0x07E0
- #define WHITE 0xFFFF
- #define GREY 0x8410
- #include "Fonts/FreeSans9pt7b.h"
- #include "Fonts/FreeSans12pt7b.h"
- #include "Fonts/FreeSerif12pt7b.h"
- #include "FreeDefaultFonts.h"
- void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg)
- {
- int16_t x1, y1;
- uint16_t wid, ht;
- tft.setFont(f);
- tft.setCursor(x, y);
- tft.setTextSize(sz);
- tft.println(msg);
- }
- uint8_t r = 255, g = 255, b = 255;
- uint16_t color;
- void setup()
- {
- Serial.begin(9600);
- uint16_t ID = tft.readID();
- tft.begin(ID);
- tft.invertDisplay(true);
- tft.setRotation(1);
- }
- void loop(void)
- {
- tft.invertDisplay(true);
- tft.fillScreen(WHITE);
- tft.drawRGBBitmap(100, 50, Logo, 350, 200);
- delay(1000);
- tft.setTextSize(2);
- for (int j = 0; j < 20; j++) {
- color = tft.color565(r -= 12, g -= 12, b -= 12);
- tft.setTextColor(color);
- showmsgXY(95, 280, 1, &FreeSans12pt7b, "ELECTROPEAK PRESENTS");
- delay(20);
- }
- delay(1000);
- for (int i = 0; i < 480; i++) {
- tft.vertScroll(0, 480, i);
- tft.drawFastVLine(i, 0, 320, 0xffff); // vertical line
- delay(5);}
- while (1);
- }
复制代码
点状图 在这个模板中,我们使用了绘制线、实心圆和字符串显示函数。 - #include "Adafruit_GFX.h"
- #include "MCUFRIEND_kbv.h"
- MCUFRIEND_kbv tft;
- uint16_t ox=0,oy=0;
- int ave=0, avec=0, avet=0;
- ////////////////////////////////////////////////////////////////
- void aveg(void)
- {int z=0;
- Serial.println(ave);
- Serial.println(avec);
- avet=ave/avec;
- Serial.println(avet);
- avet=avet*32;
- for (int i=0; i<24; i++){
- for (uint16_t a=0; a<3; a++){
- tft.drawLine(avet+a, z, avet+a, z+10, 0xFB21);} // thick
- for (uint16_t a=0; a<2; a++){ tft.drawLine(avet-a, z, avet-a, z+10, 0xFB21);} delay(100); z=z+20; } } ////////////////////////////////////////////////////////////////// void dchart_10x10(uint16_t nx,uint16_t ny) { ave+=nx; avec++; nx=nx*32; ny=ny*48; tft.drawCircle(nx, ny, 10, 0x0517); tft.drawCircle(nx, ny, 9, 0x0517); tft.fillCircle(nx, ny, 7, 0x0517); delay (100); ox=nx; oy=ny; } /////////////////////////////////////////////////////////////////////// void dotchart_10x10(uint16_t nx,uint16_t ny) { ave+=nx; avec++; nx=nx*32; ny=ny*48; int plus=0; float fplus=0; int sign=0; int y=0,x=0; y=oy; x=ox; float xmines, ymines; xmines=nx-ox; ymines=ny-oy; if (ox>nx)
- {xmines=ox-nx;
- sign=1;}
- else
- sign=0;
- for (int a=0; a<(ny-oy); a++)
- {
- fplus+=xmines/ymines;
- plus=fplus;
- if (sign==1)
- tft.drawFastHLine(0, y, x-plus, 0xBFDF);
- else
- tft.drawFastHLine(0, y, x+plus, 0xBFDF);
- y++;
- delay(5);}
- for (uint16_t a=0; a<2; a++){
- tft.drawLine(ox+a, oy, nx+a, ny, 0x01E8);} // thick
- for (uint16_t a=0; a<2; a++){
- tft.drawLine(ox, oy+a, nx, ny+a, 0x01E8);}
- ox=nx;
- oy=ny;
- }
- ////////////////////////////////////////////////////////////////////
- void setup() {
- tft.reset();
- Serial.begin(9600);
- uint16_t ID = tft.readID();
- tft.begin(ID);
- }
- void loop() {
- tft.invertDisplay(true);
- tft.fillScreen(0xffff);
- dotchart_10x10(3, 0);
- dotchart_10x10(2, 1);
- dotchart_10x10(4, 2);
- dotchart_10x10(4, 3);
- dotchart_10x10(5, 4);
- dotchart_10x10(3, 5);
- dotchart_10x10(6, 6);
- dotchart_10x10(7, 7);
- dotchart_10x10(9, 8);
- dotchart_10x10(8, 9);
- dotchart_10x10(10, 10);
- dchart_10x10(3, 0);
- dchart_10x10(2, 1);
- dchart_10x10(4, 2);
- dchart_10x10(4, 3);
- dchart_10x10(5, 4);
- dchart_10x10(3, 5);
- dchart_10x10(6, 6);
- dchart_10x10(7, 7);
- dchart_10x10(9, 8);
- dchart_10x10(8, 9);
- dchart_10x10(10, 10);
- tft.setRotation(1);
- tft.setTextSize(2);
- tft.setTextColor(0x01E8);
- tft.setCursor(20, 20);
- tft.print("Average");
- int dl=20;
- for (int i=0;i<6;i++){
- for (uint16_t a=0; a<3; a++){
- tft.drawLine(dl, 40+a, dl+10, 40+a, 0xFB21);}
- dl+=16;}
- tft.setRotation(0);
- aveg();
- while(1);
- }
复制代码
屏幕保护 在这个模板中,我们只是通过RGBbitmap和位图函数显示一些图像。 只需为触摸屏制作代码并使用此模板即可。 下载.h文件并将其添加到Arduino sketch的文件夹中。 - #include "Adafruit_GFX.h" // Core graphics library
- #include "MCUFRIEND_kbv.h" // Hardware-specific library
- MCUFRIEND_kbv tft;
- #define BLACK 0x0000
- #define RED 0xF800
- #define GREEN 0x07E0
- #define WHITE 0xFFFF
- #define GREY 0x8410
- #include "images.h"
- #include "Fonts/FreeSans9pt7b.h"
- #include "Fonts/FreeSans12pt7b.h"
- #include "Fonts/FreeSerif12pt7b.h"
- #include "FreeDefaultFonts.h"
- int a = 3000;
- int b = 4000;
- int j, j2;
- int i, i2;
- void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg)
- {
- int16_t x1, y1;
- uint16_t wid, ht;
- // tft.drawFastHLine(0, y, tft.width(), 0xffff);
- tft.setFont(f);
- tft.setCursor(x, y);
- tft.setTextColor(WHITE);
- tft.setTextSize(sz);
- tft.print(msg);
- delay(1000);
- }
- void setup()
- {
- Serial.begin(9600);
- uint16_t ID = tft.readID();
- tft.begin(ID);
- tft.invertDisplay(true);
- tft.setRotation(1);
- }
- void loop(void)
- {
- tft.invertDisplay(true);
- tft.fillScreen(BLACK);
- tft.drawRGBBitmap(0, 0, test, 480, 320);
- tft.drawBitmap(20, 20, Line1, 45, 45, 0xffff);//battery
- tft.drawBitmap(65, 20, Line2, 45, 45, 0xffff);//wifi
- tft.drawBitmap(125, 25, Line3, 45, 45, 0xffff);//mail
- tft.drawBitmap(185, 25, Line4, 45, 45, 0xffff);//instagram
- tft.drawBitmap(245, 25, Line6, 45, 45, 0xffff);//power
- tft.drawBitmap(20, 260, Line5, 45, 45, 0xffff);//twitter
- tft.drawBitmap(410, 140, Line7, 45, 45, 0xffff);//rain
- tft.setTextSize(6);
- tft.setTextColor(0xffff);
- tft.setCursor(280, 210);
- tft.print("20:45");
- tft.setTextSize(2);
- tft.setTextColor(0xffff);
- showmsgXY(330, 280, 1, &FreeSans12pt7b, "Saturday");
- showmsgXY(300, 305, 1, &FreeSans12pt7b, "6 October 2018");
- while (1);
- }
复制代码
一些注意事项 ● 所有GIF文件播放的速度都进行了编辑,我们使它们更快或更慢,以便更好地理解。 运动速度取决于处理器的速度或代码类型或代码中元素的大小和厚度。 ● 您可以在main函数页面中添加图像代码,但它会填充所有main页面。 因此,您可以创建a.h文件并添加到草图的文件夹中。 ● 如果您在包含库时遇到问题,请将“”符号更改为<>。
您也可以在ElectroPeak的官方网站上阅读这个项目: https://electropeak.com/learn/guides/absolute-beginners-guide-to-tft-lcd-displays-by-arduino/ |