littlevgl按键屏幕切换流程例程
时间: 2023-07-15 18:15:51 浏览: 150
下面是一个使用 LittlevGL 库的按键屏幕切换流程的示例程序:
```c
#include <lvgl/lvgl.h>
/* 定义屏幕 ID */
enum {
SCREEN_1,
SCREEN_2,
SCREEN_3
};
/* 定义控件指针 */
static lv_obj_t *btn1;
static lv_obj_t *btn2;
static lv_obj_t *btn3;
static lv_obj_t *label1;
static lv_obj_t *label2;
static lv_obj_t *label3;
/* 按钮回调函数 */
static void btn_click_action(lv_obj_t *obj, lv_event_t event)
{
if (event == LV_EVENT_CLICKED) {
/* 根据按钮 ID 切换到相应屏幕 */
if (obj == btn1) {
lv_scr_load(SCREEN_1);
} else if (obj == btn2) {
lv_scr_load(SCREEN_2);
} else if (obj == btn3) {
lv_scr_load(SCREEN_3);
}
}
}
/* 创建屏幕和控件 */
void create_screen(void)
{
/* 创建屏幕 1 */
lv_obj_t *scr1 = lv_obj_create(NULL, NULL);
lv_scr_load(scr1);
/* 创建控件 */
label1 = lv_label_create(scr1, NULL);
lv_label_set_text(label1, "Screen 1");
lv_obj_align(label1, NULL, LV_ALIGN_CENTER, 0, 0);
/* 创建屏幕 2 */
lv_obj_t *scr2 = lv_obj_create(NULL, NULL);
/* 创建控件 */
label2 = lv_label_create(scr2, NULL);
lv_label_set_text(label2, "Screen 2");
lv_obj_align(label2, NULL, LV_ALIGN_CENTER, 0, 0);
/* 创建屏幕 3 */
lv_obj_t *scr3 = lv_obj_create(NULL, NULL);
/* 创建控件 */
label3 = lv_label_create(scr3, NULL);
lv_label_set_text(label3, "Screen 3");
lv_obj_align(label3, NULL, LV_ALIGN_CENTER, 0, 0);
/* 创建按钮 */
btn1 = lv_btn_create(scr1, NULL);
lv_obj_set_event_cb(btn1, btn_click_action);
lv_obj_align(btn1, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -20);
lv_obj_t *label_btn1 = lv_label_create(btn1, NULL);
lv_label_set_text(label_btn1, "Go to Screen 2");
btn2 = lv_btn_create(scr2, NULL);
lv_obj_set_event_cb(btn2, btn_click_action);
lv_obj_align(btn2, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -20);
lv_obj_t *label_btn2 = lv_label_create(btn2, NULL);
lv_label_set_text(label_btn2, "Go to Screen 3");
btn3 = lv_btn_create(scr3, NULL);
lv_obj_set_event_cb(btn3, btn_click_action);
lv_obj_align(btn3, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -20);
lv_obj_t *label_btn3 = lv_label_create(btn3, NULL);
lv_label_set_text(label_btn3, "Go to Screen 1");
}
int main(void)
{
lv_init();
/* 创建屏幕和控件 */
create_screen();
while (1) {
lv_task_handler();
}
return 0;
}
```
在这个例程中,我们创建了三个屏幕和三个按钮。每个按钮都关联到一个回调函数,当用户点击按钮时,通过调用 `lv_scr_load()` 函数来切换到相应的屏幕。在每个屏幕中,我们创建了一个标签控件来显示屏幕内容。
阅读全文