这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » 【Zephyr|STM32H747】1超级快速的LVGL搭建

共2条 1/1 1 跳转至

【Zephyr|STM32H747】1超级快速的LVGL搭建

高工
2026-08-22 09:06:54     打赏

【前言】

Zephyr 与STM32有官方的支持,让LVGL部署超级简单,只需要几步就能完成LVGL工程的部署

【实现步骤】

1、在zephyr目录的app下面创建工程。

2、添加prj.conf

CONFIG_DISPLAY=y
CONFIG_INPUT=y
CONFIG_I2C=y

CONFIG_LVGL=y
CONFIG_LV_Z_MEM_POOL_SIZE=32768
CONFIG_LV_USE_LABEL=y
CONFIG_LV_USE_BUTTON=y
CONFIG_LV_FONT_DEFAULT_MONTSERRAT_24=y

CONFIG_LOG=y
CONFIG_DISPLAY_LOG_LEVEL_ERR=y
CONFIG_MAIN_STACK_SIZE=4096

在prj.conf中打开显示,开启触摸输入,打开lvgl,并给定内存栈的占用空间即可。

3、添加CmakeLists.txt

# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(stm32h747_lvgl)

target_sources(app PRIVATE src/main.c)

4、新建src目录,在其目录下面新建main.c

/*
 * STM32H747I-DISCO + B-LCD40-DSI1 (MB1166-A09) LVGL demo
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/display.h>
#include <zephyr/logging/log.h>
#include <lvgl.h>
#include <stdio.h>

LOG_MODULE_REGISTER(app, LOG_LEVEL_INF);

static lv_obj_t *count_label;
static uint32_t press_count;

static void button_event_cb(lv_event_t *e)
{
    char buf[32];

    press_count++;
    snprintf(buf, sizeof(buf), "Button pressed: %lu", press_count);
    lv_label_set_text(count_label, buf);
}

int main(void)
{
    const struct device *display_dev =
        DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
    lv_obj_t *title_label;
    lv_obj_t *button;
    lv_obj_t *button_label;

    if (!device_is_ready(display_dev)) {
        LOG_ERR("Display device not ready");
        return 0;
    }

    title_label = lv_label_create(lv_screen_active());
    lv_label_set_text(title_label, "STM32H747 + LVGL");
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 40);

    count_label = lv_label_create(lv_screen_active());
    lv_label_set_text(count_label, "Button pressed: 0");
    lv_obj_align(count_label, LV_ALIGN_CENTER, 0, -40);

    button = lv_button_create(lv_screen_active());
    lv_obj_set_size(button, 180, 60);
    lv_obj_align(button, LV_ALIGN_CENTER, 0, 60);
    lv_obj_add_event_cb(button, button_event_cb, LV_EVENT_CLICKED, NULL);

    button_label = lv_label_create(button);
    lv_label_set_text(button_label, "Touch me");
    lv_obj_center(button_label);

    display_blanking_off(display_dev);

    LOG_INF("LVGL demo started");

    while (1) {
        lv_timer_handler();
        k_sleep(K_MSEC(10));
    }

    return 0;
}

在main.c中,我创建了几个标签用于展示工程,同时添加一个按键用于测试触摸是否可用。

5、构建

构建的最重要的是需要指定开发板与指定显示屏

west build -b stm32h747i_disco/stm32h747xx/m7 --shield st_b_lcd40_dsi1_mb1166_a09 -d build .

构建结果如下:

image.png

6、下载

使用west flash即可

image.png

6、工程效果:

95a79108d8cfaa39c817e067afc1f4b6.jpg

【总结】

zephyr + lvgl相比于传统到的工程实现,要快速的上百倍。不需要复杂的工程配置、代码添加等。




关键词: Zephyr     STM32H747     lvgl    

院士
2026-08-25 11:04:42     打赏
2楼

厉害!

看着真好,又懒得学习。


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]