【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
项目之一百二十五:ESP32+GC9A01之生成交替颜色的动态网格
实验开源代码
- /*
- 【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
- 实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
- 项目之一百二十五:ESP32+GC9A01之生成交替颜色的动态网格
- */
-
- // GC9A01---------- ESP32
- // RST ------------ NC(复位引脚,此处未连接)
- // CS ------------- D4(片选引脚,连接到ESP32的D4引脚)
- // DC ------------- D2(数据/命令选择引脚,连接到ESP32的D2引脚)
- // SDA ------------ D23 (green)(主数据输出引脚,连接到ESP32的D23引脚,绿色线)
- // SCL ------------ D18 (yellow)(时钟信号引脚,连接到ESP32的D18引脚,黄色线)
- // GND ------------ GND(接地引脚,连接到ESP32的接地端)
- // VCC -------------3V3(电源引脚,连接到ESP32的3.3V电源)
-
- #include <TFT_eSPI.h>
-
- TFT_eSPI tft =
- TFT_eSPI
- ();
-
- #define SCREEN_WIDTH 240
- #define SCREEN_HEIGHT 240
- #define SPACING 30 // 线条间距
- #define REFRESH_RATE 200 // 刷新率(毫秒),越小动画越流畅
-
- int frameCount = 0
- ;
-
- void setup()
- {
- tft.
- init
- ();
- tft.
- setRotation(1); // 设定屏幕方向(横屏)
- tft.
- fillScreen(TFT_BLACK); // 清空屏幕
- randomSeed(analogRead(0)); // 初始化随机数种子
- }
-
- void loop()
- {
- drawAlternatingGrid
- ();
- frameCount++;
- delay(REFRESH_RATE); // 等待指定时间后刷新
- }
-
- /**
- 生成交替颜色的动态网格
- */
- void drawAlternatingGrid()
- {
- tft.
- fillScreen(TFT_BLACK); // 清除屏幕
-
- uint16_t
- verticalColors[] = {TFT_RED, TFT_BLUE, TFT_CYAN, TFT_MAGENTA};
- uint16_t
- horizontalColors[] = {TFT_GREEN, TFT_YELLOW, TFT_ORANGE, TFT_PURPLE};
-
- int numVerticalColors = sizeof(verticalColors) / sizeof(verticalColors[0
- ]);
- int numHorizontalColors = sizeof(horizontalColors) / sizeof(horizontalColors[0
- ]);
-
- // 根据帧计数器改变颜色模式
- int verticalOffset = (frameCount / 10) % numVerticalColors; // 每10帧变化一次
- int horizontalOffset = (frameCount / 15) % numHorizontalColors; // 每15帧变化一次
-
- // 绘制纵向线条
- for (int x = 0
- ; x < SCREEN_WIDTH; x += SPACING) {
- int
- colorIndex = (x / SPACING + verticalOffset) % numVerticalColors;
- uint16_t
- color = verticalColors[colorIndex];
- tft.
- drawLine(x, 0
- , x, SCREEN_HEIGHT, color);
- }
-
- // 绘制横向线条
- for (int y = 0
- ; y < SCREEN_HEIGHT; y += SPACING) {
- int
- colorIndex = (y / SPACING + horizontalOffset) % numHorizontalColors;
- uint16_t
- color = horizontalColors[colorIndex];
- tft.
- drawLine(0
- , y, SCREEN_WIDTH, y, color);
- }
- }
复制代码
|