Files
stc32g128k/Docs/60_coding/mod-lcd.md

3.6 KiB
Raw Blame History

编码实现 - LCD 显示与字模驱动 (Docs/60_coding/mod-lcd.md)

本文件对应 LCD 驱动与字模渲染的具体编码实现细节。

1. 对应代码源文件

2. 关键代码片段与逻辑实现

2.1 局部窗口设置 (X轴偏移量修正)

lcd.c 中配置窗口时,需要向 X 轴坐标加上 4 像素偏差:

void LCD_SetWindow(u16 x1, u16 y1, u16 x2, u16 y2) {
    x1 += 4;
    x2 += 4;
    LCD_WriteCmd(0x2A); // Column Address Set
    LCD_WriteData(x1 >> 8);
    LCD_WriteData(x1 & 0xFF);
    LCD_WriteData(x2 >> 8);
    LCD_WriteData(x2 & 0xFF);
    
    LCD_WriteCmd(0x2B); // Row Address Set
    LCD_WriteData(y1 >> 8);
    LCD_WriteData(y1 & 0xFF);
    LCD_WriteData(y2 >> 8);
    LCD_WriteData(y2 & 0xFF);
}

2.2 电池图标与百分比坐标对齐

main.c 的时钟重绘函数中,通过以下坐标在 135 像素宽 屏幕上绘制电量:

// 绘制百分比字符
LCD_ShowString(77, 24, "85%", COLOR_GRAY, COLOR_BLACK);
// 绘制电池位图
LCD_ShowImage(107, 27, 20, 10, bmp_battery_20x10, COLOR_GRAY, COLOR_BLACK);

两者 Y 轴保持 24 与 27 对齐X 轴坐标根据 135 宽屏幕右偏 15 像素(原为 62 和 92安全边距保持在 6 像素77 + 3*8 = 101与 107 之间为 6px防止重合。

2.3 字模百分号偏置

lcd_font.h 的 ASCII 字符数组中,百分号 %(十进制 37十六进制 0x25的字模通过在字模绘制时向上平移 2 像素对齐普通数字的下基准线。

2.4 水平居中渲染实现

lcd.c 中,字符串居中函数需修改宽度限制参数为 135 像素:

void LCD_ShowStringCentered(u16 y, char *str, u16 color, u16 bg_color) {
    u16 len = 0;
    char *p = str;
    while(*p++) len++;
    if(len * 8 >= 135)
        LCD_ShowString(0, y, str, color, bg_color);
    else
        LCD_ShowString((135 - len * 8) / 2, y, str, color, bg_color);
}

2.5 绑定界面 ID 渲染实现

main.c 中通过 FormatHex 接口将 bracelet_factory_id 转换为十六进制字符串,并显示在绑定界面的底部:

void UI_ShowBindingPage(void) {
    char id_str[15];
    // 渲染背景与图标
    // ...
    // 绘制手环自身的出厂 ID
    FormatHex(bracelet_factory_id, id_str);
    LCD_ShowStringCentered(180, id_str, COLOR_GRAY, COLOR_BLACK);
}

### 2.6 对码确认与序号微调页渲染
 `main.c` 中通过以下方式渲染可供按键微调的数字后缀界面:
```c
void UI_ShowPairConfirmPage(u8 type, u8 selected_suffix) {
    char num_buf[10];
    
    LCD_Clear(COLOR_BLACK);
    LCD_ShowStringCentered(40, "v RECU", COLOR_GREEN, COLOR_BLACK);
    
    // 根据类型渲染 32x32 图标
    // ...
    
    // 居中显示预设名称前缀
    LCD_ShowStringCentered(135, sensor_prefixes[type], COLOR_WHITE, COLOR_BLACK);
    
    // 拼接成带选框的 "[  02  ]" 格式字符串并高亮居中渲染
    num_buf[0] = '[';
    num_buf[1] = ' ';
    num_buf[2] = ' ';
    num_buf[3] = '0' + (selected_suffix / 10);
    num_buf[4] = '0' + (selected_suffix % 10);
    num_buf[5] = ' ';
    num_buf[6] = ' ';
    num_buf[7] = ']';
    num_buf[8] = '\0';
    LCD_ShowStringCentered(165, num_buf, COLOR_CYAN, COLOR_BLACK);
}

<!-- Checked and verified with always-on screen and sleep removal changes -->