refactor: 完整C++面向对象重构(Lcd/GT1151/HardwareInit)

This commit is contained in:
2026-04-26 21:14:00 +08:00
parent 512a80aec8
commit ff91e324cf
7 changed files with 1191 additions and 1219 deletions
+134 -167
View File
@@ -10,12 +10,8 @@
#include <cstdio>
#include <cstring>
// SDRAM 中分配的测试缓冲区
__attribute__((section(".sdram"))) uint8_t sdram_big_buffer[1024 * 1024];
// =============================================================
// 板级引脚定义
// =============================================================
#define LED1_PORT GPIOE
#define LED1_PIN GPIO_PIN_3
#define LED2_PORT GPIOD
@@ -25,10 +21,6 @@ __attribute__((section(".sdram"))) uint8_t sdram_big_buffer[1024 * 1024];
#define LED4_PORT GPIOA
#define LED4_PIN GPIO_PIN_5
// =============================================================
// 早期启动函数(16MHz,无 SysTick
// =============================================================
static void delay_16m(uint32_t ms)
{
for (volatile uint32_t i = 0; i < ms * 4000U; i++);
@@ -114,7 +106,6 @@ static void uart_putdec(uint32_t val)
while (idx > 0) uart_putchar(buf[--idx]);
}
// 时钟切换到 168MHz 后重新配置 UART 波特率
static void reinit_usart0_168m(void)
{
usart_deinit(USART0);
@@ -124,10 +115,6 @@ static void reinit_usart0_168m(void)
usart_enable(USART0);
}
// =============================================================
// 外设测试函数
// =============================================================
static void test_sdram_driver(void)
{
printf("SDRAM 驱动测试开始...\r\n");
@@ -252,31 +239,33 @@ static void test_flash_driver(void)
static void test_lcd(void)
{
Lcd &lcd = Lcd::instance();
printf("\r\n===== LCD 功能测试 =====\r\n");
printf(" LCD_Init...\r\n");
LCD_Init();
printf(" LCD ID: %s\r\n", lcd_id);
lcd.init();
printf(" LCD ID: %s\r\n", lcd.idString());
delay_1ms(500);
uint16_t bands[4] = {RED, GREEN, BLUE, WHITE};
uint16_t bands[4] = {Lcd::RED, Lcd::GREEN, Lcd::BLUE, Lcd::WHITE};
for (int i = 0; i < 4; i++) {
int y0 = i * 200;
int y1 = y0 + 199;
BlockWrite(0, 479, y0, y1);
LCD_WriteRAM_Prepare();
lcd.blockWrite(0, 479, y0, y1);
lcd.writeRamPrepare();
for (int p = 0; p < 480 * 200; p++)
LCD_WriteRAM(bands[i]);
lcd.writeRam(bands[i]);
__DSB();
}
delay_1ms(3000);
printf(" LCD_Clear(BLACK)\r\n");
LCD_Clear(BLACK);
lcd.clear(Lcd::BLACK);
delay_1ms(500);
printf(" LCD_Clear(RED)\r\n");
LCD_Clear(RED);
lcd.clear(Lcd::RED);
delay_1ms(500);
printf("LCD 测试完成\r\n");
@@ -314,8 +303,10 @@ static void test_sdram_after_lcd(void)
static void concurrent_loop(void)
{
Lcd &lcd = Lcd::instance();
printf("\r\n===== 全部外设并发运行 =====\r\n");
LCD_Clear(GREEN);
lcd.clear(Lcd::GREEN);
delay_1ms(500);
uint32_t counter = 0;
@@ -342,157 +333,137 @@ static void concurrent_loop(void)
}
printf("\r\n===== 并发测试完成 =====\r\n");
LCD_Clear(BLUE);
lcd.clear(Lcd::BLUE);
delay_1ms(1000);
}
// =============================================================
// Touch 测试
// =============================================================
static void test_touch(void)
{
Lcd &lcd = Lcd::instance();
GT1151 &touch = GT1151::instance();
static void test_touch(void)
{
printf("\r\n===== Touch 测试 (多点触控) =====\r\n");
printf("\r\n===== Touch 测试 (多点触控) =====\r\n");
uint8_t ret = GT1151_Init();
if (ret != 0)
{
printf(" Touch 初始化失败 (ret=%d)\r\n", ret);
return;
}
printf(" Touch 初始化成功,等待触摸中断...\r\n");
RetCode ret = touch.init();
if (ret != RET_OK) {
printf(" Touch 初始化失败 (ret=%d)\r\n", ret);
return;
}
printf(" Touch 初始化成功,等待触摸中断...\r\n");
static const uint16_t TP_COLORS[5] = {
0xFFE0, // YELLOW - point 0
0x07FF, // CYAN - point 1
0xF81F, // MAGENTA - point 2
0x07E0, // GREEN - point 3
0xF800 // RED - point 4
};
static const uint16_t TP_COLORS[5] = {
0xFFE0, // YELLOW - point 0
0x07FF, // CYAN - point 1
0xF81F, // MAGENTA - point 2
0x07E0, // GREEN - point 3
0xF800 // RED - point 4
};
const uint16_t BG_COLOR = 0x0841;
const uint16_t BG_COLOR = 0x0841;
LCD_Clear(BG_COLOR);
POINT_COLOR = CYAN;
LCD_ShowString(10, 10, 460, 24, 16, 0, (uint8_t *)"=== Multi-Touch (5pt) ===");
POINT_COLOR = WHITE;
LCD_ShowString(10, 30, 460, 24, 16, 0, (uint8_t *)"Touch with up to 5 fingers");
lcd.clear(BG_COLOR);
lcd.setForeground(Lcd::CYAN);
lcd.showString(10, 10, 460, 24, 16, 0, (uint8_t *)"=== Multi-Touch (5pt) ===");
lcd.setForeground(Lcd::WHITE);
lcd.showString(10, 30, 460, 24, 16, 0, (uint8_t *)"Touch with up to 5 fingers");
for (uint8_t i = 0; i < CT_MAX_TOUCH; i++)
{
POINT_COLOR = TP_COLORS[i];
char lbl[8];
sprintf(lbl, "P%d: --", i);
LCD_ShowString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)lbl);
}
uint16_t last_x[5];
uint16_t last_y[5];
bool was_active[5];
for (uint8_t i = 0; i < CT_MAX_TOUCH; i++)
{
last_x[i] = 0xFFFF;
last_y[i] = 0xFFFF;
was_active[i] = false;
}
uint32_t idle_counter = 0;
g_touch_irq_flag = 0;
while (1)
{
if (g_touch_irq_flag)
{
g_touch_irq_flag = 0;
delay_1ms(5);
GT1151_Scan(0);
idle_counter = 0;
uint8_t active_cnt = TP_ACTIVE_COUNT(tp_dev.sta);
uint8_t mask = tp_dev.sta & 0x1F;
for (uint8_t i = 0; i < CT_MAX_TOUCH; i++)
{
bool active = (mask & (1 << i)) != 0;
if (active)
{
uint16_t x = tp_dev.x[i];
uint16_t y = tp_dev.y[i];
if (x < lcddev.width && y < lcddev.height)
{
POINT_COLOR = TP_COLORS[i];
char buf[20];
sprintf(buf, "P%d: X:%-3d Y:%-3d", i, x, y);
LCD_FillRectangle(10, 52 + i * 18, 200, 52 + i * 18 + 17);
LCD_ShowString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)buf);
if (was_active[i] && last_x[i] != 0xFFFF)
{
POINT_COLOR = TP_COLORS[i];
LCD_DrawLine(last_x[i], last_y[i], x, y);
}
POINT_COLOR = TP_COLORS[i];
LCD_FillCircle(x, y, 5);
last_x[i] = x;
last_y[i] = y;
was_active[i] = true;
}
}
else
{
if (was_active[i])
{
POINT_COLOR = BG_COLOR;
LCD_FillCircle(last_x[i], last_y[i], 6);
POINT_COLOR = TP_COLORS[i];
LCD_FillRectangle(10, 52 + i * 18, 200, 52 + i * 18 + 17);
LCD_ShowString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)"released");
last_x[i] = 0xFFFF;
last_y[i] = 0xFFFF;
was_active[i] = false;
}
}
}
printf("[TOUCH] cnt=%d sta=0x%02X", active_cnt, tp_dev.sta);
for (uint8_t i = 0; i < CT_MAX_TOUCH; i++)
{
if (mask & (1 << i))
printf(" P%d(%u,%u)", i, tp_dev.x[i], tp_dev.y[i]);
}
printf("\r\n");
}
else
{
idle_counter++;
delay_1ms(10);
if (idle_counter > 500)
{
printf(" 无触摸超过 5 秒,退出 Touch 测试\r\n");
break;
}
}
}
LCD_Clear(BLUE);
printf(" Touch 测试完成\r\n");
delay_1ms(500);
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) {
lcd.setForeground(TP_COLORS[i]);
char lbl[8];
sprintf(lbl, "P%d: --", i);
lcd.showString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)lbl);
}
// =============================================================
// 主函数
// =============================================================
uint16_t last_x[5];
uint16_t last_y[5];
bool was_active[5];
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) {
last_x[i] = 0xFFFF;
last_y[i] = 0xFFFF;
was_active[i] = false;
}
uint32_t idle_counter = 0;
GT1151::irq_flag = 0;
while (1) {
if (GT1151::irq_flag) {
GT1151::irq_flag = 0;
delay_1ms(5);
touch.scan(0);
idle_counter = 0;
uint8_t sta = touch.status();
uint8_t active_cnt = touch.activeCount();
uint8_t mask = sta & 0x1F;
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) {
bool active = (mask & (1 << i)) != 0;
if (active) {
uint16_t x = touch.x(i);
uint16_t y = touch.y(i);
if (x < lcd.width() && y < lcd.height()) {
lcd.setForeground(TP_COLORS[i]);
char buf[20];
sprintf(buf, "P%d: X:%-3d Y:%-3d", i, x, y);
lcd.fillRectangle(10, 52 + i * 18, 200, 52 + i * 18 + 17);
lcd.showString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)buf);
if (was_active[i] && last_x[i] != 0xFFFF) {
lcd.setForeground(TP_COLORS[i]);
lcd.drawLine(last_x[i], last_y[i], x, y);
}
lcd.setForeground(TP_COLORS[i]);
lcd.fillCircle(x, y, 5);
last_x[i] = x;
last_y[i] = y;
was_active[i] = true;
}
} else {
if (was_active[i]) {
lcd.setForeground(BG_COLOR);
lcd.fillCircle(last_x[i], last_y[i], 6);
lcd.setForeground(TP_COLORS[i]);
lcd.fillRectangle(10, 52 + i * 18, 200, 52 + i * 18 + 17);
lcd.showString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)"released");
last_x[i] = 0xFFFF;
last_y[i] = 0xFFFF;
was_active[i] = false;
}
}
}
printf("[TOUCH] cnt=%d sta=0x%02X", active_cnt, sta);
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) {
if (mask & (1 << i))
printf(" P%d(%u,%u)", i, touch.x(i), touch.y(i));
}
printf("\r\n");
} else {
idle_counter++;
delay_1ms(10);
if (idle_counter > 500) {
printf(" 无触摸超过 5 秒,退出 Touch 测试\r\n");
break;
}
}
}
lcd.clear(Lcd::BLUE);
printf(" Touch 测试完成\r\n");
delay_1ms(500);
}
int main(void)
{
// ---- 早期启动: 16MHz,原始寄存器操作 ----
init_leds();
led_pattern(0x01);
@@ -512,7 +483,6 @@ int main(void)
uart_putdec(SystemCoreClock);
uart_puts(" Hz\r\n");
// LED 闪烁验证
for (int i = 0; i < 3; i++) {
led_on(LED2_PORT, LED2_PIN);
delay_16m(200);
@@ -521,11 +491,10 @@ int main(void)
}
uart_puts("LED blink OK\r\n");
// ---- 硬件初始化: 切换到 168MHz ----
uart_puts("Initializing hardware...\r\n");
led_pattern(0x05);
hardware_init();
HardwareInit::init();
reinit_usart0_168m();
@@ -537,7 +506,6 @@ int main(void)
uart_putdec(SystemCoreClock);
uart_puts(" Hz\r\n");
// ---- C++ 驱动初始化 ----
led_pattern(0x07);
LedManager::instance().init_all();
LedManager::instance().led(1).on();
@@ -553,13 +521,11 @@ int main(void)
}
uart_puts("UartBus init OK\r\n");
// ---- SysTick 配置 ----
led_pattern(0x0B);
systick_config();
delay_1ms(500);
uart_puts("SysTick OK, delay_1ms available\r\n");
// ---- 外设测试 ----
led_pattern(0x0C);
printf("\r\n========================================\r\n");
printf(" LSPi 全外设测试\r\n");
@@ -583,7 +549,6 @@ int main(void)
test_touch();
delay_1ms(500);
// ---- 主循环 ----
led_pattern(0x0F);
printf("\r\n===== 所有外设测试完成 =====\r\n");
printf("系统运行于 168MHz, Flash/SDRAM/LCD/UART/LED 全部正常\r\n");
@@ -598,5 +563,7 @@ int main(void)
tick++;
if (tick % 4 == 0)
printf(".");
if (tick % 80 == 0)
printf(" [%lu s]\r\n", tick / 4);
}
}