Files
STM32F4-Base/simulator/sim_port.c
T
2026-08-31 17:09:59 +08:00

72 lines
2.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 模块名称:Simulator Display Port (SDL2 模拟器端口)
* 模块功能:为 PC 模拟器提供 LVGL SDL2 显示与键盘输入端口
* 适用平台:Windows / Linux / macOS (SDL2)
* 说明:替代 MCU 端的 ui_port.c,用于 PC 上 UI 开发与调试
*/
#include "sim_port.h"
#include "lvgl.h"
/* SDL 驱动头文件(由 lv_conf.h 中 LV_SDL_INCLUDE_PATH 指定) */
#define SDL_MAIN_HANDLED
#include <SDL.h>
/* 键盘输入设备句柄(全局,供 main.c SDL 事件循环读取) */
static lv_indev_t *g_kbd_indev = NULL;
/*
* 函数功能:初始化 SDL2 显示窗口与键盘输入设备
* 入口参数:hor_res - 水平分辨率 int32_t
* ver_res - 垂直分辨率 int32_t
* 返回值:0 - 成功,-1 - 失败
* 限定条件:lv_init() 已调用
* 函数说明:1. 创建 SDL 窗口显示设备
* 2. 创建键盘输入设备及默认输入组
* 3. 不创建鼠标/触摸设备(模拟器仅用键盘导航)
*/
int sim_port_init(int32_t hor_res, int32_t ver_res)
{
/* 创建默认输入组 */
lv_group_t *g = lv_group_create();
if (g == NULL) {
return -1;
}
lv_group_set_default(g);
/* 创建 SDL 窗口显示设备 */
lv_display_t *disp = lv_sdl_window_create(hor_res, ver_res);
if (disp == NULL) {
return -1;
}
lv_display_set_default(disp);
/* 初始化 mono 主题(1bpp 单色屏:白字黑底),并应用默认字体 */
#if LV_USE_THEME_MONO
lv_theme_t *th = lv_theme_mono_init(disp, true, LV_FONT_DEFAULT);
if (th != NULL) {
lv_display_set_theme(disp, th);
}
#endif
/* 创建键盘输入设备(方向键/确认/返回 → LVGL keypad) */
g_kbd_indev = lv_sdl_keyboard_create();
if (g_kbd_indev == NULL) {
return -1;
}
lv_indev_set_group(g_kbd_indev, lv_group_get_default());
lv_indev_set_display(g_kbd_indev, disp);
return 0;
}
/*
* 函数功能:获取 SDL 显示设备句柄
* 入口参数:无
* 返回值:显示设备指针 lv_display_t * 可能为 NULL
* 限定条件:sim_port_init() 已成功调用
*/
void *sim_port_get_display(void)
{
return (void *)lv_display_get_default();
}