在 aithinker\_Ai-M6X\_SDK\examples\lvgl\demos
目录中新建一个文件夹, 可以直接cv lvgl
文件夹修改, 例如我新建的目录 lvgltest2
结构如下
在demos目录中新建test(名称自定)文件夹, 新建 lv\_demo\_test.c
和 lv\_demo\_test.h
文件,来编写页面代码
lv\_demo\_test.h
文件, 用来定义方法和引入依赖
/\*\*
\* @file lv\_demo\_test.h
\*
\*/
#ifndef LV\_DEMO\_TEST\_H
#define LV\_DEMO\_TEST\_H
#ifdef \_\_cplusplus
extern "C" {
#endif
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\* INCLUDES
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
#include "../lv\_demos.h"
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\* GLOBAL PROTOTYPES
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
void lv\_demo\_test(void);
#ifdef \_\_cplusplus
} /\* extern "C" \*/
#endif
#endif /\*LV\_DEMO\_TEST\_H\*/
lv\_demo\_test.c
文件用来写页面代码, 添加一个标题, 创建两个button
lv\_theme\_default\_init()
设置主题lv\_scr\_act()
获取当前屏幕指针lv\_style\_init()
样式对象初始化lv\_style\_set\_text\_opa()
设置样式lv\_label\_create()
创建文本lv\_label\_set\_text()
设置文本值lv\_obj\_add\_style()
设置定义的样式lv\_btn\_create()
创建按钮lv\_obj\_set\_pos()
设置位置坐标/\*\*
\* @file lv\_demo\_test.c
\*
\*/
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\* INCLUDES
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
#include "lv\_demo\_test.h"
#if LV\_USE\_DEMO\_TEST
#if LV\_MEM\_CUSTOM == 0 && LV\_MEM\_SIZE < (38ul \* 1024ul)
#error Insufficient memory for lv\_demo\_widgets. Please set LV\_MEM\_SIZE to at least 38KB (38ul \* 1024ul). 48KB is recommended.
#endif
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\* TYPEDEFS
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
// 定义枚举, 三种屏幕大小
typedef enum {
DISP\_SMALL,
DISP\_MEDIUM,
DISP\_LARGE,
} disp\_size\_t;
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\* STATIC PROTOTYPES
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\* STATIC VARIABLES
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
static disp\_size\_t disp\_size;
static lv\_style\_t style\_text\_opacity;
static lv\_style\_t style\_title;
static const lv\_font\_t \* font\_large;
static const lv\_font\_t \* font\_normal;
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\* GLOBAL FUNCTIONS
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
void lv\_demo\_test(void) {
// 判断设备尺寸
if(LV\_HOR\_RES <= 320) disp\_size = DISP\_SMALL;
else if(LV\_HOR\_RES < 720) disp\_size = DISP\_MEDIUM;
else disp\_size = DISP\_LARGE;
// 先将large字体和normal字体修改为默认大小
font\_large = LV\_FONT\_DEFAULT;
font\_normal = LV\_FONT\_DEFAULT;
// 根据屏幕大小 设置字体大小
if (disp\_size == DISP\_LARGE) {
#if LV\_FONT\_MONTSERRAT\_24
font\_large = &lv\_font\_montserrat\_24;
#else
LV\_LOG\_WARN("LV\_FONT\_MONTSERRAT\_24 is not enabled for the widgets demo. Using LV\_FONT\_DEFAULT instead.");
#endif
#if LV\_FONT\_MONTSERRAT\_16
font\_normal = &lv\_font\_montserrat\_16;
#else
LV\_LOG\_WARN("LV\_FONT\_MONTSERRAT\_16 is not enabled for the widgets demo. Using LV\_FONT\_DEFAULT instead.");
#endif
} else if (disp\_size == DISP\_MEDIUM) {
#if LV\_FONT\_MONTSERRAT\_20
font\_large = &lv\_font\_montserrat\_20;
#else
LV\_LOG\_WARN("LV\_FONT\_MONTSERRAT\_20 is not enabled for the widgets demo. Using LV\_FONT\_DEFAULT instead.");
#endif
#if LV\_FONT\_MONTSERRAT\_14
font\_normal = &lv\_font\_montserrat\_14;
#else
LV\_LOG\_WARN("LV\_FONT\_MONTSERRAT\_14 is not enabled for the widgets demo. Using LV\_FONT\_DEFAULT instead.");
#endif
} else { /\* disp\_size == DISP\_SMALL \*/
#if LV\_FONT\_MONTSERRAT\_18
font\_large = &lv\_font\_montserrat\_18;
#else
LV\_LOG\_WARN("LV\_FONT\_MONTSERRAT\_18 is not enabled for the widgets demo. Using LV\_FONT\_DEFAULT instead.");
#endif
#if LV\_FONT\_MONTSERRAT\_12
font\_normal = &lv\_font\_montserrat\_12;
#else
LV\_LOG\_WARN("LV\_FONT\_MONTSERRAT\_12 is not enabled for the widgets demo. Using LV\_FONT\_DEFAULT instead.");
#endif
}
#if LV\_USE\_THEME\_DEFAULT
// 设置默认主题
lv\_theme\_default\_init(
NULL, // 没指定 则为默认主题
lv\_palette\_main(LV\_PALETTE\_BLUE), // 主颜色为蓝色
lv\_palette\_main(LV\_PALETTE\_RED), // 次颜色为红色
LV\_THEME\_DEFAULT\_DARK, // 设置主题为暗色模式
font\_normal // 字体
);
#endif
// 设置字体透明度
lv\_style\_init(&style\_text\_opacity);
lv\_style\_set\_text\_opa(&style\_text\_opacity, LV\_OPA\_50);
// 设置标题字体大小
lv\_style\_init(&style\_title);
lv\_style\_set\_text\_font(&style\_title, font\_large);
// 标题
lv\_obj\_t \* title = lv\_label\_create(lv\_scr\_act());
lv\_label\_set\_text(title, "Test Title");
lv\_obj\_add\_style(title, &style\_title, 0);
lv\_obj\_add\_style(title, &style\_text\_opacity, 0);
// 创建按钮
lv\_obj\_t \* btn1 = lv\_btn\_create(lv\_scr\_act());
// 设置定位
lv\_obj\_set\_pos(btn1, 60, 40);
// 复制btn1
lv\_obj\_t \* btn2 = lv\_btn\_create(lv\_scr\_act());
// 设置定位
lv\_obj\_set\_pos(btn2, 60, 80);
// 给按钮添加文字
lv\_obj\_t \* label1 = lv\_label\_create(btn1);
lv\_label\_set\_text(label1, "Button 1");
lv\_obj\_t \* label2 = lv\_label\_create(btn2);
lv\_label\_set\_text(label2, "Button 2");
}
#endif
在 demos/lv\_demos.h
文件中引入 lv\_demo\_test.h
文件, 也可以不用判断LV_USE_DEMO_TEST, 我这主要沿用了之前lvgl项目的代码
/\*\*
\* @file lv\_demos.h
\*
\*/
#ifndef LV\_DEMOS\_H
#define LV\_DEMOS\_H
#ifdef \_\_cplusplus
extern "C" {
#endif
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\* INCLUDES
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
#include "lvgl.h"
#if LV\_USE\_DEMO\_TEST
#include "test/lv\_demo\_test.h"
#endif
#ifdef \_\_cplusplus
} /\* extern "C" \*/
#endif
#endif /\*LV\_DEMO\_H\*/
在 lvgltest2\lv\_conf.h
文件 可以看到 lvgltest2\lv\_conf.h
文件中还有其他配置, 用来开放lvgl的一些功能, 比如开放button功能等, 默认这些都是开启的
配置 LV\_USE\_DEMO\_TEST
为1, 引入刚刚写的页面
main.c
执行 lv\_demo\_test
方法
/\*\*
\* @file main.c
\* @brief
\*
\* Copyright (c) 2021 Bouffalolab team
\*
\* Licensed to the Apache Software Foundation (ASF) under one or more
\* contributor license agreements. See the NOTICE file distributed with
\* this work for additional information regarding copyright ownership. The
\* ASF licenses this file to you under the Apache License, Version 2.0 (the
\* "License"); you may not use this file except in compliance with the
\* License. You may obtain a copy of the License at
\*
\* http://www.apache.org/licenses/LICENSE-2.0
\*
\* Unless required by applicable law or agreed to in writing, software
\* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
\* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
\* License for the specific language governing permissions and limitations
\* under the License.
\*
\*
\*/
#include "board.h"
#include "bflb\_gpio.h"
#include "bflb\_l1c.h"
#include "bflb\_mtimer.h"
#include "lv\_conf.h"
#include "lvgl.h"
#include "lv\_port\_disp.h"
#include "lv\_port\_indev.h"
#include "lcd.h"
#include "demos/lv\_demos.h"
/\* lvgl log cb \*/
void lv\_log\_print\_g\_cb(const char \*buf)
{
printf("[LVGL] %s", buf);
}
int main(void)
{
board\_init();
printf("lvgl case\r\n");
/\* lvgl init \*/
lv\_log\_register\_print\_cb(lv\_log\_print\_g\_cb);
lv\_init();
// 初始化显示驱动
lv\_port\_disp\_init();
// 初始化输入设备驱动
lv\_port\_indev\_init();
// 屏幕绘制
lv\_demo\_test();
// 任务队列用于管理后台任务,例如绘制屏幕、处理用户输入等
lv\_task\_handler();
printf("lvgl success\r\n");
while (1) {
lv\_task\_handler();
bflb\_mtimer\_delay\_ms(1);
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。