# 通用UIL **Repository Path**: Cation_git/universal-ual ## Basic Information - **Project Name**: 通用UIL - **Description**: 希望使用统一的接口规范UI与上下层间的交互方式,打算用c/c++实现抽象接口,为上层的c/c++或者python提供硬件api的功能 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-08-03 - **Last Updated**: 2025-09-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 许多嵌入式设备(尤其是资源受限的MCU)无法运行Python。针对这种情况,我们需要重新设计架构,将核心逻辑与UI框架完全分离,并确保核心逻辑能在C/C++环境中运行。以下是优化后的解决方案: ### 跨平台架构设计(支持无Python环境) ```mermaid graph TD A[核心业务逻辑 - C/C++] -->|通过API调用| B[UI抽象层] B --> C[LVGL适配器 - C] B --> D[PyQt5适配器 - Python] B --> E[其他UI适配器] F[硬件驱动] --> A ``` ### 实现方案 #### 1. 核心业务逻辑 (C/C++ - 嵌入式可运行) ```c // core_logic.h #ifndef CORE_LOGIC_H #define CORE_LOGIC_H #ifdef __cplusplus extern "C" { #endif // 状态回调函数类型定义 typedef void (*StateUpdateCallback)(const char* key, const char* value); // 初始化核心逻辑 void core_init(StateUpdateCallback callback); // 处理用户操作 void core_handle_event(const char* event_type, const char* event_data); // 获取当前状态 const char* core_get_state(const char* key); #ifdef __cplusplus } #endif #endif // CORE_LOGIC_H ``` ```c // core_logic.c #include "core_logic.h" #include #include static StateUpdateCallback state_callback = NULL; static char app_state[256] = {0}; void core_init(StateUpdateCallback callback) { state_callback = callback; strcpy(app_state, "initial_state"); // 通知UI初始状态 if (state_callback) { state_callback("app_state", app_state); } } void core_handle_event(const char* event_type, const char* event_data) { if (strcmp(event_type, "button_click") == 0) { strcpy(app_state, "button_clicked"); // 通知UI状态变化 if (state_callback) { state_callback("app_state", app_state); } } // 处理其他事件... } const char* core_get_state(const char* key) { if (strcmp(key, "app_state") == 0) { return app_state; } return NULL; } ``` #### 2. UI抽象层 (C头文件 - 跨平台) ```c // ui_abstract.h #ifndef UI_ABSTRACT_H #define UI_ABSTRACT_H // UI事件回调 typedef void (*UIEventCallback)(const char* event_type, const char* event_data); // UI初始化函数 void ui_init(UIEventCallback event_callback); // 创建按钮 void* ui_create_button(int x, int y, int width, int height, const char* text); // 更新UI状态 void ui_update_state(const char* key, const char* value); // 运行UI主循环 void ui_run(); #endif // UI_ABSTRACT_H ``` #### 3. LVGL适配器实现 (C - 嵌入式环境) ```c // ui_lvgl.c #include "ui_abstract.h" #include "core_logic.h" #include "lvgl.h" static UIEventCallback event_callback = NULL; static lv_obj_t* status_label = NULL; // LVGL事件处理 static void btn_event_handler(lv_event_t * e) { if (event_callback) { event_callback("button_click", "main_button"); } } static void lvgl_update_state(const char* key, const char* value) { if (strcmp(key, "app_state") == 0 && status_label) { lv_label_set_text(status_label, value); } } void ui_init(UIEventCallback event_cb) { event_callback = event_cb; // 初始化LVGL lv_init(); // ... 显示初始化代码 // 初始化核心逻辑 core_init(lvgl_update_state); // 创建UI元素 lv_obj_t* btn = lv_btn_create(lv_scr_act()); lv_obj_set_pos(btn, 10, 10); lv_obj_set_size(btn, 120, 50); lv_obj_add_event_cb(btn, btn_event_handler, LV_EVENT_CLICKED, NULL); lv_obj_t* label = lv_label_create(btn); lv_label_set_text(label, "Click Me"); status_label = lv_label_create(lv_scr_act()); lv_obj_set_pos(status_label, 10, 70); } void ui_run() { while (1) { lv_timer_handler(); // ... 系统延时 } } ``` #### 4. PyQt5适配器实现 (Python - 桌面环境) ```python # ui_qt.py from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel import ctypes # 加载核心逻辑库 core_lib = ctypes.CDLL('./libcore.so') # 设置函数原型 core_lib.core_init.argtypes = [ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_char_p)] core_lib.core_handle_event.argtypes = [ctypes.c_char_p, ctypes.c_char_p] class QtUI: def __init__(self): self.app = QApplication([]) self.window = QWidget() self.window.setGeometry(100, 100, 300, 200) # 创建UI元素 self.button = QPushButton('Click Me', self.window) self.button.setGeometry(10, 10, 120, 50) self.button.clicked.connect(self.on_button_click) self.status_label = QLabel('Initializing...', self.window) self.status_label.setGeometry(10, 70, 280, 30) # 初始化核心逻辑 self.state_callback = ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_char_p)(self.update_state) core_lib.core_init(self.state_callback) def on_button_click(self): # 向核心逻辑发送事件 core_lib.core_handle_event(b"button_click", b"main_button") def update_state(self, key, value): # 从核心逻辑接收状态更新 status = ctypes.string_at(value).decode('utf-8') self.status_label.setText(f"Status: {status}") def run(self): self.window.show() self.app.exec_() ``` ### 构建和部署流程 1. **核心逻辑编译 (嵌入式设备)**: ```bash # 编译核心逻辑为静态库 gcc -c core_logic.c -o core_logic.o ar rcs libcore.a core_logic.o ``` 2. **LVGL应用构建 (嵌入式设备)**: ```bash # 链接核心库和LVGL适配器 gcc ui_lvgl.c -o embedded_app -L. -lcore -llvgl ``` 3. **桌面环境使用 (Python)**: ```bash # 编译核心逻辑为共享库 gcc -shared -fPIC core_logic.c -o libcore.so # 运行PyQt5应用 python ui_qt.py ``` ### 关键优势 1. **真正的平台无关性**: - 核心业务逻辑使用C/C++编写,可在任何嵌入式平台运行 - UI适配器按平台需求实现(C for MCU, Python for PC) 2. **双向通信机制**: - UI → 核心:通过事件传递 (`core_handle_event`) - 核心 → UI:通过状态回调 (`StateUpdateCallback`) 3. **资源优化**: - 嵌入式设备只需包含核心逻辑和LVGL适配器 - 内存占用最小化(无Python开销) 4. **开发工作流**: ```mermaid graph LR A[在PC上开发核心逻辑] --> B[使用PyQt5测试] B --> C[交叉编译到嵌入式平台] C --> D[集成LVGL适配器] D --> E[部署到设备] ``` ### 进阶优化方向 1. **通信协议抽象**: - 使用Protobuf定义事件和状态结构 - 自动生成C和Python的序列化代码 2. **远程调试接口**: ```c // 在核心逻辑中添加 #ifdef DEBUG_MODE void debug_send_state() { // 通过UART/USB发送状态信息 } #endif ``` 3. **资源文件共享**: - 使用二进制格式存储UI资源(字体、图像) - 同一资源文件可用于LVGL和PyQt5 4. **性能关键部分优化**: ```c // 核心逻辑中使用固定内存分配 #define MAX_STATES 10 static State states[MAX_STATES]; ``` ### 部署场景示例 **资源受限的STM32设备:** - 核心逻辑:编译为ARM Cortex-M库 - UI适配器:直接与LVGL集成 - 总内存占用:< 50KB **桌面开发环境:** - 核心逻辑:编译为共享库 (.so/.dll) - UI适配器:使用PyQt5实现 - 开发语言:Python(快速迭代) **工业级Linux设备:** - 核心逻辑:C++实现 - UI适配器:可同时支持LVGL(直接渲染)和Qt(通过进程通信) 这种架构确保您的核心业务逻辑可以在最基础的嵌入式设备上运行,同时保持UI层的灵活性,实现真正的"一次编写,多平台部署"。