前言
本开发板带一个135x130的TFT屏幕,使用的控制器是ST7735,SPI接口,16位。前两篇已经进行了屏幕的显示测试,具备了移植GUI的条件,本篇我们移植LVGL。
参考
https://lvgl.io/
准备代码
cd niobeu4_src/vendor/openvalley/niobeu4/demo/107_hdf_spi/
git clone https://github.com/lvgl/lvgl.git
移植概述
配置文件lv_conf.h
复制
lvgl/lv_conf_template.h
为
lvgl/lv_conf.h
将#if 0改为1,即直接#include “lv_conf.h”
将lv_conf.h添加到工程头文件包含路径。
即修改
vendor/openvalley/niobeu4/demo/107_hdf_spi/BUILD.gn
include_dirs = [
下添加一行"lvgl/",
底层驱动模板
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/examples/porting
下有对应的模板文件,分别是显示,文件系统和输入设备的驱动模板。
lv_port_disp_template.c/h
lv_port_fs_template.c/h
lv_port_indev_template.c/h
暂时只移植显示,所以复制lv_port_disp_template.c/h
到vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl下改名字为
lv_port_disp.c/h
将.c和.h里面的#if 0改为1
.c中#include "lv_port_disp_template.h"改为#include "lv_port_disp.h"
HAL层模板
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/src/hal
下
我们直接使用不修改
lv_hal_disp.c/h
lv_hal_indev.c/h
lv_hal_tick.c/h
lv_hal.h
添加文件
见工程配置,在gn文件中添加源码。
需要移植的代码
移植比较简单,直接使用底层驱动模板根据实际实现修改lv_port_disp.c,并配置lv_conf.h即可。
头文件包含模式
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/lv_port_disp.h中
#define LV_LVGL_H_INCLUDE_SIMPLE 1
这样需要将lv_conf.h所在路径配置为工程头文件包含路径。
代码中直接#include "lvgl.h"
否则是#include "../../lvgl.h"
分辨率配置
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/lv_port_disp.h中
#define MY_DISP_HOR_RES 135
#define MY_DISP_VER_RES 130
初始化
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/lv_port_disp.c中
实现disp_init
即调用自己的初始化函数LcdInit
缓冲区
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/lv_port_disp.c中
lv_port_disp_init
注释掉/* Example for 2) */
/* Example for 3) also set disp_drv.full_refresh = 1 below*/对应的代码
使用/* Example for 1) */
lv_port_disp_init
改函数调用disp_init
调用LcdInit实现初始化
刷新显示
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/lv_port_disp.c中
disp_flush
/*put_px(x, y, color_p)/改为
lcd_draw_point(x,y,color_p->full);
并调用LcdPush();
/*Flush the content of the internal buffer the specific area on the display
*You can use DMA or any hardware acceleration to do this operation in the background but
'lv_disp_flush_ready()' has to be called when finished./
static void disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
{
if(disp_flush_enabled) {
int32_t x;
int32_t y;
for(y = area->y1; y <= area->y2; y++) {
for(x = area->x1; x <= area->x2; x++) {
lcd_draw_point(x,y,color_p->full);
color_p++;
}
}
LcdPush();
}
lv_disp_flush_ready(disp_drv);
}
颜色深度
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/lv_conf.h中
#define LV_COLOR_DEPTH 16
typedef LV_CONCAT3(lv_color, LV_COLOR_DEPTH, _t) lv_color_t;
则lv_color_t类型为lv_color_16_t
堆大小配置
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/lv_conf.h中
#define LV_MEM_SIZE (10U * 1024U) /[bytes]/
按需提供堆大小,过大可能编译不过,过小可能影响创建对象。
时间滴答
如果LV_TICK_CUSTOM为1则需要用户提供相关接口
LV_TICK_CUSTOM_SYS_TIME_EXPR用于获取当前毫秒值
和头文件LV_TICK_CUSTOM_INCLUDE
在lv_conf.h中指定
否则使用lvgl/src/hal/lv_hal_tick.c的实现
每隔x毫秒调用lv_tick_inc(x),使用内部计数器定时。
周期调用lv_tick_inc更新时间滴答,比如专门在某个定时线程中
while(1)
{
LOS_Msleep(5);
lv_tick_inc(5);
lv_timer_handler();
}
日志
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/lv_conf.h中
#define LV_USE_LOG 0
改为
#define LV_USE_LOG 1使能日志
#define LV_LOG_LEVEL LV_LOG_LEVEL_TRACE设置日志,等级
LV_LOG_LEVEL_TRACE表示所有信息都打印
如果#define LV_LOG_PRINTF 0
则需要调用设置lv_log_register_print_cb()打印函数
否则使用printf,我们这里实现了printf所以设置为1
#define LV_LOG_USE_TIMESTAMP 1
使能打印时间
其他的模块打印使能
#define LV_LOG_TRACE_MEM 1
#define LV_LOG_TRACE_TIMER 1
#define LV_LOG_TRACE_INDEV 1
#define LV_LOG_TRACE_DISP_REFR 1
#define LV_LOG_TRACE_EVENT 1
#define LV_LOG_TRACE_OBJ_CREATE 1
#define LV_LOG_TRACE_LAYOUT 1
#define LV_LOG_TRACE_ANIM 1
字体配置
vendor/openvalley/niobeu4/demo/107_hdf_spi/lvgl/lv_conf.h
中按需使能对应的字体,如果有对应编译错误信息根据提示使能
#define LV_FONT_MONTSERRAT_12 1
#define LV_FONT_MONTSERRAT_14 1
#define LV_FONT_MONTSERRAT_16 1
工程配置
vendor/openvalley/niobeu4/demo/107_hdf_spi/BUILD.gn中添加源码和头文件包含路径。
kernel_module(module_name){
sources = [
"spi_example.c",
"st7735s.c",
"lvgl/src/core/lv_disp.c",
"lvgl/src/core/lv_event.c",
"lvgl/src/core/lv_group.c",
"lvgl/src/core/lv_indev_scroll.c",
"lvgl/src/core/lv_indev.c",
"lvgl/src/core/lv_obj_class.c",
"lvgl/src/core/lv_obj_draw.c",
"lvgl/src/core/lv_obj_pos.c",
"lvgl/src/core/lv_obj_scroll.c",
"lvgl/src/core/lv_obj_style_gen.c",
"lvgl/src/core/lv_obj_style.c",
"lvgl/src/core/lv_obj_tree.c",
"lvgl/src/core/lv_obj.c",
"lvgl/src/core/lv_refr.c",
"lvgl/src/core/lv_theme.c",
"lvgl/src/draw/sw/lv_draw_sw_arc.c",
"lvgl/src/draw/sw/lv_draw_sw_blend.c",
"lvgl/src/draw/sw/lv_draw_sw_dither.c",
"lvgl/src/draw/sw/lv_draw_sw_gradient.c",
"lvgl/src/draw/sw/lv_draw_sw_img.c",
"lvgl/src/draw/sw/lv_draw_sw_layer.c",
"lvgl/src/draw/sw/lv_draw_sw_letter.c",
"lvgl/src/draw/sw/lv_draw_sw_line.c",
"lvgl/src/draw/sw/lv_draw_sw_polygon.c",
"lvgl/src/draw/sw/lv_draw_sw_rect.c",
"lvgl/src/draw/sw/lv_draw_sw_transform.c",
"lvgl/src/draw/sw/lv_draw_sw.c",
"lvgl/src/draw/lv_draw_arc.c",
"lvgl/src/draw/lv_draw_img.c",
"lvgl/src/draw/lv_draw_label.c",
"lvgl/src/draw/lv_draw_layer.c",
"lvgl/src/draw/lv_draw_line.c",
"lvgl/src/draw/lv_draw_mask.c",
"lvgl/src/draw/lv_draw_rect.c",
"lvgl/src/draw/lv_draw_transform.c",
"lvgl/src/draw/lv_draw_triangle.c",
"lvgl/src/draw/lv_draw.c",
"lvgl/src/draw/lv_img_buf.c",
"lvgl/src/draw/lv_img_cache.c",
"lvgl/src/draw/lv_img_decoder.c",
"lvgl/src/font/lv_font_fmt_txt.c",
"lvgl/src/font/lv_font.c",
"lvgl/src/font/lv_font_montserrat_12.c",
"lvgl/src/font/lv_font_montserrat_14.c",
"lvgl/src/font/lv_font_montserrat_16.c",
"lvgl/src/hal/lv_hal_disp.c",
"lvgl/src/hal/lv_hal_indev.c",
"lvgl/src/hal/lv_hal_tick.c",
"lvgl/src/layouts/flex/lv_flex.c",
"lvgl/src/layouts/grid/lv_grid.c",
"lvgl/src/libs/bmp/lv_bmp.c",
"lvgl/src/libs/ffmpeg/lv_ffmpeg.c",
"lvgl/src/libs/freetype/lv_freetype.c",
"lvgl/src/libs/fsdrv/lv_fs_fatfs.c",
"lvgl/src/libs/fsdrv/lv_fs_posix.c",
"lvgl/src/libs/fsdrv/lv_fs_stdio.c",
"lvgl/src/libs/fsdrv/lv_fs_win32.c",
"lvgl/src/libs/gif/gifdec.c",
"lvgl/src/libs/gif/lv_gif.c",
"lvgl/src/libs/png/lodepng.c",
"lvgl/src/libs/png/lv_png.c",
"lvgl/src/libs/qrcode/lv_qrcode.c",
"lvgl/src/libs/qrcode/qrcodegen.c",
"lvgl/src/libs/rlottie/lv_rlottie.c",
"lvgl/src/libs/sjpg/lv_sjpg.c",
"lvgl/src/libs/sjpg/tjpgd.c",
"lvgl/src/misc/lv_anim_timeline.c",
"lvgl/src/misc/lv_anim.c",
"lvgl/src/misc/lv_area.c",
"lvgl/src/misc/lv_async.c",
"lvgl/src/misc/lv_bidi.c",
"lvgl/src/misc/lv_color.c",
"lvgl/src/misc/lv_fs.c",
"lvgl/src/misc/lv_gc.c",
"lvgl/src/misc/lv_ll.c",
"lvgl/src/misc/lv_log.c",
"lvgl/src/misc/lv_lru.c",
"lvgl/src/misc/lv_malloc_builtin.c",
"lvgl/src/misc/lv_math.c",
"lvgl/src/misc/lv_mem.c",
"lvgl/src/misc/lv_memcpy_builtin.c",
"lvgl/src/misc/lv_printf.c",
"lvgl/src/misc/lv_style_gen.c",
"lvgl/src/misc/lv_style.c",
"lvgl/src/misc/lv_templ.c",
"lvgl/src/misc/lv_timer.c",
"lvgl/src/misc/lv_tlsf.c",
"lvgl/src/misc/lv_txt_ap.c",
"lvgl/src/misc/lv_txt.c",
"lvgl/src/misc/lv_utils.c",
"lvgl/src/others/file_explorer/lv_file_explorer.c",
"lvgl/src/others/fragment/lv_fragment_manager.c",
"lvgl/src/others/fragment/lv_fragment.c",
"lvgl/src/others/gridnav/lv_gridnav.c",
"lvgl/src/others/ime/lv_ime_pinyin.c",
"lvgl/src/others/imgfont/lv_imgfont.c",
"lvgl/src/others/monkey/lv_monkey.c",
"lvgl/src/others/msg/lv_msg.c",
"lvgl/src/others/snapshot/lv_snapshot.c",
"lvgl/src/themes/basic/lv_theme_basic.c",
"lvgl/src/themes/default/lv_theme_default.c",
"lvgl/src/themes/mono/lv_theme_mono.c",
"lvgl/src/widgets/windows/lv_win.c",
"lvgl/src/widgets/animimg/lv_animimg.c",
"lvgl/src/widgets/arc/lv_arc.c",
"lvgl/src/widgets/bar/lv_bar.c",
"lvgl/src/widgets/btn/lv_btn.c",
"lvgl/src/widgets/btnmatrix/lv_btnmatrix.c",
"lvgl/src/widgets/calendar/lv_calendar_header_arrow.c",
"lvgl/src/widgets/calendar/lv_calendar_header_dropdown.c",
"lvgl/src/widgets/calendar/lv_calendar.c",
"lvgl/src/widgets/canvas/lv_canvas.c",
"lvgl/src/widgets/chart/lv_chart.c",
"lvgl/src/widgets/checkbox/lv_checkbox.c",
"lvgl/src/widgets/colorwheel/lv_colorwheel.c",
"lvgl/src/widgets/dropdown/lv_dropdown.c",
"lvgl/src/widgets/img/lv_img.c",
"lvgl/src/widgets/imgbtn/lv_imgbtn.c",
"lvgl/src/widgets/keyboard/lv_keyboard.c",
"lvgl/src/widgets/label/lv_label.c",
"lvgl/src/widgets/led/lv_led.c",
"lvgl/src/widgets/line/lv_line.c",
"lvgl/src/widgets/list/lv_list.c",
"lvgl/src/widgets/menu/lv_menu.c",
"lvgl/src/widgets/meter/lv_meter.c",
"lvgl/src/widgets/msgbox/lv_msgbox.c",
"lvgl/src/widgets/objx_templ/lv_objx_templ.c",
"lvgl/src/widgets/roller/lv_roller.c",
"lvgl/src/widgets/slider/lv_slider.c",
"lvgl/src/widgets/span/lv_span.c",
"lvgl/src/widgets/spinbox/lv_spinbox.c",
"lvgl/src/widgets/spinner/lv_spinner.c",
"lvgl/src/widgets/switch/lv_switch.c",
"lvgl/src/widgets/table/lv_table.c",
"lvgl/src/widgets/tabview/lv_tabview.c",
"lvgl/src/widgets/textarea/lv_textarea.c",
"lvgl/src/widgets/tileview/lv_tileview.c",
"lvgl/lv_port_disp.c",
"lvgl/demos/stress/lv_demo_stress.c",
]
include_dirs = [
"//drivers/hdf_core/framework/include/platform/",
"//drivers/hdf_core/framework/include/utils/",
"//drivers/hdf_core/framework/support/platform/include/spi",
"//drivers/hdf_core/adapter/khdf/liteos_m/osal/include/",
"//drivers/hdf_core/framework/include/core/",
"//drivers/hdf_core/framework/include/osal/",
"lvgl/",
"lvgl/src",
".",
]
测试代码
vendor/openvalley/niobeu4/demo/107_hdf_spi/spi_example.c中
增加
#include "lvgl.h"
#include "lv_port_disp.h"
任务中
注意初始化
lv_init();在
lv_port_disp_init();前
再创建lc对象
再周期执行
lv_task_handler();
另专门开任务周期调用
lv_tick_inc(5);
lv_timer_handler();
提供滴答和定时器处理。
static void btn_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * btn = lv_event_get_target(e);
if(code == LV_EVENT_CLICKED) {
static uint8_t cnt = 0;
cnt++;
lv_obj_t * label = lv_obj_get_child(btn, 0);
lv_label_set_text_fmt(label, "Button: %d", cnt);
}
}
void lv_log_print(const char * buf)
{
printf("%s\r\n",buf);
}
void lv_example_get_started_1(void)
{
lv_obj_t * btn = lv_btn_create(lv_scr_act());
lv_obj_set_pos(btn, 10, 10);
lv_obj_set_size(btn, 60, 25);
lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL);
lv_obj_t * label = lv_label_create(btn);
lv_label_set_text(label, "Button");
lv_obj_center(label);
}
void lv_tick_handle(UINT32 arg)
{
while(1)
{
LOS_Msleep(5);
lv_tick_inc(5);
lv_timer_handler();
}
}
void StartHdfSPITest(void)
{
UINT32 g_lv_time_task;
TSK_INIT_PARAM_S task = { 0 };
task.pfnTaskEntry = (TSK_ENTRY_FUNC)lv_tick_handle;
task.pcName = "lv_time_task";
task.uwStackSize = 0x1000;
task.usTaskPrio = 25 - 1;
lv_init();
lv_port_disp_init();
lv_log_register_print_cb(lv_log_print);
LOS_TaskCreate(&g_lv_time_task, &task);
lv_example_get_started_1();
while(1)
{
LOS_Msleep(1);
lv_task_handler();
}
}
执行效果
创建了一个按钮,由于输入驱动还没移植所以不能演示按钮事件。
总结
- 只移植了显示驱动,后面可以移植输入驱动,和文件系统接口,以实现完整的功能。
- 移植比较简单,按照模板修改即可,之前已经测试好显示操作,所以移植很快。
- 后面再基于此进行具体项目开发,比如基于ADC可以实现简易的示波器。