37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手试试多做实验,不管成功与否,都会记录下来——小小的进步或是搞不掂的问题,希望能够抛砖引玉。
【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
实验开发板使用ESP32
实验模块接线示意图
【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
项目之六十五:GC9A01园屏之倾斜 20 度的蓝色圆柱体
实验开源代码
/*
【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
项目之六十五:GC9A01园屏之倾斜 20 度的蓝色圆柱体
*/
// 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" // **包含 SPI 库,用于 TFT 屏幕通信**
#include "Adafruit_GFX.h" // **包含 Adafruit GFX 图形库,用于绘制图形**
#include "Adafruit_GC9A01A.h" // **包含 GC9A01A 屏幕驱动库**
#define TFT_CS 4 // **定义 TFT 屏幕片选引脚**
#define TFT_DC 2 // **定义 TFT 屏幕数据/命令选择引脚**
#define TFT_RST -1 // **屏幕复位引脚(-1 表示未使用)**
Adafruit_GC9A01A tft = Adafruit_GC9A01A(TFT_CS, TFT_DC, TFT_RST); // **创建 TFT 屏幕对象**
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 240
#define CENTER_X SCREEN_WIDTH / 2
#define CENTER_Y SCREEN_HEIGHT / 2
#define CYLINDER_RADIUS 35 // **定义圆柱体半径**
#define CYLINDER_HEIGHT 90 // **定义圆柱体高度**
#define ROTATION_SPEED 0.05
#define TILT_ANGLE 45 * M_PI / 180 // **倾斜角度 45°**
float angle = 0;
uint16_t fillColor = tft.color565(0, 255, 0); // **绿色填充**
uint16_t edgeColor = tft.color565(0, 0, 255); // **蓝色边框**
// **定义圆柱体上下底面的顶点坐标(这里简化为用多个点近似圆形)**
#define NUM_POINTS 20 // **定义近似圆形的点数**
float topVertices[NUM_POINTS][3];
float bottomVertices[NUM_POINTS][3];
// **初始化圆柱体顶点坐标**
void initCylinderVertices() {
for (int i = 0; i < NUM_POINTS; i++) {
float theta = (float)i / NUM_POINTS * 2 * M_PI;
topVertices[i][0] = CYLINDER_RADIUS * cos(theta);
topVertices[i][1] = CYLINDER_HEIGHT / 2;
topVertices[i][2] = CYLINDER_RADIUS * sin(theta);
bottomVertices[i][0] = CYLINDER_RADIUS * cos(theta);
bottomVertices[i][1] = -CYLINDER_HEIGHT / 2;
bottomVertices[i][2] = CYLINDER_RADIUS * sin(theta);
}
}
// **绘制填充的圆柱体面(这里绘制上下底面和侧面)**
void drawCylinderFace(int a, int b, int c, float transformedVertices[NUM_POINTS][2], uint16_t color) {
tft.fillTriangle(transformedVertices[a][0], transformedVertices[a][1],
transformedVertices[b][0], transformedVertices[b][1],
transformedVertices[c][0], transformedVertices[c][1], color);
}
// **绘制圆柱体边框(侧面的边和上下底面的边)**
void drawCylinderEdge(int i, int j, float transformedVertices[NUM_POINTS][2], uint16_t color) {
tft.drawLine(transformedVertices[i][0], transformedVertices[i][1],
transformedVertices[j][0], transformedVertices[j][1], color);
}
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(2);
tft.fillScreen(tft.color565(0, 0, 0));
initCylinderVertices();
}
void loop() {
tft.fillScreen(tft.color565(0, 0, 0));
float transformedTopVertices[NUM_POINTS][2];
float transformedBottomVertices[NUM_POINTS][2];
for (int i = 0; i < NUM_POINTS; i++) {
float xTop = topVertices[i][0];
float yTop = topVertices[i][1];
float zTop = topVertices[i][2];
float xBottom = bottomVertices[i][0];
float yBottom = bottomVertices[i][1];
float zBottom = bottomVertices[i][2];
// **绕 X 轴倾斜 45°**
float tiltedYTop = yTop * cos(TILT_ANGLE) - zTop * sin(TILT_ANGLE);
float tiltedZTop = yTop * sin(TILT_ANGLE) + zTop * cos(TILT_ANGLE);
float tiltedYBottom = yBottom * cos(TILT_ANGLE) - zBottom * sin(TILT_ANGLE);
float tiltedZBottom = yBottom * sin(TILT_ANGLE) + zBottom * cos(TILT_ANGLE);
// **绕 Y 轴旋转**
float rotatedXTop = xTop * cos(angle) - tiltedZTop * sin(angle);
float rotatedZTop = xTop * sin(angle) + tiltedZTop * cos(angle);
float rotatedXBottom = xBottom * cos(angle) - tiltedZBottom * sin(angle);
float rotatedZBottom = xBottom * sin(angle) + tiltedZBottom * cos(angle);
// **投影到 2D 屏幕**
transformedTopVertices[i][0] = CENTER_X + rotatedXTop;
transformedTopVertices[i][1] = CENTER_Y + tiltedYTop;
transformedBottomVertices[i][0] = CENTER_X + rotatedXBottom;
transformedBottomVertices[i][1] = CENTER_Y + tiltedYBottom;
}
// **绘制圆柱体上下底面**
for (int i = 0; i < NUM_POINTS; i++) {
int j = (i + 1) % NUM_POINTS;
drawCylinderFace(i, j, (j + 1) % NUM_POINTS, transformedTopVertices, fillColor);
drawCylinderFace(i, j, (j + 1) % NUM_POINTS, transformedBottomVertices, fillColor);
}
// **绘制圆柱体侧面边框**
for (int i = 0; i < NUM_POINTS; i++) {
int j = (i + 1) % NUM_POINTS;
drawCylinderEdge(i, j, transformedTopVertices, edgeColor);
drawCylinderEdge(i, j, transformedBottomVertices, edgeColor);
// 修正变量引用错误
drawCylinderEdge(i, i, transformedTopVertices, edgeColor);
drawCylinderEdge(i, i, transformedBottomVertices, edgeColor);
drawCylinderEdge(i, i + NUM_POINTS, transformedTopVertices, edgeColor); // 这里逻辑可能有误,可根据实际情况调整
}
angle += ROTATION_SPEED;
delay(50);
}
代码解读:
- 常量和变量定义:定义了圆柱体的半径、高度、倾斜角度等常量,以及颜色变量和用于存储圆柱体顶点坐标的数组。
- 初始化函数:initCylinderVertices 函数用于初始化圆柱体上下底面的顶点坐标,通过将圆近似为多个点来实现。
- 绘制函数:drawCylinderFace 函数用于绘制圆柱体的面(上下底面),drawCylinderEdge 函数用于绘制圆柱体的边框(侧面和上下底面的边)。
- setup 函数:初始化串口、屏幕,设置屏幕旋转方向,清屏,并调用 initCylinderVertices 函数初始化圆柱体顶点坐标。
- loop 函数:每次循环清屏,对圆柱体顶点进行坐标变换(倾斜、旋转、投影),绘制圆柱体的上下底面和侧面边框,更新旋转角度并延迟一段时间以控制动画速度。
这样就实现了一个倾斜 45 度的绿色圆柱体动画。
实验场景图 动态图