【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
项目之一百二十三:ESP32+GC9A01之线条间距在 5-50 之间递增和递减的动态网格
实验开源代码
- /*
- 【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
- 实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
- 项目之一百二十三:ESP32+GC9A01之线条间距在 5-50 之间递增和递减的动态网格
- */
-
- // 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 MIN_SPACING 5 // 最小间距
- #define MAX_SPACING 50 // 最大间距
- #define REFRESH_INTERVAL 500 // 0.5秒刷新一次
-
- int spacing = MIN_SPACING; // 当前间距
- bool increasing = true; // 控制增长或减小
- unsigned long lastUpdate = 0;
-
- void setup() {
- tft.init();
- tft.setRotation(1);
- tft.fillScreen(TFT_BLACK);
- randomSeed(analogRead(0));
- }
-
- void loop() {
- if (millis() - lastUpdate > REFRESH_INTERVAL) {
- lastUpdate = millis();
- drawRandomGrid();
- updateSpacing();
- }
- }
-
- /**
- * 绘制随机颜色网格
- */
- void drawRandomGrid() {
- tft.fillScreen(TFT_BLACK);
-
- for (int x = 0; x < SCREEN_WIDTH; x += spacing) {
- tft.drawLine(x, 0, x, SCREEN_HEIGHT, getRandomColor());
- }
- for (int y = 0; y < SCREEN_HEIGHT; y += spacing) {
- tft.drawLine(0, y, SCREEN_WIDTH, y, getRandomColor());
- }
- }
-
- /**
- * 生成随机颜色(红、绿、蓝)
- */
- uint16_t getRandomColor() {
- int r = random(0, 3);
- if (r == 0) return TFT_RED;
- if (r == 1) return TFT_GREEN;
- return TFT_BLUE;
- }
-
- /**
- * 更新线条间距,实现 5-50 之间循环变化
- */
- void updateSpacing() {
- if (increasing) {
- spacing += 5;
- if (spacing >= MAX_SPACING) increasing = false;
- } else {
- spacing -= 5;
- if (spacing <= MIN_SPACING) increasing = true;
- }
- }
复制代码
|