【花雕学编程】Arduino动手做(249)--GC9A01屏六角形万花筒
【花雕学编程】Arduino动手做(249)--GC9A01屏六角形万花筒
【花雕学编程】Arduino动手做(249)--GC9A01屏六角形万花筒
【花雕学编程】Arduino动手做(249)--GC9A01屏六角形万花筒
【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
项目之四十五:GC9A01屏之动态六边形和渐变色彩叠加的万花筒
实验开源代码
/*
【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
项目之四十五: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 "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_GC9A01A.h"
// 定义屏幕引脚
#define TFT_CS 4 // 片选引脚
#define TFT_DC 2 // 数据/命令引脚
#define TFT_RST -1 // 重置引脚(未使用时设置为 -1)
// 初始化屏幕对象
Adafruit_GC9A01A tft = Adafruit_GC9A01A(TFT_CS, TFT_DC, TFT_RST);
// 定义屏幕中心
#define CENTER_X (tft.width() / 2)
#define CENTER_Y (tft.height() / 2)
#define FRAME_DELAY 500 // 帧延迟
#define NUM_SECTORS 6 // 扇区数量
#define MAX_RADIUS 120 // 图案最大半径
#define ROTATION_SPEED 200.2 // 旋转速度
// 渐变颜色插值函数
uint16_t interpolateColor(uint16_t color1, uint16_t color2, float fraction) {
uint8_t r1 = (color1 >> 11) & 0x1F;
uint8_t g1 = (color1 >> 5) & 0x3F;
uint8_t b1 = color1 & 0x1F;
uint8_t r2 = (color2 >> 11) & 0x1F;
uint8_t g2 = (color2 >> 5) & 0x3F;
uint8_t b2 = color2 & 0x1F;
uint8_t r = r1 + fraction * (r2 - r1);
uint8_t g = g1 + fraction * (g2 - g1);
uint8_t b = b1 + fraction * (b2 - b1);
return (r << 11) | (g << 5) | b;
}
// 绘制动态六边形
void drawDynamicPolygon(int centerX, int centerY, int radius, int sides, float rotationAngle, float scale, uint16_t color) {
float angleStep = 2 * 3.14159 / sides;
int prevX = centerX + radius * scale * cos(rotationAngle);
int prevY = centerY + radius * scale * sin(rotationAngle);
for (int i = 1; i <= sides; i++) {
float angle = rotationAngle + i * angleStep;
int x = centerX + radius * scale * cos(angle);
int y = centerY + radius * scale * sin(angle);
tft.drawLine(prevX, prevY, x, y, color);
prevX = x;
prevY = y;
}
}
// 绘制万花筒图案
void drawKaleidoscopePattern(float rotationAngle, float scale) {
for (int sector = 0; sector < NUM_SECTORS; sector++) {
float angle = sector * (360.0 / NUM_SECTORS) + rotationAngle;
float radians = angle * 3.14159 / 180;
for (int radius = 10; radius <= MAX_RADIUS; radius += 15) {
float fraction = (float)(radius - 10) / (MAX_RADIUS - 10);
uint16_t color = interpolateColor(GC9A01A_BLUE, GC9A01A_MAGENTA, fraction);
int x = CENTER_X + cos(radians) * radius;
int y = CENTER_Y + sin(radians) * radius;
drawDynamicPolygon(x, y, radius / 2, 6, radians, scale, color);
}
}
}
void setup() {
Serial.begin(115200); // 初始化串口
Serial.println("Smooth Kaleidoscope");
tft.begin(); // 初始化屏幕
tft.setRotation(0); // 设置屏幕方向
tft.fillScreen(GC9A01A_BLACK); // 设置黑色背景
}
void loop() {
static float rotationAngle = 0; // 初始化旋转角度
static float scale = 1.0; // 初始缩放因子
static bool growing = true; // 缩放方向
// 清屏
tft.fillScreen(GC9A01A_BLACK);
// 绘制万花筒图案
drawKaleidoscopePattern(rotationAngle, scale);
// 更新旋转角度
rotationAngle = fmod(rotationAngle + ROTATION_SPEED, 360.0);
// 动态缩放
if (growing) {
scale += 0.01;
if (scale >= 1.3) growing = false;
} else {
scale -= 0.01;
if (scale <= 1.0) growing = true;
}
delay(FRAME_DELAY); // 控制帧速率
}
【花雕学编程】Arduino动手做(249)--GC9A01屏六角形万花筒
代码解读1. 平滑动态旋转
使用较低的旋转速度 (ROTATION_SPEED) 和高帧率 (FRAME_DELAY),让图案旋转更加流畅。
2. 动态缩放
增加缩放因子 (scale),图案随着时间在不同层次上放大和缩小,模拟立体效果。
动态反转缩放方向 (growing),实现周期性变化。
3. 渐变颜色
添加更细腻的渐变效果,通过插值计算色彩过渡,使颜色在视觉上更柔和和“丝滑”。
4. 增强几何复杂性
通过 drawDynamicPolygon() 添加动态变化的六边形,并允许缩放对形状产生影响。
5. 效果
视觉丝滑:旋转和缩放更加流畅自然。
图形复杂性:动态六边形和渐变色彩叠加,图案层次感更强。
立体动感:万花筒图案在旋转时周期性放大和缩小,营造立体效果。
【花雕学编程】Arduino动手做(249)--GC9A01屏六角形万花筒
实验场景图动态图
页:
[1]