驱动基本可用,方块碰撞动画

This commit is contained in:
kirltrz 2026-02-04 20:49:49 +08:00
parent 8f53b4d870
commit 482017481f
2 changed files with 90 additions and 10 deletions

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_err.h"
@ -27,11 +28,38 @@
// 绘制测试图案
// 画两条边缘线和一个中心点
bool is_rotated = 0; // 跟踪旋转状态
uint8_t *buffer = NULL;
void lcd_init(void){
buffer = heap_caps_malloc(ST7305_RESOLUTION_HOR * ST7305_RESOLUTION_VER / 8 +100, MALLOC_CAP_DMA);
assert(buffer);
memset(buffer,0x00,5100);
}
void lcd_draw_bit(int x, int y, bool enabled){
if(x>=200||y>=200) return;
//x += 4;
x = 199 - x;
uint8_t real_x = x / 4;
uint8_t real_y = y / 2;
uint16_t byte_index = real_y * 51 + real_x;
uint8_t y_group_index = y % 2 != 0;
uint8_t x_group_index = x % 4;
uint8_t bit_index = 7 - (x_group_index *2 + y_group_index);
if(enabled)
buffer[byte_index] |= (1 << bit_index);
else
buffer[byte_index] &= ~(1 << bit_index);
}
void lcd_display(esp_lcd_panel_handle_t panel_handle){
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, 0, 0, ST7305_RESOLUTION_HOR, ST7305_RESOLUTION_VER, buffer));
}
// 绘制测试图案函数
void draw_test_pattern(uint8_t *buffer, bool rotated)
{
memset(buffer, 0, ST7305_RESOLUTION_HOR * ST7305_RESOLUTION_VER / 8);
memset(buffer, 0, ST7305_RESOLUTION_HOR * ST7305_RESOLUTION_VER / 8 +100);
int width = rotated ? ST7305_RESOLUTION_VER : ST7305_RESOLUTION_HOR;
int height = rotated ? ST7305_RESOLUTION_HOR : ST7305_RESOLUTION_VER;
@ -76,6 +104,43 @@ void draw_test_pattern(uint8_t *buffer, bool rotated)
}
}
}
// 无返回值参数为LCD屏句柄XY移动+边缘反弹,极简测试写法
void lcd_anim_test(esp_lcd_panel_handle_t panel_handle)
{
lcd_init(); // LCD初始化与原代码一致
int block_x = 50; // 方块左上角X坐标初始居中
int block_y = 95; // 方块左上角Y坐标初始居中
int dir_x = 1; // X方向1=右移,-1=左移
int dir_y = 1; // Y方向1=下移,-1=上移
// 无限循环实现反弹动画
while(1)
{
/******** 1. 清屏200*200全屏置0避免拖尾 ********/
for(int i=0; i<200; i++)
for(int j=0; j<200; j++)
lcd_draw_bit(j, i, 0);
/******** 2. 绘制10*10方块XY均为变量随方向移动 ********/
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
lcd_draw_bit(block_x+j, block_y+i, 1);
/******** 3. 刷新显示+50ms延时控制动画速度 ********/
lcd_display(panel_handle);
vTaskDelay(pdMS_TO_TICKS(10));
/******** 4. 更新坐标按方向移动步长1像素 ********/
block_x += dir_x;
block_y += dir_y;
/******** 5. 边界判断+反弹:碰到四周边缘反转对应方向 ********/
// X轴反弹左边界x<=0或右边界x+方块宽>200反转X方向
if(block_x <= 0 || block_x + 10 > 200) dir_x = -dir_x;
// Y轴反弹上边界y<=0或下边界y+方块高>200反转Y方向
if(block_y <= 0 || block_y + 10 > 200) dir_y = -dir_y;
}
}
void app_main(void)
{
@ -95,7 +160,7 @@ void app_main(void)
esp_lcd_panel_io_spi_config_t io_config = {
.dc_gpio_num = LCD_PIN_DC,
.cs_gpio_num = LCD_PIN_CS,
.pclk_hz = 20 * 1000 * 1000,
.pclk_hz = 40 * 1000 * 1000,
.lcd_cmd_bits = 8,
.lcd_param_bits = 8,
.spi_mode = 0,
@ -118,17 +183,30 @@ void app_main(void)
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, is_rotated));
// ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, false, true));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, false, false));
ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, false));
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
printf("Display simple pattern");
lcd_anim_test(panel_handle);
/*
lcd_init();
for(int i=0; i<200; i++){
for(int j=0; j<200; j++){
lcd_draw_bit(j,i,1);
}
vTaskDelay(pdMS_TO_TICKS(1000));
lcd_display(panel_handle);
}
*/
/*
// 准备测试数据
uint8_t *test_pattern = heap_caps_malloc(ST7305_RESOLUTION_HOR * ST7305_RESOLUTION_VER / 8, MALLOC_CAP_DMA);
uint8_t *test_pattern = heap_caps_malloc(ST7305_RESOLUTION_HOR * ST7305_RESOLUTION_VER / 8 +100, MALLOC_CAP_DMA);
assert(test_pattern);
memset(test_pattern, 0x00, ST7305_RESOLUTION_HOR * ST7305_RESOLUTION_VER / 8);
memset(test_pattern, 0x00, ST7305_RESOLUTION_HOR * ST7305_RESOLUTION_VER / 8 +100);
// 初始填充并显示:累积字节扫描,完成一次后翻转配色并继续扫描
size_t buf_size = ST7305_RESOLUTION_HOR * ST7305_RESOLUTION_VER / 8;
size_t buf_size = ST7305_RESOLUTION_HOR * ST7305_RESOLUTION_VER / 8 +100;
size_t idx = 0;
bool scanned_is_ff = true; // true已扫描字节为 0xFFfalse已扫描字节为 0x00
while (1)
@ -150,10 +228,10 @@ void app_main(void)
scanned_is_ff = !scanned_is_ff;
vTaskDelay(pdMS_TO_TICKS(200)); // 稍作停顿以便观察配色翻转
} else {
vTaskDelay(pdMS_TO_TICKS(2)); // 平常的扫描速度
vTaskDelay(pdMS_TO_TICKS(50)); // 平常的扫描速度
}
}
// 释放资源
free(test_pattern);
free(test_pattern);*/
}

