65浏览
查看: 65|回复: 7

[项目] 【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画

[复制链接]
【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图2

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图1

驴友花雕  中级技神
 楼主|

发表于 2025-4-11 17:32:21

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图1

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图2
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2025-4-11 17:33:33

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图1

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图2
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2025-4-11 18:00:54

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画

  【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
项目之六十六:GC9A01园屏之倾斜 45 度且会旋转的绿色圆柱体动画

实验开源代码

  1. /*
  2.   【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百四十九:1.28寸圆形彩色TFT显示屏 高清IPS 模块 240*240 SPI接口GC9A01驱动
  4.   项目之六十六:GC9A01园屏之倾斜 45 度且会旋转的绿色圆柱体动画
  5. */
  6. //       GC9A01---------- ESP32
  7. //       RST ------------ NC(复位引脚,此处未连接)
  8. //       CS ------------- D4(片选引脚,连接到ESP32的D4引脚)
  9. //       DC ------------- D2(数据/命令选择引脚,连接到ESP32的D2引脚)
  10. //       SDA ------------ D23 (green)(主数据输出引脚,连接到ESP32的D23引脚,绿色线)
  11. //       SCL ------------ D18 (yellow)(时钟信号引脚,连接到ESP32的D18引脚,黄色线)
  12. //       GND ------------ GND(接地引脚,连接到ESP32的接地端)
  13. //       VCC -------------3V3(电源引脚,连接到ESP32的3.3V电源)
  14. #include "SPI.h"                      // **包含 SPI 库,用于 TFT 屏幕通信**
  15. #include "Adafruit_GFX.h"             // **包含 Adafruit GFX 图形库,用于绘制图形**
  16. #include "Adafruit_GC9A01A.h"         // **包含 GC9A01A 屏幕驱动库**
  17. #define TFT_CS 4                      // **定义 TFT 屏幕片选引脚**
  18. #define TFT_DC 2                      // **定义 TFT 屏幕数据/命令选择引脚**
  19. #define TFT_RST -1                    // **屏幕复位引脚(-1 表示未使用)**
  20. Adafruit_GC9A01A tft = Adafruit_GC9A01A(TFT_CS, TFT_DC, TFT_RST); // **创建 TFT 屏幕对象**
  21. #define SCREEN_WIDTH 240
  22. #define SCREEN_HEIGHT 240
  23. #define CENTER_X SCREEN_WIDTH / 2
  24. #define CENTER_Y SCREEN_HEIGHT / 2
  25. #define CYLINDER_RADIUS 40
  26. #define CYLINDER_HEIGHT 90
  27. #define ROTATION_SPEED 0.03
  28. #define TILT_ANGLE 45 * M_PI / 180
  29. float angle = 0;
  30. uint16_t fillColor = tft.color565(0, 255, 0);
  31. uint16_t edgeColor = tft.color565(0, 0, 255);
  32. #define NUM_POINTS 20
  33. float topVertices[NUM_POINTS][3];
  34. float bottomVertices[NUM_POINTS][3];
  35. // 初始化圆柱体顶点坐标
  36. void initCylinderVertices() {
  37.     for (int i = 0; i < NUM_POINTS; i++) {
  38.         float theta = (float)i / NUM_POINTS * 2 * M_PI;
  39.         topVertices[i][0] = CYLINDER_RADIUS * cos(theta);
  40.         topVertices[i][1] = CYLINDER_HEIGHT / 2;
  41.         topVertices[i][2] = CYLINDER_RADIUS * sin(theta);
  42.         bottomVertices[i][0] = CYLINDER_RADIUS * cos(theta);
  43.         bottomVertices[i][1] = -CYLINDER_HEIGHT / 2;
  44.         bottomVertices[i][2] = CYLINDER_RADIUS * sin(theta);
  45.     }
  46. }
  47. // 绘制填充的圆柱体面
  48. void drawCylinderFace(int a, int b, int c, float transformedVertices[NUM_POINTS][2], uint16_t color) {
  49.     tft.fillTriangle(transformedVertices[a][0], transformedVertices[a][1],
  50.                      transformedVertices[b][0], transformedVertices[b][1],
  51.                      transformedVertices[c][0], transformedVertices[c][1], color);
  52. }
  53. // 绘制圆柱体边框
  54. void drawCylinderEdge(int i, int j, float transformedVertices[NUM_POINTS][2], uint16_t color) {
  55.     tft.drawLine(transformedVertices[i][0], transformedVertices[i][1],
  56.                  transformedVertices[j][0], transformedVertices[j][1], color);
  57. }
  58. // 绘制圆柱体侧面
  59. void drawCylinderSide(int i, float transformedTopVertices[NUM_POINTS][2], float transformedBottomVertices[NUM_POINTS][2], uint16_t color) {
  60.     int j = (i + 1) % NUM_POINTS;
  61.     // 用两个三角形近似填充侧面
  62.     drawCylinderFace(i, j, j + NUM_POINTS, transformedTopVertices, color);
  63.     drawCylinderFace(i, j + NUM_POINTS, i + NUM_POINTS, transformedTopVertices, color);
  64. }
  65. void setup() {
  66.     Serial.begin(115200);
  67.     tft.begin();
  68.     tft.setRotation(2);
  69.     tft.fillScreen(tft.color565(0, 0, 0));
  70.     initCylinderVertices();
  71. }
  72. void loop() {
  73.     tft.fillScreen(tft.color565(0, 0, 0));
  74.     float transformedTopVertices[NUM_POINTS][2];
  75.     float transformedBottomVertices[NUM_POINTS][2];
  76.     for (int i = 0; i < NUM_POINTS; i++) {
  77.         float xTop = topVertices[i][0];
  78.         float yTop = topVertices[i][1];
  79.         float zTop = topVertices[i][2];
  80.         float xBottom = bottomVertices[i][0];
  81.         float yBottom = bottomVertices[i][1];
  82.         float zBottom = bottomVertices[i][2];
  83.         float tiltedYTop = yTop * cos(TILT_ANGLE) - zTop * sin(TILT_ANGLE);
  84.         float tiltedZTop = yTop * sin(TILT_ANGLE) + zTop * cos(TILT_ANGLE);
  85.         float tiltedYBottom = yBottom * cos(TILT_ANGLE) - zBottom * sin(TILT_ANGLE);
  86.         float tiltedZBottom = yBottom * sin(TILT_ANGLE) + zBottom * cos(TILT_ANGLE);
  87.         float rotatedXTop = xTop * cos(angle) - tiltedZTop * sin(angle);
  88.         float rotatedZTop = xTop * sin(angle) + tiltedZTop * cos(angle);
  89.         float rotatedXBottom = xBottom * cos(angle) - tiltedZBottom * sin(angle);
  90.         float rotatedZBottom = xBottom * sin(angle) + tiltedZBottom * cos(angle);
  91.         transformedTopVertices[i][0] = CENTER_X + rotatedXTop;
  92.         transformedTopVertices[i][1] = CENTER_Y + tiltedYTop;
  93.         transformedBottomVertices[i][0] = CENTER_X + rotatedXBottom;
  94.         transformedBottomVertices[i][1] = CENTER_Y + tiltedYBottom;
  95.     }
  96.     // 绘制圆柱体上下底面
  97.     for (int i = 0; i < NUM_POINTS; i++) {
  98.         int j = (i + 1) % NUM_POINTS;
  99.         drawCylinderFace(i, j, (j + 1) % NUM_POINTS, transformedTopVertices, fillColor);
  100.         drawCylinderFace(i, j, (j + 1) % NUM_POINTS, transformedBottomVertices, fillColor);
  101.     }
  102.     // 绘制圆柱体侧面
  103.     for (int i = 0; i < NUM_POINTS; i++) {
  104.         drawCylinderSide(i, transformedTopVertices, transformedBottomVertices, fillColor);
  105.     }
  106.     // 绘制圆柱体圆形端面边框
  107.     // 绘制上底面边框
  108.     for (int i = 0; i < NUM_POINTS; i++) {
  109.         int j = (i + 1) % NUM_POINTS;
  110.         drawCylinderEdge(i, j, transformedTopVertices, edgeColor);
  111.     }
  112.     // 绘制下底面边框
  113.     for (int i = 0; i < NUM_POINTS; i++) {
  114.         int j = (i + 1) % NUM_POINTS;
  115.         drawCylinderEdge(i, j, transformedBottomVertices, edgeColor);
  116.     }
  117.     angle += ROTATION_SPEED;
  118.     delay(300);
  119. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2025-4-11 18:02:54

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画

代码解读:
1. 常量和变量定义:定义了圆柱体的半径、高度、倾斜角度等常量,以及颜色变量和用于存储圆柱体顶点坐标的数组。
2. 初始化函数:initCylinderVertices 函数用于初始化圆柱体上下底面的顶点坐标,通过将圆近似为多个点来实现。
3. 绘制函数:drawCylinderFace 函数用于绘制圆柱体的面(上下底面),drawCylinderEdge 函数用于绘制圆柱体的边框(侧面和上下底面的边)。
4. setup 函数:初始化串口、屏幕,设置屏幕旋转方向,清屏,并调用 initCylinderVertices 函数初始化圆柱体顶点坐标。
5. loop 函数:每次循环清屏,对圆柱体顶点进行坐标变换(倾斜、旋转、投影),绘制圆柱体的上下底面和侧面边框,更新旋转角度并延迟一段时间以控制动画速度。
这样就实现了一个倾斜 45 度的绿色圆柱体动画。


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2025-4-11 18:04:05

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画

实验场景图  动态图

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图1

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图2
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2025-4-11 18:06:56

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图2

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2025-4-11 18:09:26

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画

【花雕学编程】Arduino动手做(249)--GC9A01绿色圆柱体动画图1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail