372 lines
10 KiB
C++
372 lines
10 KiB
C++
#include "screen.h"
|
||
#include "driver/i2c.h"
|
||
#include "esp_log.h"
|
||
#include "tca9555.h"
|
||
#include <cstring>
|
||
static const char *TAG = "LCD";
|
||
__attribute__((aligned(4))) uint8_t LcdScreen::full_buf[LCD_WIDTH * LCD_HEIGHT * 2];
|
||
|
||
// ===================== ST7735 屏幕驱动实现 =====================
|
||
esp_err_t LcdScreen::power_up(TCA9555 &tca)
|
||
{
|
||
uint8_t rd_val;
|
||
|
||
// 初始化RST/DC为输出
|
||
gpio_config_t ctrl_conf = {
|
||
.pin_bit_mask = (1ULL << LCD_DC_GPIO),
|
||
.mode = GPIO_MODE_OUTPUT,
|
||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||
.intr_type = GPIO_INTR_DISABLE,
|
||
};
|
||
gpio_config(&ctrl_conf);
|
||
gpio_set_level(LCD_DC_GPIO, 0);
|
||
|
||
ESP_LOGI(TAG, "=== LCD Power Up Start ===");
|
||
|
||
// ------ LCD 硬件复位 (P0.1 = SC_RESET) ------
|
||
// 时序:高 → 低(10ms) → 高(120ms等待IC启动)
|
||
ESP_LOGI(TAG, "LCD HW Reset via TCA P0.%u", TCA_P0_SC_RESET);
|
||
tca.p0_set_bit(TCA_P0_SC_RESET); // 先保证RESX=高
|
||
vTaskDelay(pdMS_TO_TICKS(5));
|
||
tca.p0_clr_bit(TCA_P0_SC_RESET); // RESX=低,复位开始
|
||
vTaskDelay(pdMS_TO_TICKS(20));
|
||
tca.p0_set_bit(TCA_P0_SC_RESET); // RESX=高,复位释放
|
||
vTaskDelay(pdMS_TO_TICKS(150)); // 等待ST7735内部初始化完成
|
||
tca.read_p1_input(&rd_val);
|
||
ESP_LOGI(TAG, "VDD reg=0x%02X", rd_val);
|
||
ESP_LOGI(TAG, "=== LCD Power Up Complete ===");
|
||
return ESP_OK;
|
||
}
|
||
|
||
esp_err_t LcdScreen::spi_init(void)
|
||
{
|
||
gpio_config_t gpio_conf = {
|
||
.pin_bit_mask = (1ULL << LCD_DC_GPIO) | (1ULL << LCD_CS_GPIO),
|
||
//.pin_bit_mask = (1ULL << LCD_DC_GPIO),
|
||
.mode = GPIO_MODE_OUTPUT,
|
||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||
.intr_type = GPIO_INTR_DISABLE,
|
||
};
|
||
gpio_config(&gpio_conf);
|
||
gpio_set_level(LCD_DC_GPIO, 0);
|
||
gpio_set_level(LCD_CS_GPIO, 1);
|
||
|
||
// spi_bus_config_t bus_cfg = {
|
||
// .mosi_io_num = LCD_MOSI_GPIO,
|
||
// .miso_io_num = -1,
|
||
// .sclk_io_num = LCD_SCK_GPIO,
|
||
// .quadwp_io_num = -1,
|
||
// .quadhd_io_num = -1,
|
||
// .data4_io_num = -1,
|
||
// .data5_io_num = -1,
|
||
// .data6_io_num = -1,
|
||
// .data7_io_num = -1,
|
||
// .data_io_default_level = 0,
|
||
// .max_transfer_sz = LCD_WIDTH * LCD_HEIGHT * 2,
|
||
// .flags = 0,
|
||
// .isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||
// .intr_flags = 0,
|
||
// };
|
||
// esp_err_t ret = spi_bus_initialize(LCD_SPI_HOST, &bus_cfg, SPI_DMA_CH_AUTO);
|
||
// if (ret != ESP_OK)
|
||
// return ret;
|
||
|
||
spi_device_interface_config_t dev_cfg = {};
|
||
dev_cfg.mode = 0;
|
||
dev_cfg.clock_speed_hz = LCD_SPI_CLK;
|
||
dev_cfg.spics_io_num = -1;
|
||
dev_cfg.cs_ena_pretrans = 1; // 1个SCK周期CS前置建立时间
|
||
dev_cfg.cs_ena_posttrans = 1; // 1个SCK周期CS后置保持时间
|
||
dev_cfg.flags = SPI_DEVICE_NO_DUMMY;
|
||
dev_cfg.queue_size = 16;
|
||
|
||
esp_err_t ret = spi_bus_add_device(SPI2_HOST, &dev_cfg, &_spi_handle);
|
||
return ret;
|
||
}
|
||
|
||
// 发送指令 DC=0
|
||
void LcdScreen::send_cmd(uint8_t cmd)
|
||
{
|
||
if (!_spi_handle)
|
||
return;
|
||
gpio_set_level(LCD_DC_GPIO, 0);
|
||
gpio_set_level(LCD_CS_GPIO, 0);
|
||
spi_transaction_t trans = {};
|
||
trans.length = 8;
|
||
trans.tx_buffer = &cmd;
|
||
spi_device_transmit(_spi_handle, &trans);
|
||
gpio_set_level(LCD_CS_GPIO, 1);
|
||
}
|
||
|
||
// 发送数据 DC=1
|
||
void LcdScreen::send_data(uint8_t dat)
|
||
{
|
||
if (!_spi_handle)
|
||
return;
|
||
gpio_set_level(LCD_DC_GPIO, 1);
|
||
gpio_set_level(LCD_CS_GPIO, 0);
|
||
spi_transaction_t trans = {};
|
||
trans.length = 8;
|
||
trans.tx_buffer = &dat;
|
||
spi_device_transmit(_spi_handle, &trans);
|
||
gpio_set_level(LCD_CS_GPIO, 1);
|
||
}
|
||
|
||
// ST7735标准初始化序列
|
||
void LcdScreen::ic_init_sequence(void)
|
||
{
|
||
ESP_LOGI(TAG, "ST7735 init start");
|
||
|
||
send_cmd(CMD_SLPOUT);
|
||
vTaskDelay(pdMS_TO_TICKS(250));
|
||
send_cmd(CMD_INVON); // 0x21,打开反转
|
||
vTaskDelay(pdMS_TO_TICKS(10));
|
||
// 帧速率
|
||
send_cmd(0xB1);
|
||
send_data(0x02);
|
||
send_data(0x35);
|
||
send_data(0x36);
|
||
send_cmd(0xB2);
|
||
send_data(0x02);
|
||
send_data(0x35);
|
||
send_data(0x36);
|
||
send_cmd(0xB3);
|
||
send_data(0x02);
|
||
send_data(0x35);
|
||
send_data(0x36);
|
||
send_data(0x02);
|
||
send_data(0x35);
|
||
send_data(0x36);
|
||
send_cmd(0xB4);
|
||
send_data(0x00);
|
||
|
||
// 电源控制
|
||
send_cmd(0xC0);
|
||
send_data(0xA2);
|
||
send_data(0x02);
|
||
send_data(0x84);
|
||
send_cmd(0xC1);
|
||
send_data(0xC5);
|
||
send_cmd(0xC2);
|
||
send_data(0x0D);
|
||
send_data(0x00);
|
||
send_cmd(0xC3);
|
||
send_data(0x8A);
|
||
send_data(0x2A);
|
||
send_cmd(0xC4);
|
||
send_data(0x8D);
|
||
send_data(0xEE);
|
||
send_cmd(0xC5);
|
||
send_data(0x02);
|
||
|
||
// 正伽马
|
||
send_cmd(0xE0);
|
||
send_data(0x12);
|
||
send_data(0x1C);
|
||
send_data(0x10);
|
||
send_data(0x18);
|
||
send_data(0x33);
|
||
send_data(0x2C);
|
||
send_data(0x25);
|
||
send_data(0x28);
|
||
send_data(0x28);
|
||
send_data(0x27);
|
||
send_data(0x2F);
|
||
send_data(0x3C);
|
||
send_data(0x00);
|
||
send_data(0x03);
|
||
send_data(0x03);
|
||
send_data(0x10);
|
||
// 负伽马
|
||
send_cmd(0xE1);
|
||
send_data(0x12);
|
||
send_data(0x1C);
|
||
send_data(0x10);
|
||
send_data(0x18);
|
||
send_data(0x2D);
|
||
send_data(0x28);
|
||
send_data(0x23);
|
||
send_data(0x28);
|
||
send_data(0x28);
|
||
send_data(0x26);
|
||
send_data(0x2F);
|
||
send_data(0x3B);
|
||
send_data(0x00);
|
||
send_data(0x03);
|
||
send_data(0x03);
|
||
send_data(0x10);
|
||
|
||
send_cmd(CMD_COLMOD);
|
||
send_data(0x05); //
|
||
send_cmd(CMD_MADCTL);
|
||
send_data(0xc8); // c8 Bit3 = 1 → BGR
|
||
|
||
// ---------- 在开显前先把 GRAM 全部刷黑(DISPON 之前清屏) ----------
|
||
// // 覆盖带偏移的完整有效区域
|
||
send_cmd(CMD_CASET);
|
||
send_data(0x00);
|
||
send_data(LCD_COL_OFFSET);
|
||
send_data(0x00);
|
||
send_data(128); // send_data(LCD_WIDTH - 1 + LCD_COL_OFFSET);
|
||
send_cmd(CMD_RASET);
|
||
send_data(0x00);
|
||
send_data(LCD_ROW_OFFSET);
|
||
send_data(0x00);
|
||
send_data(130); // send_data(LCD_HEIGHT - 1 + LCD_ROW_OFFSET);
|
||
send_cmd(CMD_RAMWR);
|
||
|
||
memset(full_buf, 0x00, sizeof(full_buf));
|
||
gpio_set_level(LCD_DC_GPIO, 1);
|
||
gpio_set_level(LCD_CS_GPIO, 0);
|
||
uint32_t total = sizeof(full_buf);
|
||
uint32_t offset = 0;
|
||
while (offset < total)
|
||
{
|
||
uint32_t chunk = (total - offset > 4092) ? 4092 : (total - offset);
|
||
spi_transaction_t trans = {};
|
||
trans.length = chunk * 8;
|
||
trans.tx_buffer = full_buf + offset;
|
||
spi_device_transmit(_spi_handle, &trans);
|
||
offset += chunk;
|
||
}
|
||
gpio_set_level(LCD_CS_GPIO, 1);
|
||
vTaskDelay(pdMS_TO_TICKS(50));
|
||
// ---------- 开显 ----------
|
||
send_cmd(CMD_DISPON);
|
||
vTaskDelay(pdMS_TO_TICKS(120));
|
||
fill_color(0x0000);
|
||
ESP_LOGI(TAG, "ST7735 init done");
|
||
}
|
||
|
||
// 全屏纯色填充:整块DMA一次性传输,无循环延时
|
||
void LcdScreen::fill_color(uint16_t color)
|
||
{
|
||
ESP_LOGI(TAG, "Filling color=0x%04X", color);
|
||
send_cmd(CMD_CASET);
|
||
send_data(0x00);
|
||
send_data(LCD_COL_OFFSET);
|
||
send_data(0x00);
|
||
send_data(LCD_WIDTH - 1 + LCD_COL_OFFSET);
|
||
send_cmd(CMD_RASET);
|
||
send_data(0x00);
|
||
send_data(LCD_ROW_OFFSET);
|
||
send_data(0x00);
|
||
send_data(LCD_HEIGHT - 1 + LCD_ROW_OFFSET);
|
||
send_cmd(CMD_RAMWR);
|
||
|
||
uint8_t h = (color >> 8) & 0xFF;
|
||
uint8_t l = color & 0xFF;
|
||
for (uint32_t i = 0; i < LCD_WIDTH * LCD_HEIGHT; i++)
|
||
{
|
||
full_buf[i * 2] = h;
|
||
full_buf[i * 2 + 1] = l;
|
||
}
|
||
|
||
// 逐行发送
|
||
gpio_set_level(LCD_DC_GPIO, 1);
|
||
gpio_set_level(LCD_CS_GPIO, 0);
|
||
uint32_t total = sizeof(full_buf);
|
||
uint32_t offset = 0;
|
||
while (offset < total)
|
||
{
|
||
uint32_t chunk = (total - offset > 4092) ? 4092 : (total - offset);
|
||
spi_transaction_t trans = {};
|
||
trans.length = chunk * 8;
|
||
trans.tx_buffer = full_buf + offset;
|
||
esp_err_t ret = spi_device_transmit(_spi_handle, &trans);
|
||
if (ret != ESP_OK)
|
||
{
|
||
ESP_LOGE(TAG, "SPI transmit failed at offset %lu: %s", offset, esp_err_to_name(ret));
|
||
gpio_set_level(LCD_CS_GPIO, 1);
|
||
return;
|
||
}
|
||
offset += chunk;
|
||
}
|
||
|
||
gpio_set_level(LCD_CS_GPIO, 1); // 传输完成后拉高CS
|
||
ESP_LOGI(TAG, "Color fill done");
|
||
}
|
||
|
||
|
||
void LcdScreen::draw_single_eye(int cx, int cy, int eye_radius, int pupil_ofs_x, int pupil_r)
|
||
{
|
||
// eye_radius:眼球【半径】,画正圆,不再使用宽、高两个参数
|
||
// 闭眼状态:画一条白色横线
|
||
if (eye_radius <= 0)
|
||
{
|
||
for (int x = cx - 40; x <= cx + 40; x++)
|
||
{
|
||
DrawPixel(x, cy, COLOR_WHITE);
|
||
}
|
||
return;
|
||
}
|
||
|
||
// ========== 第一步:绘制实心白色圆形眼球 ==========
|
||
for (int dy = -eye_radius; dy <= eye_radius; dy++)
|
||
{
|
||
for (int dx = -eye_radius; dx <= eye_radius; dx++)
|
||
{
|
||
// 圆方程 dx²+dy² ≤ r²
|
||
if ((dx*dx + dy*dy) <= (eye_radius * eye_radius))
|
||
{
|
||
DrawPixel(cx + dx, cy + dy, COLOR_WHITE);
|
||
}
|
||
}
|
||
}
|
||
|
||
// ========== 第二步:绘制黑色瞳孔 ==========
|
||
int pup_cx = cx + pupil_ofs_x;
|
||
int pup_cy = cy;
|
||
for (int dy = -pupil_r; dy <= pupil_r; dy++)
|
||
{
|
||
for (int dx = -pupil_r; dx <= pupil_r; dx++)
|
||
{
|
||
if ((dx*dx + dy*dy) <= (pupil_r * pupil_r))
|
||
{
|
||
DrawPixel(pup_cx + dx, pup_cy + dy, COLOR_BLACK);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void LcdScreen::Flush()
|
||
{
|
||
send_cmd(CMD_CASET);
|
||
send_data(0);send_data(LCD_COL_OFFSET);
|
||
send_data(0);send_data(LCD_WIDTH - 1 + LCD_COL_OFFSET);
|
||
|
||
send_cmd(CMD_RASET);
|
||
send_data(0);send_data(LCD_ROW_OFFSET);
|
||
send_data(0);send_data(LCD_HEIGHT - 1 + LCD_ROW_OFFSET);
|
||
|
||
send_cmd(CMD_RAMWR);
|
||
|
||
gpio_set_level(LCD_DC_GPIO, 1);
|
||
gpio_set_level(LCD_CS_GPIO, 0);
|
||
|
||
uint32_t total_bytes = sizeof(full_buf);
|
||
uint32_t offset = 0;
|
||
|
||
while(offset < total_bytes)
|
||
{
|
||
//单次最大4092字节
|
||
uint32_t chunk = (total_bytes - offset > 4092) ? 4092 : (total_bytes - offset);
|
||
|
||
spi_transaction_t trans{};
|
||
trans.length = chunk * 8;
|
||
trans.tx_buffer = full_buf + offset;
|
||
|
||
esp_err_t ret = spi_device_transmit(_spi_handle, &trans);
|
||
if(ret != ESP_OK)
|
||
{
|
||
ESP_LOGE(TAG,"Flush SPI fail offset:%lu, err:%s",offset,esp_err_to_name(ret));
|
||
break;
|
||
}
|
||
offset += chunk;
|
||
}
|
||
|
||
gpio_set_level(LCD_CS_GPIO, 1);
|
||
}
|