View File

@ -207,7 +207,7 @@ static esp_err_t panel_st7305_draw_bitmap(esp_lcd_panel_t *panel, int x_start, i
// 创建主缓冲区和临时缓冲区(大小基于面板分辨率)
uint16_t pages = (st7305->height) / 8;
size_t lcd_buf_size = (size_t)st7305->width * pages;
size_t lcd_buf_size = (size_t)st7305->width * pages +100;
uint8_t *lcd_buffer = heap_caps_malloc(lcd_buf_size, MALLOC_CAP_DMA);
uint8_t *temp_buffer = heap_caps_malloc(lcd_buf_size, MALLOC_CAP_DMA);
if (!lcd_buffer || !temp_buffer) {
@ -278,6 +278,7 @@ static esp_err_t panel_st7305_draw_bitmap(esp_lcd_panel_t *panel, int x_start, i
}
}
/*
// 数据格式转换
uint16_t k = 0;
uint16_t width = is_xy_swapped ? st7305->height : st7305->width;
@ -310,13 +311,14 @@ static esp_err_t panel_st7305_draw_bitmap(esp_lcd_panel_t *panel, int x_start, i
}
}
}
*/
// 设置显示范围和发送数据
uint8_t caset[] = {0x16, 0x26};
uint8_t raset[] = {0x00, 0x63};
esp_lcd_panel_io_tx_param(io, ST7305_CMD_CASET, caset, sizeof(caset));
esp_lcd_panel_io_tx_param(io, ST7305_CMD_RASET, raset, sizeof(raset));
esp_lcd_panel_io_tx_color(io, ST7305_CMD_RAMWR, temp_buffer, lcd_buf_size);
esp_lcd_panel_io_tx_color(io, ST7305_CMD_RAMWR, lcd_buffer, lcd_buf_size);
free(lcd_buffer);
free(temp_buffer);