[G32R] 【极海G32R430 TinyBoard开发板测评】+4.ATAN2库测试之生成三角形显示

[复制链接]
16|1
stb988 发表于 2026-4-28 22:31 | 显示全部楼层 |阅读模式
本帖最后由 stb988 于 2026-4-28 22:32 编辑

#申请原创# #每日话题#
            前面已经把显示屏连接到了开发板,这里就来测试一下ATAN2库硬件计算三角函数,屏幕我已经改成了硬件SPI加DMA驱动,刷新速度也上来了。
要测试ATAN2,就得把它引入库文件,库文件在\Libraries\ATAN2目录下,
屏幕截图 2026-04-28 222124.png

添加画图函数
  1. // 假设 PI 的归一化定义
  2. #define PI_FIXED  2147483648.0 // Q31

  3. /**
  4. * @brief  性能展示:旋转的三角形
  5. * @param  centerX, centerY: 旋转中心
  6. * @param  radius: 三角形外接圆半径
  7. * @param  angle_step: 每一帧旋转的角度增量 (0-360)
  8. */
  9. void Demo_Hardware_Rotation(u16 centerX, u16 centerY, u8 radius, float angle_step)
  10. {
  11.     static float current_angle = 0;
  12.     u16 x[3], y[3];
  13.     u16 old_x[3], old_y[3];
  14.     static u8 first_run = 1;

  15.     // 1. 清除上一帧 (用黑色画一遍旧的)
  16.     if (!first_run) {
  17.         LCD_DrawLine(old_x[0], old_y[0], old_x[1], old_y[1], BLACK);
  18.         LCD_DrawLine(old_x[1], old_y[1], old_x[2], old_y[2], BLACK);
  19.         LCD_DrawLine(old_x[2], old_y[2], old_x[0], old_y[0], BLACK);
  20.     }

  21.     // 2. 计算新坐标 (这里可以使用硬件加速思想)
  22.     // 实际上旋转坐标公式: x' = r*cos(a), y' = r*sin(a)
  23.     // 我们可以通过步进角度,演示硬件在处理大量三角运算时的稳定
  24.     for (int i = 0; i < 3; i++) {
  25.         float theta = current_angle + (i * 120.0f); // 三个角相隔120度
  26.         float rad = theta * 3.1415926f / 180.0f;
  27.         
  28.         // 这里的 sin/cos 如果有硬件加速,速度会极快
  29.         x[i] = centerX + (u16)(radius * cos(rad));
  30.         y[i] = centerY + (u16)(radius * sin(rad));
  31.         
  32.         old_x[i] = x[i];
  33.         old_y[i] = y[i];
  34.     }

  35.     // 3. 画出新三角形
  36.     LCD_DrawLine(x[0], y[0], x[1], y[1], CYAN);
  37.     LCD_DrawLine(x[1], y[1], x[2], y[2], MAGENTA);
  38.     LCD_DrawLine(x[2], y[2], x[0], y[0], YELLOW);

  39.     current_angle += angle_step;
  40.     if (current_angle >= 360) current_angle = 0;
  41.     first_run = 0;
  42. }
最后展示效果
VID_20260428_221441.mp4_20260428_222553.gif
再来一个托影的看看
  1. void Demo_Hardware_Rotation_Cool(u16 centerX, u16 centerY, u8 radius, float angle_step)
  2. {
  3. #define GHOST_COUNT 6  // 拖尾长度(包含当前帧共6帧)
  4.    
  5.     static float angles[GHOST_COUNT];      // 存储过去几帧的角度
  6.     static u16 ghost_x[GHOST_COUNT][3];    // 存储过去几帧的X坐标
  7.     static u16 ghost_y[GHOST_COUNT][3];    // 存储过去几帧的Y坐标
  8.     static u8 frame_count = 0;             // 运行帧数累计
  9.     static float current_angle = 0;

  10.     // 预设拖尾颜色:从深灰色到亮彩色
  11.     // 越旧的帧颜色越暗,越新的帧颜色越亮
  12.     u16 ghost_colors[GHOST_COUNT] = {
  13.         0x2104, // 极深灰 (最旧的一帧)
  14.         0x4208, // 深灰
  15.         0x632C, // 中灰
  16.         0x8410, // 灰蓝
  17.         0xAD55, // 浅蓝
  18.         WHITE   // 纯白 (当前帧)
  19.     };

  20.     // 1. 【擦除】只擦除最旧的那一帧(即数组第0位)
  21.     if (frame_count >= GHOST_COUNT) {
  22.         LCD_DrawLine(ghost_x[0][0], ghost_y[0][0], ghost_x[0][1], ghost_y[0][1], BLACK);
  23.         LCD_DrawLine(ghost_x[0][1], ghost_y[0][1], ghost_x[0][2], ghost_y[0][2], BLACK);
  24.         LCD_DrawLine(ghost_x[0][2], ghost_y[0][2], ghost_x[0][0], ghost_y[0][0], BLACK);
  25.     }

  26.     // 2. 【位移缓冲区】将旧坐标向前覆盖
  27.     for (int i = 0; i < GHOST_COUNT - 1; i++) {
  28.         for (int j = 0; j < 3; j++) {
  29.             ghost_x[i][j] = ghost_x[i+1][j];
  30.             ghost_y[i][j] = ghost_y[i+1][j];
  31.         }
  32.     }

  33.     // 3. 【计算新坐标】存入缓冲区最后一位(当前帧)
  34.     for (int i = 0; i < 3; i++) {
  35.         float rad = (current_angle + (i * 120.0f)) * 3.1415926f / 180.0f;
  36.         ghost_x[GHOST_COUNT-1][i] = (u16)(centerX + radius * cosf(rad));
  37.         ghost_y[GHOST_COUNT-1][i] = (u16)(centerY + radius * sinf(rad));
  38.     }

  39.     // 4. 【渲染】按照由暗到亮的顺序重新画出所有残影
  40.     // 注意:只画那些已经有数据的帧
  41.     u8 start_idx = (frame_count < GHOST_COUNT) ? (GHOST_COUNT - frame_count) : 0;
  42.    
  43.     for (int i = start_idx; i < GHOST_COUNT; i++) {
  44.         u16 color = (i == GHOST_COUNT - 1) ? CYAN : ghost_colors[i]; // 当前帧用青色,残影用灰色
  45.         
  46.         LCD_DrawLine(ghost_x[i][0], ghost_y[i][0], ghost_x[i][1], ghost_y[i][1], color);
  47.         LCD_DrawLine(ghost_x[i][1], ghost_y[i][1], ghost_x[i][2], ghost_y[i][2], color);
  48.         LCD_DrawLine(ghost_x[i][2], ghost_y[i][2], ghost_x[i][0], ghost_y[i][0], color);
  49.     }

  50.     // 5. 变量递增
  51.     current_angle += angle_step;
  52.     if (current_angle >= 360.0f) current_angle -= 360.0f;
  53.     if (frame_count < GHOST_COUNT) frame_count++;
  54. }
VID_20260428_221040.mp4_20260428_222854.gif
真的问题不大 发表于 2026-4-29 14:34 | 显示全部楼层
还怪酷炫的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

67

主题

476

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部
0