730 lines
28 KiB
C++
730 lines
28 KiB
C++
#include <Arduino.h>
|
||
#include <string.h>
|
||
#include <stdlib.h>
|
||
#include <esp_heap_caps.h>
|
||
#include "driver/spi_master.h"
|
||
#include "esp_lcd_panel_io.h"
|
||
#include "esp_lcd_panel_ops.h"
|
||
#include "esp_lcd_panel_st7789.h"
|
||
|
||
#include "board_config.h"
|
||
#if BOARD_HAS_QSPI
|
||
#include <Wire.h> /* 屏电源/复位挂在 TCA9554 上 */
|
||
#endif
|
||
#include "display.h"
|
||
#include "expander.h"
|
||
#include "font_asc2.h"
|
||
#include "font_cjk.h"
|
||
|
||
/* 像素时钟见 board_config.h(旧板 20MHz,QSPI 板 40MHz);每次 flush 的行带高度 */
|
||
#define FLUSH_ROWS 20
|
||
|
||
static esp_lcd_panel_io_handle_t s_io;
|
||
static esp_lcd_panel_handle_t s_panel;
|
||
static uint16_t *s_fb;
|
||
|
||
static bool s_byteswap = true; /* SPI 先发高字节,帧缓冲按大端存放 */
|
||
static bool s_bgr = false; /* ST7789 红蓝序,点屏后用 swap/bgr 命令校正 */
|
||
static bool s_mx = LCD_MIRROR_X, s_my = LCD_MIRROR_Y; /* 板型决定,串口 mirror 命令仍可临时改 */
|
||
/* 长条屏逻辑朝向 0..3,disp_flush 转置时用。QSPI 那块屏的面板基准看出厂自检页:
|
||
USB-C 朝下竖着拿时文字正立 => row0 在远离 USB 那头,col0 在那时的左边。
|
||
竖屏转横屏必须连带镜像一次逻辑 y(bit1),少这一次就是上下颠倒;bit1=1 的
|
||
rot2/rot3 才是正立/转180°,rot0/rot1 是镜像,永远不该用。默认 = USB 在右摆正。 */
|
||
#if BOARD_HAS_QSPI
|
||
static uint8_t s_rot = 2;
|
||
#else
|
||
static uint8_t s_rot = 0;
|
||
#endif
|
||
|
||
static inline uint16_t pack(uint16_t c)
|
||
{
|
||
if (s_bgr) c = (uint16_t)(((c & 0xF800) >> 11) | (c & 0x07E0) | ((c & 0x001F) << 11));
|
||
if (s_byteswap) c = (uint16_t)((c >> 8) | (c << 8));
|
||
return c;
|
||
}
|
||
|
||
#if !BOARD_HAS_XL9555
|
||
/* GPIO 直连的屏:复位时序自己来(星智 CUBE 的 RST=GPIO18) */
|
||
static void lcd_gpio_reset()
|
||
{
|
||
if (PIN_LCD_RST < 0) return;
|
||
pinMode(PIN_LCD_RST, OUTPUT);
|
||
digitalWrite(PIN_LCD_RST, HIGH); delay(20);
|
||
digitalWrite(PIN_LCD_RST, LOW); delay(40);
|
||
digitalWrite(PIN_LCD_RST, HIGH); delay(120);
|
||
}
|
||
#endif
|
||
|
||
#if BOARD_HAS_QSPI
|
||
/* AXS15231B 走 QSPI 没有 DC 线:命令和像素各带一个操作码前缀塞进 32bit 命令字,
|
||
和 espressif/esp_lcd_axs15231b 组件里 tx_param()/tx_color() 的拼法一致。 */
|
||
#define AXS_OP_CMD 0x02u
|
||
#define AXS_OP_COLOR 0x32u
|
||
#define AXS_SLPOUT 0x11
|
||
#define AXS_MADCTL 0x36
|
||
#define AXS_COLMOD 0x3A
|
||
#define AXS_CASET 0x2A
|
||
#define AXS_RAMWR 0x2C
|
||
#define AXS_RAMWRC 0x3C
|
||
#define AXS_DISPON 0x29
|
||
|
||
static void axs_cmd(uint8_t cmd, const uint8_t *param, size_t n)
|
||
{
|
||
esp_lcd_panel_io_tx_param(s_io, (int)((AXS_OP_CMD << 24) | (cmd << 8)), param, n);
|
||
}
|
||
|
||
static void axs_color(uint8_t cmd, const void *px, size_t n)
|
||
{
|
||
esp_lcd_panel_io_tx_color(s_io, (int)((AXS_OP_COLOR << 24) | (cmd << 8)), px, n);
|
||
}
|
||
|
||
/* esp_lcd_panel_io_tx_color 是异步的:函数返回时 DMA 还在读 buffer,
|
||
官方头文件明确要求 buffer 只能在 on_color_trans_done 回调之后回收。
|
||
转置带缓冲 s_band 是复用的,所以每发一带之前必须等上一带归还,否则
|
||
CPU 正在写的下一带会撕碎上一带还没发完的像素(纯色块看不出来,字形必花)。 */
|
||
static SemaphoreHandle_t s_band_free;
|
||
|
||
static bool color_done_cb(esp_lcd_panel_io_handle_t io, esp_lcd_panel_io_event_data_t *edata, void *ctx)
|
||
{
|
||
BaseType_t hp = pdFALSE;
|
||
xSemaphoreGiveFromISR(s_band_free, &hp);
|
||
if (hp) portYIELD_FROM_ISR();
|
||
return false;
|
||
}
|
||
|
||
static bool s_tca; /* 探测到 TCA9554 = V2 板;没有就是 V1 的 GPIO 直连 */
|
||
|
||
static bool tca_write(uint8_t reg, uint8_t v)
|
||
{
|
||
Wire.beginTransmission(TCA9554_ADDR);
|
||
Wire.write(reg);
|
||
Wire.write(v);
|
||
return Wire.endTransmission() == 0;
|
||
}
|
||
|
||
/* 屏电源/复位/背光。V2 全在 TCA9554 上,V1 是 GPIO 直连,靠 0x20 应答与否区分。
|
||
SYS_EN(bit6) 是电源锁存:写其他位之前先读回输出寄存器并保住这一位,拉低=整板断电。 */
|
||
static void panel_power_on()
|
||
{
|
||
Wire.begin(PIN_I2C_SDA, PIN_I2C_SCL, 400000);
|
||
Wire.beginTransmission(TCA9554_ADDR);
|
||
s_tca = (Wire.endTransmission() == 0);
|
||
|
||
if (!s_tca) {
|
||
pinMode(PIN_V1_LCD_RST, OUTPUT);
|
||
digitalWrite(PIN_V1_LCD_RST, HIGH); delay(20);
|
||
digitalWrite(PIN_V1_LCD_RST, LOW); delay(40);
|
||
digitalWrite(PIN_V1_LCD_RST, HIGH); delay(120);
|
||
pinMode(PIN_V1_LCD_BL, OUTPUT);
|
||
digitalWrite(PIN_V1_LCD_BL, HIGH);
|
||
Serial.printf("[pwr] no TCA9554 -> V1 wiring (RST=%d BL=%d)\n", PIN_V1_LCD_RST, PIN_V1_LCD_BL);
|
||
return;
|
||
}
|
||
|
||
uint8_t out = 0;
|
||
Wire.beginTransmission(TCA9554_ADDR);
|
||
Wire.write(TCA_REG_OUTPUT);
|
||
bool rd = Wire.endTransmission(false) == 0 &&
|
||
Wire.requestFrom((uint8_t)TCA9554_ADDR, (uint8_t)1) == 1;
|
||
if (rd) out = (uint8_t)Wire.read();
|
||
/* BL_EN / LCD_RST / SYS_EN 设成输出,其余(触摸/IMU/RTC 的中断脚)留输入 */
|
||
tca_write(TCA_REG_CONFIG, (uint8_t)~(TCA_IO_BL_EN | TCA_IO_LCD_RST | TCA_IO_SYS_EN));
|
||
out |= TCA_IO_SYS_EN | TCA_IO_LCD_RST;
|
||
tca_write(TCA_REG_OUTPUT, out);
|
||
delay(20);
|
||
/* 复位脉宽按官方驱动:低 250ms,高 30ms 以上 */
|
||
out &= (uint8_t)~TCA_IO_LCD_RST; tca_write(TCA_REG_OUTPUT, out); delay(250);
|
||
out |= TCA_IO_LCD_RST; tca_write(TCA_REG_OUTPUT, out); delay(120);
|
||
/* 背光:使能位 + PWM 脚(低电平亮),不用调光就固定拉低 = 满亮度 */
|
||
out |= TCA_IO_BL_EN; tca_write(TCA_REG_OUTPUT, out);
|
||
pinMode(PIN_LCD_BL_PWM, OUTPUT);
|
||
digitalWrite(PIN_LCD_BL_PWM, LOW);
|
||
Serial.printf("[pwr] TCA9554 @0x%02X (V2) out=0x%02X SYS_EN/BL_EN/RST=1\n",
|
||
TCA9554_ADDR, out);
|
||
}
|
||
#endif
|
||
|
||
bool disp_init()
|
||
{
|
||
s_fb = (uint16_t *)heap_caps_malloc(LCD_WIDTH * LCD_HEIGHT * 2, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||
if (!s_fb) { Serial.println(F("[disp] PSRAM alloc failed")); return false; }
|
||
|
||
#if BOARD_HAS_QSPI
|
||
/* 复位/背光都在扩展器(或 V1 的 GPIO)里,先给屏上电再谈总线 */
|
||
panel_power_on();
|
||
#elif BOARD_HAS_XL9555
|
||
/* 屏的 RST/PWR 由 XL9555 提供,时序照官方 spilcd_init */
|
||
xl_pin_write(XL_IO_SLCD_RST, 0); delay(100);
|
||
xl_pin_write(XL_IO_SLCD_RST, 1); delay(100);
|
||
#else
|
||
/* 带电池的小板:电源保持脚必须最先拉高,拉低=整板掉电,屏永远不会亮 */
|
||
if (PIN_PWR_HOLD >= 0) { pinMode(PIN_PWR_HOLD, OUTPUT); digitalWrite(PIN_PWR_HOLD, HIGH); }
|
||
lcd_gpio_reset();
|
||
#endif
|
||
|
||
esp_err_t e;
|
||
spi_bus_config_t bus;
|
||
memset(&bus, 0, sizeof(bus));
|
||
bus.mosi_io_num = PIN_SPI_MOSI;
|
||
bus.miso_io_num = PIN_SPI_MISO;
|
||
bus.sclk_io_num = PIN_SPI_SCK;
|
||
bus.quadwp_io_num = PIN_SPI_WP;
|
||
bus.quadhd_io_num = PIN_SPI_HD;
|
||
bus.max_transfer_sz = LCD_WIDTH * FLUSH_ROWS * 2;
|
||
e = spi_bus_initialize(LCD_SPI_HOST, &bus, SPI_DMA_CH_AUTO);
|
||
if (e != ESP_OK) { Serial.printf("[disp] spi_bus_initialize: %d\n", e); return false; }
|
||
|
||
esp_lcd_panel_io_spi_config_t io_cfg;
|
||
memset(&io_cfg, 0, sizeof(io_cfg));
|
||
io_cfg.cs_gpio_num = PIN_LCD_CS;
|
||
io_cfg.dc_gpio_num = PIN_LCD_DC;
|
||
io_cfg.spi_mode = LCD_SPI_MODE; /* 星智官方用 mode3,正点原子用 mode0 */
|
||
io_cfg.pclk_hz = LCD_PCLK_HZ;
|
||
io_cfg.trans_queue_depth = 2;
|
||
io_cfg.lcd_cmd_bits = BOARD_HAS_QSPI ? 32 : 8; /* QSPI 的命令字里含操作码 */
|
||
io_cfg.lcd_param_bits = 8;
|
||
#if BOARD_HAS_QSPI
|
||
io_cfg.flags.quad_mode = 1; /* 参数/像素走 4 线,命令仍走 1 线 */
|
||
s_band_free = xSemaphoreCreateBinary();
|
||
io_cfg.on_color_trans_done = color_done_cb;
|
||
#endif
|
||
e = esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_SPI_HOST, &io_cfg, &s_io);
|
||
if (e != ESP_OK) { Serial.printf("[disp] new_panel_io_spi: %d\n", e); return false; }
|
||
|
||
#if BOARD_HAS_QSPI
|
||
/* 这份 core 没带 esp_lcd_axs15231b 组件,初始化按官方例程实际下发的命令自己发:
|
||
出厂镜像的日志就只有 SLPOUT / MADCTL=0x00 / COLMOD=0x55 / DISPON 四条。
|
||
注意别用 DISPOFF 息屏——这块板上试过醒不过来,要息屏只关背光。 */
|
||
static const uint8_t madctl = 0x00, colmod = 0x55;
|
||
axs_cmd(AXS_SLPOUT, NULL, 0); delay(120);
|
||
axs_cmd(AXS_MADCTL, &madctl, 1);
|
||
axs_cmd(AXS_COLMOD, &colmod, 1);
|
||
axs_cmd(AXS_SLPOUT, NULL, 0); delay(120); /* 官方驱动在 DISPON 前又发了一次 */
|
||
axs_cmd(AXS_DISPON, NULL, 0); delay(120);
|
||
Serial.printf("[disp] %s AXS15231B QSPI %dx%d ok pclk=%lu fb=%p\n",
|
||
BOARD_NAME, LCD_WIDTH, LCD_HEIGHT, (unsigned long)LCD_PCLK_HZ, (void *)s_fb);
|
||
#else
|
||
esp_lcd_panel_dev_config_t p_cfg;
|
||
memset(&p_cfg, 0, sizeof(p_cfg));
|
||
p_cfg.reset_gpio_num = PIN_LCD_RST; /* 旧板没有 GPIO 复位脚(-1),星智=18 */
|
||
p_cfg.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
|
||
p_cfg.data_endian = LCD_RGB_DATA_ENDIAN_BIG;
|
||
p_cfg.bits_per_pixel = 16;
|
||
e = esp_lcd_new_panel_st7789(s_io, &p_cfg, &s_panel);
|
||
if (e != ESP_OK) { Serial.printf("[disp] new_panel_st7789: %d\n", e); return false; }
|
||
|
||
esp_lcd_panel_reset(s_panel);
|
||
esp_lcd_panel_invert_color(s_panel, true); /* ST7789V 需要色反转 */
|
||
esp_lcd_panel_init(s_panel);
|
||
esp_lcd_panel_disp_on_off(s_panel, true);
|
||
esp_lcd_panel_swap_xy(s_panel, LCD_SWAP_XY); /* 旧板竖屏控制器 -> 320x240 横屏 */
|
||
esp_lcd_panel_mirror(s_panel, s_mx, s_my);
|
||
#if BOARD_HAS_XL9555
|
||
xl_pin_write(XL_IO_SLCD_PWR, 1); /* 背光随屏电源打开 */
|
||
#else
|
||
if (PIN_LCD_BL >= 0) { pinMode(PIN_LCD_BL, OUTPUT); digitalWrite(PIN_LCD_BL, HIGH); }
|
||
#endif
|
||
Serial.printf("[disp] %s ST7789 %dx%d ok mode=%d swap=%d fb=%p\n",
|
||
BOARD_NAME, LCD_WIDTH, LCD_HEIGHT, LCD_SPI_MODE, LCD_SWAP_XY, (void *)s_fb);
|
||
#endif
|
||
return true;
|
||
}
|
||
|
||
void disp_clear(uint16_t color)
|
||
{
|
||
disp_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, color);
|
||
}
|
||
|
||
void disp_pixel(int x, int y, uint16_t color)
|
||
{
|
||
if ((unsigned)x >= LCD_WIDTH || (unsigned)y >= LCD_HEIGHT) return;
|
||
s_fb[y * LCD_WIDTH + x] = pack(color);
|
||
}
|
||
|
||
void disp_fill(int x, int y, int w, int h, uint16_t color)
|
||
{
|
||
if (x < 0) { w += x; x = 0; }
|
||
if (y < 0) { h += y; y = 0; }
|
||
if (x + w > LCD_WIDTH) w = LCD_WIDTH - x;
|
||
if (y + h > LCD_HEIGHT) h = LCD_HEIGHT - y;
|
||
if (w <= 0 || h <= 0) return;
|
||
uint16_t c = pack(color);
|
||
for (int row = y; row < y + h; row++) {
|
||
uint16_t *dst = s_fb + row * LCD_WIDTH + x;
|
||
for (int col = 0; col < w; col++) dst[col] = c;
|
||
}
|
||
}
|
||
|
||
void disp_rect(int x, int y, int w, int h, uint16_t color)
|
||
{
|
||
disp_hline(x, y, w, color);
|
||
disp_hline(x, y + h - 1, w, color);
|
||
for (int row = y; row < y + h; row++) { disp_pixel(x, row, color); disp_pixel(x + w - 1, row, color); }
|
||
}
|
||
|
||
void disp_hline(int x, int y, int w, uint16_t color)
|
||
{
|
||
disp_fill(x, y, w, 1, color);
|
||
}
|
||
|
||
void disp_disc(int cx, int cy, int r, uint16_t color)
|
||
{
|
||
for (int dy = -r; dy <= r; dy++) {
|
||
int dx = (int)sqrtf((float)(r * r - dy * dy));
|
||
disp_fill(cx - dx, cy + dy, dx * 2 + 1, 1, color);
|
||
}
|
||
}
|
||
|
||
/* ---- 背景 / 切角面板 / 图标叠加 ---------------------------------------- */
|
||
static uint32_t s_rng;
|
||
static uint32_t lcg(void) { s_rng = s_rng * 1664525u + 1013904223u; return s_rng >> 8; }
|
||
|
||
void disp_bg(void)
|
||
{
|
||
disp_clear(C_BG);
|
||
s_rng = 20261004u; /* 固定种子:每帧星点位置不变,不闪 */
|
||
static const uint16_t lvl[3] = { C_TRACE, C_STAR, C_CYAN_DIM };
|
||
for (int i = 0; i < 110; i++) {
|
||
int x = lcg() % LCD_WIDTH, y = lcg() % LCD_HEIGHT;
|
||
int r = lcg() % 100;
|
||
uint16_t c = lvl[(r < 4) ? 2 : (r < 30) ? 1 : 0];
|
||
disp_pixel(x, y, c);
|
||
if (r < 4) { disp_pixel(x + 1, y, c); disp_pixel(x, y + 1, c); disp_pixel(x + 1, y + 1, c); }
|
||
}
|
||
/* 四角电路走线:3 点折线,端点一个过孔 */
|
||
static const int tr[4][6] = {
|
||
{ 2, 34, 30, 34, 30, 52 },
|
||
{ LCD_WIDTH - 3, 40, LCD_WIDTH - 28, 40, LCD_WIDTH - 28, 20 },
|
||
{ 4, LCD_HEIGHT - 32, 38, LCD_HEIGHT - 32, 38, LCD_HEIGHT - 40 },
|
||
{ LCD_WIDTH - 4, LCD_HEIGHT - 38, LCD_WIDTH - 28, LCD_HEIGHT - 38,
|
||
LCD_WIDTH - 28, LCD_HEIGHT - 26 },
|
||
};
|
||
for (int i = 0; i < 4; i++) {
|
||
int px = tr[i][0], py = tr[i][1];
|
||
for (int k = 2; k <= 4; k += 2) {
|
||
int nx = tr[i][k], ny = tr[i][k + 1];
|
||
if (ny == py) disp_hline(min(px, nx), py, abs(nx - px) + 1, C_TRACE);
|
||
else for (int y = min(py, ny); y <= max(py, ny); y++) disp_pixel(px, y, C_TRACE);
|
||
px = nx; py = ny;
|
||
}
|
||
disp_pixel(tr[i][4], tr[i][5], C_CYAN_DIM);
|
||
}
|
||
}
|
||
|
||
static int cham_inset(int j, int h, int cut)
|
||
{
|
||
if (j < cut) return cut - j;
|
||
if (j >= h - cut) return j - (h - cut - 1);
|
||
return 0;
|
||
}
|
||
|
||
static void cham_outline(int x, int y, int w, int h, int cut, uint16_t color)
|
||
{
|
||
for (int j = 0; j < h; j++) {
|
||
int in = cham_inset(j, h, cut);
|
||
if (j == 0 || j == h - 1) {
|
||
for (int i = cut; i < w - cut; i++) disp_pixel(x + i, y + j, color);
|
||
} else {
|
||
disp_pixel(x + in, y + j, color);
|
||
disp_pixel(x + w - 1 - in, y + j, color);
|
||
}
|
||
}
|
||
}
|
||
|
||
static int clamp_cut(int w, int h, int cut)
|
||
{
|
||
if (cut * 2 >= h) cut = (h - 1) / 2;
|
||
if (cut * 2 >= w) cut = (w - 1) / 2;
|
||
return cut < 0 ? 0 : cut;
|
||
}
|
||
|
||
void disp_panel(int x, int y, int w, int h, int cut, uint16_t fill, uint16_t border)
|
||
{
|
||
cut = clamp_cut(w, h, cut);
|
||
for (int j = 0; j < h; j++) {
|
||
int in = cham_inset(j, h, cut);
|
||
disp_fill(x + in, y + j, w - 2 * in, 1, fill);
|
||
}
|
||
cham_outline(x, y, w, h, cut, border);
|
||
}
|
||
|
||
void disp_panel_glow(int x, int y, int w, int h, int cut, uint16_t glow)
|
||
{
|
||
cham_outline(x - 1, y - 1, w + 2, h + 2, clamp_cut(w + 2, h + 2, cut + 1), glow);
|
||
}
|
||
|
||
/* 帧缓冲按 pack() 后的样子存放,叠加前先还原成普通 RGB565 */
|
||
static inline uint16_t unpack(uint16_t v)
|
||
{
|
||
if (s_byteswap) v = (uint16_t)((v >> 8) | (v << 8));
|
||
if (s_bgr) v = (uint16_t)(((v & 0xF800) >> 11) | (v & 0x07E0) | ((v & 0x001F) << 11));
|
||
return v;
|
||
}
|
||
|
||
void disp_add_bitmap(int x, int y, int w, int h, const uint16_t *px, uint8_t scale)
|
||
{
|
||
if (!px || !scale) return;
|
||
for (int j = 0; j < h; j++) {
|
||
const uint16_t *src = px + (size_t)j * w;
|
||
for (int i = 0; i < w; i++) {
|
||
uint16_t s = src[i];
|
||
if (!s) continue; /* 黑底直接不加 */
|
||
for (int dy = 0; dy < scale; dy++) {
|
||
int yy = y + j * scale + dy;
|
||
if ((unsigned)yy >= LCD_HEIGHT) continue;
|
||
uint16_t *dst = s_fb + (size_t)yy * LCD_WIDTH;
|
||
for (int dx = 0; dx < scale; dx++) {
|
||
int xx = x + i * scale + dx;
|
||
if ((unsigned)xx >= LCD_WIDTH) continue;
|
||
uint16_t d = unpack(dst[xx]);
|
||
int r = ((d >> 11) & 0x1F) + ((s >> 11) & 0x1F);
|
||
int g = ((d >> 5) & 0x3F) + ((s >> 5) & 0x3F);
|
||
int b = (d & 0x1F) + (s & 0x1F);
|
||
if (r > 31) r = 31;
|
||
if (g > 63) g = 63;
|
||
if (b > 31) b = 31;
|
||
dst[xx] = pack((uint16_t)((r << 11) | (g << 5) | b));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void disp_blit(int x, int y, int w, int h, const uint16_t *px)
|
||
{
|
||
if (!px) return;
|
||
for (int j = 0; j < h; j++) {
|
||
int yy = y + j;
|
||
if ((unsigned)yy >= LCD_HEIGHT) continue;
|
||
const uint16_t *src = px + (size_t)j * w;
|
||
uint16_t *dst = s_fb + (size_t)yy * LCD_WIDTH;
|
||
for (int i = 0; i < w; i++) {
|
||
int xx = x + i;
|
||
if ((unsigned)xx >= LCD_WIDTH) continue;
|
||
dst[xx] = pack(src[i]);
|
||
}
|
||
}
|
||
}
|
||
|
||
void disp_ring(int cx, int cy, int r, uint16_t color)
|
||
{
|
||
int x = r, y = 0, err = 1 - r;
|
||
while (x >= y) {
|
||
disp_pixel(cx + x, cy + y, color); disp_pixel(cx - x, cy + y, color);
|
||
disp_pixel(cx + x, cy - y, color); disp_pixel(cx - x, cy - y, color);
|
||
disp_pixel(cx + y, cy + x, color); disp_pixel(cx - y, cy + x, color);
|
||
disp_pixel(cx + y, cy - x, color); disp_pixel(cx - y, cy - x, color);
|
||
y++;
|
||
if (err < 0) err += 2 * y + 1;
|
||
else { x--; err += 2 * (y - x) + 1; }
|
||
}
|
||
}
|
||
|
||
void disp_radar(int cx, int cy, int r)
|
||
{
|
||
disp_ring(cx, cy, r, C_CYAN_DIM);
|
||
disp_ring(cx, cy, r * 2 / 3, C_TRACE);
|
||
disp_ring(cx, cy, r / 3, C_TRACE);
|
||
disp_hline(cx - r - 4, cy, 2 * r + 9, C_TRACE);
|
||
for (int dy = -r - 4; dy <= r + 4; dy++) disp_pixel(cx, cy + dy, C_TRACE);
|
||
for (int k = 0; k < 8; k++) {
|
||
float a = k * 3.1415926f / 4.0f;
|
||
for (int t = 0; t <= 5; t++) {
|
||
float rr = (float)(r - 2 - t);
|
||
disp_pixel(cx + (int)(cosf(a) * rr), cy + (int)(sinf(a) * rr), C_CYAN_DIM);
|
||
}
|
||
}
|
||
}
|
||
|
||
/* ---- 字模:行式,每行 (width+7)/8 字节,MSB 为最左像素 ---- */
|
||
static const uint8_t *glyph_of(char ch, uint8_t size, int *bytes_per_row)
|
||
{
|
||
int idx = (uint8_t)ch - ' ';
|
||
if (idx < 0 || idx > 94) idx = 0;
|
||
*bytes_per_row = (size >= 32) ? 2 : 1;
|
||
return (size >= 32) ? asc2_3216[idx] : asc2_1608[idx];
|
||
}
|
||
|
||
static int utf8_next(const char *s, uint32_t *cp)
|
||
{
|
||
uint8_t c = (uint8_t)s[0];
|
||
if (c < 0x80) { *cp = c; return 1; }
|
||
if ((c & 0xE0) == 0xC0 && ((uint8_t)s[1] & 0xC0) == 0x80) {
|
||
*cp = ((uint32_t)(c & 0x1F) << 6) | ((uint8_t)s[1] & 0x3F);
|
||
return 2;
|
||
}
|
||
if ((c & 0xF0) == 0xE0 && ((uint8_t)s[1] & 0xC0) == 0x80 && ((uint8_t)s[2] & 0xC0) == 0x80) {
|
||
*cp = ((uint32_t)(c & 0x0F) << 12) | ((uint32_t)((uint8_t)s[1] & 0x3F) << 6)
|
||
| ((uint8_t)s[2] & 0x3F);
|
||
return 3;
|
||
}
|
||
*cp = c;
|
||
return 1;
|
||
}
|
||
|
||
static void glyph_blit(int x, int y, const uint8_t *rows, int bpr, int gw, int gh,
|
||
int f, uint16_t fg, uint16_t bg)
|
||
{
|
||
for (int row = 0; row < gh; row++) {
|
||
const uint8_t *lr = rows + row * bpr;
|
||
for (int col = 0; col < gw; col++) {
|
||
uint16_t c = (lr[col / 8] & (0x80 >> (col % 8))) ? fg : bg;
|
||
if (f == 1) disp_pixel(x + col, y + row, c);
|
||
else disp_fill(x + col * f, y + row * f, f, f, c);
|
||
}
|
||
}
|
||
}
|
||
|
||
/** 一个字符占的横向推进(含 1px 字距) */
|
||
static int char_advance(uint32_t cp, uint8_t size, uint8_t scale)
|
||
{
|
||
if (scale < 1) scale = 1;
|
||
if (cp < 0x80) return (size / 2 + 1) * scale;
|
||
return (CJK_CELL + 1) * ((size >= 32) ? 2 * scale : scale);
|
||
}
|
||
|
||
static const uint8_t kMissingGlyph[32] = {
|
||
0xFF, 0xFF,
|
||
0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
|
||
0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
|
||
0xFF, 0xFF,
|
||
};
|
||
|
||
static int disp_char(int x, int y, uint32_t cp, uint8_t size, uint8_t scale,
|
||
uint16_t fg, uint16_t bg)
|
||
{
|
||
if (scale < 1) scale = 1;
|
||
if (cp < 0x80) {
|
||
int bpr = 1;
|
||
glyph_blit(x, y, glyph_of((char)cp, size, &bpr), bpr, size / 2, size, scale, fg, bg);
|
||
} else {
|
||
int f = (size >= 32) ? 2 * scale : scale;
|
||
const uint8_t *g = cjk_glyph(cp);
|
||
glyph_blit(x, y, g ? g : kMissingGlyph, 2, CJK_CELL, CJK_CELL, f, fg, bg);
|
||
}
|
||
return char_advance(cp, size, scale);
|
||
}
|
||
|
||
int disp_text_width(const char *s, uint8_t size, uint8_t scale)
|
||
{
|
||
if (!s) return 0;
|
||
int total = 0;
|
||
while (*s) {
|
||
uint32_t cp;
|
||
s += utf8_next(s, &cp);
|
||
total += char_advance(cp, size, scale);
|
||
}
|
||
return total;
|
||
}
|
||
|
||
void disp_text(int x, int y, const char *s, uint8_t size, uint16_t fg, uint16_t bg, uint8_t scale)
|
||
{
|
||
if (!s) return;
|
||
while (*s) {
|
||
uint32_t cp;
|
||
int l = utf8_next(s, &cp);
|
||
x += disp_char(x, y, cp, size, scale, fg, bg);
|
||
s += l;
|
||
}
|
||
}
|
||
|
||
void disp_text_center(int x, int y, int w, const char *s, uint8_t size, uint16_t fg, uint16_t bg, uint8_t scale)
|
||
{
|
||
disp_text(x + (w - disp_text_width(s, size, scale)) / 2, y, s, size, fg, bg, scale);
|
||
}
|
||
|
||
/** 放不下就在字符边界截断并补 ".."(不能按字节切,否则中文成半个 UTF-8 序列) */
|
||
void disp_text_clipped(int x, int y, int maxw, const char *s, uint8_t size, uint16_t fg, uint16_t bg)
|
||
{
|
||
if (!s) return;
|
||
if (disp_text_width(s, size, 1) <= maxw) { disp_text(x, y, s, size, fg, bg); return; }
|
||
int dots = disp_text_width("..", size, 1);
|
||
int used = 0;
|
||
while (*s) {
|
||
uint32_t cp;
|
||
int l = utf8_next(s, &cp);
|
||
int adv = char_advance(cp, size, 1);
|
||
if (used + adv + dots > maxw) break;
|
||
disp_char(x + used, y, cp, size, 1, fg, bg);
|
||
used += adv;
|
||
s += l;
|
||
}
|
||
disp_text(x + used, y, "..", size, fg, bg);
|
||
}
|
||
|
||
/* 这些中文标点按排版规矩不能出现在行首(","顶格最难看) */
|
||
static bool cjk_no_lead(uint32_t cp)
|
||
{
|
||
switch (cp) {
|
||
case 0x3002: case 0xFF0C: case 0x3001: case 0xFF1B: case 0xFF1A:
|
||
case 0xFF1F: case 0xFF01: case 0xFF09: case 0x300B: case 0x3011:
|
||
case 0x201D: case 0x2019: case 0xFF05:
|
||
return true;
|
||
default: return false;
|
||
}
|
||
}
|
||
|
||
int disp_text_wrap(int x, int y, int maxw, const char *s, uint8_t size, uint16_t fg, uint16_t bg, int max_lines)
|
||
{
|
||
if (!s) return y;
|
||
char line[192];
|
||
int n = 0, cur = 0, rows = 0;
|
||
int prev_len = 0, prev_adv = 0; /* 上一个 token 的字节数/宽度,标点换行要把它带下去 */
|
||
const char *p = s;
|
||
while (*p && rows < max_lines) {
|
||
int tl = 0, tadv = 0;
|
||
if ((uint8_t)*p >= 0x80) { /* 中文:一个字一个 token */
|
||
uint32_t cp;
|
||
tl = utf8_next(p, &cp);
|
||
tadv = char_advance(cp, size, 1);
|
||
} else { /* 英文:整个词(连尾部空格)*/
|
||
while (p[tl] && (uint8_t)p[tl] < 0x80 && p[tl] != ' ') {
|
||
tadv += char_advance((uint8_t)p[tl], size, 1);
|
||
tl++;
|
||
}
|
||
while (p[tl] == ' ') { tadv += char_advance(' ', size, 1); tl++; }
|
||
}
|
||
if (tadv > maxw) {
|
||
/* 整个词比行宽还长(URL / API 地址):换行也救不了,只能按像素硬切,
|
||
否则会一路画出面板外。320 横屏上从来没有这么长的词,不影响旧板。 */
|
||
if (n) {
|
||
line[n] = 0;
|
||
disp_text(x, y, line, size, fg, bg);
|
||
y += size + 3; rows++; n = 0; cur = 0;
|
||
}
|
||
int used = 0;
|
||
while (*p && *p != ' ' && rows < max_lines) {
|
||
uint32_t cp;
|
||
int l = utf8_next(p, &cp);
|
||
int adv = char_advance(cp, size, 1);
|
||
if (used && used + adv > maxw) {
|
||
line[n] = 0;
|
||
disp_text(x, y, line, size, fg, bg);
|
||
y += size + 3; rows++; n = 0; used = 0;
|
||
if (rows >= max_lines) break;
|
||
}
|
||
if (n < (int)sizeof(line) - 4) for (int i = 0; i < l; i++) line[n++] = p[i];
|
||
used += adv;
|
||
p += l;
|
||
}
|
||
cur = used; prev_len = 0; prev_adv = 0;
|
||
if (rows >= max_lines) { n = 0; line[0] = 0; break; }
|
||
continue;
|
||
}
|
||
if (cur && cur + tadv > maxw) {
|
||
uint32_t ncp = 0;
|
||
bool pull = ((uint8_t)*p >= 0x80) && prev_len && prev_adv < cur &&
|
||
utf8_next(p, &ncp) && cjk_no_lead(ncp);
|
||
if (pull) { n -= prev_len; cur -= prev_adv; } /* 行首留标点 -> 连前一个字一起换行 */
|
||
line[n] = 0;
|
||
disp_text(x, y, line, size, fg, bg);
|
||
y += size + 3;
|
||
rows++;
|
||
if (pull) {
|
||
for (int i = 0; i < prev_len; i++) line[n++] = p[0 - prev_len + i];
|
||
cur = prev_adv;
|
||
} else {
|
||
n = 0; cur = 0;
|
||
}
|
||
if (rows >= max_lines) break;
|
||
}
|
||
for (int i = 0; i < tl && n < (int)sizeof(line) - 4; i++) line[n++] = p[i];
|
||
cur += tadv;
|
||
p += tl;
|
||
prev_len = tl; prev_adv = tadv;
|
||
}
|
||
line[n] = 0;
|
||
if (n) { disp_text(x, y, line, size, fg, bg); y += size + 3; }
|
||
return y;
|
||
}
|
||
|
||
const uint16_t *disp_fb_ptr() { return s_fb; }
|
||
|
||
void disp_flush()
|
||
{
|
||
if (!s_panel && !s_io) return;
|
||
#if BOARD_HAS_QSPI
|
||
/* QSPI 模式下这块屏只认 CASET(RASET 无效),显存写指针自己往下走,
|
||
所以整屏必须从面板第 0 行起按行带顺序发:头一带 RAMWR,后面的带 RAMWRC。
|
||
逻辑画布是横过来的 640x172,发之前逐带转置:逻辑 x 沿面板行方向、
|
||
逻辑 y 沿面板列方向,rot 的 bit0/bit1 分别是这两根的镜像。 */
|
||
static uint16_t s_band[PANEL_WIDTH * FLUSH_ROWS];
|
||
const int mv_x = s_rot & 1, mv_y = s_rot & 2;
|
||
const uint8_t col[4] = { 0, 0, (uint8_t)((PANEL_WIDTH - 1) >> 8), (uint8_t)(PANEL_WIDTH - 1) };
|
||
axs_cmd(AXS_CASET, col, 4);
|
||
xSemaphoreGive(s_band_free); /* 开机第一帧:缓冲本来就是空的 */
|
||
for (int p0 = 0; p0 < PANEL_HEIGHT; p0 += FLUSH_ROWS) {
|
||
int rows = (PANEL_HEIGHT - p0 < FLUSH_ROWS) ? (PANEL_HEIGHT - p0) : FLUSH_ROWS;
|
||
/* 先等上一带的 DMA 把 s_band 交还,再往里写下一带 */
|
||
if (xSemaphoreTake(s_band_free, pdMS_TO_TICKS(500)) != pdTRUE)
|
||
Serial.println(F("[disp] flush: DMA band timeout"));
|
||
for (int y = 0; y < LCD_HEIGHT; y++) {
|
||
int px = mv_y ? (PANEL_WIDTH - 1 - y) : y;
|
||
for (int c = 0; c < rows; c++) {
|
||
int p = p0 + c;
|
||
s_band[(size_t)c * PANEL_WIDTH + px] =
|
||
s_fb[(size_t)y * LCD_WIDTH + (mv_x ? (PANEL_HEIGHT - 1 - p) : p)];
|
||
}
|
||
}
|
||
axs_color(p0 == 0 ? AXS_RAMWR : AXS_RAMWRC, s_band, (size_t)rows * PANEL_WIDTH * 2);
|
||
}
|
||
xSemaphoreTake(s_band_free, pdMS_TO_TICKS(500)); /* 最后一带发完再返回 */
|
||
return;
|
||
#endif
|
||
for (int y0 = 0; y0 < LCD_HEIGHT; y0 += FLUSH_ROWS) {
|
||
int rows = (LCD_HEIGHT - y0 < FLUSH_ROWS) ? (LCD_HEIGHT - y0) : FLUSH_ROWS;
|
||
esp_lcd_panel_draw_bitmap(s_panel, 0, y0, LCD_WIDTH, y0 + rows, s_fb + (size_t)y0 * LCD_WIDTH);
|
||
}
|
||
delay(20); /* 等最后一块 DMA 走完,避免下一次重绘踩到在途缓冲 */
|
||
}
|
||
|
||
void disp_set_byteswap(bool on) { s_byteswap = on; }
|
||
void disp_set_bgr(bool on) { s_bgr = on; }
|
||
void disp_set_invert(bool on) { if (s_panel) esp_lcd_panel_invert_color(s_panel, on); }
|
||
|
||
void disp_set_mirror(bool mx, bool my)
|
||
{
|
||
s_mx = mx; s_my = my;
|
||
if (s_panel) esp_lcd_panel_mirror(s_panel, s_mx, s_my);
|
||
}
|
||
|
||
/* QSPI 那块屏的 MADCTL 转置和显存写入顺序对不上(QSPI 模式只认 CASET),
|
||
所以朝向在 flush 里软件转置,0..3 = 逻辑画布的四种摆法 */
|
||
void disp_set_rot(int r) { s_rot = (uint8_t)(r & 3); }
|
||
int disp_rot() { return s_rot; }
|
||
|
||
/* 点屏自检:4 条色带 + 边界 + 文字,拍照即可判断字节序/色序/镜像 */
|
||
void disp_selftest()
|
||
{
|
||
static const uint16_t bars[] = { C_RED, C_GREEN, C_BLUE, C_WHITE };
|
||
static const char *names[] = { "RED", "GRN", "BLU", "WHT" };
|
||
disp_clear(C_BLACK);
|
||
#if LCD_HEIGHT > LCD_WIDTH
|
||
/* 竖屏:色带横着叠起来,标签写在每带里侧 */
|
||
for (int i = 0; i < 4; i++) {
|
||
int by = 26 + i * 66;
|
||
disp_fill(8, by, LCD_WIDTH - 16, 52, bars[i]);
|
||
disp_text_center(8, by + 54, LCD_WIDTH - 16, names[i], 16, C_CYAN, C_BLACK);
|
||
}
|
||
int ty = 26 + 4 * 66 + 8;
|
||
#else
|
||
/* 矮屏(长条屏 172 高)色带压扁一点,标签和文字跟着往上挪 */
|
||
const int bh = (LCD_HEIGHT > 200) ? 90 : 52;
|
||
for (int i = 0; i < 4; i++) {
|
||
int bx = 8 + i * 78;
|
||
disp_fill(bx, 30, 70, bh, bars[i]);
|
||
disp_text_center(bx, 30 + bh + 4, 70, names[i], 16, C_CYAN, C_BLACK);
|
||
}
|
||
int ty = (LCD_HEIGHT > 200) ? 150 : 102;
|
||
#endif
|
||
disp_rect(1, 1, LCD_WIDTH - 2, LCD_HEIGHT - 2, C_ORANGE);
|
||
disp_text_clipped(0, 4, LCD_WIDTH, BOARD_NAME " " BOARD_PANEL " "
|
||
COSMO_STR(LCD_WIDTH) "x" COSMO_STR(LCD_HEIGHT), 16, C_GREEN, C_BLACK);
|
||
char msg[48];
|
||
snprintf(msg, sizeof(msg), "SWAP=%d BGR=%d MX=%d", (int)s_byteswap, (int)s_bgr, (int)s_mx);
|
||
disp_text_center(0, ty, LCD_WIDTH, msg, 16, C_WHITE, C_BLACK);
|
||
disp_text_clipped(0, ty + 22, LCD_WIDTH, "1234567890 ABCDEFGHIJKLMNOP", 16, C_ORANGE, C_BLACK);
|
||
disp_disc(LCD_WIDTH / 2, ty + 52, 14, C_CYAN);
|
||
disp_flush();
|
||
}
|