luat_easylvgl.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * @file luat_easylvgl.h
  3. * @summary EasyLVGL 主头文件:上下文、平台操作接口、错误码
  4. * @responsible EasyLVGL 核心数据结构定义与平台抽象接口
  5. */
  6. #ifndef EASYLVGL_H
  7. #define EASYLVGL_H
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include <stddef.h>
  14. #include "lvgl9/lvgl.h"
  15. /*********************
  16. * DEFINES
  17. *********************/
  18. /** 回调函数类型最大数量 */
  19. #define EASYLVGL_CALLBACK_MAX 16
  20. /** 颜色格式常量(用于 Lua API,直接使用 LVGL 常量值) */
  21. #define EASYLVGL_COLOR_FORMAT_RGB565 LV_COLOR_FORMAT_RGB565 /**< RGB565 格式,16位,适用于嵌入式设备 */
  22. #define EASYLVGL_COLOR_FORMAT_ARGB8888 LV_COLOR_FORMAT_ARGB8888 /**< ARGB8888 格式,32位,默认格式 */
  23. /**********************
  24. * TYPEDEFS
  25. *********************/
  26. /** 前向声明 */
  27. typedef struct easylvgl_ctx easylvgl_ctx_t;
  28. typedef struct easylvgl_buffer easylvgl_buffer_t;
  29. typedef struct easylvgl_component_meta easylvgl_component_meta_t;
  30. /**
  31. * 缓冲模式
  32. */
  33. typedef enum {
  34. EASYLVGL_BUFFER_MODE_SINGLE, /**< 单缓冲 */
  35. EASYLVGL_BUFFER_MODE_DOUBLE, /**< 双缓冲 */
  36. EASYLVGL_BUFFER_MODE_LCD_SHARED, /**< LCD 共享缓冲 */
  37. EASYLVGL_BUFFER_MODE_EXTERNAL /**< 外部缓冲 */
  38. } easylvgl_buffer_mode_t;
  39. /**
  40. * 缓冲所有权
  41. */
  42. typedef enum {
  43. EASYLVGL_BUFFER_OWNER_SYSTEM, /**< 系统 heap */
  44. EASYLVGL_BUFFER_OWNER_LUA /**< Lua heap */
  45. } easylvgl_buffer_owner_t;
  46. /**
  47. * 错误码
  48. */
  49. typedef enum {
  50. EASYLVGL_OK = 0,
  51. EASYLVGL_ERR_INVALID_PARAM = -1,
  52. EASYLVGL_ERR_NO_MEM = -2,
  53. EASYLVGL_ERR_INIT_FAILED = -3,
  54. EASYLVGL_ERR_NOT_INITIALIZED = -4,
  55. EASYLVGL_ERR_PLATFORM_ERROR = -5
  56. } easylvgl_err_t;
  57. /**
  58. * 显示驱动操作接口
  59. */
  60. typedef struct {
  61. int (*init)(easylvgl_ctx_t *ctx, uint16_t w, uint16_t h, lv_color_format_t fmt);
  62. void (*flush)(easylvgl_ctx_t *ctx, const lv_area_t *area, const uint8_t *px_map);
  63. void (*wait_vsync)(easylvgl_ctx_t *ctx);
  64. void (*deinit)(easylvgl_ctx_t *ctx);
  65. } easylvgl_display_ops_t;
  66. /**
  67. * 文件系统操作接口
  68. */
  69. typedef struct {
  70. void *(*open)(easylvgl_ctx_t *ctx, const char *path, lv_fs_mode_t mode);
  71. lv_fs_res_t (*read)(easylvgl_ctx_t *ctx, void *file, void *buf, uint32_t len, uint32_t *read);
  72. lv_fs_res_t (*write)(easylvgl_ctx_t *ctx, void *file, const void *buf, uint32_t len, uint32_t *written);
  73. lv_fs_res_t (*seek)(easylvgl_ctx_t *ctx, void *file, uint32_t pos, lv_fs_whence_t whence);
  74. lv_fs_res_t (*tell)(easylvgl_ctx_t *ctx, void *file, uint32_t *pos);
  75. lv_fs_res_t (*close)(easylvgl_ctx_t *ctx, void *file);
  76. } easylvgl_fs_ops_t;
  77. /**
  78. * 输入设备操作接口
  79. */
  80. typedef struct {
  81. bool (*read_pointer)(easylvgl_ctx_t *ctx, lv_indev_data_t *data);
  82. bool (*read_keypad)(easylvgl_ctx_t *ctx, lv_indev_data_t *data);
  83. void (*calibration)(easylvgl_ctx_t *ctx, int16_t *x, int16_t *y);
  84. } easylvgl_input_ops_t;
  85. /**
  86. * 时基操作接口
  87. */
  88. typedef struct {
  89. uint32_t (*get_tick)(easylvgl_ctx_t *ctx);
  90. void (*delay_ms)(easylvgl_ctx_t *ctx, uint32_t ms);
  91. } easylvgl_time_ops_t;
  92. /**
  93. * 日志操作接口(可选)
  94. */
  95. typedef struct {
  96. void (*log)(easylvgl_ctx_t *ctx, lv_log_level_t level, const char *fmt, ...);
  97. } easylvgl_log_ops_t;
  98. /**
  99. * 平台操作接口集合
  100. */
  101. typedef struct {
  102. const easylvgl_display_ops_t *display_ops;
  103. const easylvgl_fs_ops_t *fs_ops;
  104. const easylvgl_input_ops_t *input_ops;
  105. const easylvgl_time_ops_t *time_ops;
  106. const easylvgl_log_ops_t *log_ops; /**< 可选 */
  107. } easylvgl_platform_ops_t;
  108. /**
  109. * EasyLVGL 上下文对象
  110. * 统一管理所有运行态数据,替代全局变量
  111. */
  112. struct easylvgl_ctx {
  113. // LVGL 驱动实例
  114. lv_display_t *display; /**< 显示设备 */
  115. lv_indev_t *indev; /**< 输入设备 */
  116. lv_fs_drv_t fs_drv[2]; /**< 文件系统驱动(L:/ 和 /) */
  117. // 缓冲管理
  118. easylvgl_buffer_t *buffer; /**< 缓冲管理器 */
  119. // 平台操作接口
  120. const easylvgl_platform_ops_t *ops; /**< 平台驱动 ops */
  121. // Lua 状态
  122. void *L; /**< Lua 状态指针(lua_State*,避免直接依赖) */
  123. // 配置标志
  124. uint8_t flags; /**< 配置标志位 */
  125. uint16_t width; /**< 屏幕宽度 */
  126. uint16_t height; /**< 屏幕高度 */
  127. // 内部状态
  128. lv_timer_t *tick_timer; /**< Tick 定时器(可选) */
  129. void *platform_data; /**< 平台私有数据 */
  130. lv_obj_t *focused_textarea; /**< 当前聚焦的 textarea,供系统键盘使用 */
  131. bool system_keyboard_enabled; /**< 是否允许系统键盘输入 */
  132. int32_t system_keyboard_preedit_pos; /**< 上一次插入的 SDL 预编辑文本起始位置 */
  133. int32_t system_keyboard_preedit_len; /**< 上一次插入的 SDL 预编辑文本长度(字符数) */
  134. bool system_keyboard_preedit_active; /**< 当前是否处于 SDL 预编辑(拼音)阶段 */
  135. };
  136. /**********************
  137. * GLOBAL PROTOTYPES
  138. **********************/
  139. /**
  140. * 创建 EasyLVGL 上下文对象
  141. * @param ctx 上下文指针(输出)
  142. * @param ops 平台操作接口
  143. * @return 0 成功,<0 失败
  144. * @pre-condition ops 必须非空
  145. * @post-condition ctx 已初始化,可调用 easylvgl_init
  146. */
  147. int easylvgl_ctx_create(easylvgl_ctx_t *ctx, const easylvgl_platform_ops_t *ops);
  148. /**
  149. * 初始化 EasyLVGL
  150. * @param ctx 上下文指针
  151. * @param width 屏幕宽度
  152. * @param height 屏幕高度
  153. * @param color_format 颜色格式
  154. * @return 0 成功,<0 失败
  155. * @pre-condition ctx 已通过 easylvgl_ctx_create 创建
  156. * @post-condition LVGL 已初始化,驱动已注册,缓冲已申请
  157. */
  158. int easylvgl_init(easylvgl_ctx_t *ctx, uint16_t width, uint16_t height, lv_color_format_t color_format);
  159. /**
  160. * 反初始化 EasyLVGL
  161. * @param ctx 上下文指针
  162. * @pre-condition ctx 已初始化
  163. * @post-condition 所有资源已释放
  164. */
  165. void easylvgl_deinit(easylvgl_ctx_t *ctx);
  166. /**
  167. * 错误码转字符串
  168. * @param err 错误码
  169. * @return 错误描述字符串
  170. */
  171. const char *easylvgl_strerror(easylvgl_err_t err);
  172. /**
  173. * 创建缓冲管理器
  174. * @return 缓冲管理器指针,失败返回 NULL
  175. */
  176. easylvgl_buffer_t *easylvgl_buffer_create(void);
  177. /**
  178. * 分配缓冲
  179. * @param ctx 上下文指针
  180. * @param size 缓冲大小(字节)
  181. * @param owner 缓冲所有权
  182. * @return 缓冲指针,失败返回 NULL
  183. */
  184. void *easylvgl_buffer_alloc(easylvgl_ctx_t *ctx, size_t size, easylvgl_buffer_owner_t owner);
  185. /**
  186. * 释放所有缓冲
  187. * @param ctx 上下文指针
  188. * @pre-condition ctx 必须非空
  189. * @post-condition 所有缓冲已按所有权释放
  190. */
  191. void easylvgl_buffer_free_all(easylvgl_ctx_t *ctx);
  192. /**
  193. * 创建 HZFont(TTF)字体,用于 EasyLVGL
  194. * @param path TTF 文件路径,为 NULL 则使用内置字库
  195. * @param size 字号
  196. * @param cache_size 缓存容量
  197. * @param antialias 抗锯齿等级
  198. * @return lv_font_t 字体对象,失败返回 NULL
  199. */
  200. lv_font_t * easylvgl_font_hzfont_create(const char * path, uint16_t size, uint32_t cache_size, int antialias);
  201. /**
  202. * 设置显示缓冲
  203. * @param ctx 上下文指针
  204. * @param buf1 缓冲1指针
  205. * @param buf2 缓冲2指针(可选,双缓冲时使用)
  206. * @param buf_size 缓冲大小(字节)
  207. * @param mode 缓冲模式
  208. * @return 0 成功,<0 失败
  209. */
  210. int easylvgl_display_set_buffers(
  211. easylvgl_ctx_t *ctx,
  212. void *buf1,
  213. void *buf2,
  214. uint32_t buf_size,
  215. easylvgl_buffer_mode_t mode);
  216. /**
  217. * 初始化文件系统驱动
  218. * @param ctx 上下文指针
  219. * @return 0 成功,<0 失败
  220. * @pre-condition ctx 必须非空且已初始化
  221. * @post-condition 文件系统驱动已注册到 LVGL
  222. */
  223. int easylvgl_fs_init(easylvgl_ctx_t *ctx);
  224. /**
  225. * 记录当前聚焦的 textarea,用于系统键盘转发
  226. */
  227. void easylvgl_ctx_set_focused_textarea(easylvgl_ctx_t *ctx, lv_obj_t *textarea);
  228. lv_obj_t *easylvgl_ctx_get_focused_textarea(easylvgl_ctx_t *ctx);
  229. /**
  230. * 控制系统键盘输入
  231. */
  232. int easylvgl_system_keyboard_enable(easylvgl_ctx_t *ctx, bool enable);
  233. bool easylvgl_system_keyboard_is_enabled(easylvgl_ctx_t *ctx);
  234. void easylvgl_system_keyboard_post_key(easylvgl_ctx_t *ctx, uint32_t key, bool pressed);
  235. void easylvgl_system_keyboard_post_text(easylvgl_ctx_t *ctx, const char *text);
  236. void easylvgl_system_keyboard_set_preedit(easylvgl_ctx_t *ctx, const char *text);
  237. void easylvgl_system_keyboard_clear_preedit(easylvgl_ctx_t *ctx);
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241. #endif /* EASYLVGL_H */